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

« back to all changes in this revision

Viewing changes to Openstack.Client.Powershell/Cmdlets/Networking/NewNetworkCmdlet.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;
17
 
using System.Collections.Generic;
18
 
using System.Linq;
19
 
using System.Management.Automation;
20
 
using System.Text;
21
 
using Openstack.Client.Powershell.Cmdlets.Common;
22
 
using Openstack.Objects.DataAccess.Networking;
23
 
using Openstack.Objects.Domain.Networking;
24
 
 
25
 
namespace Openstack.Client.Powershell.Cmdlets.Networking
26
 
{
27
 
    [Cmdlet("New", "Network", SupportsShouldProcess = true)]
28
 
    public class NewNetworkCmdlet : BasePSCmdlet
29
 
    {
30
 
        private bool _adminStateUp = true;
31
 
        private bool _force = true;
32
 
        private string _name;
33
 
        private string _cidr =  "11.0.3.0/24";
34
 
 
35
 
        #region Properties
36
 
//=========================================================================================
37
 
/// <summary>
38
 
/// 
39
 
/// </summary>
40
 
//=========================================================================================        
41
 
        [Parameter(Position = 0, ParameterSetName = "NewNetwork", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The Id of the Server.")]
42
 
        [Alias("n")]
43
 
        public string Name
44
 
        {
45
 
            get { return _name; }
46
 
            set { _name = value; }
47
 
        }
48
 
//=========================================================================================
49
 
/// <summary>
50
 
/// 
51
 
/// </summary>
52
 
//=========================================================================================        
53
 
        [Parameter(Position = 3, ParameterSetName = "NewNetwork", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The Id of the Server.")]
54
 
        [Alias("cidr")]
55
 
        public string CidrValue
56
 
        {
57
 
            get { return _cidr; }
58
 
            set { _cidr = value; }
59
 
        }
60
 
//=========================================================================================
61
 
/// <summary>
62
 
/// 
63
 
/// </summary>
64
 
//========================================================================================= 
65
 
        [Parameter(Position = 1, ParameterSetName = "NewNetwork", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
66
 
        [Alias("asu")]
67
 
        [ValidateNotNullOrEmpty]
68
 
        public SwitchParameter AdminStateUp
69
 
        {
70
 
            get { return _adminStateUp; }
71
 
            set { _adminStateUp = value; }
72
 
        }
73
 
//=========================================================================================
74
 
/// <summary>
75
 
/// 
76
 
/// </summary>
77
 
//========================================================================================= 
78
 
        [Parameter(Position = 2, ParameterSetName = "NewNetwork", Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Help Text")]
79
 
        [Alias("f")]
80
 
        [ValidateNotNullOrEmpty]
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
 
/// <param name="sender"></param>
93
 
/// <param name="e"></param>
94
 
//=========================================================================================
95
 
        private void BuilderEvent(object sender, CreateNetworkEventArgs e)
96
 
        {
97
 
            Console.WriteLine("- " + e.Message);
98
 
        }
99
 
//=========================================================================================
100
 
/// <summary>
101
 
/// 
102
 
/// </summary>
103
 
//========================================================================================= 
104
 
        protected override void ProcessRecord()
105
 
        {
106
 
            Console.WriteLine("");
107
 
            NewNetwork newNetwork   = new NewNetwork();
108
 
            newNetwork.AdminStateUp = _adminStateUp;
109
 
            newNetwork.Name         = _name;
110
 
 
111
 
            if (this.CidrValue == null) {
112
 
                newNetwork.Cidr = "11.0.3.0/24";
113
 
            }
114
 
            else{
115
 
                newNetwork.Cidr = this.CidrValue;
116
 
            }
117
 
 
118
 
            INetworkRepository repository = this.RepositoryFactory.CreateNetworkRepository();
119
 
            repository.Changed += new Openstack.Objects.DataAccess.Networking.NetworkRepository.CreateNetworkEventHandler(BuilderEvent);
120
 
            repository.SaveNetwork(newNetwork, this.Force);
121
 
            repository.Changed -= new Openstack.Objects.DataAccess.Networking.NetworkRepository.CreateNetworkEventHandler(BuilderEvent);
122
 
            Console.WriteLine("  Network Build Complete!");
123
 
            Console.WriteLine("");
124
 
        }
125
 
        #endregion
126
 
    }
127
 
}