Skip to content

Commit 20e3aa9

Browse files
committed
Fix PSVirtualMachine
1 parent b3bc453 commit 20e3aa9

File tree

5 files changed

+55
-10
lines changed

5 files changed

+55
-10
lines changed

src/ResourceManager/Compute/Commands.Compute.Test/ScenarioTests/VirtualMachineTests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ function Test-VirtualMachine
166166
$vm2 = Get-AzureVM -Name $vmname2 -ResourceGroupName $rgname;
167167
Assert-NotNull $vm2;
168168
Assert-AreEqual $vm2.AvailabilitySetId $asetId;
169+
Assert-True { $vm2.ResourceGroupName -eq $rgname }
169170

170171
# Remove
171172
Remove-AzureVM -Name $vmname2 -ResourceGroupName $rgname -Force;
@@ -191,6 +192,11 @@ function Test-VirtualMachineList
191192
$s1 = Get-AzureVM -All;
192193
$s2 = Get-AzureVM;
193194

195+
if ($s2 -ne $null)
196+
{
197+
Assert-True { $s2[0].ResourceGroupName -ne $null; }
198+
}
199+
194200
Assert-ThrowsContains { $s3 = Get-AzureVM -NextLink "http://www.test.com/test"; } "Unexpected character"
195201

196202
$passed = $true;

src/ResourceManager/Compute/Commands.Compute/Microsoft.Azure.Commands.Compute.Types.ps1xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<Name>ProvisioningState</Name>
1919
<Name>OSConfiguration</Name>
2020
<Name>SourceImageId</Name>
21+
<Name>ImageReference</Name>
2122
<Name>DestinationVHDsContainer</Name>
2223
<Name>OSDisk</Name>
2324
<Name>DataDisks</Name>

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

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using Microsoft.Azure.Management.Compute.Models;
16-
using System;
1716
using System.Collections.Generic;
1817
using System.Linq;
18+
using System.Text.RegularExpressions;
1919

2020
namespace Microsoft.Azure.Commands.Compute.Models
2121
{
@@ -41,7 +41,9 @@ public string VMSize
4141
}
4242

4343
public IDictionary<string, string> Tags { get; set; }
44+
4445
public string AvailabilitySetId { get; set; }
46+
4547
public string ProvisioningState { get; set; }
4648

4749
public string OSConfiguration
@@ -70,16 +72,29 @@ public string SourceImageId
7072
}
7173
}
7274

73-
public string OSDisk
75+
public ImageReference ImageReference
76+
{
77+
get
78+
{
79+
if (this.StorageProfile != null && this.StorageProfile.SourceImage != null)
80+
{
81+
return this.StorageProfile.ImageReference;
82+
}
83+
84+
return null;
85+
}
86+
}
87+
88+
public OSDisk OSDisk
7489
{
7590
get
7691
{
7792
if (this.StorageProfile != null && this.StorageProfile.OSDisk != null)
7893
{
79-
return this.StorageProfile.OSDisk.Name;
94+
return this.StorageProfile.OSDisk;
8095
}
8196

82-
return string.Empty;
97+
return null;
8398
}
8499
}
85100

@@ -111,8 +126,6 @@ public IList<string> NetworkInterfaces
111126

112127
public IList<VirtualMachineExtension> Extensions { get; set; }
113128

114-
public VirtualMachineInstanceView Status { get; set; }
115-
116129
public HardwareProfile HardwareProfile { get; set; }
117130

118131
public NetworkProfile NetworkProfile { get; set; }
@@ -136,15 +149,19 @@ public static PSVirtualMachine ToPSVirtualMachine(this VirtualMachineGetResponse
136149

137150
public static PSVirtualMachine ToPSVirtualMachine(this VirtualMachine virtualMachine, string rgName = null)
138151
{
152+
if (string.IsNullOrEmpty(rgName))
153+
{
154+
bool parsed = TryParseResourceGroupName(virtualMachine.Id, out rgName);
155+
}
156+
139157
PSVirtualMachine result = new PSVirtualMachine
140158
{
141159
ResourceGroupName = rgName,
142160
Name = virtualMachine == null ? null : virtualMachine.Name,
143161
Location = virtualMachine == null ? null : virtualMachine.Location,
144162
ProvisioningState = virtualMachine.ProvisioningState,
145163
Tags = virtualMachine.Tags,
146-
Extensions = virtualMachine.Extensions,
147-
Status = null, // TODO: VM response does not return Status info yet
164+
Extensions = virtualMachine.Extensions
148165
};
149166

150167
var asetRef = virtualMachine.AvailabilitySetReference;
@@ -176,5 +193,25 @@ public static List<PSVirtualMachine> ToPSVirtualMachineList(this VirtualMachineL
176193

177194
return results;
178195
}
196+
197+
private static bool TryParseResourceGroupName(string virtualMachineId, out string rgName)
198+
{
199+
const string group = "rgname";
200+
const string pattern = @"(.*?)/resourcegroups/(?<" + group + @">\S+)/providers/Microsoft.Compute/virtualMachines/(.*?)";
201+
202+
Regex r = new Regex(pattern, RegexOptions.IgnoreCase);
203+
Match m = r.Match(virtualMachineId);
204+
205+
if (m.Success)
206+
{
207+
rgName = m.Groups[group].Value;
208+
return true;
209+
}
210+
else
211+
{
212+
rgName = null;
213+
return false;
214+
}
215+
}
179216
}
180217
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class PSVirtualMachineInstanceView
3030
public IList<VirtualMachineExtensionInstanceView> Extensions { get; set; }
3131

3232
public int? PlatformFaultDomain { get; set; }
33+
3334
public int? PlatformUpdateDomain { get; set; }
3435

3536
public string RemoteDesktopThumbprint { get; set; }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ public override void ExecuteCmdlet()
117117
},
118118
SourceImage = string.IsNullOrEmpty(this.SourceImageUri) ? null : new VirtualHardDisk
119119
{
120-
Uri = SourceImageUri
120+
Uri = this.SourceImageUri
121121
},
122-
CreateOption = CreateOption
122+
CreateOption = this.CreateOption
123123
};
124124

125125
WriteObject(this.VM);

0 commit comments

Comments
 (0)