Skip to content

[Do not Mege] Updated Set-AzureRmVMChefExtension cmd #1676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@
<ItemGroup>
<Compile Include="Common\ComputeTestController.cs" />
<Compile Include="ScenarioTests\AddVhdTests.cs" />
<Compile Include="ScenarioTests\ChefExtensionTests.cs" />
<Compile Include="ScenarioTests\ComputeCloudExceptionTests.cs" />
<Compile Include="ScenarioTests\DiagnosticsExtensionTests.cs" />
<Compile Include="ScenarioTests\DscExtensionTests.cs" />
Expand Down Expand Up @@ -219,6 +220,7 @@
<None Include="ScenarioTests\AddVhdTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="ScenarioTests\ChefExtensionTests.ps1" />
<None Include="ScenarioTests\ComputeCloudExceptionTests.ps1">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down Expand Up @@ -401,6 +403,8 @@
<None Include="Templates\azuredeploy.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Templates\client.rb" />
<None Include="Templates\tstorgnztn-validator.pem" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Microsoft.WindowsAzure.Commands.ScenarioTest;
using Xunit;

namespace Microsoft.Azure.Commands.Compute.Test.ScenarioTests
{
public class ChefExtensionTests
{
[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
public void TestChefExtensionBasic()
{
ComputeTestController.NewInstance.RunPsTest("Test-SetChefExtensionBasic");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# ChefExtensionTests.ps1
#

<#
.SYNOPSIS
Test the usage of the Set virtual machine chef extension command
#>
function Test-SetChefExtensionBasic
{
$rgname = Get-ComputeTestResourceName
$loc = Get-ComputeVMLocation

try
{
# Create Virtual Machine
$vm = Create-VirtualMachine -rgname $rgname -loc $loc
$vmname = $vm.Name
$version = "1210.12"
$client_rb = ".\Templates\client.rb";
$validationPemFile = ".\Templates\tstorgnztn-validator.pem";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please align the white-spaces in this file.


# Set Chef extension
Set-AzureRmVMChefExtension -ResourceGroupName $rgname -VMName $vmname -TypeHandlerVersion $version -ClientRb $client.rb -ValidationPem $validationPemFile -Windows
$extension = Get-AzureRmVMChefExtension -ResourceGroupName $rgname -VMName $vmname

Assert-NotNull $extension
Assert-AreEqual $extension.Publisher 'Chef.Bootstrap.WindowsAzure'
Assert-AreEqual $extension.ExtensionType 'ChefClient'
Assert-AreEqual $extension.Name 'ChefClient'
$settings = $extension.PublicSettings | ConvertFrom-Json
Assert-AreEqual $settings.autoUpdateClient "false"
Assert-AreEqual $settings.deleteChefConfig "false"

# Test Remove command.
Remove-AzureRmVMChefExtension -ResourceGroupName $rgname -VMName $vmname
$extension = Get-AzureRmVMChefExtension -ResourceGroupName $rgname -VMName $vmname
Assert-Null $extension
}
finally
{
# Cleanup
Clean-ResourceGroup $rgname
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
log_level :debug
chef_server_url "https://api.chef.io/organizations/tstorgnztn"
validation_client_name "tstorgnztn-validator"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAuxOLMJhoDZXc2WsRGPjj6UTw67Q1l0qdm8Zd/TQ092OCogbm
Azk/84FdtMlZLCneZLkshn1l+KiD3W+lz4JG/We3SpDfO+pGDkV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7VbS6j36rSjU99ae1d7HAjCEWTUgM15LIoIM4kdZ8lfoMc89sT
dLBRttwUfPlyAEknCanH2FkCgYBQzZXV5EkXIUkHtxDU3F7UIEBhM/lz5tAi3/6B
w5GhXa7bU7Xc/XTarxBaA3UsiPVppRL4RmHhzFkTwgw7vhb/CvUeHVR0TFgE3/la
GUtfKR4AwDrrodR6nE8G1wWG/spyIjlIeW6hVN6z+wEFxSTfCr9ZKJE+2ikcbFmq
ciiQbQKBgQDRk/8FcDiKZ/m3isj474ZZ6c/MHlsjtnAAH0PgYFyCzOAslggwL5IP
dmgCkpPetEVn2hoiAU6Cwxo4HHvBJSgTvY4m5j/piOYZy6/ohbHewHoPk21B689O
YHjZKpzZnd5ybZVTy6OoSfouq1q6UJjED/ol8m8pKUj60+jFCiVHaw==
-----END RSA PRIVATE KEY-----
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,9 @@
<Compile Include="Extension\AzureVMBackup\SetAzureVMBackupExtension.cs" />
<Compile Include="Extension\BGInfo\SetAzureVMBGInfoExtension.cs" />
<Compile Include="Extension\BGInfo\VirtualMachineBGInfoExtensionContext.cs" />
<Compile Include="Extension\Chef\GetAzureRmVMChefExtension.cs" />
<Compile Include="Extension\Chef\RemoveAzureRmVMChefExtension.cs" />
<Compile Include="Extension\Chef\SetAzureVMChefExtension.cs" />
<Compile Include="Extension\CustomScript\GetAzureVMCustomScriptExtensionCommand.cs" />
<Compile Include="Extension\CustomScript\CustomScriptExtensionPrivateSettings.cs" />
<Compile Include="Extension\CustomScript\CustomScriptExtensionPublicSettings.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Compute.Common;
using Microsoft.Azure.Commands.Compute.Models;
using Microsoft.Azure.Management.Compute;
using Microsoft.Azure.Management.Compute.Models;
using Microsoft.Rest.Azure;

namespace Microsoft.Azure.Commands.Compute.Extension.Chef
{
[Cmdlet(
VerbsCommon.Get,
ProfileNouns.VirtualMachineChefExtension),
OutputType(
typeof(PSVirtualMachineExtension))]
public class GetAzureRmVMChefExtension : VirtualMachineExtensionBaseCmdlet
{
private string ExtensionDefaultPublisher = "Chef.Bootstrap.WindowsAzure";
private string ExtensionDefaultName = "ChefClient";
private string LinuxExtensionName = "LinuxChefClient";

protected const string LinuxParameterSetName = "Linux";
protected const string WindowsParameterSetName = "Windows";

[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource group name.")]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

[Alias("ResourceName")]
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The virtual machine name.")]
[ValidateNotNullOrEmpty]
public string VMName { get; set; }

[Alias("ExtensionName")]
[Parameter(
Mandatory = false,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Extension Name.")]
public string Name { get; set; }

[Parameter(
Position = 3,
ValueFromPipelineByPropertyName = true,
HelpMessage = "To show the status.")]
[ValidateNotNullOrEmpty]
public SwitchParameter Status { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = LinuxParameterSetName,
HelpMessage = "Set extension for Linux.")]
public SwitchParameter Linux { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = WindowsParameterSetName,
HelpMessage = "Set extension for Windows.")]
public SwitchParameter Windows { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

if (this.Linux.IsPresent)
{
this.Name = LinuxExtensionName;
}
else if (this.Windows.IsPresent)
{
this.Name = ExtensionDefaultName;
}

ExecuteClientAction(() =>
{
if (string.IsNullOrEmpty(this.Name))
{
var virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName);
var chefExtension = virtualMachine.Resources != null
? virtualMachine.Resources.FirstOrDefault(extension =>
extension.Publisher.Equals(ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
extension.VirtualMachineExtensionType.Equals(this.Name, StringComparison.InvariantCultureIgnoreCase))
: null;

if (chefExtension == null)
{
WriteObject(null);
return;
}
else
{
this.Name = chefExtension.Name;
}
}

AzureOperationResponse<VirtualMachineExtension> virtualMachineExtensionGetResponse = null;
if (Status.IsPresent)
{
virtualMachineExtensionGetResponse =
this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName,
this.VMName, this.Name);
}
else
{
virtualMachineExtensionGetResponse = this.VirtualMachineExtensionClient.GetWithHttpMessagesAsync(
this.ResourceGroupName,
this.VMName,
this.Name).GetAwaiter().GetResult();
}

var returnedExtension = virtualMachineExtensionGetResponse.ToPSVirtualMachineExtension(this.ResourceGroupName);
WriteObject(returnedExtension);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
using System;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Compute.Common;
using Microsoft.Azure.Management.Compute;
using System.Globalization;
using AutoMapper;
using Microsoft.Azure.Commands.Compute.Models;
using Microsoft.Azure.Management.Compute.Models;

namespace Microsoft.Azure.Commands.Compute.Extension.Chef
{
[Cmdlet(
VerbsCommon.Remove,
ProfileNouns.VirtualMachineChefExtension)]
[OutputType(typeof(PSAzureOperationResponse))]
public class RemoveAzureRmVMChefExtension : VirtualMachineExtensionBaseCmdlet
{
private string ExtensionDefaultPublisher = "Chef.Bootstrap.WindowsAzure";
private string ExtensionDefaultName = "ChefClient";
private string LinuxExtensionName = "LinuxChefClient";

protected const string LinuxParameterSetName = "Linux";
protected const string WindowsParameterSetName = "Windows";

[Parameter(
Mandatory = true,
Position = 0,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The resource group name.")]
[ValidateNotNullOrEmpty]
public string ResourceGroupName { get; set; }

[Alias("ResourceName")]
[Parameter(
Mandatory = true,
Position = 1,
ValueFromPipelineByPropertyName = true,
HelpMessage = "The virtual machine name.")]
[ValidateNotNullOrEmpty]
public string VMName { get; set; }

[Alias("ExtensionName")]
[Parameter(
Mandatory = false,
Position = 2,
ValueFromPipelineByPropertyName = true,
HelpMessage = "Extension Name.")]
public string Name
{
get
{
return this.ExtensionDefaultName;
}
set
{
this.ExtensionDefaultName = value;
}
}

[Parameter(
Mandatory = true,
ParameterSetName = LinuxParameterSetName,
HelpMessage = "Set extension for Linux.")]
public SwitchParameter Linux { get; set; }

[Parameter(
Mandatory = true,
ParameterSetName = WindowsParameterSetName,
HelpMessage = "Set extension for Windows.")]
public SwitchParameter Windows { get; set; }

public override void ExecuteCmdlet()
{
base.ExecuteCmdlet();

if (this.Linux.IsPresent)
{
this.Name = LinuxExtensionName;
}
else if (this.Windows.IsPresent)
{
this.Name = ExtensionDefaultName;
}

ExecuteClientAction(() =>
{
if (string.IsNullOrEmpty(this.Name))
{
VirtualMachine virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(
this.ResourceGroupName, this.VMName);
var chefExtension = virtualMachine.Resources != null
? virtualMachine.Resources.FirstOrDefault(extension =>
extension.Publisher.Equals(ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) &&
extension.VirtualMachineExtensionType.Equals(this.Name, StringComparison.InvariantCultureIgnoreCase))
: null;

if (chefExtension == null)
{
WriteWarning(string.Format(CultureInfo.InvariantCulture, Properties.Resources.ChefExtensionNotFound, this.ResourceGroupName, this.VMName));
return;
}
else
{
this.Name = chefExtension.Name;
}
}

var op = this.VirtualMachineExtensionClient.DeleteWithHttpMessagesAsync(
this.ResourceGroupName,
this.VMName,
this.Name).GetAwaiter().GetResult();
var result = Mapper.Map<PSAzureOperationResponse>(op);
WriteObject(result);
});
}
}
}
Loading