public override void ExecuteCmdlet() { base.ExecuteCmdlet(); ExecuteClientAction(() => { if (string.IsNullOrEmpty(this.Name)) { var virtualMachine = ComputeClient.ComputeManagementClient.VirtualMachines.Get(this.ResourceGroupName, this.VMName); var osdisk = virtualMachine.StorageProfile.OsDisk; if (String.IsNullOrEmpty(this.OSType)) { this.OSType = osdisk.OsType.ToString(); } if (String.IsNullOrEmpty(this.OSType)) { WriteError("Could not determine Operating System of the VM. Please provide the Operating System type ({0} or {1}) via parameter OSType", AEMExtensionConstants.OSTypeWindows, AEMExtensionConstants.OSTypeLinux); return; } var aemExtension = virtualMachine.Resources != null ? virtualMachine.Resources.FirstOrDefault(extension => extension.Publisher.Equals(AEMExtensionConstants.AEMExtensionPublisher[OSType], StringComparison.InvariantCultureIgnoreCase) && extension.VirtualMachineExtensionType.Equals(AEMExtensionConstants.AEMExtensionType[OSType], StringComparison.InvariantCultureIgnoreCase)) : null; if (aemExtension == null) { WriteObject(null); return; } else { this.Name = aemExtension.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, this.VMName); WriteObject(returnedExtension); }); }
private bool IsExtensionInstalled(OSType currentOSType) { if (OSType.Windows.Equals(currentOSType)) { this.Name = this.Name ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultName; } else if (OSType.Linux.Equals(currentOSType)) { this.Name = this.Name ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName; } AzureOperationResponse <VirtualMachineExtension> extensionResult = this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName, this.VMName, this.Name); if (extensionResult == null) { return(false); } PSVirtualMachineExtension returnedExtension = extensionResult.ToPSVirtualMachineExtension( this.ResourceGroupName, this.VMName); if ((returnedExtension == null) || (string.IsNullOrWhiteSpace(returnedExtension.Publisher)) || (string.IsNullOrWhiteSpace(returnedExtension.ExtensionType))) { return(false); } bool publisherMatch = false; if (OSType.Linux.Equals(currentOSType)) { if (returnedExtension.Publisher.Equals(this.ExtensionPublisherName ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) && returnedExtension.ExtensionType.Equals(this.ExtensionType ?? AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultType, StringComparison.InvariantCultureIgnoreCase)) { publisherMatch = true; } } else if (OSType.Windows.Equals(currentOSType)) { if (returnedExtension.Publisher.Equals(this.ExtensionPublisherName ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) && returnedExtension.ExtensionType.Equals(this.ExtensionType ?? AzureDiskEncryptionExtensionContext.ExtensionDefaultType, StringComparison.InvariantCultureIgnoreCase)) { publisherMatch = true; } } if (publisherMatch) { return(true); } return(false); }
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, this.VMName); WriteObject(returnedExtension); }); }
private string GetExtensionStatusMessage(OSType currentOSType, bool returnSubstatusMessage = false) { AzureOperationResponse <VirtualMachineExtension> extensionResult = this.VirtualMachineExtensionClient.GetWithInstanceView(this.ResourceGroupName, this.VMName, this.Name); if (extensionResult == null) { ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Failed to retrieve extension status")), "InvalidResult", ErrorCategory.InvalidResult, null)); } PSVirtualMachineExtension returnedExtension = extensionResult.ToPSVirtualMachineExtension( this.ResourceGroupName, this.VMName); if ((returnedExtension == null) || (string.IsNullOrWhiteSpace(returnedExtension.Publisher)) || (string.IsNullOrWhiteSpace(returnedExtension.ExtensionType))) { ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Missing extension publisher and type info")), "InvalidResult", ErrorCategory.InvalidResult, null)); } bool publisherMatch = false; if (OSType.Linux.Equals(currentOSType)) { if (returnedExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) && returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.LinuxExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase)) { publisherMatch = true; } } else if (OSType.Windows.Equals(currentOSType)) { if (returnedExtension.Publisher.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultPublisher, StringComparison.InvariantCultureIgnoreCase) && returnedExtension.ExtensionType.Equals(AzureDiskEncryptionExtensionContext.ExtensionDefaultName, StringComparison.InvariantCultureIgnoreCase)) { publisherMatch = true; } } if (publisherMatch) { AzureDiskEncryptionExtensionContext context = new AzureDiskEncryptionExtensionContext(returnedExtension); if ((context == null) || (context.Statuses == null) || (context.Statuses.Count < 1) || (string.IsNullOrWhiteSpace(context.Statuses[0].Message))) { throw new KeyNotFoundException(string.Format(CultureInfo.CurrentUICulture, "Invalid extension status")); } if (returnSubstatusMessage) { if ((context == null) || (context.SubStatuses == null) || (context.SubStatuses.Count < 1)) { throw new KeyNotFoundException(string.Format(CultureInfo.CurrentUICulture, "Invalid extension substatus")); } else { return(context.SubStatuses[0].Message); } } return(context.Statuses[0].Message); } else { ThrowTerminatingError(new ErrorRecord(new ApplicationFailedException(string.Format(CultureInfo.CurrentUICulture, "Extension publisher and type mismatched")), "InvalidResult", ErrorCategory.InvalidResult, null)); } return(null); }