Skip to content

Commit af86129

Browse files
committed
Fix error case handling for device details related cmdlets
1 parent 07b47e1 commit af86129

File tree

4 files changed

+17
-45
lines changed

4 files changed

+17
-45
lines changed

src/ServiceManagement/StorSimple/Commands.StorSimple/Cmdlets/DeviceDetails/NewAzureStorSimpleNetworkConfig.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,9 @@ public class NewAzureStorSimpleNetworkConfig : StorSimpleCmdletBase
113113

114114
public override void ExecuteCmdlet()
115115
{
116-
if (!ProcessParameters())
117-
{
118-
WriteObject(null);
119-
return;
120-
}
121116
try
122117
{
118+
ProcessParameters();
123119
var netConfig = new NetworkConfig
124120
{
125121
IsIscsiEnabled = EnableIscsi,
@@ -144,12 +140,11 @@ public override void ExecuteCmdlet()
144140
}
145141
}
146142

147-
private bool ProcessParameters(){
143+
private void ProcessParameters(){
148144
// parse interfaceAlias
149145
if (!Enum.TryParse<NetInterfaceId>(InterfaceAlias, out interfaceAlias))
150146
{
151-
WriteVerbose(string.Format(Resources.InvalidInterfaceId, InterfaceAlias));
152-
return false;
147+
throw new ArgumentException(string.Format(Resources.InvalidInterfaceId, InterfaceAlias));
153148
}
154149

155150
// Try and set all the IP address
@@ -166,23 +161,18 @@ private bool ProcessParameters(){
166161
if(IPv4Address != null || IPv4Gateway != null || IPv4Netmask != null
167162
&& IPv6Gateway != null || IPv6Prefix != null || EnableCloud != null)
168163
{
169-
WriteVerbose(Resources.NetworkConfigData0AllowedSettings);
170-
return false;
164+
throw new ArgumentException(Resources.NetworkConfigData0AllowedSettings);
171165
}
172166
}
173167
// On other interfaces (non-Data0), Controller0 and Controller1 IP Addresses cannot be set
174168
else
175169
{
176170
if (Controller0IPv4Address != null || Controller1IPv4Address != null)
177171
{
178-
WriteVerbose(Resources.NetworkConfigControllerIPsNotAllowedOnOthers);
179-
return false;
172+
throw new ArgumentException(Resources.NetworkConfigControllerIPsNotAllowedOnOthers);
180173
}
181174
}
182-
183-
return true;
184175
}
185-
186176
}
187177
}
188178

src/ServiceManagement/StorSimple/Commands.StorSimple/Cmdlets/DeviceDetails/SetAzureStorSimpleDevice.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -82,35 +82,25 @@ public override void ExecuteCmdlet()
8282
try
8383
{
8484
// Make sure params were supplied appropriately.
85-
if (!ProcessParameters())
86-
{
87-
return;
88-
}
85+
ProcessParameters();
8986

9087
// Get the current device details.
9188
var deviceDetails = StorSimpleClient.GetDeviceDetails(DeviceId);
9289

9390
if (deviceDetails == null)
9491
{
95-
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenIdInResourceMessage, StorSimpleContext.ResourceName, DeviceId));
96-
WriteObject(null);
97-
return;
92+
throw new ArgumentException(string.Format(Resources.NoDeviceFoundWithGivenIdInResourceMessage, StorSimpleContext.ResourceName, DeviceId));
9893
}
9994

10095
// If the device is being configured for the first time, validate that mandatory params
10196
// for first setup have been provided
10297
if (!deviceDetails.DeviceProperties.IsConfigUpdated && !ValidParamsForFirstDeviceConfiguration(StorSimpleNetworkConfig, TimeZone, SecondaryDnsServer))
10398
{
104-
WriteVerbose(Resources.MandatoryParamsMissingForInitialDeviceConfiguration);
105-
WriteObject(null);
106-
return;
107-
}
108-
109-
if (!this.ValidateNetworkConfigs(deviceDetails, StorSimpleNetworkConfig))
110-
{
111-
return;
99+
throw new ArgumentException(Resources.MandatoryParamsMissingForInitialDeviceConfiguration);
112100
}
113101

102+
// Validate Network configs - this method throws an exception if any validation fails
103+
ValidateNetworkConfigs(deviceDetails, StorSimpleNetworkConfig);
114104

115105
WriteVerbose(string.Format(Resources.BeginningDeviceConfiguration, deviceDetails.DeviceProperties.FriendlyName));
116106

@@ -133,22 +123,19 @@ public override void ExecuteCmdlet()
133123
}
134124
}
135125

136-
private bool ProcessParameters()
126+
private void ProcessParameters()
137127
{
138128
// Make sure that the DeviceId property has the appropriate value irrespective of the parameter set
139129
if (ParameterSetName == StorSimpleCmdletParameterSet.IdentifyByName)
140130
{
141131
var deviceId = StorSimpleClient.GetDeviceId(DeviceName);
142132
if (deviceId == null)
143133
{
144-
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
145-
WriteObject(null);
146-
return false;
134+
throw new ArgumentException(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
147135
}
148136
DeviceId = deviceId;
149137
}
150138
TrySetIPAddress(SecondaryDnsServer, out secondaryDnsServer, "SecondaryDnsServer");
151-
return true;
152139
}
153140
}
154141
}

src/ServiceManagement/StorSimple/Commands.StorSimple/Cmdlets/DeviceDetails/SetAzureStorSimpleVirtualDevice.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,7 @@ public override void ExecuteCmdlet()
6161
var deviceId = StorSimpleClient.GetDeviceId(DeviceName);
6262
if (deviceId == null)
6363
{
64-
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
65-
WriteObject(null);
66-
return;
64+
throw new ArgumentException(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
6765
}
6866

6967
// Get the current device details. ( If we found id from the name, this call is bound to succeed)

src/ServiceManagement/StorSimple/Commands.StorSimple/StorSimpleCmdletBase.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -414,12 +414,11 @@ internal Exception GetGenericException(string exceptionMessage, Exception innerE
414414
/// Its mandatory to provide either (IPv4 Address and netmask) or IPv6 orefix for an interface that
415415
/// is being enabled. ( Was previously disabled and is now being configured)
416416
/// </summary>
417-
/// <returns></returns>
418-
internal bool ValidateNetworkConfigs(DeviceDetails details, NetworkConfig[] StorSimpleNetworkConfig)
417+
internal void ValidateNetworkConfigs(DeviceDetails details, NetworkConfig[] StorSimpleNetworkConfig)
419418
{
420419
if (StorSimpleNetworkConfig == null)
421420
{
422-
return true;
421+
return;
423422
}
424423
foreach (var netConfig in StorSimpleNetworkConfig)
425424
{
@@ -431,13 +430,10 @@ internal bool ValidateNetworkConfigs(DeviceDetails details, NetworkConfig[] Stor
431430
// If its not an enabled interface either IPv6(prefix) or IPv4(address and mask) must be provided.
432431
if ((netConfig.IPv4Address == null || netConfig.IPv4Netmask == null) && netConfig.IPv6Prefix == null)
433432
{
434-
WriteVerbose(string.Format(Resources.IPAddressesNotProvidedForNetInterfaceBeingEnabled, StorSimpleContext.ResourceName, details.DeviceProperties.DeviceId));
435-
WriteObject(null);
436-
return false;
433+
throw new ArgumentException(string.Format(Resources.IPAddressesNotProvidedForNetInterfaceBeingEnabled, StorSimpleContext.ResourceName, details.DeviceProperties.DeviceId));
437434
}
438435
}
439436
}
440-
return true;
441437
}
442438

443439
/// <summary>
@@ -451,6 +447,7 @@ internal void TrySetIPAddress(string data, out IPAddress ipAddress, string param
451447
if (data == null)
452448
{
453449
ipAddress = null;
450+
return;
454451
}
455452
try
456453
{

0 commit comments

Comments
 (0)