Skip to content

Commit c590953

Browse files
committed
Update Set-AzureVMDataDisk cmdlet
1 parent bbd0447 commit c590953

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/ResourceManager/Compute/Commands.Compute/VirtualMachine/Config/SetAzureVMDataDiskCommand.cs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,18 @@ public class SetAzureVMDataDiskCommand : AzurePSCmdlet
5353
[ValidateNotNullOrEmpty]
5454
public string Name { get; set; }
5555

56+
[Parameter(
57+
Mandatory = true,
58+
Position = 1,
59+
ParameterSetName = LunParameterSet,
60+
ValueFromPipelineByPropertyName = true,
61+
HelpMessage = HelpMessages.VMDataDiskLun)]
62+
[ValidateNotNullOrEmpty]
63+
public int? Lun { get; set; }
64+
5665
[Parameter(
5766
Mandatory = false,
58-
Position = 3,
67+
Position = 2,
5968
ValueFromPipelineByPropertyName = true,
6069
HelpMessage = HelpMessages.VMDataDiskCaching)]
6170
[ValidateNotNullOrEmpty]
@@ -64,33 +73,19 @@ public class SetAzureVMDataDiskCommand : AzurePSCmdlet
6473

6574
[Parameter(
6675
Mandatory = false,
67-
Position = 4,
76+
Position = 3,
6877
ValueFromPipelineByPropertyName = true,
6978
HelpMessage = HelpMessages.VMDataDiskSizeInGB)]
7079
[AllowNull]
7180
public int? DiskSizeInGB { get; set; }
7281

73-
[Parameter(
74-
Mandatory = true,
75-
Position = 5,
76-
ParameterSetName = LunParameterSet,
77-
ValueFromPipelineByPropertyName = true,
78-
HelpMessage = HelpMessages.VMDataDiskLun)]
79-
[ValidateNotNullOrEmpty]
80-
public int? Lun { get; set; }
81-
8282
public override void ExecuteCmdlet()
8383
{
8484
var storageProfile = this.VM.StorageProfile;
8585

86-
if (storageProfile == null)
86+
if (storageProfile == null || storageProfile.DataDisks == null)
8787
{
88-
storageProfile = new StorageProfile();
89-
}
90-
91-
if (storageProfile.DataDisks == null)
92-
{
93-
storageProfile.DataDisks = new List<DataDisk>();
88+
ThrowDataDiskNotExistError();
9489
}
9590

9691
var dataDisk = (this.ParameterSetName.Equals(LunParameterSet))
@@ -99,15 +94,7 @@ public override void ExecuteCmdlet()
9994

10095
if (dataDisk == null)
10196
{
102-
var missingDisk = (this.ParameterSetName.Equals(LunParameterSet))
103-
? string.Format("LUN # {0}", this.Lun)
104-
: "Name: " + this.Name;
105-
ThrowTerminatingError(
106-
new ErrorRecord(
107-
new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, Properties.Resources.DataDiskNotAssignedForVM, missingDisk)),
108-
string.Empty,
109-
ErrorCategory.InvalidData,
110-
null));
97+
ThrowDataDiskNotExistError();
11198
}
11299
else
113100
{
@@ -125,5 +112,20 @@ public override void ExecuteCmdlet()
125112

126113
WriteObject(this.VM);
127114
}
115+
116+
private void ThrowDataDiskNotExistError()
117+
{
118+
var missingDisk = (this.ParameterSetName.Equals(LunParameterSet))
119+
? string.Format("LUN # {0}", this.Lun)
120+
: "Name: " + this.Name;
121+
122+
ThrowTerminatingError
123+
(new ErrorRecord(
124+
new InvalidOperationException(string.Format(CultureInfo.InvariantCulture,
125+
Properties.Resources.DataDiskNotAssignedForVM, missingDisk)),
126+
string.Empty,
127+
ErrorCategory.InvalidData,
128+
null));
129+
}
128130
}
129131
}

0 commit comments

Comments
 (0)