Skip to content

Commit bc6fc13

Browse files
committed
Merge pull request #8 from AzCiS/device-config
Device config and Device Job related cmdlet and test changes
2 parents d3a9bd5 + ac32b72 commit bc6fc13

16 files changed

+1520
-8
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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+
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
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.Linq;
17+
using System.Management.Automation;
18+
using Hyak.Common;
19+
using Microsoft.WindowsAzure.Management.StorSimple.Models;
20+
using Microsoft.WindowsAzure.Commands.StorSimple.Properties;
21+
using System.Net.Sockets;
22+
using System.Net;
23+
using System.Collections.Generic;
24+
25+
namespace Microsoft.WindowsAzure.Commands.StorSimple.Cmdlets
26+
{
27+
/// <summary>
28+
/// Update device config for an Azure StorSimple Device.
29+
///
30+
/// If the device is being configured for the first time, then some of the
31+
/// arguments will be mandatory - TimeZone, PrimaryDnsServer, Config for Data0
32+
/// </summary>
33+
[Cmdlet(VerbsCommon.Set, "AzureStorSimpleDevice", DefaultParameterSetName=StorSimpleCmdletParameterSet.IdentifyByName), OutputType(typeof(DeviceDetails))]
34+
public class SetAzureStorSimpleDevice : StorSimpleCmdletBase
35+
{
36+
/// <summary>
37+
/// Device Id of the device to configure.
38+
/// </summary>
39+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = StorSimpleCmdletParameterSet.IdentifyById, HelpMessage = StorSimpleCmdletHelpMessage.DeviceId)]
40+
[ValidateNotNullOrEmpty]
41+
public string DeviceId { get; set; }
42+
43+
/// <summary>
44+
/// Friendly Name of the device to configure.
45+
/// </summary>
46+
[Parameter(Mandatory = true, Position = 0, ParameterSetName = StorSimpleCmdletParameterSet.IdentifyByName, HelpMessage = StorSimpleCmdletHelpMessage.DeviceName)]
47+
[ValidateNotNullOrEmpty]
48+
public string DeviceName { get; set; }
49+
50+
/// <summary>
51+
/// New friendly name for the device.
52+
/// </summary>
53+
[Parameter(Mandatory = false, Position = 1, HelpMessage = StorSimpleCmdletHelpMessage.NewDeviceName)]
54+
[ValidateNotNullOrEmpty]
55+
public string NewName { get; set; }
56+
57+
/// <summary>
58+
/// TimeZone for the device.
59+
/// </summary>
60+
[Parameter(Mandatory = false, Position = 2, HelpMessage = StorSimpleCmdletHelpMessage.TimeZone)]
61+
[ValidateNotNullOrEmpty]
62+
public TimeZoneInfo TimeZone { get; set; }
63+
64+
/// <summary>
65+
/// Primary DNS server for the device.
66+
/// </summary>
67+
[Parameter(Mandatory = false, Position = 3, HelpMessage = StorSimpleCmdletHelpMessage.PrimaryDnsServer)]
68+
[ValidateNotNullOrEmpty]
69+
public string PrimaryDnsServer { get; set; }
70+
71+
/// <summary>
72+
/// Secondary DNS server for the device.
73+
/// </summary>
74+
[Parameter(Mandatory = false, Position = 4, HelpMessage = StorSimpleCmdletHelpMessage.SecondaryDnsServer)]
75+
[ValidateNotNullOrEmpty]
76+
public string SecondaryDnsServer { get; set; }
77+
78+
/// <summary>
79+
/// A collection of network configs for interfaces on the device.
80+
/// </summary>
81+
[Parameter(Mandatory = false, Position = 5, HelpMessage = StorSimpleCmdletHelpMessage.StorSimpleNetworkConfig)]
82+
[ValidateNotNullOrEmpty]
83+
public NetworkConfig[] StorSimpleNetworkConfig { get; set; }
84+
85+
private IPAddress primaryDnsServer;
86+
private IPAddress secondaryDnsServer;
87+
88+
public override void ExecuteCmdlet()
89+
{
90+
try
91+
{
92+
// Make sure params were supplied appropriately.
93+
if (!ProcessParameters())
94+
{
95+
return;
96+
}
97+
98+
// Get the current device details.
99+
var deviceDetails = StorSimpleClient.GetDeviceDetails(DeviceId);
100+
101+
if (deviceDetails == null)
102+
{
103+
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenIdInResourceMessage, StorSimpleContext.ResourceName, DeviceId));
104+
WriteObject(null);
105+
return;
106+
}
107+
108+
// If the device is being configured for the first time, validate that mandatory params
109+
// for first setup have been provided
110+
if (!this.IsDeviceConfigurationCompleteForDevice(deviceDetails) && !ValidParamsForFirstDeviceConfiguration(StorSimpleNetworkConfig, TimeZone,PrimaryDnsServer))
111+
{
112+
WriteVerbose(Resources.MandatoryParamsMissingForInitialDeviceConfiguration);
113+
WriteObject(null);
114+
return;
115+
}
116+
117+
if (!this.ValidateNetworkConfigs(deviceDetails, StorSimpleNetworkConfig))
118+
{
119+
return;
120+
}
121+
122+
123+
WriteVerbose(string.Format(Resources.BeginningDeviceConfiguration, deviceDetails.DeviceProperties.FriendlyName));
124+
125+
// Update device details objects with the details provided to the cmdlet
126+
// and make request with updated data
127+
var taskStatusInfo = StorSimpleClient.UpdateDeviceDetails(deviceDetails, this.NewName, this.TimeZone, this.primaryDnsServer, this.secondaryDnsServer, this.StorSimpleNetworkConfig);
128+
129+
HandleSyncTaskResponse(taskStatusInfo, "Setup");
130+
if (taskStatusInfo.AsyncTaskAggregatedResult == AsyncTaskAggregatedResult.Succeeded)
131+
{
132+
var updatedDetails = StorSimpleClient.GetDeviceDetails(DeviceId.ToString());
133+
WriteObject(updatedDetails);
134+
WriteVerbose(string.Format(Resources.StorSimpleDeviceUpdatedSuccessfully, updatedDetails.DeviceProperties.FriendlyName, updatedDetails.DeviceProperties.DeviceId));
135+
}
136+
137+
}
138+
catch (Exception exception)
139+
{
140+
this.HandleException(exception);
141+
}
142+
}
143+
144+
private bool ProcessParameters()
145+
{
146+
// Make sure that the DeviceId property has the appropriate value irrespective of the parameter set
147+
if (ParameterSetName == StorSimpleCmdletParameterSet.IdentifyByName)
148+
{
149+
var deviceId = StorSimpleClient.GetDeviceId(DeviceName);
150+
if (deviceId == null)
151+
{
152+
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
153+
WriteObject(null);
154+
return false;
155+
}
156+
DeviceId = deviceId;
157+
}
158+
if (!TrySetIPAddress(PrimaryDnsServer, out primaryDnsServer, "PrimaryDnsServer"))
159+
{
160+
return false;
161+
}
162+
if (!TrySetIPAddress(SecondaryDnsServer, out secondaryDnsServer, "SecondaryDnsServer"))
163+
{
164+
return false;
165+
}
166+
return true;
167+
}
168+
}
169+
}
170+

0 commit comments

Comments
 (0)