~travis-plummer/openstack-cli-powershell/master

« back to all changes in this revision

Viewing changes to Openstack.Client.Powershell/Cmdlets/BlockStorage/NewSnapshotCmdlet.cs

  • Committer: Monty Taylor
  • Date: 2015-10-17 20:03:56 UTC
  • Revision ID: git-v1:803406e7e09f6f4199f7a92f11f2904ad1d6cc15
Retire stackforge/openstack-cli-powershell

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* ============================================================================
2
 
Copyright 2014 Hewlett Packard
3
 
 
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
7
 
 
8
 
    http://www.apache.org/licenses/LICENSE-2.0
9
 
 
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;
21
 
using System;
22
 
 
23
 
namespace Openstack.Client.Powershell.Cmdlets.BlockStorage
24
 
25
 
    [Cmdlet(VerbsCommon.New, "Snapshot", SupportsShouldProcess = true)]
26
 
    [RequiredServiceIdentifierAttribute(Openstack.Objects.Domain.Admin.Services.BlockStorage)]
27
 
    public class NewSnapshotCmdlet : BasePSCmdlet
28
 
    {
29
 
        private string _name;
30
 
        private SwitchParameter _force = false;
31
 
        private string _description;
32
 
        private string _volumeId;
33
 
 
34
 
        #region Parameters 
35
 
//=========================================================================================
36
 
/// <summary>
37
 
/// 
38
 
/// </summary>
39
 
//=========================================================================================
40
 
        [Parameter(Position = 0, ParameterSetName = "NewSnapshot", Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
41
 
        [Alias("n")]
42
 
        [ValidateNotNullOrEmpty]
43
 
        public string Name
44
 
        {
45
 
            get { return _name; }
46
 
            set { _name = value; }
47
 
        }
48
 
//=========================================================================================
49
 
/// <summary>
50
 
/// 
51
 
/// </summary>
52
 
//=========================================================================================
53
 
        [Parameter(Position = 1, ParameterSetName = "NewSnapshot", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
54
 
        [Alias("d")]
55
 
        [ValidateNotNullOrEmpty]
56
 
        public string Description
57
 
        {
58
 
            get { return _description; }
59
 
            set { _description = value; }
60
 
        }
61
 
//=========================================================================================
62
 
/// <summary>
63
 
/// 
64
 
/// </summary>
65
 
//=========================================================================================  
66
 
        [Parameter(Position = 2, ParameterSetName = "NewSnapshot", Mandatory = true, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
67
 
        [Alias("v")]
68
 
        [ValidateNotNullOrEmpty]
69
 
        public string VolumeId
70
 
        {
71
 
            get { return _volumeId; }
72
 
            set { _volumeId = value; }
73
 
        }
74
 
//=========================================================================================
75
 
/// <summary>
76
 
/// 
77
 
/// </summary>
78
 
//=========================================================================================  
79
 
        [Parameter(Position = 4, ParameterSetName = "NewSnapshot", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
80
 
        [Alias("f")]
81
 
        public SwitchParameter Force
82
 
        {
83
 
            get { return _force; }
84
 
            set { _force = value; }
85
 
        }
86
 
        #endregion
87
 
        #region Methods
88
 
//=========================================================================================
89
 
/// <summary>
90
 
/// 
91
 
/// </summary>
92
 
//=========================================================================================
93
 
        private bool IsVolumeAvailable(string volumeId)
94
 
        {
95
 
            Volume volume = this.RepositoryFactory.CreateVolumeRepository().GetVolume(volumeId);
96
 
            if (volume.Status == "in-use")
97
 
                return false;
98
 
            else
99
 
                return true;
100
 
        }
101
 
//=========================================================================================
102
 
/// <summary>
103
 
/// 
104
 
/// </summary>
105
 
//=========================================================================================
106
 
        protected override void ProcessRecord()
107
 
        {
108
 
            if (this.IsVolumeAvailable(this.VolumeId))
109
 
            {
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;
115
 
 
116
 
                this.RepositoryFactory.CreateSnapshotRepository().SaveSnapshot(snapshot);
117
 
                this.UpdateCache<SnapshotsUIContainer>();
118
 
            }
119
 
            else
120
 
            {
121
 
                Console.WriteLine("");
122
 
                Console.WriteLine("The specified Volume is already in use.");
123
 
                Console.WriteLine("");
124
 
            }
125
 
        }
126
 
        #endregion
127
 
    }
128
 
}
129
 
 
130
 
 
131
 
 
132