Skip to content

Commit 6aad8f4

Browse files
authored
Merge pull request #2587 from MsysTechnologiesllc/nim/Update_Set-AzureRmVMChefExtension
Chef extension ARM commands
2 parents f2038cd + b868867 commit 6aad8f4

File tree

7 files changed

+1349
-90
lines changed

7 files changed

+1349
-90
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@
238238
<Compile Include="Extension\AzureVMBackup\SetAzureVMBackupExtension.cs" />
239239
<Compile Include="Extension\BGInfo\SetAzureVMBGInfoExtension.cs" />
240240
<Compile Include="Extension\BGInfo\VirtualMachineBGInfoExtensionContext.cs" />
241+
<Compile Include="Extension\Chef\GetAzureRmVMChefExtension.cs" />
242+
<Compile Include="Extension\Chef\RemoveAzureRmVMChefExtension.cs" />
243+
<Compile Include="Extension\Chef\SetAzureVMChefExtension.cs" />
241244
<Compile Include="Extension\CustomScript\GetAzureVMCustomScriptExtensionCommand.cs" />
242245
<Compile Include="Extension\CustomScript\CustomScriptExtensionPrivateSettings.cs" />
243246
<Compile Include="Extension\CustomScript\CustomScriptExtensionPublicSettings.cs" />
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Compute.Common;
19+
using Microsoft.Azure.Commands.Compute.Models;
20+
using Microsoft.Azure.Management.Compute;
21+
using Microsoft.Azure.Management.Compute.Models;
22+
using Microsoft.Rest.Azure;
23+
24+
namespace Microsoft.Azure.Commands.Compute.Extension.Chef
25+
{
26+
[Cmdlet(
27+
VerbsCommon.Get,
28+
ProfileNouns.VirtualMachineChefExtension),
29+
OutputType(
30+
typeof(PSVirtualMachineExtension))]
31+
public class GetAzureRmVMChefExtension : VirtualMachineExtensionBaseCmdlet
32+
{
33+
private string ExtensionDefaultPublisher = "Chef.Bootstrap.WindowsAzure";
34+
private string ExtensionDefaultName = "ChefClient";
35+
private string LinuxExtensionName = "LinuxChefClient";
36+
37+
protected const string LinuxParameterSetName = "Linux";
38+
protected const string WindowsParameterSetName = "Windows";
39+
40+
[Parameter(
41+
Mandatory = true,
42+
Position = 0,
43+
ValueFromPipelineByPropertyName = true,
44+
HelpMessage = "The resource group name.")]
45+
[ValidateNotNullOrEmpty]
46+
public string ResourceGroupName { get; set; }
47+
48+
[Alias("ResourceName")]
49+
[Parameter(
50+
Mandatory = true,
51+
Position = 1,
52+
ValueFromPipelineByPropertyName = true,
53+
HelpMessage = "The virtual machine name.")]
54+
[ValidateNotNullOrEmpty]
55+
public string VMName { get; set; }
56+
57+
[Alias("ExtensionName")]
58+
[Parameter(
59+
Mandatory = false,
60+
Position = 2,
61+
ValueFromPipelineByPropertyName = true,
62+
HelpMessage = "Extension Name.")]
63+
public string Name { get; set; }
64+
65+
[Parameter(
66+
Position = 3,
67+
ValueFromPipelineByPropertyName = true,
68+
HelpMessage = "To show the status.")]
69+
[ValidateNotNullOrEmpty]
70+
public SwitchParameter Status { get; set; }
71+
72+
[Parameter(
73+
Mandatory = true,
74+
ParameterSetName = LinuxParameterSetName,
75+
HelpMessage = "Set extension for Linux.")]
76+
public SwitchParameter Linux { get; set; }
77+
78+
[Parameter(
79+
Mandatory = true,
80+
ParameterSetName = WindowsParameterSetName,
81+
HelpMessage = "Set extension for Windows.")]
82+
public SwitchParameter Windows { get; set; }
83+
84+
public override void ExecuteCmdlet()
85+
{
86+
base.ExecuteCmdlet();
87+
88+
if (this.Linux.IsPresent)
89+
{
90+
this.Name = LinuxExtensionName;
91+
}
92+
else if (this.Windows.IsPresent)
93+
{
94+
this.Name = ExtensionDefaultName;
95+
}
96+
97+
ExecuteClientAction(() =>
98+
{
99+
if (string.IsNullOrEmpty(this.Name))
100+
{
101+
var virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
102+
var chefExtension = virtualMachine.Resources != null
103+
? virtualMachine.Resources.FirstOrDefault(extension =>
104+
extension.Publisher.Equals(ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
105+
extension.VirtualMachineExtensionType.Equals(this.Name, StringComparison.InvariantCultureIgnoreCase))
106+
: null;
107+
108+
if (chefExtension == null)
109+
{
110+
WriteObject(null);
111+
return;
112+
}
113+
else
114+
{
115+
this.Name = chefExtension.Name;
116+
}
117+
}
118+
119+
AzureOperationResponse<VirtualMachineExtension> virtualMachineExtensionGetResponse = null;
120+
if (Status.IsPresent)
121+
{
122+
virtualMachineExtensionGetResponse =
123+
this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName,
124+
this.VMName, this.Name);
125+
}
126+
else
127+
{
128+
virtualMachineExtensionGetResponse = this.VirtualMachineExtensionClient.GetWithHttpMessagesAsync(
129+
this.ResourceGroupName,
130+
this.VMName,
131+
this.Name).GetAwaiter().GetResult();
132+
}
133+
134+
var returnedExtension = virtualMachineExtensionGetResponse.ToPSVirtualMachineExtension(this.ResourceGroupName, this.VMName);
135+
WriteObject(returnedExtension);
136+
});
137+
}
138+
}
139+
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Linq;
17+
using System.Management.Automation;
18+
using Microsoft.Azure.Commands.Compute.Common;
19+
using Microsoft.Azure.Management.Compute;
20+
using System.Globalization;
21+
using AutoMapper;
22+
using Microsoft.Azure.Commands.Compute.Models;
23+
using Microsoft.Azure.Management.Compute.Models;
24+
25+
namespace Microsoft.Azure.Commands.Compute.Extension.Chef
26+
{
27+
[Cmdlet(
28+
VerbsCommon.Remove, ProfileNouns.VirtualMachineChefExtension, SupportsShouldProcess = true)]
29+
[OutputType(typeof(PSAzureOperationResponse))]
30+
public class RemoveAzureRmVMChefExtension : VirtualMachineExtensionBaseCmdlet
31+
{
32+
private string ExtensionDefaultPublisher = "Chef.Bootstrap.WindowsAzure";
33+
private string ExtensionDefaultName = "ChefClient";
34+
private string LinuxExtensionName = "LinuxChefClient";
35+
36+
protected const string LinuxParameterSetName = "Linux";
37+
protected const string WindowsParameterSetName = "Windows";
38+
39+
[Parameter(
40+
Mandatory = true,
41+
Position = 0,
42+
ValueFromPipelineByPropertyName = true,
43+
HelpMessage = "The resource group name.")]
44+
[ValidateNotNullOrEmpty]
45+
public string ResourceGroupName { get; set; }
46+
47+
[Alias("ResourceName")]
48+
[Parameter(
49+
Mandatory = true,
50+
Position = 1,
51+
ValueFromPipelineByPropertyName = true,
52+
HelpMessage = "The virtual machine name.")]
53+
[ValidateNotNullOrEmpty]
54+
public string VMName { get; set; }
55+
56+
[Alias("ExtensionName")]
57+
[Parameter(
58+
Mandatory = false,
59+
Position = 2,
60+
ValueFromPipelineByPropertyName = true,
61+
HelpMessage = "Extension Name.")]
62+
public string Name
63+
{
64+
get
65+
{
66+
return this.ExtensionDefaultName;
67+
}
68+
set
69+
{
70+
this.ExtensionDefaultName = value;
71+
}
72+
}
73+
74+
[Parameter(
75+
Mandatory = true,
76+
ParameterSetName = LinuxParameterSetName,
77+
HelpMessage = "Set extension for Linux.")]
78+
public SwitchParameter Linux { get; set; }
79+
80+
[Parameter(
81+
Mandatory = true,
82+
ParameterSetName = WindowsParameterSetName,
83+
HelpMessage = "Set extension for Windows.")]
84+
public SwitchParameter Windows { get; set; }
85+
86+
public override void ExecuteCmdlet()
87+
{
88+
base.ExecuteCmdlet();
89+
90+
if (this.Linux.IsPresent)
91+
{
92+
this.Name = LinuxExtensionName;
93+
}
94+
else if (this.Windows.IsPresent)
95+
{
96+
this.Name = ExtensionDefaultName;
97+
}
98+
99+
ConfirmAction("Remove Chef Extension", this.VMName,
100+
() =>
101+
{
102+
if (string.IsNullOrEmpty(this.Name))
103+
{
104+
VirtualMachine virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(
105+
this.ResourceGroupName, this.VMName);
106+
var chefExtension = virtualMachine.Resources != null
107+
? virtualMachine.Resources.FirstOrDefault(extension =>
108+
extension.Publisher.Equals(ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
109+
extension.VirtualMachineExtensionType.Equals(this.Name, StringComparison.InvariantCultureIgnoreCase))
110+
: null;
111+
112+
if (chefExtension == null)
113+
{
114+
WriteWarning(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ChefExtensionNotFound, this.ResourceGroupName, this.VMName));
115+
return;
116+
}
117+
else
118+
{
119+
this.Name = chefExtension.Name;
120+
}
121+
}
122+
123+
var op = this.VirtualMachineExtensionClient.DeleteWithHttpMessagesAsync(
124+
this.ResourceGroupName,
125+
this.VMName,
126+
this.Name).GetAwaiter().GetResult();
127+
var result = Mapper.Map<PSAzureOperationResponse>(op);
128+
WriteObject(result);
129+
});
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)