1
/* ============================================================================
2
Copyright 2014 Hewlett Packard
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
============================================================================ */
16
using System.Management.Automation;
17
using Openstack.Client.Powershell.Cmdlets.Common;
18
using Openstack.Client.Powershell.Providers.Common;
19
using Openstack.Objects.Domain.BlockStorage;
20
using Openstack.Client.Powershell.Providers.Security;
23
namespace Openstack.Client.Powershell.Cmdlets.BlockStorage
25
[Cmdlet(VerbsCommon.New, "Snapshot", SupportsShouldProcess = true)]
26
[RequiredServiceIdentifierAttribute(Openstack.Objects.Domain.Admin.Services.BlockStorage)]
27
public class NewSnapshotCmdlet : BasePSCmdlet
30
private SwitchParameter _force = false;
31
private string _description;
32
private string _volumeId;
35
//=========================================================================================
39
//=========================================================================================
40
[Parameter(Position = 0, ParameterSetName = "NewSnapshot", Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
42
[ValidateNotNullOrEmpty]
46
set { _name = value; }
48
//=========================================================================================
52
//=========================================================================================
53
[Parameter(Position = 1, ParameterSetName = "NewSnapshot", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
55
[ValidateNotNullOrEmpty]
56
public string Description
58
get { return _description; }
59
set { _description = value; }
61
//=========================================================================================
65
//=========================================================================================
66
[Parameter(Position = 2, ParameterSetName = "NewSnapshot", Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
68
[ValidateNotNullOrEmpty]
69
public string VolumeId
71
get { return _volumeId; }
72
set { _volumeId = value; }
74
//=========================================================================================
78
//=========================================================================================
79
[Parameter(Position = 4, ParameterSetName = "NewSnapshot", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
81
public SwitchParameter Force
83
get { return _force; }
84
set { _force = value; }
88
//=========================================================================================
92
//=========================================================================================
93
private bool IsVolumeAvailable(string volumeId)
95
Volume volume = this.RepositoryFactory.CreateVolumeRepository().GetVolume(volumeId);
96
if (volume.Status == "in-use")
101
//=========================================================================================
105
//=========================================================================================
106
protected override void ProcessRecord()
108
if (this.IsVolumeAvailable(this.VolumeId))
110
NewSnapshot snapshot = new NewSnapshot();
111
snapshot.Description = this.Description;
112
snapshot.Name = this.Name;
113
snapshot.VolumeId = this.VolumeId;
114
snapshot.Force = this.Force;
116
this.RepositoryFactory.CreateSnapshotRepository().SaveSnapshot(snapshot);
117
this.UpdateCache<SnapshotsUIContainer>();
121
Console.WriteLine("");
122
Console.WriteLine("The specified Volume is already in use.");
123
Console.WriteLine("");