public async Task Update()
        {
            var vmssName = Recording.GenerateAssetName("testVMSS-");
            var vmss     = await CreateVirtualMachineScaleSetAsync(vmssName);

            // Create a PPG here and add this PPG to this virtual machine using Update
            var ppgName = Recording.GenerateAssetName("testPPG-");
            var ppgData = new ProximityPlacementGroupData(DefaultLocation)
            {
            };
            var ppgLro = await _resourceGroup.GetProximityPlacementGroups().CreateOrUpdateAsync(ppgName, ppgData);

            ProximityPlacementGroup ppg = ppgLro.Value;
            // update PPG requires the VM to be deallocated
            await vmss.DeallocateAsync();

            var update = new VirtualMachineScaleSetUpdate()
            {
                ProximityPlacementGroup = new SubResource()
                {
                    Id = ppg.Id
                }
            };
            var lro = await vmss.UpdateAsync(update);

            VirtualMachineScaleSet updatedVM = lro.Value;

            Assert.AreEqual(ppg.Id, updatedVM.Data.ProximityPlacementGroup.Id);
        }
Пример #2
0
        protected void ExecuteVirtualMachineScaleSetUpdateMethod(object[] invokeMethodInputParameters)
        {
            string resourceGroupName = (string)ParseParameter(invokeMethodInputParameters[0]);
            string vmScaleSetName    = (string)ParseParameter(invokeMethodInputParameters[1]);
            VirtualMachineScaleSetUpdate parameters = (VirtualMachineScaleSetUpdate)ParseParameter(invokeMethodInputParameters[2]);

            var result = VirtualMachineScaleSetsClient.Update(resourceGroupName, vmScaleSetName, parameters);

            WriteObject(result);
        }
Пример #3
0
        protected PSArgument[] CreateVirtualMachineScaleSetUpdateParameters()
        {
            string resourceGroupName = string.Empty;
            string vmScaleSetName    = string.Empty;
            VirtualMachineScaleSetUpdate parameters = new VirtualMachineScaleSetUpdate();

            return(ConvertFromObjectsToArguments(
                       new string[] { "ResourceGroupName", "VMScaleSetName", "Parameters" },
                       new object[] { resourceGroupName, vmScaleSetName, parameters }));
        }
Пример #4
0
 internal void HandleExternalIdentities(VirtualMachineScaleSetUpdate vmssUpdate)
 {
     if (this.HandleRemoveAllExternalIdentitiesCase(vmssUpdate))
     {
         return;
     }
     else
     {
         // At this point one of the following condition is met:
         //
         // 1. User don't want touch the 'VMSS.Identity.UserAssignedIdentities' property
         //      [this.userAssignedIdentities.Empty() == true]
         // 2. User want to add some identities to 'VMSS.Identity.UserAssignedIdentities'
         //      [this.userAssignedIdentities.Empty() == false and this.scaleSet.Inner().Identity() != null]
         // 3. User want to remove some (not all) identities in 'VMSS.Identity.UserAssignedIdentities'
         //      [this.userAssignedIdentities.Empty() == false and this.scaleSet.Inner().Identity() != null]
         //      Note: The scenario where this.scaleSet.Inner().Identity() is null in #3 is already handled in
         //      handleRemoveAllExternalIdentitiesCase method
         // 4. User want to add and remove (all or subset) some identities in 'VMSS.Identity.UserAssignedIdentities'
         //      [this.userAssignedIdentities.Empty() == false and this.scaleSet.Inner().Identity() != null]
         //
         var currentIdentity = this.scaleSet.Inner.Identity;
         vmssUpdate.Identity = currentIdentity;
         if (this.userAssignedIdentities.Any())
         {
             // At this point its guaranteed that 'currentIdentity' is not null so vmUpdate.Identity() is.
             vmssUpdate.Identity.UserAssignedIdentities = this.userAssignedIdentities;
         }
         else
         {
             // User don't want to touch 'VM.Identity.UserAssignedIdentities' property
             if (currentIdentity != null)
             {
                 // and currently there is identity exists or user want to manipulate some other properties of
                 // identity, set identities to null so that it won't send over wire.
                 currentIdentity.UserAssignedIdentities = null;
             }
         }
     }
 }
Пример #5
0
 // This method is used to Update VM Scale Set but it internally calls PATCH verb instead of PUT.
 // PATCH verb is more relaxed and does not puts constraint to specify full parameters.
 protected async Task PatchVMScaleSet(string rgName, string vmssName, VirtualMachineScaleSetUpdate inputVMScaleSet)
 {
     var patchResponse = await WaitForCompletionAsync(await VirtualMachineScaleSetsOperations.StartUpdateAsync(rgName, vmssName, inputVMScaleSet));
 }
Пример #6
0
 // This method is used to Update VM Scale Set but it internally calls PATCH verb instead of PUT.
 // PATCH verb is more relaxed and does not puts constraint to specify full parameters.
 protected void PatchVMScaleSet(string rgName, string vmssName, VirtualMachineScaleSetUpdate inputVMScaleSet)
 {
     var patchResponse = m_CrpClient.VirtualMachineScaleSets.Update(rgName, vmssName, inputVMScaleSet);
 }
Пример #7
0
        public void TestVMScaleSetPatchOperations()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                EnsureClientsInitialized(context);

                ImageReference imageRef = GetPlatformVMImage(useWindowsImage: true);
                // Create resource group
                var    rgName             = TestUtilities.GenerateName(TestPrefix);
                var    vmssName           = TestUtilities.GenerateName("vmss");
                string storageAccountName = TestUtilities.GenerateName(TestPrefix);
                VirtualMachineScaleSet inputVMScaleSet;
                try
                {
                    var storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);

                    m_CrpClient.VirtualMachineScaleSets.Delete(rgName, "VMScaleSetDoesNotExist");

                    var vmScaleSet = CreateVMScaleSet_NoAsyncTracking(rgName, vmssName, storageAccountOutput, imageRef, out inputVMScaleSet);

                    var getResponse = m_CrpClient.VirtualMachineScaleSets.Get(rgName, vmScaleSet.Name);
                    ValidateVMScaleSet(inputVMScaleSet, getResponse);

                    // Adding an extension to the VMScaleSet. We will use Patch to update this.
                    VirtualMachineScaleSetExtensionProfile extensionProfile = new VirtualMachineScaleSetExtensionProfile()
                    {
                        Extensions = new List <VirtualMachineScaleSetExtension>()
                        {
                            GetTestVMSSVMExtension(autoUpdateMinorVersion: false),
                        }
                    };

                    VirtualMachineScaleSetUpdate patchVMScaleSet = new VirtualMachineScaleSetUpdate()
                    {
                        VirtualMachineProfile = new VirtualMachineScaleSetUpdateVMProfile()
                        {
                            ExtensionProfile = extensionProfile,
                        },
                    };
                    PatchVMScaleSet(rgName, vmssName, patchVMScaleSet);

                    // Update the inputVMScaleSet and then compare it with response to verify the result.
                    inputVMScaleSet.VirtualMachineProfile.ExtensionProfile = extensionProfile;
                    getResponse = m_CrpClient.VirtualMachineScaleSets.Get(rgName, vmScaleSet.Name);
                    ValidateVMScaleSet(inputVMScaleSet, getResponse);


                    // Scaling the VMScaleSet now to 3 instances
                    VirtualMachineScaleSetUpdate patchVMScaleSet2 = new VirtualMachineScaleSetUpdate()
                    {
                        Sku = new Sku()
                        {
                            Capacity = 3,
                        },
                    };
                    PatchVMScaleSet(rgName, vmssName, patchVMScaleSet2);

                    // Validate that ScaleSet Scaled to 3 instances
                    inputVMScaleSet.Sku.Capacity = 3;
                    getResponse = m_CrpClient.VirtualMachineScaleSets.Get(rgName, vmScaleSet.Name);
                    ValidateVMScaleSet(inputVMScaleSet, getResponse);

                    m_CrpClient.VirtualMachineScaleSets.Delete(rgName, vmScaleSet.Name);
                }
                finally
                {
                    //Cleanup the created resources. But don't wait since it takes too long, and it's not the purpose
                    //of the test to cover deletion. CSM does persistent retrying over all RG resources.
                    m_ResourcesClient.ResourceGroups.Delete(rgName);
                }
            }
        }
 /// <summary>
 /// Update a VM scale set.
 /// </summary>
 /// <param name='operations'>
 /// The operations group for this extension method.
 /// </param>
 /// <param name='resourceGroupName'>
 /// The name of the resource group.
 /// </param>
 /// <param name='vmScaleSetName'>
 /// The name of the VM scale set to create or update.
 /// </param>
 /// <param name='parameters'>
 /// The scale set object.
 /// </param>
 /// <param name='cancellationToken'>
 /// The cancellation token.
 /// </param>
 public static async Task <VirtualMachineScaleSetInner> UpdateAsync(this IVirtualMachineScaleSetsOperations operations, string resourceGroupName, string vmScaleSetName, VirtualMachineScaleSetUpdate parameters, CancellationToken cancellationToken = default(CancellationToken))
 {
     using (var _result = await operations.UpdateWithHttpMessagesAsync(resourceGroupName, vmScaleSetName, parameters, null, cancellationToken).ConfigureAwait(false))
     {
         return(_result.Body);
     }
 }
        public void TestVMScaleSetScalingWithPublicIPPrefix()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                string originalTestLocation = Environment.GetEnvironmentVariable("AZURE_VM_TEST_LOCATION");
                if (originalTestLocation == null)
                {
                    originalTestLocation = String.Empty;
                }

                // Create resource group
                string rgName             = TestUtilities.GenerateName(TestPrefix) + 1;
                var    vmssName           = TestUtilities.GenerateName("vmss");
                var    dnsname            = TestUtilities.GenerateName("dnsname");
                string storageAccountName = TestUtilities.GenerateName(TestPrefix);
                VirtualMachineScaleSet inputVMScaleSet;

                bool passed = false;
                try
                {
                    EnsureClientsInitialized(context);

                    ImageReference imageRef             = GetPlatformVMImage(useWindowsImage: true);
                    var            storageAccountOutput = CreateStorageAccount(rgName, storageAccountName);
                    var            vnetResponse         = CreateVNETWithSubnets(rgName, 2);
                    var            vmssSubnet           = vnetResponse.Subnets[1];
                    var            publicIpPrefix       = CreatePublicIPPrefix(rgName, 30);

                    var publicipConfiguration = new VirtualMachineScaleSetPublicIPAddressConfiguration();
                    publicipConfiguration.Name = "pip1";
                    publicipConfiguration.IdleTimeoutInMinutes        = 10;
                    publicipConfiguration.DnsSettings                 = new VirtualMachineScaleSetPublicIPAddressConfigurationDnsSettings();
                    publicipConfiguration.DnsSettings.DomainNameLabel = dnsname;
                    publicipConfiguration.PublicIPPrefix              = new Microsoft.Azure.Management.Compute.Models.SubResource();
                    publicipConfiguration.PublicIPPrefix.Id           = publicIpPrefix.Id;

                    // Creating a VMSS with 2 instances
                    VirtualMachineScaleSet vmScaleSet = CreateVMScaleSet_NoAsyncTracking(
                        rgName: rgName,
                        vmssName: vmssName,
                        storageAccount: storageAccountOutput,
                        imageRef: imageRef,
                        inputVMScaleSet: out inputVMScaleSet,
                        vmScaleSetCustomizer:
                        (virtualMachineScaleSet) =>
                        virtualMachineScaleSet.VirtualMachineProfile.NetworkProfile
                        .NetworkInterfaceConfigurations[0].IpConfigurations[0].PublicIPAddressConfiguration = publicipConfiguration,
                        createWithPublicIpAddress: false,
                        subnet: vmssSubnet,
                        capacity: 2);

                    var vmss = m_CrpClient.VirtualMachineScaleSets.Get(rgName, vmssName);

                    Assert.NotNull(vmss.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations[0].IpConfigurations[0].PublicIPAddressConfiguration.PublicIPPrefix);
                    Assert.Equal(publicIpPrefix.Id, vmss.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations[0].IpConfigurations[0].PublicIPAddressConfiguration.PublicIPPrefix.Id);

                    var getPublicIpPrefixResponse = m_NrpClient.PublicIPPrefixes.Get(rgName, publicIpPrefix.Name);
                    Assert.Equal(2, getPublicIpPrefixResponse.PublicIPAddresses.Count());

                    // Scaling the VMScaleSet now to 3 instances
                    VirtualMachineScaleSetUpdate patchVMScaleSet = new VirtualMachineScaleSetUpdate()
                    {
                        Sku = new Sku()
                        {
                            Capacity = 3,
                        },
                    };
                    PatchVMScaleSet(rgName, vmssName, patchVMScaleSet);

                    getPublicIpPrefixResponse = m_NrpClient.PublicIPPrefixes.Get(rgName, publicIpPrefix.Name);
                    Assert.Equal(3, getPublicIpPrefixResponse.PublicIPAddresses.Count());

                    // Scaling the VMScaleSet now to 3 instances
                    patchVMScaleSet = new VirtualMachineScaleSetUpdate()
                    {
                        Sku = new Sku()
                        {
                            Capacity = 4,
                        },
                    };
                    PatchVMScaleSet(rgName, vmssName, patchVMScaleSet);

                    getPublicIpPrefixResponse = m_NrpClient.PublicIPPrefixes.Get(rgName, publicIpPrefix.Name);
                    Assert.Equal(4, getPublicIpPrefixResponse.PublicIPAddresses.Count());

                    passed = true;
                }
                finally
                {
                    Environment.SetEnvironmentVariable("AZURE_VM_TEST_LOCATION", originalTestLocation);
                    m_ResourcesClient.ResourceGroups.DeleteIfExists(rgName);
                }

                Assert.True(passed);
            }
        }
Пример #10
0
        public async Task TestVMScaleSetPatchOperations()
        {
            EnsureClientsInitialized(DefaultLocation);

            ImageReference imageRef = await GetPlatformVMImage(useWindowsImage : true);

            // Create resource group
            var    rgName             = Recording.GenerateAssetName(TestPrefix);
            var    vmssName           = Recording.GenerateAssetName("vmss");
            string storageAccountName = Recording.GenerateAssetName(TestPrefix);
            VirtualMachineScaleSet inputVMScaleSet;
            var storageAccountOutput = await CreateStorageAccount(rgName, storageAccountName);

            await WaitForCompletionAsync(await VirtualMachineScaleSetsOperations.StartDeleteAsync(rgName, "VMScaleSetDoesNotExist"));

            var getTwoVirtualMachineScaleSet = await CreateVMScaleSet_NoAsyncTracking(rgName, vmssName, storageAccountOutput, imageRef);

            VirtualMachineScaleSet vmScaleSet = getTwoVirtualMachineScaleSet.Item1;

            inputVMScaleSet = getTwoVirtualMachineScaleSet.Item2;
            var getResponse = await VirtualMachineScaleSetsOperations.GetAsync(rgName, vmScaleSet.Name);

            ValidateVMScaleSet(inputVMScaleSet, getResponse);

            // Adding an extension to the VMScaleSet. We will use Patch to update this.
            VirtualMachineScaleSetExtensionProfile extensionProfile = new VirtualMachineScaleSetExtensionProfile()
            {
                Extensions =
                {
                    GetTestVMSSVMExtension(),
                }
            };

            VirtualMachineScaleSetUpdate patchVMScaleSet = new VirtualMachineScaleSetUpdate()
            {
                VirtualMachineProfile = new VirtualMachineScaleSetUpdateVMProfile()
                {
                    ExtensionProfile = extensionProfile,
                },
            };

            await PatchVMScaleSet(rgName, vmssName, patchVMScaleSet);

            // Update the inputVMScaleSet and then compare it with response to verify the result.
            inputVMScaleSet.VirtualMachineProfile.ExtensionProfile = extensionProfile;
            getResponse = await VirtualMachineScaleSetsOperations.GetAsync(rgName, vmScaleSet.Name);

            ValidateVMScaleSet(inputVMScaleSet, getResponse);


            // Scaling the VMScaleSet now to 3 instances
            VirtualMachineScaleSetUpdate patchVMScaleSet2 = new VirtualMachineScaleSetUpdate()
            {
                Sku = new Sku()
                {
                    Capacity = 3,
                },
            };

            await PatchVMScaleSet(rgName, vmssName, patchVMScaleSet2);

            // Validate that ScaleSet Scaled to 3 instances
            inputVMScaleSet.Sku.Capacity = 3;
            getResponse = await VirtualMachineScaleSetsOperations.GetAsync(rgName, vmScaleSet.Name);

            ValidateVMScaleSet(inputVMScaleSet, getResponse);

            await WaitForCompletionAsync(await VirtualMachineScaleSetsOperations.StartDeleteAsync(rgName, vmScaleSet.Name));
        }
Пример #11
0
        /// <summary>
        /// Method that handle the case where user request indicates all it want to do is remove all identities associated
        /// with the virtual machine.
        /// </summary>
        /// <param name="vmssUpdate">The vm update payload model.</param>
        /// <return>True if user indented to remove all the identities.</return>
        private bool HandleRemoveAllExternalIdentitiesCase(VirtualMachineScaleSetUpdate vmssUpdate)
        {
            if (this.userAssignedIdentities.Any())
            {
                int rmCount = 0;
                foreach (var v in this.userAssignedIdentities.Values)
                {
                    if (v == null)
                    {
                        rmCount++;
                    }
                    else
                    {
                        break;
                    }
                }
                bool containsRemoveOnly = rmCount > 0 && rmCount == this.userAssignedIdentities.Count;
                // Check if user request contains only request for removal of identities.
                if (containsRemoveOnly)
                {
                    var currentIds      = new HashSet <string>();
                    var currentIdentity = this.scaleSet.Inner.Identity;
                    if (currentIdentity != null && currentIdentity.UserAssignedIdentities != null)
                    {
                        foreach (var id in currentIdentity.UserAssignedIdentities.Keys)
                        {
                            currentIds.Add(id.ToLower());
                        }
                    }

                    var removeIds = new HashSet <string>();
                    foreach (var entrySet in this.userAssignedIdentities)
                    {
                        if (entrySet.Value == null)
                        {
                            removeIds.Add(entrySet.Key.ToLower());
                        }
                    }
                    // If so check user want to remove all the identities
                    bool removeAllCurrentIds = currentIds.Count == removeIds.Count && !removeIds.Any(id => !currentIds.Contains(id));  // Java part looks like this -> && currentIds.ContainsAll(removeIds);
                    if (removeAllCurrentIds)
                    {
                        // If so adjust  the identity type [Setting type to SYSTEM_ASSIGNED orNONE will remove all the identities]
                        if (currentIdentity == null || currentIdentity.Type == null)
                        {
                            vmssUpdate.Identity = new VirtualMachineScaleSetIdentity {
                                Type = ResourceIdentityTypeEnumExtension.ToSerializedValue(ResourceIdentityType.None)
                            };
                        }
                        else if (currentIdentity.Type.Equals(ResourceIdentityTypeEnumExtension.ToSerializedValue(ResourceIdentityType.SystemAssignedUserAssigned), StringComparison.OrdinalIgnoreCase))
                        {
                            vmssUpdate.Identity      = currentIdentity;
                            vmssUpdate.Identity.Type = ResourceIdentityTypeEnumExtension.ToSerializedValue(ResourceIdentityType.SystemAssigned);
                        }
                        else if (currentIdentity.Type.Equals(ResourceIdentityTypeEnumExtension.ToSerializedValue(ResourceIdentityType.UserAssigned), StringComparison.OrdinalIgnoreCase))
                        {
                            vmssUpdate.Identity      = currentIdentity;
                            vmssUpdate.Identity.Type = ResourceIdentityTypeEnumExtension.ToSerializedValue(ResourceIdentityType.None);
                        }
                        // and set identities property in the payload model to null so that it won't be sent
                        vmssUpdate.Identity.UserAssignedIdentities = null;
                        return(true);
                    }
                    else
                    {
                        // Check user is asking to remove identities though there is no identities currently associated
                        if (currentIds.Count == 0 && removeIds.Count != 0 && currentIdentity == null)
                        {
                            // If so we are in a invalid state but we want to send user input to service and let service
                            // handle it (ignore or error).
                            vmssUpdate.Identity = new VirtualMachineScaleSetIdentity {
                                Type = ResourceIdentityTypeEnumExtension.ToSerializedValue(ResourceIdentityType.None)
                            };
                            vmssUpdate.Identity.UserAssignedIdentities = null;
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }