/// <summary> /// Update the Managed Service Identity extension installed in the virtual machine. /// </summary> /// <param name="virtualMachine">The virtual machine.</param> /// <param name="extension">The Managed Service Identity extension.</param> /// <param name="typeName">The Managed Service Identity extension type name.</param> /// <return>Task that produces true if extension is updated, false otherwise</return> private async Task <bool> UpdateMSIExtensionAsync(IVirtualMachine virtualMachine, IVirtualMachineExtension extension, string typeName, CancellationToken cancellationToken = default(CancellationToken)) { int?currentTokenPort = ComputeUtils.ObjectToInteger(extension.PublicSettings["port"]); int?tokenPortToUse; if (this.tokenPort != null) { // User specified a port tokenPortToUse = this.tokenPort; } else if (currentTokenPort == null) { // User didn't specify a port and port is not already set tokenPortToUse = this.DEFAULT_TOKEN_PORT; } else { // User didn't specify a port and port is already set in the extension // No need to do a PUT on extension // return(false); } var publicSettings = new Dictionary <string, object> { { "port", tokenPortToUse } }; extension.Inner.Settings = publicSettings; await virtualMachine.Manager.Inner.VirtualMachineExtensions.CreateOrUpdateAsync(virtualMachine.ResourceGroupName, virtualMachine.Name, typeName, extension.Inner, cancellationToken); return(true); }
/// <summary> /// Add or update the Managed Service Identity extension for the given virtual machine scale set. /// </summary> /// <param name="scaleSetImpl">The scale set.</param> internal void AddOrUpdateMSIExtension(VirtualMachineScaleSetImpl scaleSetImpl) { if (!this.installExtensionIfNotInstalled) { return; } // To add or update MSI extension, we relay on methods exposed from interfaces instead of from // impl so that any breaking change in the contract cause a compile time error here. So do not // change the below 'updateExtension' or 'defineNewExtension' to use impls. // String msiExtensionType = scaleSetImpl.OSTypeIntern() == OperatingSystemTypes.Linux ? "ManagedIdentityExtensionForLinux" : "ManagedIdentityExtensionForWindows"; IVirtualMachineScaleSetExtension msiExtension = GetMSIExtension(scaleSetImpl.Extensions(), msiExtensionType); if (msiExtension != null) { Object currentTokenPortObj = msiExtension.PublicSettings["port"]; int? currentTokenPort = ComputeUtils.ObjectToInteger(currentTokenPortObj); int? newPort; if (this.tokenPort != null) { // user specified a port newPort = this.tokenPort; } else if (currentTokenPort != null) { // user didn't specify a port and currently there is a port newPort = currentTokenPort; } else { // user didn't specify a port and currently there is no port newPort = DEFAULT_TOKEN_PORT; } VirtualMachineScaleSet.Update.IUpdate appliableVMSS = scaleSetImpl; appliableVMSS.UpdateExtension(msiExtension.Name) .WithPublicSetting("port", newPort) .Parent(); } else { int? port; if (this.tokenPort != null) { port = this.tokenPort; } else { port = DEFAULT_TOKEN_PORT; } if (scaleSetImpl.Inner.Id == null) // InCreateMode { VirtualMachineScaleSet.Definition.IWithCreate creatableVMSS = scaleSetImpl; creatableVMSS.DefineNewExtension(msiExtensionType) .WithPublisher(MSI_EXTENSION_PUBLISHER_NAME) .WithType(msiExtensionType) .WithVersion("1.0") .WithMinorVersionAutoUpgrade() .WithPublicSetting("port", port) .Attach(); } else { VirtualMachineScaleSet.Update.IUpdate appliableVMSS = scaleSetImpl; appliableVMSS.DefineNewExtension(msiExtensionType) .WithPublisher(MSI_EXTENSION_PUBLISHER_NAME) .WithType(msiExtensionType) .WithVersion("1.0") .WithMinorVersionAutoUpgrade() .WithPublicSetting("port", port) .Attach(); } } this.installExtensionIfNotInstalled = false; this.tokenPort = null; }