Skip to content

Commit 39d57bf

Browse files
unknownunknown
unknown
authored and
unknown
committed
Fix piping issue for Remove-AzureVMDataDisk
and Remove-AzureVMNetworkInterface
1 parent 58fcb41 commit 39d57bf

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/ResourceManager/Compute/Commands.Compute/Models/PSVirtualMachine.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public string ResourceGroupName
3333
{
3434
get
3535
{
36+
if (string.IsNullOrEmpty(Id)) return null;
3637
Regex r = new Regex(@"(.*?)/resourcegroups/(?<rgname>\S+)/providers/(.*?)", RegexOptions.IgnoreCase);
3738
Match m = r.Match(Id);
3839
return m.Success ? m.Groups["rgname"].Value : null;
@@ -134,5 +135,37 @@ public string StorageProfileText
134135
{
135136
get { return JsonConvert.SerializeObject(StorageProfile, Formatting.Indented); }
136137
}
138+
139+
[JsonIgnore]
140+
public string [] DataDiskNames
141+
{
142+
get
143+
{
144+
if (this.StorageProfile == null) return null;
145+
146+
var dataDiskNames = new List<string>();
147+
foreach (var disk in StorageProfile.DataDisks)
148+
{
149+
dataDiskNames.Add(disk.Name);
150+
}
151+
return dataDiskNames.ToArray();
152+
}
153+
}
154+
155+
[JsonIgnore]
156+
public string[] NicIds
157+
{
158+
get
159+
{
160+
if (this.NetworkProfile == null) return null;
161+
162+
var nicIds = new List<string>();
163+
foreach (var nic in NetworkProfile.NetworkInterfaces)
164+
{
165+
nicIds.Add(nic.ReferenceUri);
166+
}
167+
return nicIds.ToArray();
168+
}
169+
}
137170
}
138171
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,14 @@ public class RemoveAzureVMDataDiskCommand : AzurePSCmdlet
3838
[ValidateNotNullOrEmpty]
3939
public PSVirtualMachine VM { get; set; }
4040

41+
[Alias("Name")]
4142
[Parameter(
4243
Mandatory = true,
4344
Position = 1,
4445
ValueFromPipelineByPropertyName = true,
4546
HelpMessage = HelpMessages.VMDataDiskName)]
4647
[ValidateNotNullOrEmpty]
47-
public string Name { get; set; }
48+
public string [] DataDiskNames { get; set; }
4849

4950
public override void ExecuteCmdlet()
5051
{
@@ -54,7 +55,10 @@ public override void ExecuteCmdlet()
5455
{
5556
var disks = storageProfile.DataDisks.ToList();
5657
var comp = StringComparison.OrdinalIgnoreCase;
57-
disks.RemoveAll(d => string.Equals(d.Name, this.Name, comp));
58+
foreach (var diskName in DataDiskNames)
59+
{
60+
disks.RemoveAll(d => string.Equals(d.Name, diskName, comp));
61+
}
5862
storageProfile.DataDisks = disks;
5963
}
6064

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,35 @@ public class RemoveAzureVMNetworkInterfaceCommand : AzurePSCmdlet
4141
[ValidateNotNullOrEmpty]
4242
public PSVirtualMachine VM { get; set; }
4343

44-
[Alias("NicId", "NetworkInterfaceId")]
44+
[Alias("Id", "NetworkInterfaceId")]
4545
[Parameter(
4646
Mandatory = true,
4747
Position = 1,
4848
ValueFromPipelineByPropertyName = true,
4949
HelpMessage = HelpMessages.VMNetworkInterfaceID)]
5050
[ValidateNotNullOrEmpty]
51-
public string Id { get; set; }
51+
public string [] NicIds { get; set; }
5252

5353
public override void ExecuteCmdlet()
5454
{
5555
var networkProfile = this.VM.NetworkProfile;
5656

57-
if (networkProfile != null && networkProfile.NetworkInterfaces != null && networkProfile.NetworkInterfaces.Any(nic => string.Equals(nic.ReferenceUri, this.Id, StringComparison.OrdinalIgnoreCase)))
57+
foreach (var id in this.NicIds)
5858
{
59-
var nicReference = networkProfile.NetworkInterfaces.First(nic => string.Equals(nic.ReferenceUri, this.Id, StringComparison.OrdinalIgnoreCase));
60-
networkProfile.NetworkInterfaces.Remove(nicReference);
61-
62-
if (!networkProfile.NetworkInterfaces.Any())
59+
if (networkProfile != null &&
60+
networkProfile.NetworkInterfaces != null &&
61+
networkProfile.NetworkInterfaces.Any(nic =>
62+
string.Equals(nic.ReferenceUri, id, StringComparison.OrdinalIgnoreCase)))
6363
{
64-
networkProfile = null;
64+
var nicReference = networkProfile.NetworkInterfaces.First(nic => string.Equals(nic.ReferenceUri, id, StringComparison.OrdinalIgnoreCase));
65+
networkProfile.NetworkInterfaces.Remove(nicReference);
6566
}
6667
}
6768

69+
if (!networkProfile.NetworkInterfaces.Any())
70+
{
71+
networkProfile = null;
72+
}
6873
this.VM.NetworkProfile = networkProfile;
6974

7075
WriteObject(this.VM);

0 commit comments

Comments
 (0)