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