|
| 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 | +} |
0 commit comments