Skip to content

Commit 79ca0ee

Browse files
committed
Merge pull request #33 from AzCiS/fix-exceptions
Fix exceptions
2 parents e1c3bcb + f51a4bc commit 79ca0ee

File tree

6 files changed

+32
-97
lines changed

6 files changed

+32
-97
lines changed

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

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,9 @@ public class NewAzureStorSimpleNetworkConfig : StorSimpleCmdletBase
114114

115115
public override void ExecuteCmdlet()
116116
{
117-
if (!ProcessParameters())
118-
{
119-
WriteObject(null);
120-
return;
121-
}
122117
try
123118
{
119+
ProcessParameters();
124120
var netConfig = new NetworkConfig
125121
{
126122
IsIscsiEnabled = EnableIscsi,
@@ -145,63 +141,39 @@ public override void ExecuteCmdlet()
145141
}
146142
}
147143

148-
private bool ProcessParameters(){
144+
private void ProcessParameters(){
149145
// parse interfaceAlias
150146
if (!Enum.TryParse<NetInterfaceId>(InterfaceAlias, out interfaceAlias))
151147
{
152-
WriteVerbose(string.Format(Resources.InvalidInterfaceId, InterfaceAlias));
153-
return false;
148+
throw new ArgumentException(string.Format(Resources.InvalidInterfaceId, InterfaceAlias));
154149
}
155150

156151
// Try and set all the IP address
157-
if (!TrySetIPAddress(Controller0IPv4Address, out controller0Address, "Controller0IPv4Address"))
158-
{
159-
return false;
160-
}
161-
if (!TrySetIPAddress(Controller1IPv4Address, out controller1Address, "Controller1IPv4Address"))
162-
{
163-
return false;
164-
}
165-
if (!TrySetIPAddress(IPv4Address, out ipv4Address, "IPv4Address"))
166-
{
167-
return false;
168-
}
169-
if (!TrySetIPAddress(IPv4Gateway, out ipv4Gateway, "IPv4Gateway"))
170-
{
171-
return false;
172-
}
173-
if (!TrySetIPAddress(IPv4Netmask, out ipv4Netmask, "IPv4Netmask"))
174-
{
175-
return false;
176-
}
177-
if(!TrySetIPAddress(IPv6Gateway, out ipv6Gateway, "IPv6Gateway"))
178-
{
179-
return false;
180-
}
152+
TrySetIPAddress(Controller0IPv4Address, out controller0Address, "Controller0IPv4Address");
153+
TrySetIPAddress(Controller1IPv4Address, out controller1Address, "Controller1IPv4Address");
154+
TrySetIPAddress(IPv4Address, out ipv4Address, "IPv4Address");
155+
TrySetIPAddress(IPv4Gateway, out ipv4Gateway, "IPv4Gateway");
156+
TrySetIPAddress(IPv4Netmask, out ipv4Netmask, "IPv4Netmask");
157+
TrySetIPAddress(IPv6Gateway, out ipv6Gateway, "IPv6Gateway");
181158

182159
// Only EnableIscsi, Controller0 and controller1 IP Addresses can be set on Data0
183160
if (InterfaceAlias == NetInterfaceId.Data0.ToString())
184161
{
185162
if(IPv4Address != null || IPv4Gateway != null || IPv4Netmask != null
186163
&& IPv6Gateway != null || IPv6Prefix != null || EnableCloud != null)
187164
{
188-
WriteVerbose(Resources.NetworkConfigData0AllowedSettings);
189-
return false;
165+
throw new ArgumentException(Resources.NetworkConfigData0AllowedSettings);
190166
}
191167
}
192168
// On other interfaces (non-Data0), Controller0 and Controller1 IP Addresses cannot be set
193169
else
194170
{
195171
if (Controller0IPv4Address != null || Controller1IPv4Address != null)
196172
{
197-
WriteVerbose(Resources.NetworkConfigControllerIPsNotAllowedOnOthers);
198-
return false;
173+
throw new ArgumentException(Resources.NetworkConfigControllerIPsNotAllowedOnOthers);
199174
}
200175
}
201-
202-
return true;
203176
}
204-
205177
}
206178
}
207179

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

Lines changed: 8 additions & 24 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,7 +123,7 @@ public override void ExecuteCmdlet()
133123
}
134124
}
135125

136-
private bool ProcessParameters()
126+
private void ProcessParameters()
137127
{
138128
// Make sure that atleast one of the settings has been provided.
139129
// Its no use making a request when the user didnt specify any of
@@ -151,17 +141,11 @@ private bool ProcessParameters()
151141
var deviceId = StorSimpleClient.GetDeviceId(DeviceName);
152142
if (deviceId == null)
153143
{
154-
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
155-
WriteObject(null);
156-
return false;
144+
throw new ArgumentException(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
157145
}
158146
DeviceId = deviceId;
159147
}
160-
if (!TrySetIPAddress(SecondaryDnsServer, out secondaryDnsServer, "SecondaryDnsServer"))
161-
{
162-
return false;
163-
}
164-
return true;
148+
TrySetIPAddress(SecondaryDnsServer, out secondaryDnsServer, "SecondaryDnsServer");
165149
}
166150
}
167151
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ public override void ExecuteCmdlet()
7777
var deviceId = StorSimpleClient.GetDeviceId(DeviceName);
7878
if (deviceId == null)
7979
{
80-
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
81-
WriteObject(null);
82-
return;
80+
throw new ArgumentException(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage, StorSimpleContext.ResourceName, DeviceName));
8381
}
8482

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

src/ServiceManagement/StorSimple/Commands.StorSimple/Cmdlets/DeviceJobs/GetAzureStorSimpleJob.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ public override void ExecuteCmdlet()
103103
try
104104
{
105105
// Make sure params were supplied appropriately.
106-
if (!ProcessParameters())
107-
{
108-
return;
109-
}
106+
ProcessParameters();
110107

111108
// Make call to get device jobs.
112109
var response = StorSimpleClient.GetDeviceJobs(deviceId, Type, Status, InstanceId, fromDateTimeIsoString, toDateTimeIsoString, (int)Skip, (int)First);
@@ -115,9 +112,7 @@ public override void ExecuteCmdlet()
115112
{
116113
if (response == null || response.DeviceJobList.Count < 1)
117114
{
118-
WriteVerbose(string.Format(Resources.NoDeviceJobFoundWithGivenIdMessage, InstanceId));
119-
WriteObject(null);
120-
return;
115+
throw new ArgumentException(string.Format(Resources.NoDeviceJobFoundWithGivenIdMessage, InstanceId));
121116
}
122117
WriteObject(response.DeviceJobList.First());
123118
return;
@@ -155,7 +150,7 @@ public override void ExecuteCmdlet()
155150
}
156151
}
157152

158-
private bool ProcessParameters()
153+
private void ProcessParameters()
159154
{
160155
// Default values for first and skip are 0
161156
First = First ?? 0;
@@ -174,13 +169,9 @@ private bool ProcessParameters()
174169
deviceId = StorSimpleClient.GetDeviceId(DeviceName);
175170
if (deviceId == null)
176171
{
177-
WriteVerbose(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage,StorSimpleContext.ResourceName, DeviceName));
178-
WriteObject(null);
179-
return false;
172+
throw new ArgumentException(string.Format(Resources.NoDeviceFoundWithGivenNameInResourceMessage,StorSimpleContext.ResourceName, DeviceName));
180173
}
181174
}
182-
183-
return true;
184175
}
185176
}
186177
}

src/ServiceManagement/StorSimple/Commands.StorSimple/Cmdlets/DeviceJobs/StopAzureStorSimpleJob.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,13 @@ public override void ExecuteCmdlet()
5757
var deviceJobDetails = StorSimpleClient.GetDeviceJobById(InstanceId);
5858
if (deviceJobDetails == null)
5959
{
60-
WriteVerbose(string.Format(Resources.NoDeviceJobFoundWithGivenIdMessage, InstanceId));
61-
WriteObject(null);
62-
return;
60+
throw new ArgumentException(string.Format(Resources.NoDeviceJobFoundWithGivenIdMessage, InstanceId));
6361
}
6462

6563
// Make sure the job is running and cancellable, else fail.
6664
if (!(deviceJobDetails.IsJobCancellable && deviceJobDetails.Status == "Running"))
6765
{
68-
WriteVerbose(string.Format(Resources.JobNotRunningOrCancellable, InstanceId));
69-
WriteObject(null);
70-
return;
66+
throw new ArgumentException(string.Format(Resources.JobNotRunningOrCancellable, InstanceId));
7167
}
7268

7369
// issue call to cancel the job.

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

Lines changed: 6 additions & 12 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>
@@ -446,23 +442,21 @@ internal bool ValidateNetworkConfigs(DeviceDetails details, NetworkConfig[] Stor
446442
/// <param name="data">IP Address string</param>
447443
/// <param name="ipAddress"></param>
448444
/// <param name="paramName">Name of the param which is being processed (to be used for errors)</param>
449-
internal bool TrySetIPAddress(string data, out IPAddress ipAddress, string paramName)
445+
internal void TrySetIPAddress(string data, out IPAddress ipAddress, string paramName)
450446
{
451447
if (data == null)
452448
{
453449
ipAddress = null;
454-
return true;
450+
return;
455451
}
456452
try
457453
{
458454
ipAddress = IPAddress.Parse(data);
459-
return true;
460455
}
461456
catch (FormatException)
462457
{
463458
ipAddress = null;
464-
WriteVerbose(string.Format(Resources.InvalidIPAddressProvidedMessage, paramName));
465-
return false;
459+
throw new ArgumentException(string.Format(Resources.InvalidIPAddressProvidedMessage, paramName));
466460
}
467461
}
468462

0 commit comments

Comments
 (0)