|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 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 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using System; |
| 16 | +using System.Management.Automation; |
| 17 | +using Hyak.Common; |
| 18 | +using Microsoft.WindowsAzure.Management.StorSimple.Models; |
| 19 | +using Microsoft.WindowsAzure.Commands.StorSimple.Properties; |
| 20 | +using System.Net; |
| 21 | + |
| 22 | +namespace Microsoft.WindowsAzure.Commands.StorSimple.Cmdlets |
| 23 | +{ |
| 24 | + /// <summary> |
| 25 | + /// Create Network config object to be used for Setting and Updating DeviceDetails |
| 26 | + /// </summary> |
| 27 | + [Cmdlet(VerbsCommon.New, "AzureStorSimpleNetworkConfig"), |
| 28 | + OutputType(typeof (NetworkConfig))] |
| 29 | + |
| 30 | + public class NewAzureStorSimpleNetworkConfig : StorSimpleCmdletBase |
| 31 | + { |
| 32 | + #region Parameters |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Whether the net interface is iscsi enabled/disabled |
| 36 | + /// </summary> |
| 37 | + [Parameter(Mandatory=false, Position = 0, HelpMessage = StorSimpleCmdletHelpMessage.IsIscsiEnabled)] |
| 38 | + [ValidateNotNullOrEmpty] |
| 39 | + public bool? EnableIscsi { get; set; } |
| 40 | + |
| 41 | + /// <summary> |
| 42 | + /// Whether the net interface is cloud enabled/disabled |
| 43 | + /// </summary> |
| 44 | + [Parameter(Mandatory = false, Position = 1, HelpMessage = StorSimpleCmdletHelpMessage.IsCloudEnabled)] |
| 45 | + [ValidateNotNullOrEmpty] |
| 46 | + public bool? EnableCloud { get; set; } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// IPv4Address for controller 0, should be used only with Data0 interface |
| 50 | + /// </summary> |
| 51 | + [Parameter(Mandatory = false, Position = 2, HelpMessage = StorSimpleCmdletHelpMessage.Controller0IPv4Address)] |
| 52 | + [ValidateNotNullOrEmpty] |
| 53 | + public string Controller0IPv4Address { get; set; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// IPv4Address for controller 1, should be used only with Data0 interface |
| 57 | + /// </summary> |
| 58 | + [Parameter(Mandatory = false, Position = 3, HelpMessage = StorSimpleCmdletHelpMessage.Controller1IPv4Address)] |
| 59 | + [ValidateNotNullOrEmpty] |
| 60 | + public string Controller1IPv4Address { get; set; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// IPv4 net mask for interface |
| 64 | + /// </summary> |
| 65 | + [Parameter(Mandatory = false, Position = 4, HelpMessage = StorSimpleCmdletHelpMessage.IPv6Gateway)] |
| 66 | + [ValidateNotNullOrEmpty] |
| 67 | + public string IPv6Gateway { get; set; } |
| 68 | + |
| 69 | + /// <summary> |
| 70 | + /// IPv4 Address of gateway |
| 71 | + /// </summary> |
| 72 | + [Parameter(Mandatory = false, Position = 5, HelpMessage = StorSimpleCmdletHelpMessage.IPv4Gateway)] |
| 73 | + [ValidateNotNullOrEmpty] |
| 74 | + public string IPv4Gateway { get; set; } |
| 75 | + |
| 76 | + /// <summary> |
| 77 | + /// IPv4 Address for the net interface |
| 78 | + /// </summary> |
| 79 | + [Parameter(Mandatory = false, Position = 6, HelpMessage = StorSimpleCmdletHelpMessage.IPv4Address)] |
| 80 | + [ValidateNotNullOrEmpty] |
| 81 | + public string IPv4Address { get; set; } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// IPv6 Prefix for the net interface |
| 85 | + /// </summary> |
| 86 | + [Parameter(Mandatory = false, Position = 7, HelpMessage = StorSimpleCmdletHelpMessage.IPv6Prefix)] |
| 87 | + [ValidateNotNullOrEmpty] |
| 88 | + public string IPv6Prefix { get; set; } |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// IPv4 netmask for this interface |
| 92 | + /// </summary> |
| 93 | + [Parameter(Mandatory = false, Position = 8, HelpMessage = StorSimpleCmdletHelpMessage.IPv4Netmask)] |
| 94 | + [ValidateNotNullOrEmpty] |
| 95 | + public string IPv4Netmask { get; set; } |
| 96 | + |
| 97 | + /// <summary> |
| 98 | + /// Interface alias of interface for which settings are being supplied. A value |
| 99 | + /// from Data0 to Data5 |
| 100 | + /// </summary> |
| 101 | + [Parameter(Mandatory = true, Position = 9, HelpMessage = StorSimpleCmdletHelpMessage.InterfaceAlias)] |
| 102 | + [ValidateSetAttribute(new string[] { "Data0", "Data1", "Data2", "Data3", "Data4", "Data5" })] |
| 103 | + public string InterfaceAlias { get; set; } |
| 104 | + #endregion |
| 105 | + |
| 106 | + private IPAddress controller0Address; |
| 107 | + private IPAddress controller1Address; |
| 108 | + private IPAddress ipv4Address; |
| 109 | + private IPAddress ipv4Gateway; |
| 110 | + private IPAddress ipv4Netmask; |
| 111 | + private IPAddress ipv6Gateway; |
| 112 | + NetInterfaceId interfaceAlias; |
| 113 | + |
| 114 | + public override void ExecuteCmdlet() |
| 115 | + { |
| 116 | + if (!ProcessParameters()) |
| 117 | + { |
| 118 | + WriteObject(null); |
| 119 | + return; |
| 120 | + } |
| 121 | + try |
| 122 | + { |
| 123 | + var netConfig = new NetworkConfig |
| 124 | + { |
| 125 | + IsIscsiEnabled = EnableIscsi, |
| 126 | + IsCloudEnabled = EnableCloud, |
| 127 | + Controller0IPv4Address = controller0Address, |
| 128 | + Controller1IPv4Address = controller1Address, |
| 129 | + IPv6Gateway = ipv6Gateway, |
| 130 | + IPv4Gateway = ipv4Gateway, |
| 131 | + IPv4Address = ipv4Address, |
| 132 | + IPv6Prefix = IPv6Prefix, |
| 133 | + IPv4Netmask = ipv4Netmask, |
| 134 | + InterfaceAlias = interfaceAlias |
| 135 | + }; |
| 136 | + |
| 137 | + WriteObject(netConfig); |
| 138 | + WriteVerbose(string.Format(Resources.NewNetworkConfigCreated,InterfaceAlias.ToString())); |
| 139 | + return; |
| 140 | + } |
| 141 | + catch (Exception exception) |
| 142 | + { |
| 143 | + this.HandleException(exception); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + private bool ProcessParameters(){ |
| 148 | + // parse interfaceAlias |
| 149 | + if (!Enum.TryParse<NetInterfaceId>(InterfaceAlias, out interfaceAlias)) |
| 150 | + { |
| 151 | + WriteVerbose(string.Format(Resources.InvalidInterfaceId, InterfaceAlias)); |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + // Try and set all the IP address |
| 156 | + if (!TrySetIPAddress(Controller0IPv4Address, out controller0Address, "Controller0IPv4Address")) |
| 157 | + { |
| 158 | + return false; |
| 159 | + } |
| 160 | + if (!TrySetIPAddress(Controller1IPv4Address, out controller1Address, "Controller1IPv4Address")) |
| 161 | + { |
| 162 | + return false; |
| 163 | + } |
| 164 | + if (!TrySetIPAddress(IPv4Address, out ipv4Address, "IPv4Address")) |
| 165 | + { |
| 166 | + return false; |
| 167 | + } |
| 168 | + if (!TrySetIPAddress(IPv4Gateway, out ipv4Gateway, "IPv4Gateway")) |
| 169 | + { |
| 170 | + return false; |
| 171 | + } |
| 172 | + if (!TrySetIPAddress(IPv4Netmask, out ipv4Netmask, "IPv4Netmask")) |
| 173 | + { |
| 174 | + return false; |
| 175 | + } |
| 176 | + if(!TrySetIPAddress(IPv6Gateway, out ipv6Gateway, "IPv6Gateway")) |
| 177 | + { |
| 178 | + return false; |
| 179 | + } |
| 180 | + |
| 181 | + // Only EnableCloud, Controller0 and controller1 IP Addresses can be set on Data0 |
| 182 | + if (InterfaceAlias == NetInterfaceId.Data0.ToString()) |
| 183 | + { |
| 184 | + if(IPv4Address != null || IPv4Gateway != null || IPv4Netmask != null |
| 185 | + && IPv6Gateway != null || IPv6Prefix != null || EnableIscsi != null) |
| 186 | + { |
| 187 | + WriteVerbose(Resources.NetworkConfigData0AllowedSettings); |
| 188 | + return false; |
| 189 | + } |
| 190 | + } |
| 191 | + // On other interfaces (non-Data0), Controller0 and Controller1 IP Addresses cannot be set |
| 192 | + else |
| 193 | + { |
| 194 | + if (Controller0IPv4Address != null || Controller1IPv4Address != null) |
| 195 | + { |
| 196 | + WriteVerbose(Resources.NetworkConfigControllerIPsNotAllowedOnOthers); |
| 197 | + return false; |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + return true; |
| 202 | + } |
| 203 | + |
| 204 | + } |
| 205 | +} |
| 206 | + |
0 commit comments