Skip to content

Commit fea53ee

Browse files
author
nimisha
committed
Implemented Get-AzureRmVMChefExtension command
1 parent 9ed63a6 commit fea53ee

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/ResourceManager/Compute/Commands.Compute/Commands.Compute.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@
234234
<Compile Include="Extension\AzureVMBackup\SetAzureVMBackupExtension.cs" />
235235
<Compile Include="Extension\BGInfo\SetAzureVMBGInfoExtension.cs" />
236236
<Compile Include="Extension\BGInfo\VirtualMachineBGInfoExtensionContext.cs" />
237+
<Compile Include="Extension\Chef\GetAzureRmVMChefExtension.cs" />
237238
<Compile Include="Extension\Chef\RemoveAzureRmVMChefExtension.cs" />
238239
<Compile Include="Extension\Chef\SetAzureVMChefExtension.cs" />
239240
<Compile Include="Extension\CustomScript\GetAzureVMCustomScriptExtensionCommand.cs" />
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
using System;
2+
using System.Linq;
3+
using System.Management.Automation;
4+
using Microsoft.Azure.Commands.Compute.Common;
5+
using Microsoft.Azure.Commands.Compute.Models;
6+
using Microsoft.Azure.Management.Compute;
7+
using Microsoft.Azure.Management.Compute.Models;
8+
using Microsoft.Rest.Azure;
9+
10+
namespace Microsoft.Azure.Commands.Compute.Extension.Chef
11+
{
12+
[Cmdlet(
13+
VerbsCommon.Get,
14+
ProfileNouns.VirtualMachineChefExtension),
15+
OutputType(
16+
typeof(PSVirtualMachineExtension))]
17+
public class GetAzureRmVMChefExtension : VirtualMachineExtensionBaseCmdlet
18+
{
19+
private string ExtensionDefaultPublisher = "Chef.Bootstrap.WindowsAzure";
20+
private string ExtensionDefaultName = "ChefClient";
21+
private string LinuxExtensionName = "LinuxChefClient";
22+
23+
protected const string LinuxParameterSetName = "Linux";
24+
protected const string WindowsParameterSetName = "Windows";
25+
26+
[Parameter(
27+
Mandatory = true,
28+
Position = 0,
29+
ValueFromPipelineByPropertyName = true,
30+
HelpMessage = "The resource group name.")]
31+
[ValidateNotNullOrEmpty]
32+
public string ResourceGroupName { get; set; }
33+
34+
[Alias("ResourceName")]
35+
[Parameter(
36+
Mandatory = true,
37+
Position = 1,
38+
ValueFromPipelineByPropertyName = true,
39+
HelpMessage = "The virtual machine name.")]
40+
[ValidateNotNullOrEmpty]
41+
public string VMName { get; set; }
42+
43+
[Alias("ExtensionName")]
44+
[Parameter(
45+
Mandatory = false,
46+
Position = 2,
47+
ValueFromPipelineByPropertyName = true,
48+
HelpMessage = "Extension Name.")]
49+
public string Name { get; set; }
50+
51+
[Parameter(
52+
Position = 3,
53+
ValueFromPipelineByPropertyName = true,
54+
HelpMessage = "To show the status.")]
55+
[ValidateNotNullOrEmpty]
56+
public SwitchParameter Status { get; set; }
57+
58+
[Parameter(
59+
Mandatory = true,
60+
ParameterSetName = LinuxParameterSetName,
61+
HelpMessage = "Set extension for Linux.")]
62+
public SwitchParameter Linux { get; set; }
63+
64+
[Parameter(
65+
Mandatory = true,
66+
ParameterSetName = WindowsParameterSetName,
67+
HelpMessage = "Set extension for Windows.")]
68+
public SwitchParameter Windows { get; set; }
69+
70+
public override void ExecuteCmdlet()
71+
{
72+
base.ExecuteCmdlet();
73+
74+
if (this.Linux.IsPresent)
75+
{
76+
this.Name = LinuxExtensionName;
77+
}
78+
else if (this.Windows.IsPresent)
79+
{
80+
this.Name = ExtensionDefaultName;
81+
}
82+
83+
ExecuteClientAction(() =>
84+
{
85+
if (string.IsNullOrEmpty(this.Name))
86+
{
87+
var virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
88+
var chefExtension = virtualMachine.Resources != null
89+
? virtualMachine.Resources.FirstOrDefault(extension =>
90+
extension.Publisher.Equals(ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
91+
extension.VirtualMachineExtensionType.Equals(this.Name, StringComparison.InvariantCultureIgnoreCase))
92+
: null;
93+
94+
if (chefExtension == null)
95+
{
96+
WriteObject(null);
97+
return;
98+
}
99+
else
100+
{
101+
this.Name = chefExtension.Name;
102+
}
103+
}
104+
105+
AzureOperationResponse<VirtualMachineExtension> virtualMachineExtensionGetResponse = null;
106+
if (Status.IsPresent)
107+
{
108+
virtualMachineExtensionGetResponse =
109+
this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName,
110+
this.VMName, this.Name);
111+
}
112+
else
113+
{
114+
virtualMachineExtensionGetResponse = this.VirtualMachineExtensionClient.GetWithHttpMessagesAsync(
115+
this.ResourceGroupName,
116+
this.VMName,
117+
this.Name).GetAwaiter().GetResult();
118+
}
119+
120+
var returnedExtension = virtualMachineExtensionGetResponse.ToPSVirtualMachineExtension(this.ResourceGroupName);
121+
WriteObject(returnedExtension);
122+
});
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)