Skip to content

Commit 5e0bb00

Browse files
author
nimisha
committed
Added Remove-AzureRmVMChefExtension command in ARM
1 parent 2d2bd72 commit 5e0bb00

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-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
@@ -219,6 +219,7 @@
219219
<Compile Include="Extension\AzureVMBackup\SetAzureVMBackupExtension.cs" />
220220
<Compile Include="Extension\BGInfo\SetAzureVMBGInfoExtension.cs" />
221221
<Compile Include="Extension\BGInfo\VirtualMachineBGInfoExtensionContext.cs" />
222+
<Compile Include="Extension\Chef\RemoveAzureRmVMChefExtension.cs" />
222223
<Compile Include="Extension\Chef\SetAzureVMChefExtension.cs" />
223224
<Compile Include="Extension\CustomScript\GetAzureVMCustomScriptExtensionCommand.cs" />
224225
<Compile Include="Extension\CustomScript\CustomScriptExtensionPrivateSettings.cs" />
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
using System;
2+
using System.Linq;
3+
using System.Management.Automation;
4+
using Microsoft.Azure.Commands.Compute.Common;
5+
using Microsoft.Azure.Management.Compute;
6+
using System.Globalization;
7+
using AutoMapper;
8+
using Microsoft.Azure.Commands.Compute.Models;
9+
using Microsoft.Azure.Management.Compute.Models;
10+
11+
namespace Microsoft.Azure.Commands.Compute.Extension.Chef
12+
{
13+
[Cmdlet(
14+
VerbsCommon.Remove,
15+
ProfileNouns.VirtualMachineChefExtension)]
16+
[OutputType(typeof(PSAzureOperationResponse))]
17+
public class RemoveAzureRmVMChefExtension : 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
50+
{
51+
get
52+
{
53+
return this.ExtensionDefaultName;
54+
}
55+
set
56+
{
57+
this.ExtensionDefaultName = value;
58+
}
59+
}
60+
61+
[Parameter(
62+
Mandatory = true,
63+
ParameterSetName = LinuxParameterSetName,
64+
HelpMessage = "Set extension for Linux.")]
65+
public SwitchParameter Linux { get; set; }
66+
67+
[Parameter(
68+
Mandatory = true,
69+
ParameterSetName = WindowsParameterSetName,
70+
HelpMessage = "Set extension for Windows.")]
71+
public SwitchParameter Windows { get; set; }
72+
73+
public override void ExecuteCmdlet()
74+
{
75+
base.ExecuteCmdlet();
76+
77+
if (this.Linux.IsPresent)
78+
{
79+
this.Name = LinuxExtensionName;
80+
}
81+
else if (this.Windows.IsPresent)
82+
{
83+
this.Name = ExtensionDefaultName;
84+
}
85+
86+
ExecuteClientAction(() =>
87+
{
88+
if (string.IsNullOrEmpty(this.Name))
89+
{
90+
VirtualMachine virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(
91+
this.ResourceGroupName, this.VMName);
92+
var chefExtension = virtualMachine.Resources != null
93+
? virtualMachine.Resources.FirstOrDefault(extension =>
94+
extension.Publisher.Equals(ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
95+
extension.VirtualMachineExtensionType.Equals(this.Name, StringComparison.InvariantCultureIgnoreCase))
96+
: null;
97+
98+
if (chefExtension == null)
99+
{
100+
WriteWarning(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ChefExtensionNotFound, this.ResourceGroupName, this.VMName));
101+
return;
102+
}
103+
else
104+
{
105+
this.Name = chefExtension.Name;
106+
}
107+
}
108+
109+
var op = this.VirtualMachineExtensionClient.DeleteWithHttpMessagesAsync(
110+
this.ResourceGroupName,
111+
this.VMName,
112+
this.Name).GetAwaiter().GetResult();
113+
var result = Mapper.Map<PSAzureOperationResponse>(op);
114+
WriteObject(result);
115+
});
116+
}
117+
}
118+
}

src/ResourceManager/Compute/Commands.Compute/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/Compute/Commands.Compute/Properties/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,8 @@ The file needs to be a PowerShell script (.ps1 or .psm1) or a ZIP archive (.zip)
497497
<data name="DiagnosticsExtensionXmlConfigWadCfgTagNotMatch" xml:space="preserve">
498498
<value>WadCfg start element in the config is not matching the end element.</value>
499499
</data>
500+
<data name="ChefExtensionNotFound" xml:space="preserve">
501+
<value>No chef extension found under resource group '{0}', virtual machine '{1}'.</value>
502+
<comment>{0} = resource group name, {1} = virtual machine name</comment>
503+
</data>
500504
</root>

0 commit comments

Comments
 (0)