From 2b7d733bdbeb3c874de9fbdefd7dc190b1298782 Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 9 Sep 2016 18:50:48 -0700 Subject: [PATCH 01/10] VirtualMachineScaleSet interface with defintion and update stages of the primary load balancers --- .../compute/VirtualMachineScaleSet.java | 250 ++++++++++++++++++ .../VirtualMachineScaleSetImpl.java | 10 + 2 files changed, 260 insertions(+) create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java new file mode 100644 index 000000000000..8111182673bd --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -0,0 +1,250 @@ +package com.microsoft.azure.management.compute; + +import com.microsoft.azure.management.network.LoadBalancer; +import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; +import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; + +/** + * An immutable client-side representation of an Azure virtual machine scale set. + */ +public interface VirtualMachineScaleSet { + + /** + * The entirety of the load balancer definition. + */ + interface Definition extends + DefinitionStages.Blank, + DefinitionStages.WithGroup, + DefinitionStages.WithPrimaryInternetFacingLoadBalancer, + DefinitionStages.WithPrimaryInternalLoadBalancer, + DefinitionStages.WithPrimaryInternetFacingLoadBalancerBackendOrNatPool, + DefinitionStages.WithInternalLoadBalancerBackendOrNatPool, + DefinitionStages.WithCreate { + } + + /** + * Grouping of virtual machine scale set definition stages. + */ + interface DefinitionStages { + /** + * The first stage of a virtual machine scale set definition. + */ + interface Blank + extends GroupableResource.DefinitionWithRegion { + } + + /** + * The stage of the virtual machine scale set definition allowing to specify the resource group. + */ + interface WithGroup + extends GroupableResource.DefinitionStages.WithGroup { + } + + /** + * The stage of the virtual machine scale set definition allowing to specify a public load balancer for + * the primary network interface configuration. + */ + interface WithPrimaryInternetFacingLoadBalancer { + /** + * Specify the public load balancer where it's backends and/or NAT pools can be assigned to the primary network + * interface configuration of virtual machine scale set. + *

+ * By default all the backend and inbound NAT pool of the load balancer will be associated with the primary + * network interface configuration unless one of them is selected in the next stage + * {@link WithInternalLoadBalancerBackendOrNatPool}. + *

+ * @param loadBalancer an existing public load balancer + * @return the next stage of the definition + */ + WithPrimaryInternetFacingLoadBalancerBackendOrNatPool withPrimaryInternetFacingLoadBalancer(LoadBalancer loadBalancer); + + /** + * Specifies that no public load balancer needs to be associated with virtual machine scale set. + * + * @return the next stage of the virtual machine scale set definition + */ + WithPrimaryInternalLoadBalancer withoutPrimaryInternetFacingLoadBalancer(); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify an internal load balancer for + * the primary network interface configuration. + */ + interface WithPrimaryInternalLoadBalancer { + /** + * Specify the internal load balancer where it's backends and/or NAT pools can be assigned to the primary + * network interface configuration of the virtual machine scale set. + *

+ * By default all the backend and inbound NAT pool of the load balancer will be associated with the primary + * network interface configuration unless one of them is selected in the next stage + * {@link WithInternalLoadBalancerBackendOrNatPool}. + *

+ * @param loadBalancer an existing internal load balancer + * @return the next stage of the definition + */ + WithInternalLoadBalancerBackendOrNatPool withPrimaryInternalLoadBalancer(LoadBalancer loadBalancer); + + /** + * Specifies that no internal load balancer needs to be associated with virtual machine scale set. + * + * @return the next stage of the virtual machine scale set definition + */ + WithCreate withoutPrimaryInternalLoadBalancer(); + } + + /** + * The stage of the virtual machine scale set definition allowing to associate backend pool and/or inbound NAT pool + * of the internet facing load balancer selected in the previous state {@link WithPrimaryInternetFacingLoadBalancer} + * with the primary network interface configuration. + */ + interface WithPrimaryInternetFacingLoadBalancerBackendOrNatPool extends WithPrimaryInternetFacingLoadBalancerNatPool { + /** + * Associate internet facing load balancer backends with the primary network interface configuration of the + * virtual machine scale set. + * + * @param backendNames the backend names + * @return the next stage of the virtual machine scale set definition + */ + WithPrimaryInternetFacingLoadBalancerNatPool withPrimaryInternetFacingLoadBalancerBackend(String ...backendNames); + } + + /** + * The stage of the virtual machine scale set definition allowing to associate inbound NAT pool of the internet + * facing load balancer selected in the previous state {@link WithPrimaryInternetFacingLoadBalancer} with the + * primary network interface configuration. + */ + interface WithPrimaryInternetFacingLoadBalancerNatPool extends WithPrimaryInternalLoadBalancer { + /** + * Associate internet facing load balancer inbound NAT pools with the to the primary network interface + * configuration of the virtual machine scale set. + * + * @param natPoolNames the inbound NAT pool names + * @return the next stage of the virtual machine scale set definition + */ + WithPrimaryInternalLoadBalancer withPrimaryInternetFacingLoadBalancerInboundNatPool(String ...natPoolNames); + } + + /** + * The stage of the virtual machine scale set definition allowing to associate backend pool and/or inbound NAT pool + * of the internal load balancer selected in the previous state {@link WithPrimaryInternalLoadBalancer} with the + * primary network interface configuration. + */ + interface WithInternalLoadBalancerBackendOrNatPool extends WithCreate { + /** + * Associates internal load balancer backend pools with the primary network interface configuration + * of the virtual machine scale set. + * + * @param backendNames the backend names + * @return the next stage of the virtual machine scale set definition + */ + WithInternalInternalLoadBalancerNatPool withPrimaryInternalLoadBalancerBackend(String ...backendNames); + } + + /** + * The stage of the virtual machine scale set definition allowing to assign inbound NAT pool of the internal + * load balancer selected in the previous state {@link WithPrimaryInternalLoadBalancer} with the + * primary network interface configuration. + */ + interface WithInternalInternalLoadBalancerNatPool extends WithCreate { + /** + * Associates internal load balancer inbound NAT pools with the primary network interface configuration + * of the virtual machine scale set. + * + * @param natPoolNames inbound NAT pool names + * @return the next stage of the virtual machine scale set definition + */ + WithCreate withPrimaryInternalLoadBalancerInboundNatPool(String ...natPoolNames); + } + + /** + * The stage of a virtual machine scale set definition containing all the required inputs for the resource + * to be created (via {@link WithCreate#create()}), but also allowing for any other optional settings + * to be specified. + */ + interface WithCreate extends + Creatable, + Resource.DefinitionWithTags { + } + } + + /** + * Grouping of virtual machine scale set update stages. + */ + interface UpdateStages { + /** + * Stage of the virtual machine scale set update allowing to remove public and internal load balancer + * from the primary network interface configuration. + */ + interface WithoutPrimaryLoadBalancer { + /** + * Remove the internet facing load balancer associated to the primary network interface configuration. + *

+ * This removes the association between primary network interface configuration and all backend and + * inbound NAT pools in the load balancer. + *

+ * + * @return the next stage of the virtual machine scale set update + */ + Update withoutPrimaryInternetFacingLoadBalancer(); + + /** + * Remove the internal load balancer associated to the primary network interface configuration. + *

+ * This removes the association between primary network interface configuration and all backend and + * inbound NAT pools in the load balancer. + *

+ * + * @return the next stage of the virtual machine scale set update + */ + Update withoutPrimaryInternalLoadBalancer(); + } + + /** + * Stage of the virtual machine scale set update allowing to associate a backend from the internet facing + * and internal load balancer with the primary network interface configuration. + */ + interface WithPrimaryLoadBalancerBackend { + /** + * + * @param backendName the name of an existing backend + * @return the next stage of the virtual machine scale set update + */ + Update withPrimaryInternetFacingLoadBalancerBackend(String backendName); + + /** + * + * @param backendName the name of an existing backend + * @return the next stage of the virtual machine scale set update + */ + Update withPrimaryInternalLoadBalancerBackend(String backendName); + } + + /** + * Stage of the virtual machine scale set update allowing to remove association between the primary network interface + * configuration and inbound NAT pool of the internet facing and internal load balancer. + */ + interface WithPrimaryLoadBalancerNatPool { + /** + * Removes association between the primary network interface configuration and inbound NAT pool of the + * internet facing load balancer. + * + * @param natPoolName the name of an existing inbound NAT pool + * @return the next stage of the virtual machine scale set update + */ + Update withoutPrimaryInternetFacingLoadBalancerNatPool(String natPoolName); + + /** + * Removes association between the primary network interface configuration and inbound NAT pool of the + * internal load balancer. + * + * @param natPoolName the name of an existing inbound NAT pool + * @return the next stage of the virtual machine scale set update + */ + Update withoutPrimaryInternalLoadBalancerNatPool(String natPoolName); + } + + interface Update { + } + } +} \ No newline at end of file diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java new file mode 100644 index 000000000000..0489c9da327f --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -0,0 +1,10 @@ +package com.microsoft.azure.management.compute.implementation; + +import com.microsoft.azure.management.compute.VirtualMachineScaleSet; + +/** + * Implementation of {@link VirtualMachineScaleSet}. + */ +public class VirtualMachineScaleSetImpl + implements VirtualMachineScaleSet { +} From 68bd2f75588df4812d92c5673f47b8147b3c1415 Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 9 Sep 2016 19:01:27 -0700 Subject: [PATCH 02/10] Adding more intermediate stage interfaces to update stages --- .../compute/VirtualMachineScaleSet.java | 55 +++++++++++++++++-- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java index 8111182673bd..d140c18f99ae 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -201,11 +201,12 @@ interface WithoutPrimaryLoadBalancer { } /** - * Stage of the virtual machine scale set update allowing to associate a backend from the internet facing - * and internal load balancer with the primary network interface configuration. + * Stage of the virtual machine scale set update allowing to associate a backend from the load balancer + * with the primary network interface configuration. */ interface WithPrimaryLoadBalancerBackend { /** + * Associate a backend of the internet facing load balancer with the the primary network interface configuration. * * @param backendName the name of an existing backend * @return the next stage of the virtual machine scale set update @@ -213,6 +214,7 @@ interface WithPrimaryLoadBalancerBackend { Update withPrimaryInternetFacingLoadBalancerBackend(String backendName); /** + * Associate a backend of the internal load balancer with the the primary network interface configuration. * * @param backendName the name of an existing backend * @return the next stage of the virtual machine scale set update @@ -220,11 +222,56 @@ interface WithPrimaryLoadBalancerBackend { Update withPrimaryInternalLoadBalancerBackend(String backendName); } + /** + * Stage of the virtual machine scale set update allowing to associate a inbound NAT pool from the load balancer + * with the primary network interface configuration. + */ + interface WithPrimaryLoadBalancerNatPoold { + /** + * Associate an inbound NAT pool of the internet facing load balancer with the the primary network interface configuration. + * + * @param natPoolName the name of an existing inbound NAT pool + * @return the next stage of the virtual machine scale set update + */ + Update withPrimaryInternetFacingLoadBalancerNatPool(String natPoolName); + + /** + * Associate an inbound NAT pool of the internal load balancer with the the primary network interface configuration. + * + * @param natPoolName the name of an existing inbound NAT pool + * @return the next stage of the virtual machine scale set update + */ + Update withPrimaryInternalLoadBalancerNatPool(String natPoolName); + } + + /** + * Stage of the virtual machine scale set update allowing to remove association between the primary network interface + * configuration and backend of the load balancer. + */ + interface WithoutPrimaryLoadBalancerBackend { + /** + * Removes association between the primary network interface configuration and backend of the internet facing + * load balancer. + * + * @param backendName the name of an existing backend + * @return the next stage of the virtual machine scale set update + */ + Update withoutPrimaryInternetFacingLoadBalancerBackend(String backendName); + + /** + * Removes association between the primary network interface configuration and backend of the internal load balancer. + * + * @param backendName the name of an existing backend + * @return the next stage of the virtual machine scale set update + */ + Update withoutPrimaryInternalLoadBalancerBackend(String backendName); + } + /** * Stage of the virtual machine scale set update allowing to remove association between the primary network interface - * configuration and inbound NAT pool of the internet facing and internal load balancer. + * configuration and inbound NAT pool of the load balancer. */ - interface WithPrimaryLoadBalancerNatPool { + interface WithoutPrimaryLoadBalancerNatPool { /** * Removes association between the primary network interface configuration and inbound NAT pool of the * internet facing load balancer. From 1539d2e9fd303f8544ce0da0fffb0b8f49075120 Mon Sep 17 00:00:00 2001 From: anuchan Date: Fri, 9 Sep 2016 21:52:13 -0700 Subject: [PATCH 03/10] Adding more definition stages of virtual machine scale set --- .../compute/VirtualMachineScaleSet.java | 376 +++++++++++++++++- 1 file changed, 372 insertions(+), 4 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java index d140c18f99ae..5a83eceeef99 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -1,9 +1,11 @@ package com.microsoft.azure.management.compute; import com.microsoft.azure.management.network.LoadBalancer; +import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.storage.StorageAccount; /** * An immutable client-side representation of an Azure virtual machine scale set. @@ -16,10 +18,16 @@ public interface VirtualMachineScaleSet { interface Definition extends DefinitionStages.Blank, DefinitionStages.WithGroup, + DefinitionStages.WithNetwork, DefinitionStages.WithPrimaryInternetFacingLoadBalancer, DefinitionStages.WithPrimaryInternalLoadBalancer, DefinitionStages.WithPrimaryInternetFacingLoadBalancerBackendOrNatPool, DefinitionStages.WithInternalLoadBalancerBackendOrNatPool, + DefinitionStages.WithOS, + DefinitionStages.WithAdminUserName, + DefinitionStages.WithRootUserName, + DefinitionStages.WithLinuxCreate, + DefinitionStages.WithWindowsCreate, DefinitionStages.WithCreate { } @@ -38,7 +46,59 @@ interface Blank * The stage of the virtual machine scale set definition allowing to specify the resource group. */ interface WithGroup - extends GroupableResource.DefinitionStages.WithGroup { + extends GroupableResource.DefinitionStages.WithGroup { + } + + /** + * The stage of the virtual machine scale set definition allowing to specify virtual network for the primary + * network configuration. + */ + interface WithNetwork { + /** + * Create a new virtual network to associate with the primary network interface of virtual machines in the + * virtual machine scale set, based on the provided definition. + * + * @param creatable a creatable definition for a new virtual network + * @return the next stage of the virtual machine scale set definition + */ + WithPrimaryInternetFacingLoadBalancer withNewPrimaryNetwork(Creatable creatable); + + /** + * Creates a new virtual network to associate with the primary network interface of the virtual machines + * in the scale set. + *

+ * the virtual network will be created in the same resource group and region as of virtual machine scale set, + * it will be reated with the specified address space and a default subnet covering the entirety of the + * network IP address space. + *

+ * @param addressSpace the address space for the virtual network + * @return the next stage of the virtual machine scale set definition + */ + WithPrimaryInternetFacingLoadBalancer withNewPrimaryNetwork(String addressSpace); + + /** + * Associate an existing virtual network with the primary network interface of the virtual machines + * in the scale set. + * + * @param network an existing virtual network + * @return the next stage of the virtual machine scale set definition + */ + WithSubnet withExistingPrimaryNetwork(Network network); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify virtual network subnet for the + * virtual machine's primary network interface in the scale set. + * + */ + interface WithSubnet { + /** + * Associates a subnet with the primary network interface of virtual machines in the scale set. + * + * @param name the subnet name + * @return the next stage of the definition + */ + WithPrimaryInternetFacingLoadBalancer withSubnet(String name); } /** @@ -90,7 +150,7 @@ interface WithPrimaryInternalLoadBalancer { * * @return the next stage of the virtual machine scale set definition */ - WithCreate withoutPrimaryInternalLoadBalancer(); + WithOS withoutPrimaryInternalLoadBalancer(); } /** @@ -146,7 +206,7 @@ interface WithInternalLoadBalancerBackendOrNatPool extends WithCreate { * load balancer selected in the previous state {@link WithPrimaryInternalLoadBalancer} with the * primary network interface configuration. */ - interface WithInternalInternalLoadBalancerNatPool extends WithCreate { + interface WithInternalInternalLoadBalancerNatPool extends WithOS { /** * Associates internal load balancer inbound NAT pools with the primary network interface configuration * of the virtual machine scale set. @@ -154,7 +214,309 @@ interface WithInternalInternalLoadBalancerNatPool extends WithCreate { * @param natPoolNames inbound NAT pool names * @return the next stage of the virtual machine scale set definition */ - WithCreate withPrimaryInternalLoadBalancerInboundNatPool(String ...natPoolNames); + WithOS withPrimaryInternalLoadBalancerInboundNatPool(String ...natPoolNames); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify the Operation System image. + */ + interface WithOS { + /** + * Specifies the known marketplace Windows image used as OS for virtual machines in the scale set. + * + * @param knownImage enum value indicating known market-place image + * @return the next stage of the virtual machine scale set definition + */ + WithAdminUserName withPopularWindowsImage(KnownWindowsVirtualMachineImage knownImage); + + /** + * Specifies that the latest version of a marketplace Windows image needs to be used. + * + * @param publisher specifies the publisher of the image + * @param offer specifies the offer of the image + * @param sku specifies the SKU of the image + * @return the next stage of the virtual machine scale set definition + */ + WithAdminUserName withLatestWindowsImage(String publisher, String offer, String sku); + + /** + * Specifies the version of a marketplace Windows image needs to be used. + * + * @param imageReference describes publisher, offer, sku and version of the market-place image + * @return the next stage of the virtual machine scale set definition + */ + WithAdminUserName withSpecificWindowsImageVersion(ImageReference imageReference); + + /** + * Specifies the user (generalized) Windows image used for as the OS for virtual machines in the + * scale set. + * + * @param imageUrl the url the the VHD + * @return the next stage of the virtual machine scale set definition + */ + WithAdminUserName withStoredWindowsImage(String imageUrl); + + /** + * Specifies the known marketplace Linux image used for the virtual machine's OS. + * + * @param knownImage enum value indicating known market-place image + * @return the next stage of the virtual machine scale set definition + */ + WithRootUserName withPopularLinuxImage(KnownLinuxVirtualMachineImage knownImage); + + /** + * Specifies that the latest version of a marketplace Linux image needs to be used. + * + * @param publisher specifies the publisher of the image + * @param offer specifies the offer of the image + * @param sku specifies the SKU of the image + * @return the next stage of the virtual machine scale set definition + */ + WithRootUserName withLatestLinuxImage(String publisher, String offer, String sku); + + /** + * Specifies the version of a market-place Linux image needs to be used. + * + * @param imageReference describes publisher, offer, sku and version of the market-place image + * @return the next stage of the virtual machine scale set definition + */ + WithRootUserName withSpecificLinuxImageVersion(ImageReference imageReference); + + /** + * Specifies the user (generalized) Linux image used for the virtual machine's OS. + * + * @param imageUrl the url the the VHD + * @return the next stage of the virtual machine scale set definition + */ + WithRootUserName withStoredLinuxImage(String imageUrl); + + /** + * Specifies the specialized operating system disk to be attached to the virtual machines in the + * scale set. + * + * @param osDiskUrl osDiskUrl the url to the OS disk in the Azure Storage account + * @param osType the OS type + * @return the next stage of the Windows virtual machine scale set definition + */ + WithCreate withOsDisk(String osDiskUrl, OperatingSystemTypes osType); + } + + /** + * The stage of the Linux virtual machine scale set definition allowing to specify root user name. + */ + interface WithRootUserName { + /** + * Specifies the root user name for the Linux virtual machines in the scale set. + * + * @param rootUserName the Linux root user name. This must follow the required naming convention for Linux user name + * @return the next stage of the Linux virtual machine scale set definition + */ + WithLinuxCreate withRootUserName(String rootUserName); + } + + /** + * The stage of the Windows virtual machine scale set definition allowing to specify administrator user name. + */ + interface WithAdminUserName { + /** + * Specifies the administrator user name for the Windows virtual machines in the scale set. + * + * @param adminUserName the Windows administrator user name. This must follow the required naming convention for Windows user name. + * @return the stage representing creatable Windows VM scale set definition + */ + WithWindowsCreate withAdminUserName(String adminUserName); + } + + /** + * The stage of the Linux virtual machine scale set definition which contains all the minimum required inputs + * for the resource to be created (via {@link WithCreate#create()}), but also allows for any other optional + * settings to be specified. + * + */ + interface WithLinuxCreate extends WithCreate { + /** + * Specifies the SSH public key. + *

+ * Each call to this method adds the given public key to the list of VM's public keys. + * + * @param publicKey the SSH public key in PEM format. + * @return the stage representing creatable Linux VM scale set definition + */ + WithLinuxCreate withSsh(String publicKey); + } + + /** + * The stage of the Windows virtual machine scale set definition which contains all the minimum required + * inputs for the resource to be created (via {@link WithCreate#create()}, but also allows for any other + * optional settings to be specified. + * + */ + interface WithWindowsCreate extends WithCreate { + /** + * Specifies that VM Agent should not be provisioned. + * + * @return the stage representing creatable Windows VM scale set definition + */ + WithWindowsCreate disableVmAgent(); + + /** + * Specifies that automatic updates should be disabled. + * + * @return the stage representing creatable Windows VM scale set definition + */ + WithWindowsCreate disableAutoUpdate(); + + /** + * Specifies the time-zone. + * + * @param timeZone the timezone + * @return the stage representing creatable Windows VM scale set definition + */ + WithWindowsCreate withTimeZone(String timeZone); + + /** + * Specifies the WINRM listener. + *

+ * Each call to this method adds the given listener to the list of VM's WinRM listeners. + * + * @param listener the WinRmListener + * @return the stage representing creatable Windows VM scale set definition + */ + WithWindowsCreate withWinRm(WinRMListener listener); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify password. + */ + interface WithPassword { + /** + * Specifies the password for the virtual machines in the scale set. + * + * @param password the password. This must follow the criteria for Azure VM password. + * @return the stage representing creatable VM scale set definition + */ + WithCreate withPassword(String password); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify VM size. + */ + interface WithVMSize { + /** + * Specifies size for the virtual machines in the scale set. + * + * @param sizeName the name of the size for the virtual machine as text + * @return the stage representing creatable VM scale set definition + */ + WithCreate withSize(String sizeName); + + /** + * Specifies size for the virtual machines in the scale set. + * + * @param size a size from the list of available sizes for the virtual machine + * @return the stage representing creatable VM scale set definition + */ + WithCreate withSize(VirtualMachineSizeTypes size); + } + + /** + * The stage of the virtual machine scale set definition allowing to upgrade policy. + */ + interface WithUpgradePolicy { + /** + * Specifies virtual machine scale set upgrade policy. + * + * @param upgradePolicy upgrade policy + * @return the stage representing creatable VM scale set definition + */ + WithCreate withUpgradePolicy(UpgradePolicy upgradePolicy); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify whether + * or not over provision virtual machines in the scale set. + */ + interface WithOverProvision { + /** + * Enable or disable over provisioning of virtual machines in the scale set. + * + * @param enabled true to enable over provisioning of virtual machines in the + * scale set. + * @return Enable over provision of virtual machines. + */ + WithCreate withOverProvision(boolean enabled); + + /** + * Enable over provision of virtual machines. + * + * @return the stage representing creatable VM scale set definition + */ + WithCreate withOverProvisionEnabled(); + + /** + * Disable over provision of virtual machines. + * + * @return the stage representing creatable VM scale set definition + */ + WithCreate withOverProvisionDisabled(); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify OS disk configurations. + */ + interface WithOsDiskSettings { + /** + * Specifies the caching type for the Operating System disk. + * + * @param cachingType the caching type. + * @return the stage representing creatable VM scale set definition + */ + WithCreate withOsDiskCaching(CachingTypes cachingType); + } + + /** + * The stage of the virtual machine scale set definition allowing to specify storage account. + */ + interface WithStorageAccount { + /** + * Specifies the name of a new storage account to put the OS and data disk VHD of the virtual machines + * in the scale set. + * + * @param name the name of the storage account + * @return the stage representing creatable VM scale set definition + */ + WithCreate withNewStorageAccount(String name); + + /** + * Specifies definition of a not-yet-created storage account definition + * to put OS and data disk VHDs of virtual machines in the scale set. + * + * @param creatable the storage account in creatable stage + * @return the stage representing creatable VM scale set definition + */ + WithCreate withNewStorageAccount(Creatable creatable); + + /** + * Specifies an existing {@link StorageAccount} storage account to put the OS and data disk VHD of + * virtual machines in the scale set. + * + * @param storageAccount an existing storage account + * @return the stage representing creatable VM scale set definition + */ + WithCreate withExistingStorageAccount(StorageAccount storageAccount); + } + + /** + * The stage of the virtual machine definition allowing to specify extensions. + */ + interface WithExtension { + /** + * Specifies definition of an extension to be attached to the virtual machines in the scale set. + * + * @param name the reference name for the extension + * @return the stage representing configuration for the extension + */ + VirtualMachineExtension.DefinitionStages.Blank defineNewExtension(String name); } /** @@ -164,6 +526,12 @@ interface WithInternalInternalLoadBalancerNatPool extends WithCreate { */ interface WithCreate extends Creatable, + DefinitionStages.WithPassword, + DefinitionStages.WithOsDiskSettings, + DefinitionStages.WithVMSize, + DefinitionStages.WithOverProvision, + DefinitionStages.WithStorageAccount, + DefinitionStages.WithExtension, Resource.DefinitionWithTags { } } From 0596d0349a9bed79870f63551a6c992541bc1cc7 Mon Sep 17 00:00:00 2001 From: anuchan Date: Sat, 10 Sep 2016 14:04:02 -0700 Subject: [PATCH 04/10] Initial VirtualMachineScaleSetImpl and Sku models --- .../compute/VirtualMachineScaleSet.java | 187 +++++-- .../compute/VirtualMachineScaleSetSku.java | 21 + .../VirtualMachineScaleSetSkuTypes.java | 244 +++++++++ .../VirtualMachineExtensionImageImpl.java | 2 +- .../VirtualMachineScaleSetImpl.java | 500 +++++++++++++++++- .../VirtualMachineScaleSetSkuImpl.java | 33 ++ 6 files changed, 954 insertions(+), 33 deletions(-) create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSku.java create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSkuTypes.java create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetSkuImpl.java diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java index 5a83eceeef99..987fc475f970 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -1,16 +1,122 @@ package com.microsoft.azure.management.compute; +import com.microsoft.azure.CloudException; +import com.microsoft.azure.PagedList; +import com.microsoft.azure.management.compute.implementation.VirtualMachineScaleSetInner; +import com.microsoft.azure.management.network.Backend; +import com.microsoft.azure.management.network.InboundNatPool; import com.microsoft.azure.management.network.LoadBalancer; import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.resources.fluentcore.model.Refreshable; +import com.microsoft.azure.management.resources.fluentcore.model.Wrapper; import com.microsoft.azure.management.storage.StorageAccount; +import java.io.IOException; +import java.util.Map; + /** * An immutable client-side representation of an Azure virtual machine scale set. */ -public interface VirtualMachineScaleSet { +public interface VirtualMachineScaleSet extends + GroupableResource, + Refreshable, + Wrapper { + // Actions + // + /** + * @return available skus for the virtual machine scale set including the minimum and maximum vm instances + * allowed for a particular sku. + * @throws CloudException thrown for an invalid response from the service. + * @throws IOException exception thrown from serialization/deserialization + */ + PagedList availableSkus() throws CloudException, IOException; + + // Getters + // + + /** + * @return the name prefix of the virtual machines in the scale set. + */ + String computerNamePrefix(); + + /** + * @return the operating system of the virtual machines in the scale set. + */ + OperatingSystemTypes osType(); + + /** + * @return the operating system disk caching type, valid values are 'None', 'ReadOnly', 'ReadWrite' + */ + CachingTypes osDiskCachingType(); + + /** + * @return the upgradePolicy + */ + UpgradePolicy upgradePolicy(); + + /** + * @return true if over provision is enabled for the virtual machines, false otherwise. + */ + boolean overProvisionEnabled(); + + /** + * @return the sku of the virtual machines in the scale set. + */ + VirtualMachineScaleSetSkuTypes sku(); + + /** + * @return the internet facing load balancer associated with the primary network interface of + * the virtual machines in the scale set. + */ + LoadBalancer primaryInternetFacingLoadBalancer(); + + /** + * @return the internet facing load balancer's backends associated with the primary network interface + * of the virtual machines in the scale set. + */ + Map primaryInternetFacingLoadBalancerBackEnds(); + + /** + * @return the internet facing load balancer's inbound NAT pool associated with the primary network interface + * of the virtual machines in the scale set. + */ + Map primaryInternetFacingLoadBalancerInboundNatPools(); + + /** + * @return the internal load balancer associated with the primary network interface of + * the virtual machines in the scale set. + */ + LoadBalancer primaryInternalLoadBalancer(); + + /** + * @return the internal load balancer's backends associated with the primary network interface + * of the virtual machines in the scale set. + */ + Map primaryInternalLoadBalancerBackEnds(); + + /** + * @return the internal load balancer's inbound NAT pool associated with the primary network interface + * of the virtual machines in the scale set. + */ + Map primaryInternalLoadBalancerInboundNatPools(); + + /** + * @return the storage profile. + */ + VirtualMachineScaleSetStorageProfile storageProfile(); + + /** + * @return the network profile + */ + VirtualMachineScaleSetNetworkProfile networkProfile(); + + /** + * @return the extensions attached to the Azure Virtual Machine + */ + Map extensions(); /** * The entirety of the load balancer definition. @@ -18,11 +124,15 @@ public interface VirtualMachineScaleSet { interface Definition extends DefinitionStages.Blank, DefinitionStages.WithGroup, + DefinitionStages.WithSku, DefinitionStages.WithNetwork, + DefinitionStages.WithSubnet, DefinitionStages.WithPrimaryInternetFacingLoadBalancer, DefinitionStages.WithPrimaryInternalLoadBalancer, DefinitionStages.WithPrimaryInternetFacingLoadBalancerBackendOrNatPool, DefinitionStages.WithInternalLoadBalancerBackendOrNatPool, + DefinitionStages.WithPrimaryInternetFacingLoadBalancerNatPool, + DefinitionStages.WithInternalInternalLoadBalancerNatPool, DefinitionStages.WithOS, DefinitionStages.WithAdminUserName, DefinitionStages.WithRootUserName, @@ -46,7 +156,28 @@ interface Blank * The stage of the virtual machine scale set definition allowing to specify the resource group. */ interface WithGroup - extends GroupableResource.DefinitionStages.WithGroup { + extends GroupableResource.DefinitionStages.WithGroup { + } + + /** + * The stage of the virtual machine scale set definition allowing to specify Sku for the virtual machines. + */ + interface WithSku { + /** + * Specifies sku for the virtual machines in the scale set. + * + * @param skuType the sku type + * @return the stage representing creatable VM scale set definition + */ + WithNetwork withSku(VirtualMachineScaleSetSkuTypes skuType); + + /** + * Specifies sku for the virtual machines in the scale set. + * + * @param sku a sku from the list of available sizes for the virtual machines in this scale set + * @return the stage representing creatable VM scale set definition + */ + WithNetwork withSku(VirtualMachineScaleSetSku sku); } /** @@ -248,9 +379,13 @@ interface WithOS { WithAdminUserName withSpecificWindowsImageVersion(ImageReference imageReference); /** - * Specifies the user (generalized) Windows image used for as the OS for virtual machines in the + * Specifies the user (custom) Windows image used for as the OS for virtual machines in the * scale set. - * + *

+ * Custom images are currently limited to single storage account and the number of virtual machines + * in the scale set that can be created using custom image is limited to 40 when over provision + * is disabled {@link WithOverProvision} and up to 20 when enabled. + *

* @param imageUrl the url the the VHD * @return the next stage of the virtual machine scale set definition */ @@ -283,22 +418,16 @@ interface WithOS { WithRootUserName withSpecificLinuxImageVersion(ImageReference imageReference); /** - * Specifies the user (generalized) Linux image used for the virtual machine's OS. - * + * Specifies the user (custom) Linux image used for the virtual machine's OS. + *

+ * Custom images are currently limited to single storage account and the number of virtual machines + * in the scale set that can be created using custom image is limited to 40 when over provision + * is disabled {@link WithOverProvision} and up to 20 when enabled. + *

* @param imageUrl the url the the VHD * @return the next stage of the virtual machine scale set definition */ WithRootUserName withStoredLinuxImage(String imageUrl); - - /** - * Specifies the specialized operating system disk to be attached to the virtual machines in the - * scale set. - * - * @param osDiskUrl osDiskUrl the url to the OS disk in the Azure Storage account - * @param osType the OS type - * @return the next stage of the Windows virtual machine scale set definition - */ - WithCreate withOsDisk(String osDiskUrl, OperatingSystemTypes osType); } /** @@ -399,28 +528,21 @@ interface WithPassword { } /** - * The stage of the virtual machine scale set definition allowing to specify VM size. + * The stage of the virtual machine scale set definition allowing to specify number of + * virtual machines in the scale set. */ - interface WithVMSize { - /** - * Specifies size for the virtual machines in the scale set. - * - * @param sizeName the name of the size for the virtual machine as text - * @return the stage representing creatable VM scale set definition - */ - WithCreate withSize(String sizeName); - + interface WithCapacity { /** - * Specifies size for the virtual machines in the scale set. + * Specifies the number of virtual machines in the scale set. * - * @param size a size from the list of available sizes for the virtual machine + * @param capacity the virtual machine capacity * @return the stage representing creatable VM scale set definition */ - WithCreate withSize(VirtualMachineSizeTypes size); + WithCreate withCapacity(long capacity); } /** - * The stage of the virtual machine scale set definition allowing to upgrade policy. + * The stage of the virtual machine scale set definition allowing to specify upgrade policy. */ interface WithUpgradePolicy { /** @@ -528,7 +650,7 @@ interface WithCreate extends Creatable, DefinitionStages.WithPassword, DefinitionStages.WithOsDiskSettings, - DefinitionStages.WithVMSize, + WithCapacity, DefinitionStages.WithOverProvision, DefinitionStages.WithStorageAccount, DefinitionStages.WithExtension, @@ -659,6 +781,9 @@ interface WithoutPrimaryLoadBalancerNatPool { Update withoutPrimaryInternalLoadBalancerNatPool(String natPoolName); } + /** + * The entirety of the load balancer update. + */ interface Update { } } diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSku.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSku.java new file mode 100644 index 000000000000..8fa9043c887c --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSku.java @@ -0,0 +1,21 @@ +package com.microsoft.azure.management.compute; + +/** + * A type representing sku available for virtual machines in a scale set. + */ +public interface VirtualMachineScaleSetSku { + /** + * @return the type of resource the sku applies to. + */ + String resourceType(); + + /** + * @return the Sku type. + */ + VirtualMachineScaleSetSkuTypes skuType(); + + /** + * @return available scaling information. + */ + VirtualMachineScaleSetSkuCapacity capacity(); +} diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSkuTypes.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSkuTypes.java new file mode 100644 index 000000000000..19397a1687a4 --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetSkuTypes.java @@ -0,0 +1,244 @@ +package com.microsoft.azure.management.compute; + +/** + * Scale set virtual machine sku types. + */ +public class VirtualMachineScaleSetSkuTypes { + /** Static value Standard_A0 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A0 = new VirtualMachineScaleSetSkuTypes("Standard_A0", "Standard"); + + /** Static value Standard_A1 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A1 = new VirtualMachineScaleSetSkuTypes("Standard_A1", "Standard"); + + /** Static value Standard_A2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A2 = new VirtualMachineScaleSetSkuTypes("Standard_A2", "Standard"); + + /** Static value Standard_A3 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A3 = new VirtualMachineScaleSetSkuTypes("Standard_A3", "Standard"); + + /** Static value Standard_A4 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A4 = new VirtualMachineScaleSetSkuTypes("Standard_A4", "Standard"); + + /** Static value Standard_A5 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A5 = new VirtualMachineScaleSetSkuTypes("Standard_A5", "Standard"); + + /** Static value Standard_A6 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A6 = new VirtualMachineScaleSetSkuTypes("Standard_A6", "Standard"); + + /** Static value Standard_A7 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A7 = new VirtualMachineScaleSetSkuTypes("Standard_A7", "Standard"); + + /** Static value Standard_A8 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A8 = new VirtualMachineScaleSetSkuTypes("Standard_A8", "Standard"); + + /** Static value Standard_A9 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A9 = new VirtualMachineScaleSetSkuTypes("Standard_A9", "Standard"); + + /** Static value Standard_A10 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A10 = new VirtualMachineScaleSetSkuTypes("Standard_A10", "Standard"); + + /** Static value Standard_A11 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_A11 = new VirtualMachineScaleSetSkuTypes("Standard_A11", "Standard"); + + /** Static value Standard_D1 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D1 = new VirtualMachineScaleSetSkuTypes("Standard_D1", "Standard"); + + /** Static value Standard_D2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D2 = new VirtualMachineScaleSetSkuTypes("Standard_D2", "Standard"); + + /** Static value Standard_D3 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D3 = new VirtualMachineScaleSetSkuTypes("Standard_D3", "Standard"); + + /** Static value Standard_D4 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D4 = new VirtualMachineScaleSetSkuTypes("Standard_D4", "Standard"); + + /** Static value Standard_D11 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D11 = new VirtualMachineScaleSetSkuTypes("Standard_D11", "Standard"); + + /** Static value Standard_D12 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D12 = new VirtualMachineScaleSetSkuTypes("Standard_D12", "Standard"); + + /** Static value Standard_D13 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D13 = new VirtualMachineScaleSetSkuTypes("Standard_D13", "Standard"); + + /** Static value Standard_D14 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D14 = new VirtualMachineScaleSetSkuTypes("Standard_D14", "Standard"); + + /** Static value Standard_D1_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D1_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D1_v2", "Standard"); + + /** Static value Standard_D2_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D2_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D2_v2", "Standard"); + + /** Static value Standard_D3_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D3_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D3_v2", "Standard"); + + /** Static value Standard_D4_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D4_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D4_v2", "Standard"); + + /** Static value Standard_D5_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D5_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D5_v2", "Standard"); + + /** Static value Standard_D11_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D11_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D11_v2", "Standard"); + + /** Static value Standard_D12_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D12_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D12_v2", "Standard"); + + /** Static value Standard_D13_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D13_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D13_v2", "Standard"); + + /** Static value Standard_D14_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D14_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D14_v2", "Standard"); + + /** Static value Standard_D15_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_D15_V2 = new VirtualMachineScaleSetSkuTypes("Standard_D15_v2", "Standard"); + + /** Static value Standard_DS1 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS1 = new VirtualMachineScaleSetSkuTypes("Standard_DS1", "Standard"); + + /** Static value Standard_DS2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS2 = new VirtualMachineScaleSetSkuTypes("Standard_DS2", "Standard"); + + /** Static value Standard_DS3 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS3 = new VirtualMachineScaleSetSkuTypes("Standard_DS3", "Standard"); + + /** Static value Standard_DS4 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS4 = new VirtualMachineScaleSetSkuTypes("Standard_DS4", "Standard"); + + /** Static value Standard_DS11 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS11 = new VirtualMachineScaleSetSkuTypes("Standard_DS11", "Standard"); + + /** Static value Standard_DS12 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS12 = new VirtualMachineScaleSetSkuTypes("Standard_DS12", "Standard"); + + /** Static value Standard_DS13 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS13 = new VirtualMachineScaleSetSkuTypes("Standard_DS13", "Standard"); + + /** Static value Standard_DS14 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS14 = new VirtualMachineScaleSetSkuTypes("Standard_DS14", "Standard"); + + /** Static value Standard_DS1_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS1_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS1_v2", "Standard"); + + /** Static value Standard_DS2_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS2_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS2_v2", "Standard"); + + /** Static value Standard_DS3_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS3_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS3_v2", "Standard"); + + /** Static value Standard_DS4_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS4_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS4_v2", "Standard"); + + /** Static value Standard_DS5_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS5_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS5_v2", "Standard"); + + /** Static value Standard_DS11_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS11_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS11_v2", "Standard"); + + /** Static value Standard_DS12_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS12_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS12_v2", "Standard"); + + /** Static value Standard_DS13_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS13_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS13_v2", "Standard"); + + /** Static value Standard_DS14_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS14_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS14_v2", "Standard"); + + /** Static value Standard_DS15_v2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_DS15_V2 = new VirtualMachineScaleSetSkuTypes("Standard_DS15_v2", "Standard"); + + /** Static value STANDARD_F1S for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F1S = new VirtualMachineScaleSetSkuTypes("STANDARD_F1S", "Standard"); + + /** Static value STANDARD_F2S for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F2S = new VirtualMachineScaleSetSkuTypes("STANDARD_F2S", "Standard"); + + /** Static value STANDARD_F4S for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F4S = new VirtualMachineScaleSetSkuTypes("STANDARD_F4S", "Standard"); + + /** Static value STANDARD_F8S for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F8S = new VirtualMachineScaleSetSkuTypes("STANDARD_F8S", "Standard"); + + /** Static value STANDARD_F16S for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F16S = new VirtualMachineScaleSetSkuTypes("STANDARD_F16S", "Standard"); + + /** Static value STANDARD_F1 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F1 = new VirtualMachineScaleSetSkuTypes("STANDARD_F1", "Standard"); + + /** Static value STANDARD_F2 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F2 = new VirtualMachineScaleSetSkuTypes("STANDARD_F2", "Standard"); + + /** Static value STANDARD_F4 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F4 = new VirtualMachineScaleSetSkuTypes("STANDARD_F4", "Standard"); + + /** Static value STANDARD_F8 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F8 = new VirtualMachineScaleSetSkuTypes("STANDARD_F8", "Standard"); + + /** Static value STANDARD_F16 for VirtualMachineScaleSetSkuTypes. */ + public static final VirtualMachineScaleSetSkuTypes STANDARD_F16 = new VirtualMachineScaleSetSkuTypes("STANDARD_F16", "Standard"); + + /** + * the sku corresponding to this size. + */ + private Sku sku; + + /** + * The string value of the sku. + */ + private String value; + /** + * Creates a custom value for VirtualMachineSizeTypes. + * @param skuName the sku name + * @param skuTier thr sku tier + */ + public VirtualMachineScaleSetSkuTypes(String skuName, String skuTier) { + this(new Sku().withName(skuName).withTier(skuTier)); + } + + /** + * Creates a custom value for VirtualMachineSizeTypes. + * @param sku the sku + */ + public VirtualMachineScaleSetSkuTypes(Sku sku) { + this.sku = sku; + this.value = this.sku.name(); + if (this.sku.tier() != null) { + this.value = this.value + "_" + this.sku.tier(); + } + } + + /** + * @return the sku + */ + public Sku sku() { + return this.sku; + } + + @Override + public String toString() { + return this.value; + } + + @Override + public int hashCode() { + return this.value.hashCode(); + } + + @Override + public boolean equals(Object obj) { + String value = this.toString(); + if (!(obj instanceof VirtualMachineSizeTypes)) { + return false; + } + if (obj == this) { + return true; + } + VirtualMachineScaleSetSkuTypes rhs = (VirtualMachineScaleSetSkuTypes) obj; + if (value == null) { + return rhs.value == null; + } else { + return value.equals(rhs.value); + } + } +} diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineExtensionImageImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineExtensionImageImpl.java index d08165aec8f3..c42373a31a63 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineExtensionImageImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineExtensionImageImpl.java @@ -9,7 +9,7 @@ /** * The implementation for {@link VirtualMachineExtensionImage}. */ -public class VirtualMachineExtensionImageImpl +class VirtualMachineExtensionImageImpl extends WrapperImpl implements VirtualMachineExtensionImage { private final VirtualMachineExtensionImageVersion version; diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java index 0489c9da327f..eedee976db2b 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -1,10 +1,508 @@ package com.microsoft.azure.management.compute.implementation; +import com.microsoft.azure.CloudException; +import com.microsoft.azure.PagedList; +import com.microsoft.azure.management.compute.CachingTypes; +import com.microsoft.azure.management.compute.DiskCreateOptionTypes; +import com.microsoft.azure.management.compute.ImageReference; +import com.microsoft.azure.management.compute.KnownLinuxVirtualMachineImage; +import com.microsoft.azure.management.compute.KnownWindowsVirtualMachineImage; +import com.microsoft.azure.management.compute.LinuxConfiguration; +import com.microsoft.azure.management.compute.OperatingSystemTypes; +import com.microsoft.azure.management.compute.SshConfiguration; +import com.microsoft.azure.management.compute.SshPublicKey; +import com.microsoft.azure.management.compute.UpgradePolicy; +import com.microsoft.azure.management.compute.VirtualHardDisk; +import com.microsoft.azure.management.compute.VirtualMachineExtension; import com.microsoft.azure.management.compute.VirtualMachineScaleSet; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetNetworkProfile; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetOSDisk; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetOSProfile; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetSku; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetSkuTypes; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetStorageProfile; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetVMProfile; +import com.microsoft.azure.management.compute.WinRMConfiguration; +import com.microsoft.azure.management.compute.WinRMListener; +import com.microsoft.azure.management.compute.WindowsConfiguration; +import com.microsoft.azure.management.network.Backend; +import com.microsoft.azure.management.network.InboundNatPool; +import com.microsoft.azure.management.network.LoadBalancer; +import com.microsoft.azure.management.network.Network; +import com.microsoft.azure.management.network.implementation.NetworkManager; +import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; +import com.microsoft.azure.management.storage.StorageAccount; +import com.microsoft.azure.management.storage.implementation.StorageManager; +import rx.Observable; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Map; /** * Implementation of {@link VirtualMachineScaleSet}. */ public class VirtualMachineScaleSetImpl - implements VirtualMachineScaleSet { + extends GroupableResourceImpl< + VirtualMachineScaleSet, + VirtualMachineScaleSetInner, + VirtualMachineScaleSetImpl, + ComputeManager> + implements + VirtualMachineScaleSet, + VirtualMachineScaleSet.Definition { + + // Clients + private final VirtualMachineScaleSetsInner client; + private final StorageManager storageManager; + private final NetworkManager networkManager; + // the name of the virtual machine scale set + private final String scaleSetName; + // used to generate unique name for any dependency resources + private final ResourceNamer namer; + private boolean isMarketplaceLinuxImage = false; + + VirtualMachineScaleSetImpl(String name, + VirtualMachineScaleSetInner innerModel, + VirtualMachineScaleSetsInner client, + final ComputeManager computeManager, + final StorageManager storageManager, + final NetworkManager networkManager) { + super(name, innerModel, computeManager); + this.client = client; + this.storageManager = storageManager; + this.networkManager = networkManager; + this.scaleSetName = name; + this.namer = new ResourceNamer(this.scaleSetName); + } + + static VirtualMachineScaleSetImpl create(String name) { + VirtualMachineScaleSetInner inner = new VirtualMachineScaleSetInner(); + + inner.withVirtualMachineProfile(new VirtualMachineScaleSetVMProfile()); + inner.virtualMachineProfile() + .withStorageProfile(new VirtualMachineScaleSetStorageProfile() + .withOsDisk(new VirtualMachineScaleSetOSDisk().withVhdContainers(new ArrayList()))); + inner.virtualMachineProfile() + .withOsProfile(new VirtualMachineScaleSetOSProfile()); + inner.virtualMachineProfile() + .withNetworkProfile(new VirtualMachineScaleSetNetworkProfile()); + return new VirtualMachineScaleSetImpl(name, + inner, + null, + null, + null, + null); + } + + @Override + public PagedList availableSkus() throws CloudException, IOException { + return null; + } + + @Override + public String computerNamePrefix() { + return this.inner().virtualMachineProfile().osProfile().computerNamePrefix(); + } + + @Override + public OperatingSystemTypes osType() { + return this.inner().virtualMachineProfile().storageProfile().osDisk().osType(); + } + + @Override + public CachingTypes osDiskCachingType() { + return this.inner().virtualMachineProfile().storageProfile().osDisk().caching(); + } + + @Override + public UpgradePolicy upgradePolicy() { + return this.inner().upgradePolicy(); + } + + @Override + public boolean overProvisionEnabled() { + return this.inner().overProvision(); + } + + @Override + public VirtualMachineScaleSetSkuTypes sku() { + return new VirtualMachineScaleSetSkuTypes(this.inner().sku()); + } + + @Override + public LoadBalancer primaryInternetFacingLoadBalancer() { + return null; + } + + @Override + public Map primaryInternetFacingLoadBalancerBackEnds() { + return null; + } + + @Override + public Map primaryInternetFacingLoadBalancerInboundNatPools() { + return null; + } + + @Override + public LoadBalancer primaryInternalLoadBalancer() { + return null; + } + + @Override + public Map primaryInternalLoadBalancerBackEnds() { + return null; + } + + @Override + public Map primaryInternalLoadBalancerInboundNatPools() { + return null; + } + + @Override + public VirtualMachineScaleSetStorageProfile storageProfile() { + return this.inner().virtualMachineProfile().storageProfile(); + } + + @Override + public VirtualMachineScaleSetNetworkProfile networkProfile() { + return this.inner().virtualMachineProfile().networkProfile(); + } + + @Override + public Map extensions() { + return null; + } + + @Override + public VirtualMachineScaleSetImpl withAdminUserName(String adminUserName) { + this.inner().virtualMachineProfile() + .osProfile() + .withAdminPassword(adminUserName); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withSsh(String publicKeyData) { + VirtualMachineScaleSetOSProfile osProfile = this.inner() + .virtualMachineProfile() + .osProfile(); + if (osProfile.linuxConfiguration().ssh() == null) { + SshConfiguration sshConfiguration = new SshConfiguration(); + sshConfiguration.withPublicKeys(new ArrayList()); + osProfile.linuxConfiguration().withSsh(sshConfiguration); + } + SshPublicKey sshPublicKey = new SshPublicKey(); + sshPublicKey.withKeyData(publicKeyData); + sshPublicKey.withPath("/home/" + osProfile.adminUsername() + "/.ssh/authorized_keys"); + osProfile.linuxConfiguration().ssh().publicKeys().add(sshPublicKey); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withCapacity(long capacity) { + this.inner().sku().withCapacity(capacity); + return this; + } + + @Override + public VirtualMachineExtension.DefinitionStages.Blank defineNewExtension(String name) { + return null; + } + + @Override + public VirtualMachineScaleSetImpl withNewPrimaryNetwork(Creatable creatable) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withNewPrimaryNetwork(String addressSpace) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withExistingPrimaryNetwork(Network network) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withSubnet(String name) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPopularWindowsImage(KnownWindowsVirtualMachineImage knownImage) { + return withSpecificWindowsImageVersion(knownImage.imageReference()); + } + + @Override + public VirtualMachineScaleSetImpl withLatestWindowsImage(String publisher, String offer, String sku) { + ImageReference imageReference = new ImageReference() + .withPublisher(publisher) + .withOffer(offer) + .withSku(sku) + .withVersion("latest"); + return withSpecificWindowsImageVersion(imageReference); + } + + @Override + public VirtualMachineScaleSetImpl withSpecificWindowsImageVersion(ImageReference imageReference) { + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withCreateOption(DiskCreateOptionTypes.FROM_IMAGE); + this.inner() + .virtualMachineProfile() + .storageProfile().withImageReference(imageReference); + this.inner() + .virtualMachineProfile() + .osProfile().withWindowsConfiguration(new WindowsConfiguration()); + // sets defaults for "Stored(Custom)Image" or "VM(Platform)Image" + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withProvisionVMAgent(true); + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withEnableAutomaticUpdates(true); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withStoredWindowsImage(String imageUrl) { + VirtualHardDisk userImageVhd = new VirtualHardDisk(); + userImageVhd.withUri(imageUrl); + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withCreateOption(DiskCreateOptionTypes.FROM_IMAGE); + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withImage(userImageVhd); + // For platform image osType will be null, azure will pick it from the image metadata. + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withOsType(OperatingSystemTypes.WINDOWS); + this.inner() + .virtualMachineProfile() + .osProfile().withWindowsConfiguration(new WindowsConfiguration()); + // sets defaults for "Stored(Custom)Image" or "VM(Platform)Image" + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withProvisionVMAgent(true); + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withEnableAutomaticUpdates(true); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPopularLinuxImage(KnownLinuxVirtualMachineImage knownImage) { + return withSpecificLinuxImageVersion(knownImage.imageReference()); + } + + @Override + public VirtualMachineScaleSetImpl withLatestLinuxImage(String publisher, String offer, String sku) { + ImageReference imageReference = new ImageReference() + .withPublisher(publisher) + .withOffer(offer) + .withSku(sku) + .withVersion("latest"); + return withSpecificLinuxImageVersion(imageReference); + } + + @Override + public VirtualMachineScaleSetImpl withSpecificLinuxImageVersion(ImageReference imageReference) { + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withCreateOption(DiskCreateOptionTypes.FROM_IMAGE); + this.inner() + .virtualMachineProfile() + .storageProfile().withImageReference(imageReference); + this.inner() + .virtualMachineProfile() + .osProfile().withLinuxConfiguration(new LinuxConfiguration()); + this.isMarketplaceLinuxImage = true; + return this; + } + + @Override + public VirtualMachineScaleSetImpl withStoredLinuxImage(String imageUrl) { + VirtualHardDisk userImageVhd = new VirtualHardDisk(); + userImageVhd.withUri(imageUrl); + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withCreateOption(DiskCreateOptionTypes.FROM_IMAGE); + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withImage(userImageVhd); + // For platform image osType will be null, azure will pick it from the image metadata. + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withOsType(OperatingSystemTypes.LINUX); + this.inner() + .virtualMachineProfile() + .osProfile().withLinuxConfiguration(new LinuxConfiguration()); + return this; + } + + @Override + public VirtualMachineScaleSetImpl disableVmAgent() { + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withProvisionVMAgent(false); + return this; + } + + @Override + public VirtualMachineScaleSetImpl disableAutoUpdate() { + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withEnableAutomaticUpdates(false); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withTimeZone(String timeZone) { + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withTimeZone(timeZone); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withWinRm(WinRMListener listener) { + if (this.inner().virtualMachineProfile().osProfile().windowsConfiguration().winRM() == null) { + WinRMConfiguration winRMConfiguration = new WinRMConfiguration(); + this.inner() + .virtualMachineProfile() + .osProfile().windowsConfiguration().withWinRM(winRMConfiguration); + } + this.inner() + .virtualMachineProfile() + .osProfile() + .windowsConfiguration() + .winRM() + .listeners() + .add(listener); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withOsDiskCaching(CachingTypes cachingType) { + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withCaching(cachingType); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withOverProvision(boolean enabled) { + this.inner().withOverProvision(enabled); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withOverProvisionEnabled() { + return this.withOverProvision(true); + } + + @Override + public VirtualMachineScaleSetImpl withOverProvisionDisabled() { + return this.withOverProvision(false); + } + + @Override + public VirtualMachineScaleSetImpl withPassword(String password) { + this.inner() + .virtualMachineProfile() + .osProfile().withAdminPassword(password); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer loadBalancer) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withoutPrimaryInternalLoadBalancer() { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancer(LoadBalancer loadBalancer) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withoutPrimaryInternetFacingLoadBalancer() { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(String... backendNames) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNatPool(String... natPoolNames) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPool(String... natPoolNames) { + return null; + } + + @Override + public VirtualMachineScaleSetImpl withRootUserName(String rootUserName) { + return this.withAdminUserName(rootUserName); + } + + @Override + public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSkuTypes skuType) { + this.inner().withSku(skuType.sku()); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSku sku) { + return this.withSku(sku.skuType()); + } + + @Override + public VirtualMachineScaleSetImpl withNewStorageAccount(String name) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withNewStorageAccount(Creatable creatable) { + return this; + } + + @Override + public VirtualMachineScaleSetImpl withExistingStorageAccount(StorageAccount storageAccount) { + return this; + } + + @Override + public Observable applyAsync() { + return null; + } + + @Override + public Observable createResourceAsync() { + return null; + } + + @Override + public VirtualMachineScaleSetImpl refresh() throws Exception { + return this; + } } diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetSkuImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetSkuImpl.java new file mode 100644 index 000000000000..65fbde3779a5 --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetSkuImpl.java @@ -0,0 +1,33 @@ +package com.microsoft.azure.management.compute.implementation; + +import com.microsoft.azure.management.compute.VirtualMachineScaleSetSku; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetSkuCapacity; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetSkuTypes; +import com.microsoft.azure.management.resources.fluentcore.model.implementation.WrapperImpl; + +/** + * implementation of {@link VirtualMachineScaleSetSku}. + */ +class VirtualMachineScaleSetSkuImpl + extends WrapperImpl + implements VirtualMachineScaleSetSku { + VirtualMachineScaleSetSkuImpl(VirtualMachineScaleSetSkuInner inner) { + super(inner); + } + + @Override + public String resourceType() { + return this.inner().resourceType(); + } + + @Override + public VirtualMachineScaleSetSkuTypes skuType() { + return new VirtualMachineScaleSetSkuTypes(this.inner().sku()); + } + + @Override + public VirtualMachineScaleSetSkuCapacity capacity() { + return this.inner().capacity(); + } +} + From 5d750dc3bbfa5d5f31f3cd5a63d37eb211bdc226 Mon Sep 17 00:00:00 2001 From: anuchan Date: Sat, 10 Sep 2016 17:04:07 -0700 Subject: [PATCH 05/10] Enabling wither for load balancer & it's backends and nat pools --- .../compute/VirtualMachineScaleSet.java | 8 +- .../VirtualMachineScaleSetImpl.java | 228 +++++++++++++++++- 2 files changed, 228 insertions(+), 8 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java index 987fc475f970..5c36cf0a3c38 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -70,8 +70,10 @@ public interface VirtualMachineScaleSet extends /** * @return the internet facing load balancer associated with the primary network interface of * the virtual machines in the scale set. + * + * @throws throws IOException the IO exception */ - LoadBalancer primaryInternetFacingLoadBalancer(); + LoadBalancer primaryInternetFacingLoadBalancer() throws IOException; /** * @return the internet facing load balancer's backends associated with the primary network interface @@ -88,8 +90,10 @@ public interface VirtualMachineScaleSet extends /** * @return the internal load balancer associated with the primary network interface of * the virtual machines in the scale set. + * + * @throws throws IOException the IO exception */ - LoadBalancer primaryInternalLoadBalancer(); + LoadBalancer primaryInternalLoadBalancer() throws IOException; /** * @return the internal load balancer's backends associated with the primary network interface diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java index eedee976db2b..78ba59a069c5 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -2,6 +2,7 @@ import com.microsoft.azure.CloudException; import com.microsoft.azure.PagedList; +import com.microsoft.azure.SubResource; import com.microsoft.azure.management.compute.CachingTypes; import com.microsoft.azure.management.compute.DiskCreateOptionTypes; import com.microsoft.azure.management.compute.ImageReference; @@ -30,6 +31,7 @@ import com.microsoft.azure.management.network.LoadBalancer; import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.network.implementation.NetworkManager; +import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; @@ -39,6 +41,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.List; import java.util.Map; /** @@ -63,6 +66,15 @@ public class VirtualMachineScaleSetImpl // used to generate unique name for any dependency resources private final ResourceNamer namer; private boolean isMarketplaceLinuxImage = false; + // the primary load balancers + private LoadBalancer primaryInternetFacingLoadBalancer; + private LoadBalancer primaryInternalLoadBalancer; + // unique key of a creatable network that needs to be used in virtual machine's primary network interface + private String creatablePrimaryNetworkKey; + // reference to an existing network that needs to be used in virtual machine's primary network interface + private Network existingPrimaryNetworkToAssociate; + // name of an existing subnet in the network to use + private String existingSubnetNameToAssociate; VirtualMachineScaleSetImpl(String name, VirtualMachineScaleSetInner innerModel, @@ -87,8 +99,28 @@ static VirtualMachineScaleSetImpl create(String name) { .withOsDisk(new VirtualMachineScaleSetOSDisk().withVhdContainers(new ArrayList()))); inner.virtualMachineProfile() .withOsProfile(new VirtualMachineScaleSetOSProfile()); + inner.virtualMachineProfile() .withNetworkProfile(new VirtualMachineScaleSetNetworkProfile()); + + inner.virtualMachineProfile() + .networkProfile() + .withNetworkInterfaceConfigurations(new ArrayList()); + + VirtualMachineScaleSetNetworkConfigurationInner primaryNetworkInterfaceConfiguration = + new VirtualMachineScaleSetNetworkConfigurationInner() + .withPrimary(true) + .withName("default") + .withIpConfigurations(new ArrayList()); + primaryNetworkInterfaceConfiguration + .ipConfigurations() + .add(new VirtualMachineScaleSetIPConfigurationInner()); + + inner.virtualMachineProfile() + .networkProfile() + .networkInterfaceConfigurations() + .add(primaryNetworkInterfaceConfiguration); + return new VirtualMachineScaleSetImpl(name, inner, null, @@ -133,8 +165,9 @@ public VirtualMachineScaleSetSkuTypes sku() { } @Override - public LoadBalancer primaryInternetFacingLoadBalancer() { - return null; + public LoadBalancer primaryInternetFacingLoadBalancer() throws IOException { + setPrimaryLoadBalancersIfAvailable(); + return this.primaryInternetFacingLoadBalancer; } @Override @@ -148,8 +181,9 @@ public Map primaryInternetFacingLoadBalancerInboundNatPo } @Override - public LoadBalancer primaryInternalLoadBalancer() { - return null; + public LoadBalancer primaryInternalLoadBalancer() throws IOException { + setPrimaryLoadBalancersIfAvailable(); + return this.primaryInternalLoadBalancer; } @Override @@ -187,6 +221,12 @@ public VirtualMachineScaleSetImpl withAdminUserName(String adminUserName) { @Override public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { + if (this.primaryInternalLoadBalancer != null) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = primaryNicDefaultIPConfiguration(); + this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), + defaultPrimaryIpConfig, + backendNames); + } return this; } @@ -220,21 +260,35 @@ public VirtualMachineExtension.DefinitionStages.Blank creatable) { + this.addCreatableDependency(creatable); + this.creatablePrimaryNetworkKey = creatable.key(); return this; } @Override public VirtualMachineScaleSetImpl withNewPrimaryNetwork(String addressSpace) { - return this; + Network.DefinitionStages.WithGroup definitionWithGroup = this.networkManager + .networks() + .define(this.namer.randomName("vnet", 20)) + .withRegion(this.region()); + Network.DefinitionStages.WithCreate definitionAfterGroup; + if (this.creatableGroup != null) { + definitionAfterGroup = definitionWithGroup.withNewResourceGroup(this.creatableGroup); + } else { + definitionAfterGroup = definitionWithGroup.withExistingResourceGroup(this.resourceGroupName()); + } + return withNewPrimaryNetwork(definitionAfterGroup.withAddressSpace(addressSpace)); } @Override public VirtualMachineScaleSetImpl withExistingPrimaryNetwork(Network network) { + this.existingPrimaryNetworkToAssociate = network; return this; } @Override public VirtualMachineScaleSetImpl withSubnet(String name) { + this.existingSubnetNameToAssociate = name; return this; } @@ -427,6 +481,7 @@ public VirtualMachineScaleSetImpl withPassword(String password) { @Override public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer loadBalancer) { + this.primaryInternalLoadBalancer = loadBalancer; return this; } @@ -437,6 +492,7 @@ public VirtualMachineScaleSetImpl withoutPrimaryInternalLoadBalancer() { @Override public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancer(LoadBalancer loadBalancer) { + this.primaryInternetFacingLoadBalancer = loadBalancer; return this; } @@ -447,17 +503,35 @@ public VirtualMachineScaleSetImpl withoutPrimaryInternetFacingLoadBalancer() { @Override public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(String... backendNames) { + if (this.primaryInternetFacingLoadBalancer != null) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); + this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + defaultPrimaryIpConfig, + backendNames); + } return this; } @Override public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNatPool(String... natPoolNames) { + if (this.primaryInternetFacingLoadBalancer != null) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); + this.associateInboundNATPoolsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + defaultPrimaryIpConfig, + natPoolNames); + } return this; } @Override public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPool(String... natPoolNames) { - return null; + if (this.primaryInternalLoadBalancer != null) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); + this.associateInboundNATPoolsToIpConfiguration(this.primaryInternalLoadBalancer.id(), + defaultPrimaryIpConfig, + natPoolNames); + } + return this; } @Override @@ -503,6 +577,148 @@ public Observable createResourceAsync() { @Override public VirtualMachineScaleSetImpl refresh() throws Exception { + // GET VMSS + this.resetPrimaryLoadBalancers(); return this; } + + private VirtualMachineScaleSetIPConfigurationInner primaryNicDefaultIPConfiguration() { + List nicConfigurations = this.inner() + .virtualMachineProfile() + .networkProfile() + .networkInterfaceConfigurations(); + + for (VirtualMachineScaleSetNetworkConfigurationInner nicConfiguration : nicConfigurations) { + if (nicConfiguration.primary()) { + if (nicConfiguration.ipConfigurations().size() > 0) { + VirtualMachineScaleSetIPConfigurationInner ipConfig = nicConfiguration.ipConfigurations().get(0); + if (ipConfig.loadBalancerBackendAddressPools() == null) { + ipConfig.withLoadBalancerBackendAddressPools(new ArrayList()); + } + if (ipConfig.loadBalancerInboundNatPools() == null) { + ipConfig.withLoadBalancerInboundNatPools(new ArrayList()); + } + } + } + } + throw new RuntimeException("Could not find the primary nic configuration or an IP configuration in it"); + } + + private void resetPrimaryLoadBalancers() { + this.primaryInternetFacingLoadBalancer = null; + this.primaryInternalLoadBalancer = null; + } + + private void setPrimaryLoadBalancersIfAvailable() throws IOException { + String firstLoadBalancerId = null; + VirtualMachineScaleSetIPConfigurationInner ipConfig = primaryNicDefaultIPConfiguration(); + if (!ipConfig.loadBalancerBackendAddressPools().isEmpty()) { + firstLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(ipConfig.loadBalancerBackendAddressPools().get(0).id()); + } + + if (firstLoadBalancerId == null && !ipConfig.loadBalancerInboundNatPools().isEmpty()) { + firstLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(ipConfig.loadBalancerInboundNatPools().get(0).id()); + } + + if (firstLoadBalancerId == null) { + return; + } + + if (this.primaryInternetFacingLoadBalancer != null + && !this.primaryInternetFacingLoadBalancer.id().equalsIgnoreCase(firstLoadBalancerId)) { + if (this.primaryInternalLoadBalancer == null) { + this.primaryInternalLoadBalancer = this.networkManager + .loadBalancers() + .getById(firstLoadBalancerId); + return; + } + } + + if (this.primaryInternalLoadBalancer != null + && !this.primaryInternalLoadBalancer.id().equalsIgnoreCase(firstLoadBalancerId)) { + if (this.primaryInternetFacingLoadBalancer == null) { + this.primaryInternetFacingLoadBalancer = this.networkManager + .loadBalancers() + .getById(firstLoadBalancerId); + return; + } + } + + LoadBalancer loadBalancer1 = this.networkManager + .loadBalancers() + .getById(firstLoadBalancerId); + if (loadBalancer1.publicIpAddressIds() != null && loadBalancer1.publicIpAddressIds().size() > 0) { + this.primaryInternetFacingLoadBalancer = loadBalancer1; + } else { + this.primaryInternalLoadBalancer = loadBalancer1; + } + + String secondLoadBalancerId = null; + for (SubResource subResource: ipConfig.loadBalancerBackendAddressPools()) { + secondLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(subResource.id()); + break; + } + + if (secondLoadBalancerId == null) { + for (SubResource subResource: ipConfig.loadBalancerInboundNatPools()) { + if (!subResource.id().toLowerCase().startsWith(firstLoadBalancerId.toLowerCase())) { + secondLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(subResource.id()); + break; + } + } + } + + if (secondLoadBalancerId == null) { + return; + } + + LoadBalancer loadBalancer2 = this.networkManager + .loadBalancers() + .getById(secondLoadBalancerId); + if (loadBalancer2.publicIpAddressIds() != null && loadBalancer2.publicIpAddressIds().size() > 0) { + this.primaryInternetFacingLoadBalancer = loadBalancer2; + } else { + this.primaryInternalLoadBalancer = loadBalancer2; + } + } + + private void associateBackEndsToIpConfiguration(String loadBalancerId, + VirtualMachineScaleSetIPConfigurationInner ipConfig, + String... backendNames) { + for (String backendName : backendNames) { + String backendPoolId = loadBalancerId + "/" + "backendAddressPools" + "/" + backendName; + boolean found = false; + for (SubResource subResource : ipConfig.loadBalancerBackendAddressPools()) { + if (subResource.id().equalsIgnoreCase(backendPoolId)) { + found = true; + break; + } + } + if (!found) { + ipConfig.loadBalancerBackendAddressPools().add(new SubResource().withId(backendPoolId)); + } + } + } + + private void associateInboundNATPoolsToIpConfiguration(String loadBalancerId, + VirtualMachineScaleSetIPConfigurationInner ipConfig, + String... inboundNatPools) { + for (String inboundNatPool : inboundNatPools) { + String inboundNatPoolId = loadBalancerId + "/" + "inboundNatPools" + "/" + inboundNatPool; + boolean found = false; + for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { + if (subResource.id().equalsIgnoreCase(inboundNatPoolId)) { + found = true; + break; + } + } + if (!found) { + ipConfig.loadBalancerInboundNatPools().add(new SubResource().withId(inboundNatPoolId)); + } + } + } } From b4f3018324bcb395210be8fb491331016e8667d1 Mon Sep 17 00:00:00 2001 From: anuchan Date: Sat, 10 Sep 2016 20:39:47 -0700 Subject: [PATCH 06/10] Logic to add & remove backend and inbound nat pool association with the ipconfig --- .../VirtualMachineScaleSetImpl.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java index 78ba59a069c5..91f5138a69a4 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -41,6 +41,7 @@ import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Map; @@ -222,6 +223,11 @@ public VirtualMachineScaleSetImpl withAdminUserName(String adminUserName) { @Override public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { if (this.primaryInternalLoadBalancer != null) { + this.removeAllBackendsAssociation(this.primaryInternalLoadBalancer, + this.primaryNicDefaultIPConfiguration()); + this.removeAllInboundNatPoolsAssociation(this.primaryInternalLoadBalancer, + this.primaryNicDefaultIPConfiguration()); + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = primaryNicDefaultIPConfiguration(); this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), defaultPrimaryIpConfig, @@ -481,7 +487,12 @@ public VirtualMachineScaleSetImpl withPassword(String password) { @Override public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer loadBalancer) { + if (!loadBalancer.publicIpAddressIds().isEmpty()) { + throw new IllegalArgumentException("Parameter loadBalancer must be an internal load balancer"); + } this.primaryInternalLoadBalancer = loadBalancer; + this.associateAllBackendsAndInboundNatPools(this.primaryInternalLoadBalancer, + this.primaryNicDefaultIPConfiguration()); return this; } @@ -492,7 +503,12 @@ public VirtualMachineScaleSetImpl withoutPrimaryInternalLoadBalancer() { @Override public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancer(LoadBalancer loadBalancer) { + if (loadBalancer.publicIpAddressIds().isEmpty()) { + throw new IllegalArgumentException("Parameter loadBalancer must be an internet facing load balancer"); + } this.primaryInternetFacingLoadBalancer = loadBalancer; + this.associateAllBackendsAndInboundNatPools(this.primaryInternetFacingLoadBalancer, + this.primaryNicDefaultIPConfiguration()); return this; } @@ -504,6 +520,10 @@ public VirtualMachineScaleSetImpl withoutPrimaryInternetFacingLoadBalancer() { @Override public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(String... backendNames) { if (this.primaryInternetFacingLoadBalancer != null) { + this.removeAllBackendsAssociation(this.primaryInternetFacingLoadBalancer, + this.primaryNicDefaultIPConfiguration()); + this.removeAllInboundNatPoolsAssociation(this.primaryInternetFacingLoadBalancer, + this.primaryNicDefaultIPConfiguration()); VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), defaultPrimaryIpConfig, @@ -515,6 +535,8 @@ public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(S @Override public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNatPool(String... natPoolNames) { if (this.primaryInternetFacingLoadBalancer != null) { + this.removeAllInboundNatPoolsAssociation(this.primaryInternetFacingLoadBalancer, + this.primaryNicDefaultIPConfiguration()); VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); this.associateInboundNATPoolsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), defaultPrimaryIpConfig, @@ -526,6 +548,8 @@ public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNa @Override public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPool(String... natPoolNames) { if (this.primaryInternalLoadBalancer != null) { + this.removeAllInboundNatPoolsAssociation(this.primaryInternalLoadBalancer, + this.primaryNicDefaultIPConfiguration()); VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); this.associateInboundNATPoolsToIpConfiguration(this.primaryInternalLoadBalancer.id(), defaultPrimaryIpConfig, @@ -721,4 +745,62 @@ private void associateInboundNATPoolsToIpConfiguration(String loadBalancerId, } } } + + private void associateAllBackendsAndInboundNatPools(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { + Collection backends = loadBalancer.backends().values(); + String[] backendNames = new String[backends.size()]; + int i = 0; + for (Backend backend : backends) { + backendNames[i] = backend.name(); + i++; + } + + Collection inboundNatPools = loadBalancer.inboundNatPools().values(); + String[] natPoolNames = new String[inboundNatPools.size()]; + i = 0; + for (InboundNatPool inboundNatPool : inboundNatPools) { + natPoolNames[i] = inboundNatPool.name(); + i++; + } + + this.associateBackEndsToIpConfiguration(loadBalancer.id(), + ipConfig, + backendNames); + this.associateInboundNATPoolsToIpConfiguration(loadBalancer.id(), + ipConfig, + natPoolNames); + } + + private void removeAllBackendsAssociation(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { + List toRemoveIndicies = new ArrayList<>(); + int i = 0; + for (SubResource subResource : ipConfig.loadBalancerBackendAddressPools()) { + if (subResource.id().toLowerCase().startsWith(loadBalancer.id().toLowerCase() + "/")) { + toRemoveIndicies.add(i); + } + i++; + } + + for (Integer index : toRemoveIndicies) { + ipConfig.loadBalancerBackendAddressPools().remove(index); + } + } + + private void removeAllInboundNatPoolsAssociation(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { + List toRemoveIndicies = new ArrayList<>(); + int i = 0; + for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { + if (subResource.id().toLowerCase().startsWith(loadBalancer.id().toLowerCase() + "/")) { + toRemoveIndicies.add(i); + } + i++; + } + + for (Integer index : toRemoveIndicies) { + ipConfig.loadBalancerInboundNatPools().remove(index); + } + } } From ffa013968947c075d8975aa56128ef7390b5515c Mon Sep 17 00:00:00 2001 From: anuchan Date: Sun, 11 Sep 2016 13:31:58 -0700 Subject: [PATCH 07/10] Adding support for virtual machine scale set extensions --- .../compute/VirtualMachineScaleSet.java | 36 +- .../VirtualMachineScaleSetExtension.java | 463 ++++++++++++++++++ .../VirtualMachineScaleSetExtensionImpl.java | 174 +++++++ .../VirtualMachineScaleSetImpl.java | 279 ++++++++--- 4 files changed, 868 insertions(+), 84 deletions(-) create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetExtension.java create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetExtensionImpl.java diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java index 5c36cf0a3c38..84a9bebb0924 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -71,41 +71,49 @@ public interface VirtualMachineScaleSet extends * @return the internet facing load balancer associated with the primary network interface of * the virtual machines in the scale set. * - * @throws throws IOException the IO exception + * @throws IOException the IO exception */ LoadBalancer primaryInternetFacingLoadBalancer() throws IOException; /** * @return the internet facing load balancer's backends associated with the primary network interface * of the virtual machines in the scale set. + * + * @throws IOException the IO exception */ - Map primaryInternetFacingLoadBalancerBackEnds(); + Map primaryInternetFacingLoadBalancerBackEnds() throws IOException; /** * @return the internet facing load balancer's inbound NAT pool associated with the primary network interface * of the virtual machines in the scale set. + * + * @throws IOException the IO exception */ - Map primaryInternetFacingLoadBalancerInboundNatPools(); + Map primaryInternetFacingLoadBalancerInboundNatPools() throws IOException; /** * @return the internal load balancer associated with the primary network interface of * the virtual machines in the scale set. * - * @throws throws IOException the IO exception + * @throws IOException the IO exception */ LoadBalancer primaryInternalLoadBalancer() throws IOException; /** * @return the internal load balancer's backends associated with the primary network interface * of the virtual machines in the scale set. + * + * @throws IOException the IO exception */ - Map primaryInternalLoadBalancerBackEnds(); + Map primaryInternalLoadBalancerBackEnds() throws IOException; /** * @return the internal load balancer's inbound NAT pool associated with the primary network interface * of the virtual machines in the scale set. + * + * @throws IOException the IO exception */ - Map primaryInternalLoadBalancerInboundNatPools(); + Map primaryInternalLoadBalancerInboundNatPools() throws IOException; /** * @return the storage profile. @@ -118,9 +126,9 @@ public interface VirtualMachineScaleSet extends VirtualMachineScaleSetNetworkProfile networkProfile(); /** - * @return the extensions attached to the Azure Virtual Machine + * @return the extensions attached to the Virtual Machines in the scale set. */ - Map extensions(); + Map extensions(); /** * The entirety of the load balancer definition. @@ -642,7 +650,7 @@ interface WithExtension { * @param name the reference name for the extension * @return the stage representing configuration for the extension */ - VirtualMachineExtension.DefinitionStages.Blank defineNewExtension(String name); + VirtualMachineScaleSetExtension.DefinitionStages.Blank defineNewExtension(String name); } /** @@ -784,11 +792,11 @@ interface WithoutPrimaryLoadBalancerNatPool { */ Update withoutPrimaryInternalLoadBalancerNatPool(String natPoolName); } + } - /** - * The entirety of the load balancer update. - */ - interface Update { - } + /** + * The entirety of the load balancer update. + */ + interface Update { } } \ No newline at end of file diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetExtension.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetExtension.java new file mode 100644 index 000000000000..576b76cd65f1 --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetExtension.java @@ -0,0 +1,463 @@ +package com.microsoft.azure.management.compute; + +import com.microsoft.azure.management.compute.implementation.VirtualMachineScaleSetExtensionInner; +import com.microsoft.azure.management.resources.fluentcore.arm.models.ChildResource; +import com.microsoft.azure.management.resources.fluentcore.model.Attachable; +import com.microsoft.azure.management.resources.fluentcore.model.Settable; +import com.microsoft.azure.management.resources.fluentcore.model.Wrapper; + +import java.util.HashMap; +import java.util.Map; + +/** + * An immutable client-side representation of an extension associated with virtual machines in a scale set. + * An extension associated with a virtual machine scale set will be created from a {@link VirtualMachineExtensionImage }. + */ +public interface VirtualMachineScaleSetExtension extends + Wrapper, + ChildResource { + /** + * @return the publisher name of the virtual machine scale set extension image this extension is created from + */ + String publisherName(); + + /** + * @return the type name of the virtual machine scale set extension image this extension is created from + */ + String typeName(); + + /** + * @return the version name of the virtual machine scale set extension image this extension is created from + */ + String versionName(); + + /** + * @return true if this extension is configured to upgrade automatically when a new minor version of + * virtual machine scale set extension image that this extension based on is published + */ + boolean autoUpgradeMinorVersionEnabled(); + + /** + * @return the public settings of the virtual machine scale set extension as key value pairs + */ + Map publicSettings(); + + /** + * @return the public settings of the virtual machine extension as a json string + */ + String publicSettingsAsJsonString(); + + /** + * @return the provisioning state of this virtual machine scale set extension + */ + String provisioningState(); + + /** + * Grouping of virtual machine scale set extension definition stages as a part of parent virtual machine scale set definition. + */ + interface DefinitionStages { + /** + * The first stage of a virtual machine scale set extension definition. + * + * @param the return type of the final {@link WithAttach#attach()} + */ + interface Blank + extends WithImageOrPublisher { + } + + /** + * The stage of the virtual machinescale set extension definition allowing to specify extension image or specify name of + * the virtual machine scale set extension publisher. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithImageOrPublisher + extends WithPublisher { + /** + * Specifies the virtual machine scale set extension image to use. + * + * @param image the image + * @return the next stage of the definition + */ + WithAttach withImage(VirtualMachineExtensionImage image); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the publisher of the + * virtual machine scale set extension image this extension is based on. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithPublisher { + /** + * Specifies the name of the virtual machine scale set extension image publisher. + * + * @param extensionImagePublisherName the publisher name + * @return the next stage of the definition + */ + WithType withPublisher(String extensionImagePublisherName); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the type of the virtual machine + * scale set extension image this extension is based on. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithType { + /** + * Specifies the type of the virtual machine scale set extension image. + * + * @param extensionImageTypeName the image type name + * @return the next stage of the definition + */ + WithVersion withType(String extensionImageTypeName); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the type of the virtual machine + * scale set extension version this extension is based on. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithVersion { + /** + * Specifies the version of the virtual machine scale set image extension. + * + * @param extensionImageVersionName the version name + * @return the next stage of the definition + */ + WithAttach withVersion(String extensionImageVersionName); + } + + /** The final stage of the virtual machine scale set extension definition. + *

+ * At this stage, any remaining optional settings can be specified, or the virtual machine scale set extension definition + * can be attached to the parent virtual machine scale set definition using {@link VirtualMachineExtension.DefinitionStages.WithAttach#attach()}. + * @param the return type of {@link VirtualMachineExtension.DefinitionStages.WithAttach#attach()} + */ + interface WithAttach extends + Attachable.InDefinition, + WithAutoUpgradeMinorVersion, + WithSettings { + } + + /** + * The stage of the virtual machine scale set extension definition allowing to enable or disable auto upgrade of the + * extension when when a new minor version of virtual machine scale set extension image gets published. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithAutoUpgradeMinorVersion { + /** + * enables auto upgrade of the extension. + * + * @return the next stage of the definition + */ + WithAttach withAutoUpgradeMinorVersionEnabled(); + + /** + * disables auto upgrade of the extension. + * + * @return the next stage of the definition + */ + WithAttach withAutoUpgradeMinorVersionDisabled(); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the public and private settings. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithSettings { + /** + * Specifies a public settings entry. + * + * @param key the key of a public settings entry + * @param value the value of the public settings entry + * @return the next stage of the definition + */ + WithAttach withPublicSetting(String key, Object value); + + /** + * Specifies a private settings entry. + * + * @param key the key of a private settings entry + * @param value the value of the private settings entry + * @return the next stage of the definition + */ + WithAttach withProtectedSetting(String key, Object value); + + /** + * Specifies public settings. + * + * @param settings the public settings + * @return the next stage of the definition + */ + WithAttach withPublicSettings(HashMap settings); + + /** + * Specifies private settings. + * + * @param settings the private settings + * @return the next stage of the definition + */ + WithAttach withProtectedSettings(HashMap settings); + } + } + + /** + * The entirety of a virtual machine scale set extension definition as a part of parent definition. + * + * @param the return type of the final {@link Attachable#attach()} + */ + interface Definition extends + DefinitionStages.Blank, + DefinitionStages.WithImageOrPublisher, + DefinitionStages.WithPublisher, + DefinitionStages.WithType, + DefinitionStages.WithVersion, + DefinitionStages.WithAttach { + } + + /** + * Grouping of virtual machine scale set extension definition stages as part of parent virtual machine scale set update. + */ + interface UpdateDefinitionStages { + /** + * The first stage of a virtual machine scale set extension definition. + * + * @param the return type of the final {@link WithAttach#attach()} + */ + interface Blank + extends WithImageOrPublisher { + } + + /** + * The stage of the virtual machine scale set extension allowing to specify extension image or specify name of the + * virtual machine extension publisher. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithImageOrPublisher + extends WithPublisher { + /** + * Specifies the virtual machine scale set extension image to use. + * + * @param image the image + * @return the next stage of the definition + */ + WithAttach withImage(VirtualMachineExtensionImage image); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the publisher of the + * virtual machine scale set extension image this extension is based on. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithPublisher { + /** + * Specifies the name of the virtual machine scale set extension image publisher. + * + * @param extensionImagePublisherName the publisher name + * @return the next stage of the definition + */ + WithType withPublisher(String extensionImagePublisherName); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the type of the virtual machine + * scale set extension image this extension is based on. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithType { + /** + * Specifies the type of the virtual machine scale set extension image. + * + * @param extensionImageTypeName the image type name + * @return the next stage of the definition + */ + WithVersion withType(String extensionImageTypeName); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the type of the virtual machine + * scale set extension version this extension is based on. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithVersion { + /** + * Specifies the version of the virtual machine scale set image extension. + * + * @param extensionImageVersionName the version name + * @return the next stage of the definition + */ + WithAttach withVersion(String extensionImageVersionName); + } + + /** The final stage of the virtual machine scale set extension definition. + *

+ * At this stage, any remaining optional settings can be specified, or the virtual machine scale set extension definition + * can be attached to the parent virtual machine scale set definition using {@link VirtualMachineExtension.UpdateDefinitionStages.WithAttach#attach()}. + * @param the return type of {@link VirtualMachineExtension.UpdateDefinitionStages.WithAttach#attach()} + */ + interface WithAttach extends + Attachable.InUpdate, + WithAutoUpgradeMinorVersion, + WithSettings { + } + + /** + * The stage of the virtual machine scale set extension definition allowing to enable or disable auto upgrade of the + * extension when when a new minor version of virtual machine scale set extension image gets published. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithAutoUpgradeMinorVersion { + /** + * enables auto upgrade of the extension. + * + * @return the next stage of the definition + */ + WithAttach withAutoUpgradeMinorVersionEnabled(); + + /** + * disables auto upgrade of the extension. + * + * @return the next stage of the definition + */ + WithAttach withAutoUpgradeMinorVersionDisabled(); + } + + /** + * The stage of the virtual machine scale set extension definition allowing to specify the public and private settings. + * + * @param the return type of {@link WithAttach#attach()} + */ + interface WithSettings { + /** + * Specifies a public settings entry. + * + * @param key the key of a public settings entry + * @param value the value of the public settings entry + * @return the next stage of the definition + */ + WithAttach withPublicSetting(String key, Object value); + + /** + * Specifies a private settings entry. + * + * @param key the key of a private settings entry + * @param value the value of the private settings entry + * @return the next stage of the definition + */ + WithAttach withProtectedSetting(String key, Object value); + + /** + * Specifies public settings. + * + * @param settings the public settings + * @return the next stage of the definition + */ + WithAttach withPublicSettings(HashMap settings); + + /** + * Specifies private settings. + * + * @param settings the private settings + * @return the next stage of the definition + */ + WithAttach withProtectedSettings(HashMap settings); + } + } + + /** + * The entirety of a virtual machine scale set extension definition as a part of parent update. + * @param the return type of the final {@link Attachable#attach()} + */ + interface UpdateDefinition extends + UpdateDefinitionStages.Blank, + UpdateDefinitionStages.WithImageOrPublisher, + UpdateDefinitionStages.WithPublisher, + UpdateDefinitionStages.WithType, + UpdateDefinitionStages.WithVersion, + UpdateDefinitionStages.WithAttach { + } + + /** + * Grouping of virtual machine extension update stages. + */ + interface UpdateStages { + /** + * The stage of the virtual machine scale set extension update allowing to enable or disable auto upgrade of the + * extension when when a new minor version of virtual machine scale set extension image gets published. + */ + interface WithAutoUpgradeMinorVersion { + /** + * enables auto upgrade of the extension. + * + * @return the next stage of the update + */ + Update withAutoUpgradeMinorVersionEnabled(); + + /** + * enables auto upgrade of the extension. + * + * @return the next stage of the update + */ + Update withAutoUpgradeMinorVersionDisabled(); + } + + /** + * The stage of the virtual machine scale set extension update allowing to add or update public and private settings. + */ + interface WithSettings { + /** + * Specifies a public settings entry. + * + * @param key the key of a public settings entry + * @param value the value of the public settings entry + * @return the next stage of the update + */ + Update withPublicSetting(String key, Object value); + + /** + * Specifies a private settings entry. + * + * @param key the key of a private settings entry + * @param value the value of the private settings entry + * @return the next stage of the update + */ + Update withProtectedSetting(String key, Object value); + + /** + * Specifies public settings. + * + * @param settings the public settings + * @return the next stage of the update + */ + Update withPublicSettings(HashMap settings); + + /** + * Specifies private settings. + * + * @param settings the private settings + * @return the next stage of the update + */ + Update withProtectedSettings(HashMap settings); + } + } + + /** + * The entirety of virtual machine scale set extension update as a part of parent virtual machine scale set update. + */ + interface Update extends + Settable, + UpdateStages.WithAutoUpgradeMinorVersion, + UpdateStages.WithSettings { + + } +} diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetExtensionImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetExtensionImpl.java new file mode 100644 index 000000000000..eb1424403777 --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetExtensionImpl.java @@ -0,0 +1,174 @@ +package com.microsoft.azure.management.compute.implementation; + +import com.microsoft.azure.management.compute.VirtualMachineExtensionImage; +import com.microsoft.azure.management.compute.VirtualMachineScaleSet; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetExtension; +import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.ChildResourceImpl; + +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * Implementation of {@link VirtualMachineScaleSetExtension}. + */ +public class VirtualMachineScaleSetExtensionImpl + extends ChildResourceImpl + implements + VirtualMachineScaleSetExtension, + VirtualMachineScaleSetExtension.Definition + /*VirtualMachineScaleSetExtension.UpdateDefinition, + VirtualMachineScaleSetExtension.Update*/ { + + private HashMap publicSettings; + private HashMap protectedSettings; + + protected VirtualMachineScaleSetExtensionImpl(VirtualMachineScaleSetExtensionInner inner, + VirtualMachineScaleSetImpl parent) { + super(inner, parent); + initializeSettings(); + } + + // Getters + // + @Override + public String name() { + return this.inner().name(); + } + + @Override + public String publisherName() { + return this.inner().publisher(); + } + + @Override + public String typeName() { + return this.inner().type(); + } + + @Override + public String versionName() { + return this.inner().typeHandlerVersion(); + } + + @Override + public boolean autoUpgradeMinorVersionEnabled() { + return this.inner().autoUpgradeMinorVersion(); + } + + @Override + public Map publicSettings() { + return Collections.unmodifiableMap(this.publicSettings); + } + + @Override + public String publicSettingsAsJsonString() { + return null; + } + + @Override + public String provisioningState() { + return this.inner().provisioningState(); + } + + // Withers + // + + @Override + public VirtualMachineScaleSetExtensionImpl withAutoUpgradeMinorVersionEnabled() { + this.inner().withAutoUpgradeMinorVersion(true); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withAutoUpgradeMinorVersionDisabled() { + this.inner().withAutoUpgradeMinorVersion(false); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withImage(VirtualMachineExtensionImage image) { + this.inner().withPublisher(image.publisherName()) + .withType(image.typeName()) + .withTypeHandlerVersion(image.versionName()); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withPublisher(String extensionImagePublisherName) { + this.inner().withPublisher(extensionImagePublisherName); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withPublicSetting(String key, Object value) { + this.publicSettings.put(key, value); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withProtectedSetting(String key, Object value) { + this.protectedSettings.put(key, value); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withPublicSettings(HashMap settings) { + this.publicSettings.clear(); + this.publicSettings.putAll(settings); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withProtectedSettings(HashMap settings) { + this.protectedSettings.clear(); + this.protectedSettings.putAll(settings); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withType(String extensionImageTypeName) { + this.inner().withType(extensionImageTypeName); + return this; + } + + @Override + public VirtualMachineScaleSetExtensionImpl withVersion(String extensionImageVersionName) { + this.inner().withTypeHandlerVersion(extensionImageVersionName); + return this; + } + + // Helper methods + // + private void nullifySettingsIfEmpty() { + if (this.publicSettings.size() == 0) { + this.inner().withSettings(null); + } + if (this.protectedSettings.size() == 0) { + this.inner().withProtectedSettings(null); + } + } + + private void initializeSettings() { + if (this.inner().settings() == null) { + this.publicSettings = new LinkedHashMap<>(); + this.inner().withSettings(this.publicSettings); + } else { + this.publicSettings = (LinkedHashMap) this.inner().settings(); + } + + if (this.inner().protectedSettings() == null) { + this.protectedSettings = new LinkedHashMap<>(); + this.inner().withProtectedSettings(this.protectedSettings); + } else { + this.protectedSettings = (LinkedHashMap) this.inner().protectedSettings(); + } + } + + @Override + public VirtualMachineScaleSetImpl attach() { + nullifySettingsIfEmpty(); + return this.parent().withExtension(this); + } +} diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java index 91f5138a69a4..f1fa16e05a24 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -14,8 +14,9 @@ import com.microsoft.azure.management.compute.SshPublicKey; import com.microsoft.azure.management.compute.UpgradePolicy; import com.microsoft.azure.management.compute.VirtualHardDisk; -import com.microsoft.azure.management.compute.VirtualMachineExtension; import com.microsoft.azure.management.compute.VirtualMachineScaleSet; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetExtension; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetExtensionProfile; import com.microsoft.azure.management.compute.VirtualMachineScaleSetNetworkProfile; import com.microsoft.azure.management.compute.VirtualMachineScaleSetOSDisk; import com.microsoft.azure.management.compute.VirtualMachineScaleSetOSProfile; @@ -32,8 +33,9 @@ import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.network.implementation.NetworkManager; import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; -import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableResourceImpl; +import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableParentResourceImpl; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; +import com.microsoft.azure.management.resources.fluentcore.utils.PagedListConverter; import com.microsoft.azure.management.resources.fluentcore.utils.ResourceNamer; import com.microsoft.azure.management.storage.StorageAccount; import com.microsoft.azure.management.storage.implementation.StorageManager; @@ -42,6 +44,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -49,11 +53,11 @@ * Implementation of {@link VirtualMachineScaleSet}. */ public class VirtualMachineScaleSetImpl - extends GroupableResourceImpl< - VirtualMachineScaleSet, - VirtualMachineScaleSetInner, - VirtualMachineScaleSetImpl, - ComputeManager> + extends GroupableParentResourceImpl< + VirtualMachineScaleSet, + VirtualMachineScaleSetInner, + VirtualMachineScaleSetImpl, + ComputeManager> implements VirtualMachineScaleSet, VirtualMachineScaleSet.Definition { @@ -67,8 +71,9 @@ public class VirtualMachineScaleSetImpl // used to generate unique name for any dependency resources private final ResourceNamer namer; private boolean isMarketplaceLinuxImage = false; - // the primary load balancers + // reference to the primary internet facing load balancer private LoadBalancer primaryInternetFacingLoadBalancer; + // reference to the primary internal load balancer private LoadBalancer primaryInternalLoadBalancer; // unique key of a creatable network that needs to be used in virtual machine's primary network interface private String creatablePrimaryNetworkKey; @@ -76,6 +81,19 @@ public class VirtualMachineScaleSetImpl private Network existingPrimaryNetworkToAssociate; // name of an existing subnet in the network to use private String existingSubnetNameToAssociate; + // unique key of a creatable storage accounts to be used for virtual machines child resources that + // requires storage [OS disk] + private List creatableStorageAccountKeys; + // reference to an existing storage account to be used for virtual machines child resources that + // requires storage [OS disk] + private List existingStorageAccountsToAssociate; + + private List internetFacingLBBackendsToAssociateOnUpdate; + private List internetFacingLBInboundNatPoolsToAssociateOnUpdate; + private List internalLBBackendsToAssociateOnUpdate; + private List internalLBInboundNatPoolsToAssociateOnUpdate; + private Map extensions; + private PagedListConverter skuConverter; VirtualMachineScaleSetImpl(String name, VirtualMachineScaleSetInner innerModel, @@ -89,6 +107,19 @@ public class VirtualMachineScaleSetImpl this.networkManager = networkManager; this.scaleSetName = name; this.namer = new ResourceNamer(this.scaleSetName); + + this.creatableStorageAccountKeys = new ArrayList<>(); + this.existingStorageAccountsToAssociate = new ArrayList<>(); + this.internetFacingLBBackendsToAssociateOnUpdate = new ArrayList<>(); + this.internetFacingLBInboundNatPoolsToAssociateOnUpdate = new ArrayList<>(); + this.internalLBBackendsToAssociateOnUpdate = new ArrayList<>(); + this.internalLBInboundNatPoolsToAssociateOnUpdate = new ArrayList<>(); + this.skuConverter = new PagedListConverter() { + @Override + public VirtualMachineScaleSetSku typeConvert(VirtualMachineScaleSetSkuInner inner) { + return new VirtualMachineScaleSetSkuImpl(inner); + } + }; } static VirtualMachineScaleSetImpl create(String name) { @@ -132,7 +163,7 @@ static VirtualMachineScaleSetImpl create(String name) { @Override public PagedList availableSkus() throws CloudException, IOException { - return null; + return this.skuConverter.convert(this.client.listSkus(this.resourceGroupName(), this.name())); } @Override @@ -172,12 +203,14 @@ public LoadBalancer primaryInternetFacingLoadBalancer() throws IOException { } @Override - public Map primaryInternetFacingLoadBalancerBackEnds() { + public Map primaryInternetFacingLoadBalancerBackEnds() throws IOException { + setPrimaryLoadBalancersIfAvailable(); return null; } @Override - public Map primaryInternetFacingLoadBalancerInboundNatPools() { + public Map primaryInternetFacingLoadBalancerInboundNatPools() throws IOException { + setPrimaryLoadBalancersIfAvailable(); return null; } @@ -188,12 +221,14 @@ public LoadBalancer primaryInternalLoadBalancer() throws IOException { } @Override - public Map primaryInternalLoadBalancerBackEnds() { + public Map primaryInternalLoadBalancerBackEnds() throws IOException { + setPrimaryLoadBalancersIfAvailable(); return null; } @Override - public Map primaryInternalLoadBalancerInboundNatPools() { + public Map primaryInternalLoadBalancerInboundNatPools() throws IOException { + setPrimaryLoadBalancersIfAvailable(); return null; } @@ -208,8 +243,8 @@ public VirtualMachineScaleSetNetworkProfile networkProfile() { } @Override - public Map extensions() { - return null; + public Map extensions() { + return Collections.unmodifiableMap(this.extensions); } @Override @@ -220,22 +255,6 @@ public VirtualMachineScaleSetImpl withAdminUserName(String adminUserName) { return this; } - @Override - public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { - if (this.primaryInternalLoadBalancer != null) { - this.removeAllBackendsAssociation(this.primaryInternalLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - this.removeAllInboundNatPoolsAssociation(this.primaryInternalLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - - VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = primaryNicDefaultIPConfiguration(); - this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), - defaultPrimaryIpConfig, - backendNames); - } - return this; - } - @Override public VirtualMachineScaleSetImpl withSsh(String publicKeyData) { VirtualMachineScaleSetOSProfile osProfile = this.inner() @@ -260,8 +279,8 @@ public VirtualMachineScaleSetImpl withCapacity(long capacity) { } @Override - public VirtualMachineExtension.DefinitionStages.Blank defineNewExtension(String name) { - return null; + public VirtualMachineScaleSetExtensionImpl defineNewExtension(String name) { + return new VirtualMachineScaleSetExtensionImpl(new VirtualMachineScaleSetExtensionInner(), this); } @Override @@ -520,14 +539,18 @@ public VirtualMachineScaleSetImpl withoutPrimaryInternetFacingLoadBalancer() { @Override public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(String... backendNames) { if (this.primaryInternetFacingLoadBalancer != null) { - this.removeAllBackendsAssociation(this.primaryInternetFacingLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - this.removeAllInboundNatPoolsAssociation(this.primaryInternetFacingLoadBalancer, - this.primaryNicDefaultIPConfiguration()); VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); + this.removeLoadBalancerAssociation(this.primaryInternetFacingLoadBalancer, defaultPrimaryIpConfig); this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), defaultPrimaryIpConfig, backendNames); + } else { + if (!isInCreateMode()) { + this.internetFacingLBBackendsToAssociateOnUpdate.clear(); + for (String backendName : backendNames) { + this.internetFacingLBBackendsToAssociateOnUpdate.add(backendName); + } + } } return this; } @@ -541,6 +564,36 @@ public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNa this.associateInboundNATPoolsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), defaultPrimaryIpConfig, natPoolNames); + } else { + if (!isInCreateMode()) { + this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.clear(); + for (String natPoolName : natPoolNames) { + this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.add(natPoolName); + } + } + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { + if (this.primaryInternalLoadBalancer != null) { + this.removeAllBackendsAssociation(this.primaryInternalLoadBalancer, + this.primaryNicDefaultIPConfiguration()); + this.removeAllInboundNatPoolsAssociation(this.primaryInternalLoadBalancer, + this.primaryNicDefaultIPConfiguration()); + + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = primaryNicDefaultIPConfiguration(); + this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), + defaultPrimaryIpConfig, + backendNames); + } else { + if (!isInCreateMode()) { + this.internalLBBackendsToAssociateOnUpdate.clear(); + for (String backendName : backendNames) { + this.internalLBBackendsToAssociateOnUpdate.add(backendName); + } + } } return this; } @@ -554,6 +607,13 @@ public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPool( this.associateInboundNATPoolsToIpConfiguration(this.primaryInternalLoadBalancer.id(), defaultPrimaryIpConfig, natPoolNames); + } else { + if (!isInCreateMode()) { + this.internalLBInboundNatPoolsToAssociateOnUpdate.clear(); + for (String natPoolName : natPoolNames) { + this.internalLBInboundNatPoolsToAssociateOnUpdate.add(natPoolName); + } + } } return this; } @@ -576,24 +636,99 @@ public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSku sku) { @Override public VirtualMachineScaleSetImpl withNewStorageAccount(String name) { - return this; + StorageAccount.DefinitionStages.WithGroup definitionWithGroup = this.storageManager + .storageAccounts() + .define(name) + .withRegion(this.regionName()); + Creatable definitionAfterGroup; + if (this.creatableGroup != null) { + definitionAfterGroup = definitionWithGroup.withNewResourceGroup(this.creatableGroup); + } else { + definitionAfterGroup = definitionWithGroup.withExistingResourceGroup(this.resourceGroupName()); + } + return withNewStorageAccount(definitionAfterGroup); } @Override public VirtualMachineScaleSetImpl withNewStorageAccount(Creatable creatable) { + this.creatableStorageAccountKeys.add(creatable.key()); + this.addCreatableDependency(creatable); return this; } @Override public VirtualMachineScaleSetImpl withExistingStorageAccount(StorageAccount storageAccount) { + this.existingStorageAccountsToAssociate.add(storageAccount); return this; } @Override public Observable applyAsync() { + if (!this.internetFacingLBBackendsToAssociateOnUpdate.isEmpty() + || !this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.isEmpty() + || !this.internalLBBackendsToAssociateOnUpdate.isEmpty() + || !this.internalLBInboundNatPoolsToAssociateOnUpdate.isEmpty()) { + try { + this.setPrimaryLoadBalancersIfAvailable(); + if (this.primaryInternetFacingLoadBalancer != null) { + this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + this.primaryNicDefaultIPConfiguration(), + this.internalLBBackendsToAssociateOnUpdate.toArray(new String[0])); + + this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + this.primaryNicDefaultIPConfiguration(), + this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.toArray(new String[0])); + } + if (this.primaryInternalLoadBalancer != null) { + this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), + this.primaryNicDefaultIPConfiguration(), + this.internalLBBackendsToAssociateOnUpdate.toArray(new String[0])); + + this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), + this.primaryNicDefaultIPConfiguration(), + this.internalLBInboundNatPoolsToAssociateOnUpdate.toArray(new String[0])); + } + } catch (IOException ioException) { + throw new RuntimeException(ioException); + } + } return null; } + @Override + protected Observable createInner() { + return null; + } + + @Override + protected void initializeChildrenFromInner() { + this.extensions = new HashMap<>(); + if (this.inner().virtualMachineProfile().extensionProfile() != null) { + if (this.inner().virtualMachineProfile().extensionProfile().extensions() != null) { + for (VirtualMachineScaleSetExtensionInner inner : this.inner().virtualMachineProfile().extensionProfile().extensions()) { + this.extensions.put(inner.name(), new VirtualMachineScaleSetExtensionImpl(inner, this)); + } + } + } + } + + @Override + protected void beforeCreating() { + // Reset and update extensions + if (this.extensions.size() > 0) { + this.inner() + .virtualMachineProfile() + .withExtensionProfile(new VirtualMachineScaleSetExtensionProfile()) + .extensionProfile() + .withExtensions(innersFromWrappers(this.extensions.values())); + } + } + + @Override + protected void afterCreating() { + + } + @Override public Observable createResourceAsync() { return null; @@ -601,8 +736,14 @@ public Observable createResourceAsync() { @Override public VirtualMachineScaleSetImpl refresh() throws Exception { - // GET VMSS - this.resetPrimaryLoadBalancers(); + VirtualMachineScaleSetInner inner = this.client.get(this.resourceGroupName(), this.name()); + this.setInner(inner); + this.refreshPrimaryLoadBalancersCache(); + return this; + } + + VirtualMachineScaleSetImpl withExtension(VirtualMachineScaleSetExtensionImpl extension) { + // TODO return this; } @@ -628,11 +769,6 @@ private VirtualMachineScaleSetIPConfigurationInner primaryNicDefaultIPConfigurat throw new RuntimeException("Could not find the primary nic configuration or an IP configuration in it"); } - private void resetPrimaryLoadBalancers() { - this.primaryInternetFacingLoadBalancer = null; - this.primaryInternalLoadBalancer = null; - } - private void setPrimaryLoadBalancersIfAvailable() throws IOException { String firstLoadBalancerId = null; VirtualMachineScaleSetIPConfigurationInner ipConfig = primaryNicDefaultIPConfiguration(); @@ -650,26 +786,6 @@ private void setPrimaryLoadBalancersIfAvailable() throws IOException { return; } - if (this.primaryInternetFacingLoadBalancer != null - && !this.primaryInternetFacingLoadBalancer.id().equalsIgnoreCase(firstLoadBalancerId)) { - if (this.primaryInternalLoadBalancer == null) { - this.primaryInternalLoadBalancer = this.networkManager - .loadBalancers() - .getById(firstLoadBalancerId); - return; - } - } - - if (this.primaryInternalLoadBalancer != null - && !this.primaryInternalLoadBalancer.id().equalsIgnoreCase(firstLoadBalancerId)) { - if (this.primaryInternetFacingLoadBalancer == null) { - this.primaryInternetFacingLoadBalancer = this.networkManager - .loadBalancers() - .getById(firstLoadBalancerId); - return; - } - } - LoadBalancer loadBalancer1 = this.networkManager .loadBalancers() .getById(firstLoadBalancerId); @@ -713,6 +829,7 @@ private void setPrimaryLoadBalancersIfAvailable() throws IOException { private void associateBackEndsToIpConfiguration(String loadBalancerId, VirtualMachineScaleSetIPConfigurationInner ipConfig, String... backendNames) { + List backendSubResourcesToAssociate = new ArrayList<>(); for (String backendName : backendNames) { String backendPoolId = loadBalancerId + "/" + "backendAddressPools" + "/" + backendName; boolean found = false; @@ -723,14 +840,19 @@ private void associateBackEndsToIpConfiguration(String loadBalancerId, } } if (!found) { - ipConfig.loadBalancerBackendAddressPools().add(new SubResource().withId(backendPoolId)); + backendSubResourcesToAssociate.add(new SubResource().withId(backendPoolId)); } } + + for (SubResource backendSubResource : backendSubResourcesToAssociate) { + ipConfig.loadBalancerBackendAddressPools().add(backendSubResource); + } } private void associateInboundNATPoolsToIpConfiguration(String loadBalancerId, VirtualMachineScaleSetIPConfigurationInner ipConfig, String... inboundNatPools) { + List inboundNatPoolSubResourcesToAssociate = new ArrayList<>(); for (String inboundNatPool : inboundNatPools) { String inboundNatPoolId = loadBalancerId + "/" + "inboundNatPools" + "/" + inboundNatPool; boolean found = false; @@ -741,9 +863,13 @@ private void associateInboundNATPoolsToIpConfiguration(String loadBalancerId, } } if (!found) { - ipConfig.loadBalancerInboundNatPools().add(new SubResource().withId(inboundNatPoolId)); + inboundNatPoolSubResourcesToAssociate.add(new SubResource().withId(inboundNatPoolId)); } } + + for (SubResource backendSubResource : inboundNatPoolSubResourcesToAssociate) { + ipConfig.loadBalancerInboundNatPools().add(backendSubResource); + } } private void associateAllBackendsAndInboundNatPools(LoadBalancer loadBalancer, @@ -756,6 +882,10 @@ private void associateAllBackendsAndInboundNatPools(LoadBalancer loadBalancer, i++; } + this.associateBackEndsToIpConfiguration(loadBalancer.id(), + ipConfig, + backendNames); + Collection inboundNatPools = loadBalancer.inboundNatPools().values(); String[] natPoolNames = new String[inboundNatPools.size()]; i = 0; @@ -764,9 +894,6 @@ private void associateAllBackendsAndInboundNatPools(LoadBalancer loadBalancer, i++; } - this.associateBackEndsToIpConfiguration(loadBalancer.id(), - ipConfig, - backendNames); this.associateInboundNATPoolsToIpConfiguration(loadBalancer.id(), ipConfig, natPoolNames); @@ -788,6 +915,12 @@ private void removeAllBackendsAssociation(LoadBalancer loadBalancer, } } + private void removeLoadBalancerAssociation(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { + this.removeAllBackendsAssociation(loadBalancer, ipConfig); + this.removeAllInboundNatPoolsAssociation(loadBalancer, ipConfig); + } + private void removeAllInboundNatPoolsAssociation(LoadBalancer loadBalancer, VirtualMachineScaleSetIPConfigurationInner ipConfig) { List toRemoveIndicies = new ArrayList<>(); @@ -803,4 +936,10 @@ private void removeAllInboundNatPoolsAssociation(LoadBalancer loadBalancer, ipConfig.loadBalancerInboundNatPools().remove(index); } } + + private void refreshPrimaryLoadBalancersCache() throws IOException { + this.primaryInternetFacingLoadBalancer = null; + this.primaryInternalLoadBalancer = null; + this.setPrimaryLoadBalancersIfAvailable(); + } } From 9641f2ec860c54af473a8ce90a0039954031dacd Mon Sep 17 00:00:00 2001 From: anuchan Date: Sun, 11 Sep 2016 14:57:44 -0700 Subject: [PATCH 08/10] Adding scale sets entry point --- .../compute/VirtualMachineScaleSets.java | 10 ++ .../implementation/ComputeManager.java | 15 +++ .../VirtualMachineScaleSetImpl.java | 41 ------- .../VirtualMachineScaleSetsImpl.java | 100 ++++++++++++++++++ .../compute/ComputeManagementTestBase.java | 5 + ...VirtualMachineScaleSetOperationsTests.java | 58 ++++++++++ 6 files changed, 188 insertions(+), 41 deletions(-) create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSets.java create mode 100644 azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java create mode 100644 azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSets.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSets.java new file mode 100644 index 000000000000..15bf4d10ecdf --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSets.java @@ -0,0 +1,10 @@ +package com.microsoft.azure.management.compute; + +import com.microsoft.azure.management.resources.fluentcore.collection.SupportsCreating; + +/** + * Entry point to virtual machine scale set management API. + */ +public interface VirtualMachineScaleSets extends + SupportsCreating { +} diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/ComputeManager.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/ComputeManager.java index 50744531ae9a..e48c6854dc8b 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/ComputeManager.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/ComputeManager.java @@ -4,6 +4,7 @@ import com.microsoft.azure.management.compute.AvailabilitySets; import com.microsoft.azure.management.compute.VirtualMachineExtensionImages; import com.microsoft.azure.management.compute.VirtualMachineImages; +import com.microsoft.azure.management.compute.VirtualMachineScaleSets; import com.microsoft.azure.management.compute.VirtualMachines; import com.microsoft.azure.management.network.implementation.NetworkManager; import com.microsoft.azure.management.resources.fluentcore.arm.AzureConfigurable; @@ -25,6 +26,7 @@ public final class ComputeManager extends Manager()))); - inner.virtualMachineProfile() - .withOsProfile(new VirtualMachineScaleSetOSProfile()); - - inner.virtualMachineProfile() - .withNetworkProfile(new VirtualMachineScaleSetNetworkProfile()); - - inner.virtualMachineProfile() - .networkProfile() - .withNetworkInterfaceConfigurations(new ArrayList()); - - VirtualMachineScaleSetNetworkConfigurationInner primaryNetworkInterfaceConfiguration = - new VirtualMachineScaleSetNetworkConfigurationInner() - .withPrimary(true) - .withName("default") - .withIpConfigurations(new ArrayList()); - primaryNetworkInterfaceConfiguration - .ipConfigurations() - .add(new VirtualMachineScaleSetIPConfigurationInner()); - - inner.virtualMachineProfile() - .networkProfile() - .networkInterfaceConfigurations() - .add(primaryNetworkInterfaceConfiguration); - - return new VirtualMachineScaleSetImpl(name, - inner, - null, - null, - null, - null); - } - @Override public PagedList availableSkus() throws CloudException, IOException { return this.skuConverter.convert(this.client.listSkus(this.resourceGroupName(), this.name())); diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java new file mode 100644 index 000000000000..5ad3006099bd --- /dev/null +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java @@ -0,0 +1,100 @@ +package com.microsoft.azure.management.compute.implementation; + +import com.microsoft.azure.CloudException; +import com.microsoft.azure.management.compute.VirtualMachineScaleSet; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetNetworkProfile; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetOSDisk; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetOSProfile; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetStorageProfile; +import com.microsoft.azure.management.compute.VirtualMachineScaleSetVMProfile; +import com.microsoft.azure.management.compute.VirtualMachineScaleSets; +import com.microsoft.azure.management.network.implementation.NetworkManager; +import com.microsoft.azure.management.resources.fluentcore.arm.collection.implementation.GroupableResourcesImpl; +import com.microsoft.azure.management.storage.implementation.StorageManager; + +import java.io.IOException; +import java.util.ArrayList; + +/** + * The implementation for {@link VirtualMachineScaleSets}. + */ +public class VirtualMachineScaleSetsImpl + extends GroupableResourcesImpl< + VirtualMachineScaleSet, + VirtualMachineScaleSetImpl, + VirtualMachineScaleSetInner, + VirtualMachineScaleSetsInner, + ComputeManager> + implements VirtualMachineScaleSets { + private final StorageManager storageManager; + private final NetworkManager networkManager; + + VirtualMachineScaleSetsImpl(VirtualMachineScaleSetsInner client, + ComputeManager computeManager, + StorageManager storageManager, + NetworkManager networkManager) { + super(client, computeManager); + this.storageManager = storageManager; + this.networkManager = networkManager; + } + + @Override + public VirtualMachineScaleSetImpl define(String name) { + return wrapModel(name); + } + + @Override + public VirtualMachineScaleSet getByGroup(String groupName, String name) throws CloudException, IOException { + return null; + } + + @Override + protected VirtualMachineScaleSetImpl wrapModel(String name) { + VirtualMachineScaleSetInner inner = new VirtualMachineScaleSetInner(); + + inner.withVirtualMachineProfile(new VirtualMachineScaleSetVMProfile()); + inner.virtualMachineProfile() + .withStorageProfile(new VirtualMachineScaleSetStorageProfile() + .withOsDisk(new VirtualMachineScaleSetOSDisk().withVhdContainers(new ArrayList()))); + inner.virtualMachineProfile() + .withOsProfile(new VirtualMachineScaleSetOSProfile()); + + inner.virtualMachineProfile() + .withNetworkProfile(new VirtualMachineScaleSetNetworkProfile()); + + inner.virtualMachineProfile() + .networkProfile() + .withNetworkInterfaceConfigurations(new ArrayList()); + + VirtualMachineScaleSetNetworkConfigurationInner primaryNetworkInterfaceConfiguration = + new VirtualMachineScaleSetNetworkConfigurationInner() + .withPrimary(true) + .withName("default") + .withIpConfigurations(new ArrayList()); + primaryNetworkInterfaceConfiguration + .ipConfigurations() + .add(new VirtualMachineScaleSetIPConfigurationInner()); + + inner.virtualMachineProfile() + .networkProfile() + .networkInterfaceConfigurations() + .add(primaryNetworkInterfaceConfiguration); + + return new VirtualMachineScaleSetImpl(name, + inner, + this.innerCollection, + super.myManager, + this.storageManager, + this.networkManager); + } + + @Override + protected VirtualMachineScaleSetImpl wrapModel(VirtualMachineScaleSetInner inner) { + return new VirtualMachineScaleSetImpl(inner.name(), + inner, + this.innerCollection, + super.myManager, + this.storageManager, + this.networkManager); + } +} diff --git a/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/ComputeManagementTestBase.java b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/ComputeManagementTestBase.java index d592d2da2a92..f09de6974309 100644 --- a/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/ComputeManagementTestBase.java +++ b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/ComputeManagementTestBase.java @@ -3,6 +3,7 @@ import com.microsoft.azure.AzureEnvironment; import com.microsoft.azure.credentials.ApplicationTokenCredentials; import com.microsoft.azure.management.compute.implementation.ComputeManager; +import com.microsoft.azure.management.network.implementation.NetworkManager; import com.microsoft.azure.management.resources.implementation.ResourceManager; import com.microsoft.azure.RestClient; import okhttp3.logging.HttpLoggingInterceptor; @@ -10,6 +11,7 @@ public abstract class ComputeManagementTestBase { protected static ResourceManager resourceManager; protected static ComputeManager computeManager; + protected static NetworkManager networkManager; public static void createClients() { ApplicationTokenCredentials credentials = new ApplicationTokenCredentials( @@ -29,5 +31,8 @@ public static void createClients() { computeManager = ComputeManager .authenticate(restClient, System.getenv("subscription-id")); + + networkManager = NetworkManager + .authenticate(restClient, System.getenv("subscription-id")); } } diff --git a/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java new file mode 100644 index 000000000000..db62c21beaf1 --- /dev/null +++ b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java @@ -0,0 +1,58 @@ +package com.microsoft.azure.management.compute; + +import com.microsoft.azure.management.resources.ResourceGroup; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +public class VirtualMachineScaleSetOperationsTests extends ComputeManagementTestBase { + private static final String RG_NAME = "javacsmrg"; + private static final String LOCATION = "southcentralus"; + private static final String VMSCALESETNAME = "javavm"; + + @BeforeClass + public static void setup() throws Exception { + createClients(); + } + + @AfterClass + public static void cleanup() throws Exception { + resourceManager.resourceGroups().delete(RG_NAME); + } + + @Test + public void canCreateVirtualMachineScaleSet() throws Exception { + final String mySqlInstallScript = "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/4397e808d07df60ff3cdfd1ae40999f0130eb1b3/mysql-standalone-server-ubuntu/scripts/install_mysql_server_5.6.sh"; + final String installCommand = "bash install_mysql_server_5.6.sh Abc.123x("; + List fileUris = new ArrayList<>(); + fileUris.add(mySqlInstallScript); + + ResourceGroup.DefinitionStages.WithCreate resourceGroupCreatable = this.resourceManager.resourceGroups() + .define(RG_NAME) + .withRegion(LOCATION); + + VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets() + .define(VMSCALESETNAME) + .withRegion(LOCATION) + .withNewResourceGroup(resourceGroupCreatable) + .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0) + .withNewPrimaryNetwork("10.0.0.0/28") + .withoutPrimaryInternetFacingLoadBalancer() + .withoutPrimaryInternalLoadBalancer() + .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS) + .withRootUserName("jvuser") + .withPassword("123OData!@#123") + .defineNewExtension("CustomScriptForLinux") + .withPublisher("Microsoft.OSTCExtensions") + .withType("CustomScriptForLinux") + .withVersion("1.4") + .withAutoUpgradeMinorVersionEnabled() + .withPublicSetting("fileUris",fileUris) + .withPublicSetting("commandToExecute", installCommand) + .attach() + .create(); + } +} From 527b1e6be6cf3a067691cc0c264777decebd0b7f Mon Sep 17 00:00:00 2001 From: anuchan Date: Mon, 12 Sep 2016 13:04:50 -0700 Subject: [PATCH 09/10] using lb state variables on update --- .../compute/VirtualMachineScaleSet.java | 117 +-- .../VirtualMachineScaleSetImpl.java | 696 +++++++++++------- ...VirtualMachineScaleSetOperationsTests.java | 23 + 3 files changed, 539 insertions(+), 297 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java index 84a9bebb0924..2564dd694da6 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -9,6 +9,7 @@ import com.microsoft.azure.management.network.Network; import com.microsoft.azure.management.resources.fluentcore.arm.models.GroupableResource; import com.microsoft.azure.management.resources.fluentcore.arm.models.Resource; +import com.microsoft.azure.management.resources.fluentcore.model.Appliable; import com.microsoft.azure.management.resources.fluentcore.model.Creatable; import com.microsoft.azure.management.resources.fluentcore.model.Refreshable; import com.microsoft.azure.management.resources.fluentcore.model.Wrapper; @@ -675,75 +676,97 @@ interface WithCreate extends */ interface UpdateStages { /** - * Stage of the virtual machine scale set update allowing to remove public and internal load balancer - * from the primary network interface configuration. + * The stage of the virtual machine scale set definition allowing to update Sku for the virtual machines in the scale set. */ - interface WithoutPrimaryLoadBalancer { + interface WithSku { /** - * Remove the internet facing load balancer associated to the primary network interface configuration. - *

- * This removes the association between primary network interface configuration and all backend and - * inbound NAT pools in the load balancer. - *

+ * Specifies sku for the virtual machines in the scale set. * + * @param skuType the sku type * @return the next stage of the virtual machine scale set update */ - Update withoutPrimaryInternetFacingLoadBalancer(); + Update withSku(VirtualMachineScaleSetSkuTypes skuType); /** - * Remove the internal load balancer associated to the primary network interface configuration. - *

- * This removes the association between primary network interface configuration and all backend and - * inbound NAT pools in the load balancer. - *

+ * Specifies sku for the virtual machines in the scale set. * + * @param sku a sku from the list of available sizes for the virtual machines in this scale set * @return the next stage of the virtual machine scale set update */ - Update withoutPrimaryInternalLoadBalancer(); + Update withSku(VirtualMachineScaleSetSku sku); } /** - * Stage of the virtual machine scale set update allowing to associate a backend from the load balancer - * with the primary network interface configuration. + * The stage of the virtual machine scale set definition allowing to specify number of + * virtual machines in the scale set. */ - interface WithPrimaryLoadBalancerBackend { + interface WithCapacity { /** - * Associate a backend of the internet facing load balancer with the the primary network interface configuration. + * Specifies the new number of virtual machines in the scale set. * - * @param backendName the name of an existing backend + * @param capacity the virtual machine capacity * @return the next stage of the virtual machine scale set update */ - Update withPrimaryInternetFacingLoadBalancerBackend(String backendName); + Update withCapacity(long capacity); + } + /** + * The stage of the virtual machine definition allowing to specify extensions. + */ + interface WithExtension { /** - * Associate a backend of the internal load balancer with the the primary network interface configuration. + * Specifies definition of an extension to be attached to the virtual machines in the scale set. * - * @param backendName the name of an existing backend - * @return the next stage of the virtual machine scale set update + * @param name the reference name for the extension + * @return the stage representing configuration for the extension */ - Update withPrimaryInternalLoadBalancerBackend(String backendName); + VirtualMachineScaleSetExtension + .UpdateDefinitionStages + .Blank defineNewExtension(String name); + + /** + * Begins the description of an update of an existing extension assigned to the virtual machines in the scale set. + * + * @param name the reference name for the extension + * @return the stage representing updatable extension definition + */ + VirtualMachineScaleSetExtension.Update updateExtension(String name); + + /** + * Detaches an extension with the given name from the virtual machines in the scale set. + * + * @param name the reference name for the extension to be removed/uninstalled + * @return the stage representing updatable VM scale set definition + */ + Update withoutExtension(String name); } /** - * Stage of the virtual machine scale set update allowing to associate a inbound NAT pool from the load balancer - * with the primary network interface configuration. + * Stage of the virtual machine scale set update allowing to remove public and internal load balancer + * from the primary network interface configuration. */ - interface WithPrimaryLoadBalancerNatPoold { + interface WithoutPrimaryLoadBalancer { /** - * Associate an inbound NAT pool of the internet facing load balancer with the the primary network interface configuration. + * Remove the internet facing load balancer associated to the primary network interface configuration. + *

+ * This removes the association between primary network interface configuration and all backend and + * inbound NAT pools in the load balancer. + *

* - * @param natPoolName the name of an existing inbound NAT pool * @return the next stage of the virtual machine scale set update */ - Update withPrimaryInternetFacingLoadBalancerNatPool(String natPoolName); + Update withoutPrimaryInternetFacingLoadBalancer(); /** - * Associate an inbound NAT pool of the internal load balancer with the the primary network interface configuration. + * Remove the internal load balancer associated to the primary network interface configuration. + *

+ * This removes the association between primary network interface configuration and all backend and + * inbound NAT pools in the load balancer. + *

* - * @param natPoolName the name of an existing inbound NAT pool * @return the next stage of the virtual machine scale set update */ - Update withPrimaryInternalLoadBalancerNatPool(String natPoolName); + Update withoutPrimaryInternalLoadBalancer(); } /** @@ -755,18 +778,18 @@ interface WithoutPrimaryLoadBalancerBackend { * Removes association between the primary network interface configuration and backend of the internet facing * load balancer. * - * @param backendName the name of an existing backend + * @param backendNames the existing backend names to remove * @return the next stage of the virtual machine scale set update */ - Update withoutPrimaryInternetFacingLoadBalancerBackend(String backendName); + Update withoutPrimaryInternetFacingLoadBalancerBackend(String ...backendNames); /** * Removes association between the primary network interface configuration and backend of the internal load balancer. * - * @param backendName the name of an existing backend + * @param backendNames the existing backend names to remove * @return the next stage of the virtual machine scale set update */ - Update withoutPrimaryInternalLoadBalancerBackend(String backendName); + Update withoutPrimaryInternalLoadBalancerBackend(String ...backendNames); } /** @@ -778,25 +801,33 @@ interface WithoutPrimaryLoadBalancerNatPool { * Removes association between the primary network interface configuration and inbound NAT pool of the * internet facing load balancer. * - * @param natPoolName the name of an existing inbound NAT pool + * @param natPoolNames the name of an existing inbound NAT pools to remove * @return the next stage of the virtual machine scale set update */ - Update withoutPrimaryInternetFacingLoadBalancerNatPool(String natPoolName); + Update withoutPrimaryInternetFacingLoadBalancerNatPool(String ...natPoolNames); /** * Removes association between the primary network interface configuration and inbound NAT pool of the * internal load balancer. * - * @param natPoolName the name of an existing inbound NAT pool + * @param natPoolNames the name of an existing inbound NAT pools to remove * @return the next stage of the virtual machine scale set update */ - Update withoutPrimaryInternalLoadBalancerNatPool(String natPoolName); + Update withoutPrimaryInternalLoadBalancerNatPool(String ...natPoolNames); } } /** * The entirety of the load balancer update. */ - interface Update { + interface Update extends + Appliable, + Resource.UpdateWithTags, + UpdateStages.WithSku, + UpdateStages.WithCapacity, + UpdateStages.WithExtension, + UpdateStages.WithoutPrimaryLoadBalancer, + UpdateStages.WithoutPrimaryLoadBalancerBackend, + UpdateStages.WithoutPrimaryLoadBalancerNatPool { } } \ No newline at end of file diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java index 4c043a8eda42..86dc012215d0 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -85,13 +85,23 @@ public class VirtualMachineScaleSetImpl // reference to an existing storage account to be used for virtual machines child resources that // requires storage [OS disk] private List existingStorageAccountsToAssociate; - - private List internetFacingLBBackendsToAssociateOnUpdate; - private List internetFacingLBInboundNatPoolsToAssociateOnUpdate; - private List internalLBBackendsToAssociateOnUpdate; - private List internalLBInboundNatPoolsToAssociateOnUpdate; private Map extensions; private PagedListConverter skuConverter; + // Load balancer specific variables used during update + // + private boolean removePrimaryInternetFacingLoadBalancerOnUpdate; + private boolean removePrimaryInternalLoadBalancerOnUpdate; + private LoadBalancer primaryInternetFacingLoadBalancerToAttachOnUpdate; + private LoadBalancer primaryInternalLoadBalancerToAttachOnUpdate; + private List primaryInternetFacingLBBackendsToRemoveOnUpdate; + private List primaryInternetFacingLBInboundNatPoolsToRemoveOnUpdate; + private List primaryInternalLBBackendsToRemoveOnUpdate; + private List primaryInternalLBInboundNatPoolsToRemoveOnUpdate; + private List primaryInternetFacingLBBackendsToAddOnUpdate; + private List primaryInternetFacingLBInboundNatPoolsToAddOnUpdate; + private List primaryInternalLBBackendsToAddOnUpdate; + private List primaryInternalLBInboundNatPoolsToAddOnUpdate; + VirtualMachineScaleSetImpl(String name, VirtualMachineScaleSetInner innerModel, @@ -108,10 +118,7 @@ public class VirtualMachineScaleSetImpl this.creatableStorageAccountKeys = new ArrayList<>(); this.existingStorageAccountsToAssociate = new ArrayList<>(); - this.internetFacingLBBackendsToAssociateOnUpdate = new ArrayList<>(); - this.internetFacingLBInboundNatPoolsToAssociateOnUpdate = new ArrayList<>(); - this.internalLBBackendsToAssociateOnUpdate = new ArrayList<>(); - this.internalLBInboundNatPoolsToAssociateOnUpdate = new ArrayList<>(); + resetLoadBalancerStateVariables(); this.skuConverter = new PagedListConverter() { @Override public VirtualMachineScaleSetSku typeConvert(VirtualMachineScaleSetSkuInner inner) { @@ -157,38 +164,50 @@ public VirtualMachineScaleSetSkuTypes sku() { @Override public LoadBalancer primaryInternetFacingLoadBalancer() throws IOException { - setPrimaryLoadBalancersIfAvailable(); + loadCurrentPrimaryLoadBalancersIfAvailable(); return this.primaryInternetFacingLoadBalancer; } @Override public Map primaryInternetFacingLoadBalancerBackEnds() throws IOException { - setPrimaryLoadBalancersIfAvailable(); - return null; + if (this.primaryInternetFacingLoadBalancer() != null) { + return getBackendsAssociatedWithIpConfiguration(this.primaryInternetFacingLoadBalancer, + primaryNicDefaultIPConfiguration()); + } + return new HashMap<>(); } @Override public Map primaryInternetFacingLoadBalancerInboundNatPools() throws IOException { - setPrimaryLoadBalancersIfAvailable(); - return null; + if (this.primaryInternetFacingLoadBalancer() != null) { + return getInboundNatPoolsAssociatedWithIpConfiguration(this.primaryInternetFacingLoadBalancer, + primaryNicDefaultIPConfiguration()); + } + return new HashMap<>(); } @Override public LoadBalancer primaryInternalLoadBalancer() throws IOException { - setPrimaryLoadBalancersIfAvailable(); + loadCurrentPrimaryLoadBalancersIfAvailable(); return this.primaryInternalLoadBalancer; } @Override public Map primaryInternalLoadBalancerBackEnds() throws IOException { - setPrimaryLoadBalancersIfAvailable(); - return null; + if (this.primaryInternalLoadBalancer() != null) { + return getBackendsAssociatedWithIpConfiguration(this.primaryInternalLoadBalancer, + primaryNicDefaultIPConfiguration()); + } + return new HashMap<>(); } @Override public Map primaryInternalLoadBalancerInboundNatPools() throws IOException { - setPrimaryLoadBalancersIfAvailable(); - return null; + if (this.primaryInternalLoadBalancer() != null) { + return getInboundNatPoolsAssociatedWithIpConfiguration(this.primaryInternalLoadBalancer, + primaryNicDefaultIPConfiguration()); + } + return new HashMap<>(); } @Override @@ -206,40 +225,17 @@ public Map extensions() { return Collections.unmodifiableMap(this.extensions); } - @Override - public VirtualMachineScaleSetImpl withAdminUserName(String adminUserName) { - this.inner().virtualMachineProfile() - .osProfile() - .withAdminPassword(adminUserName); - return this; - } + // Fluent setters @Override - public VirtualMachineScaleSetImpl withSsh(String publicKeyData) { - VirtualMachineScaleSetOSProfile osProfile = this.inner() - .virtualMachineProfile() - .osProfile(); - if (osProfile.linuxConfiguration().ssh() == null) { - SshConfiguration sshConfiguration = new SshConfiguration(); - sshConfiguration.withPublicKeys(new ArrayList()); - osProfile.linuxConfiguration().withSsh(sshConfiguration); - } - SshPublicKey sshPublicKey = new SshPublicKey(); - sshPublicKey.withKeyData(publicKeyData); - sshPublicKey.withPath("/home/" + osProfile.adminUsername() + "/.ssh/authorized_keys"); - osProfile.linuxConfiguration().ssh().publicKeys().add(sshPublicKey); - return this; - } - - @Override - public VirtualMachineScaleSetImpl withCapacity(long capacity) { - this.inner().sku().withCapacity(capacity); + public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSkuTypes skuType) { + this.inner().withSku(skuType.sku()); return this; } @Override - public VirtualMachineScaleSetExtensionImpl defineNewExtension(String name) { - return new VirtualMachineScaleSetExtensionImpl(new VirtualMachineScaleSetExtensionInner(), this); + public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSku sku) { + return this.withSku(sku.skuType()); } @Override @@ -276,6 +272,111 @@ public VirtualMachineScaleSetImpl withSubnet(String name) { return this; } + @Override + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancer(LoadBalancer loadBalancer) { + if (loadBalancer.publicIpAddressIds().isEmpty()) { + throw new IllegalArgumentException("Parameter loadBalancer must be an internet facing load balancer"); + } + if (isInCreateMode()) { + this.primaryInternetFacingLoadBalancer = loadBalancer; + associateLoadBalancerToIpConfiguration(this.primaryInternetFacingLoadBalancer, + this.primaryNicDefaultIPConfiguration()); + } else { + this.primaryInternetFacingLoadBalancerToAttachOnUpdate = loadBalancer; + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(String... backendNames) { + if (this.isInCreateMode()) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); + removeAllBackendAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancer, defaultPrimaryIpConfig); + associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + defaultPrimaryIpConfig, + backendNames); + } else { + addToList(this.primaryInternetFacingLBBackendsToAddOnUpdate, backendNames); + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNatPool(String... natPoolNames) { + if (this.isInCreateMode()) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); + removeAllInboundNatPoolAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancer, + defaultPrimaryIpConfig); + associateInboundNATPoolsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + defaultPrimaryIpConfig, + natPoolNames); + } else { + addToList(this.primaryInternetFacingLBInboundNatPoolsToAddOnUpdate, natPoolNames); + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer loadBalancer) { + if (!loadBalancer.publicIpAddressIds().isEmpty()) { + throw new IllegalArgumentException("Parameter loadBalancer must be an internal load balancer"); + } + if (isInCreateMode()) { + this.primaryInternalLoadBalancer = loadBalancer; + associateLoadBalancerToIpConfiguration(this.primaryInternalLoadBalancer, + this.primaryNicDefaultIPConfiguration()); + } else { + this.primaryInternalLoadBalancerToAttachOnUpdate = loadBalancer; + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { + if (this.isInCreateMode()) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = primaryNicDefaultIPConfiguration(); + this.removeAllBackendAssociationFromIpConfiguration(this.primaryInternalLoadBalancer, + defaultPrimaryIpConfig); + this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), + defaultPrimaryIpConfig, + backendNames); + } else { + addToList(this.primaryInternalLBBackendsToAddOnUpdate, backendNames); + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPool(String... natPoolNames) { + if (this.isInCreateMode()) { + VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); + this.removeAllInboundNatPoolAssociationFromIpConfiguration(this.primaryInternalLoadBalancer, + defaultPrimaryIpConfig); + this.associateInboundNATPoolsToIpConfiguration(this.primaryInternalLoadBalancer.id(), + defaultPrimaryIpConfig, + natPoolNames); + } else { + addToList(this.primaryInternalLBInboundNatPoolsToAddOnUpdate, natPoolNames); + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withoutPrimaryInternalLoadBalancer() { + if (this.isInUpdateMode()) { + this.removePrimaryInternalLoadBalancerOnUpdate = true; + } + return this; + } + + @Override + public VirtualMachineScaleSetImpl withoutPrimaryInternetFacingLoadBalancer() { + if (this.isInUpdateMode()) { + this.removePrimaryInternetFacingLoadBalancerOnUpdate = true; + } + return this; + } + @Override public VirtualMachineScaleSetImpl withPopularWindowsImage(KnownWindowsVirtualMachineImage knownImage) { return withSpecificWindowsImageVersion(knownImage.imageReference()); @@ -347,10 +448,10 @@ public VirtualMachineScaleSetImpl withPopularLinuxImage(KnownLinuxVirtualMachine @Override public VirtualMachineScaleSetImpl withLatestLinuxImage(String publisher, String offer, String sku) { ImageReference imageReference = new ImageReference() - .withPublisher(publisher) - .withOffer(offer) - .withSku(sku) - .withVersion("latest"); + .withPublisher(publisher) + .withOffer(offer) + .withSku(sku) + .withVersion("latest"); return withSpecificLinuxImageVersion(imageReference); } @@ -389,6 +490,44 @@ public VirtualMachineScaleSetImpl withStoredLinuxImage(String imageUrl) { return this; } + @Override + public VirtualMachineScaleSetImpl withAdminUserName(String adminUserName) { + this.inner().virtualMachineProfile() + .osProfile() + .withAdminPassword(adminUserName); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withRootUserName(String rootUserName) { + return this.withAdminUserName(rootUserName); + } + + @Override + public VirtualMachineScaleSetImpl withPassword(String password) { + this.inner() + .virtualMachineProfile() + .osProfile().withAdminPassword(password); + return this; + } + + @Override + public VirtualMachineScaleSetImpl withSsh(String publicKeyData) { + VirtualMachineScaleSetOSProfile osProfile = this.inner() + .virtualMachineProfile() + .osProfile(); + if (osProfile.linuxConfiguration().ssh() == null) { + SshConfiguration sshConfiguration = new SshConfiguration(); + sshConfiguration.withPublicKeys(new ArrayList()); + osProfile.linuxConfiguration().withSsh(sshConfiguration); + } + SshPublicKey sshPublicKey = new SshPublicKey(); + sshPublicKey.withKeyData(publicKeyData); + sshPublicKey.withPath("/home/" + osProfile.adminUsername() + "/.ssh/authorized_keys"); + osProfile.linuxConfiguration().ssh().publicKeys().add(sshPublicKey); + return this; + } + @Override public VirtualMachineScaleSetImpl disableVmAgent() { this.inner() @@ -456,143 +595,11 @@ public VirtualMachineScaleSetImpl withOverProvisionDisabled() { } @Override - public VirtualMachineScaleSetImpl withPassword(String password) { - this.inner() - .virtualMachineProfile() - .osProfile().withAdminPassword(password); - return this; - } - - @Override - public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer loadBalancer) { - if (!loadBalancer.publicIpAddressIds().isEmpty()) { - throw new IllegalArgumentException("Parameter loadBalancer must be an internal load balancer"); - } - this.primaryInternalLoadBalancer = loadBalancer; - this.associateAllBackendsAndInboundNatPools(this.primaryInternalLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - return this; - } - - @Override - public VirtualMachineScaleSetImpl withoutPrimaryInternalLoadBalancer() { - return this; - } - - @Override - public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancer(LoadBalancer loadBalancer) { - if (loadBalancer.publicIpAddressIds().isEmpty()) { - throw new IllegalArgumentException("Parameter loadBalancer must be an internet facing load balancer"); - } - this.primaryInternetFacingLoadBalancer = loadBalancer; - this.associateAllBackendsAndInboundNatPools(this.primaryInternetFacingLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - return this; - } - - @Override - public VirtualMachineScaleSetImpl withoutPrimaryInternetFacingLoadBalancer() { - return this; - } - - @Override - public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(String... backendNames) { - if (this.primaryInternetFacingLoadBalancer != null) { - VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); - this.removeLoadBalancerAssociation(this.primaryInternetFacingLoadBalancer, defaultPrimaryIpConfig); - this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), - defaultPrimaryIpConfig, - backendNames); - } else { - if (!isInCreateMode()) { - this.internetFacingLBBackendsToAssociateOnUpdate.clear(); - for (String backendName : backendNames) { - this.internetFacingLBBackendsToAssociateOnUpdate.add(backendName); - } - } - } - return this; - } - - @Override - public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNatPool(String... natPoolNames) { - if (this.primaryInternetFacingLoadBalancer != null) { - this.removeAllInboundNatPoolsAssociation(this.primaryInternetFacingLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); - this.associateInboundNATPoolsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), - defaultPrimaryIpConfig, - natPoolNames); - } else { - if (!isInCreateMode()) { - this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.clear(); - for (String natPoolName : natPoolNames) { - this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.add(natPoolName); - } - } - } - return this; - } - - @Override - public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { - if (this.primaryInternalLoadBalancer != null) { - this.removeAllBackendsAssociation(this.primaryInternalLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - this.removeAllInboundNatPoolsAssociation(this.primaryInternalLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - - VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = primaryNicDefaultIPConfiguration(); - this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), - defaultPrimaryIpConfig, - backendNames); - } else { - if (!isInCreateMode()) { - this.internalLBBackendsToAssociateOnUpdate.clear(); - for (String backendName : backendNames) { - this.internalLBBackendsToAssociateOnUpdate.add(backendName); - } - } - } - return this; - } - - @Override - public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPool(String... natPoolNames) { - if (this.primaryInternalLoadBalancer != null) { - this.removeAllInboundNatPoolsAssociation(this.primaryInternalLoadBalancer, - this.primaryNicDefaultIPConfiguration()); - VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); - this.associateInboundNATPoolsToIpConfiguration(this.primaryInternalLoadBalancer.id(), - defaultPrimaryIpConfig, - natPoolNames); - } else { - if (!isInCreateMode()) { - this.internalLBInboundNatPoolsToAssociateOnUpdate.clear(); - for (String natPoolName : natPoolNames) { - this.internalLBInboundNatPoolsToAssociateOnUpdate.add(natPoolName); - } - } - } - return this; - } - - @Override - public VirtualMachineScaleSetImpl withRootUserName(String rootUserName) { - return this.withAdminUserName(rootUserName); - } - - @Override - public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSkuTypes skuType) { - this.inner().withSku(skuType.sku()); + public VirtualMachineScaleSetImpl withCapacity(long capacity) { + this.inner().sku().withCapacity(capacity); return this; } - @Override - public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSku sku) { - return this.withSku(sku.skuType()); - } - @Override public VirtualMachineScaleSetImpl withNewStorageAccount(String name) { StorageAccount.DefinitionStages.WithGroup definitionWithGroup = this.storageManager @@ -621,36 +628,14 @@ public VirtualMachineScaleSetImpl withExistingStorageAccount(StorageAccount stor return this; } + @Override + public VirtualMachineScaleSetExtensionImpl defineNewExtension(String name) { + return new VirtualMachineScaleSetExtensionImpl(new VirtualMachineScaleSetExtensionInner(), this); + } + @Override public Observable applyAsync() { - if (!this.internetFacingLBBackendsToAssociateOnUpdate.isEmpty() - || !this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.isEmpty() - || !this.internalLBBackendsToAssociateOnUpdate.isEmpty() - || !this.internalLBInboundNatPoolsToAssociateOnUpdate.isEmpty()) { - try { - this.setPrimaryLoadBalancersIfAvailable(); - if (this.primaryInternetFacingLoadBalancer != null) { - this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), - this.primaryNicDefaultIPConfiguration(), - this.internalLBBackendsToAssociateOnUpdate.toArray(new String[0])); - - this.associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), - this.primaryNicDefaultIPConfiguration(), - this.internetFacingLBInboundNatPoolsToAssociateOnUpdate.toArray(new String[0])); - } - if (this.primaryInternalLoadBalancer != null) { - this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), - this.primaryNicDefaultIPConfiguration(), - this.internalLBBackendsToAssociateOnUpdate.toArray(new String[0])); - - this.associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancer.id(), - this.primaryNicDefaultIPConfiguration(), - this.internalLBInboundNatPoolsToAssociateOnUpdate.toArray(new String[0])); - } - } catch (IOException ioException) { - throw new RuntimeException(ioException); - } - } + this.handlePrimaryIpConfigurationOnUpdate(); return null; } @@ -702,33 +687,24 @@ public VirtualMachineScaleSetImpl refresh() throws Exception { } VirtualMachineScaleSetImpl withExtension(VirtualMachineScaleSetExtensionImpl extension) { - // TODO + this.extensions.put(extension.name(), extension); return this; } - private VirtualMachineScaleSetIPConfigurationInner primaryNicDefaultIPConfiguration() { - List nicConfigurations = this.inner() - .virtualMachineProfile() - .networkProfile() - .networkInterfaceConfigurations(); + // Helpers + // - for (VirtualMachineScaleSetNetworkConfigurationInner nicConfiguration : nicConfigurations) { - if (nicConfiguration.primary()) { - if (nicConfiguration.ipConfigurations().size() > 0) { - VirtualMachineScaleSetIPConfigurationInner ipConfig = nicConfiguration.ipConfigurations().get(0); - if (ipConfig.loadBalancerBackendAddressPools() == null) { - ipConfig.withLoadBalancerBackendAddressPools(new ArrayList()); - } - if (ipConfig.loadBalancerInboundNatPools() == null) { - ipConfig.withLoadBalancerInboundNatPools(new ArrayList()); - } - } - } - } - throw new RuntimeException("Could not find the primary nic configuration or an IP configuration in it"); + private void refreshPrimaryLoadBalancersCache() throws IOException { + this.primaryInternetFacingLoadBalancer = null; + this.primaryInternalLoadBalancer = null; + this.loadCurrentPrimaryLoadBalancersIfAvailable(); } - private void setPrimaryLoadBalancersIfAvailable() throws IOException { + private void loadCurrentPrimaryLoadBalancersIfAvailable() throws IOException { + if (this.primaryInternetFacingLoadBalancer != null && this.primaryInternalLoadBalancer != null) { + return; + } + String firstLoadBalancerId = null; VirtualMachineScaleSetIPConfigurationInner ipConfig = primaryNicDefaultIPConfiguration(); if (!ipConfig.loadBalancerBackendAddressPools().isEmpty()) { @@ -785,7 +761,145 @@ private void setPrimaryLoadBalancersIfAvailable() throws IOException { } } - private void associateBackEndsToIpConfiguration(String loadBalancerId, + private VirtualMachineScaleSetIPConfigurationInner primaryNicDefaultIPConfiguration() { + List nicConfigurations = this.inner() + .virtualMachineProfile() + .networkProfile() + .networkInterfaceConfigurations(); + + for (VirtualMachineScaleSetNetworkConfigurationInner nicConfiguration : nicConfigurations) { + if (nicConfiguration.primary()) { + if (nicConfiguration.ipConfigurations().size() > 0) { + VirtualMachineScaleSetIPConfigurationInner ipConfig = nicConfiguration.ipConfigurations().get(0); + if (ipConfig.loadBalancerBackendAddressPools() == null) { + ipConfig.withLoadBalancerBackendAddressPools(new ArrayList()); + } + if (ipConfig.loadBalancerInboundNatPools() == null) { + ipConfig.withLoadBalancerInboundNatPools(new ArrayList()); + } + } + } + } + throw new RuntimeException("Could not find the primary nic configuration or an IP configuration in it"); + } + + private boolean isInUpdateMode() { + return !this.isInCreateMode(); + } + + private void resetLoadBalancerStateVariables() { + this.removePrimaryInternetFacingLoadBalancerOnUpdate = false; + this.removePrimaryInternalLoadBalancerOnUpdate = false; + this.primaryInternetFacingLoadBalancerToAttachOnUpdate = null; + this.primaryInternalLoadBalancerToAttachOnUpdate = null; + this.primaryInternetFacingLBBackendsToRemoveOnUpdate = new ArrayList<>(); + this.primaryInternetFacingLBInboundNatPoolsToRemoveOnUpdate = new ArrayList<>(); + this.primaryInternalLBBackendsToRemoveOnUpdate = new ArrayList<>(); + this.primaryInternalLBInboundNatPoolsToRemoveOnUpdate = new ArrayList<>(); + this.primaryInternetFacingLBBackendsToAddOnUpdate = new ArrayList<>(); + this.primaryInternetFacingLBInboundNatPoolsToAddOnUpdate = new ArrayList<>(); + this.primaryInternalLBBackendsToAddOnUpdate = new ArrayList<>(); + this.primaryInternalLBInboundNatPoolsToAddOnUpdate = new ArrayList<>(); + } + + private void handlePrimaryIpConfigurationOnUpdate() { + try { + this.loadCurrentPrimaryLoadBalancersIfAvailable(); + } catch (IOException ioException) { + throw new RuntimeException(ioException); + } + + VirtualMachineScaleSetIPConfigurationInner primaryIpConfig = primaryNicDefaultIPConfiguration(); + if (this.primaryInternetFacingLoadBalancer != null) { + removeBackendsFromIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + primaryIpConfig, + this.primaryInternetFacingLBBackendsToRemoveOnUpdate.toArray(new String[0])); + + associateBackEndsToIpConfiguration(primaryInternetFacingLoadBalancer.id(), + primaryIpConfig, + this.primaryInternetFacingLBBackendsToAddOnUpdate.toArray(new String[0])); + + removeInboundNatPoolsFromIpConfiguration(this.primaryInternetFacingLoadBalancer.id(), + primaryIpConfig, + this.primaryInternetFacingLBInboundNatPoolsToRemoveOnUpdate.toArray(new String[0])); + + associateInboundNATPoolsToIpConfiguration(primaryInternetFacingLoadBalancer.id(), + primaryIpConfig, + this.primaryInternetFacingLBInboundNatPoolsToAddOnUpdate.toArray(new String[0])); + } + + if (this.primaryInternalLoadBalancer != null) { + removeBackendsFromIpConfiguration(this.primaryInternalLoadBalancer.id(), + primaryIpConfig, + this.primaryInternalLBBackendsToRemoveOnUpdate.toArray(new String[0])); + + associateBackEndsToIpConfiguration(primaryInternalLoadBalancer.id(), + primaryIpConfig, + this.primaryInternalLBBackendsToAddOnUpdate.toArray(new String[0])); + + removeInboundNatPoolsFromIpConfiguration(this.primaryInternalLoadBalancer.id(), + primaryIpConfig, + this.primaryInternalLBInboundNatPoolsToRemoveOnUpdate.toArray(new String[0])); + + associateInboundNATPoolsToIpConfiguration(primaryInternalLoadBalancer.id(), + primaryIpConfig, + this.primaryInternalLBInboundNatPoolsToAddOnUpdate.toArray(new String[0])); + } + + if (this.removePrimaryInternetFacingLoadBalancerOnUpdate) { + if (this.primaryInternetFacingLoadBalancer != null) { + removeLoadBalancerAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancer, primaryIpConfig); + } + } + + if (this.removePrimaryInternalLoadBalancerOnUpdate) { + if (this.primaryInternalLoadBalancer != null) { + removeLoadBalancerAssociationFromIpConfiguration(this.primaryInternalLoadBalancer, primaryIpConfig); + } + } + + if (this.primaryInternetFacingLoadBalancerToAttachOnUpdate != null) { + if (this.primaryInternetFacingLoadBalancer != null) { + removeLoadBalancerAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancer, primaryIpConfig); + } + associateLoadBalancerToIpConfiguration(this.primaryInternetFacingLoadBalancerToAttachOnUpdate, primaryIpConfig); + if (!this.primaryInternetFacingLBBackendsToAddOnUpdate.isEmpty()) { + removeAllBackendAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancerToAttachOnUpdate, primaryIpConfig); + associateBackEndsToIpConfiguration(this.primaryInternetFacingLoadBalancerToAttachOnUpdate.id(), + primaryIpConfig, + this.primaryInternetFacingLBBackendsToAddOnUpdate.toArray(new String[0])); + } + if (!this.primaryInternetFacingLBInboundNatPoolsToAddOnUpdate.isEmpty()) { + removeAllInboundNatPoolAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancerToAttachOnUpdate, primaryIpConfig); + associateInboundNATPoolsToIpConfiguration(this.primaryInternetFacingLoadBalancerToAttachOnUpdate.id(), + primaryIpConfig, + this.primaryInternetFacingLBInboundNatPoolsToAddOnUpdate.toArray(new String[0])); + } + } + + if (this.primaryInternalLoadBalancerToAttachOnUpdate != null) { + if (this.primaryInternalLoadBalancer != null) { + removeLoadBalancerAssociationFromIpConfiguration(this.primaryInternalLoadBalancer, primaryIpConfig); + } + associateLoadBalancerToIpConfiguration(this.primaryInternalLoadBalancerToAttachOnUpdate, primaryIpConfig); + if (!this.primaryInternalLBBackendsToAddOnUpdate.isEmpty()) { + removeAllBackendAssociationFromIpConfiguration(this.primaryInternalLoadBalancerToAttachOnUpdate, primaryIpConfig); + associateBackEndsToIpConfiguration(this.primaryInternalLoadBalancerToAttachOnUpdate.id(), + primaryIpConfig, + this.primaryInternalLBBackendsToAddOnUpdate.toArray(new String[0])); + } + + if (!this.primaryInternalLBInboundNatPoolsToAddOnUpdate.isEmpty()) { + removeAllInboundNatPoolAssociationFromIpConfiguration(this.primaryInternalLoadBalancerToAttachOnUpdate, primaryIpConfig); + associateInboundNATPoolsToIpConfiguration(this.primaryInternalLoadBalancerToAttachOnUpdate.id(), + primaryIpConfig, + this.primaryInternalLBInboundNatPoolsToAddOnUpdate.toArray(new String[0])); + } + } + this.resetLoadBalancerStateVariables(); + } + + private static void associateBackEndsToIpConfiguration(String loadBalancerId, VirtualMachineScaleSetIPConfigurationInner ipConfig, String... backendNames) { List backendSubResourcesToAssociate = new ArrayList<>(); @@ -808,7 +922,7 @@ private void associateBackEndsToIpConfiguration(String loadBalancerId, } } - private void associateInboundNATPoolsToIpConfiguration(String loadBalancerId, + private static void associateInboundNATPoolsToIpConfiguration(String loadBalancerId, VirtualMachineScaleSetIPConfigurationInner ipConfig, String... inboundNatPools) { List inboundNatPoolSubResourcesToAssociate = new ArrayList<>(); @@ -831,8 +945,40 @@ private void associateInboundNATPoolsToIpConfiguration(String loadBalancerId, } } - private void associateAllBackendsAndInboundNatPools(LoadBalancer loadBalancer, - VirtualMachineScaleSetIPConfigurationInner ipConfig) { + private static Map getBackendsAssociatedWithIpConfiguration(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { + String loadBalancerId = loadBalancer.id(); + Map attachedBackends = new HashMap<>(); + Map lbBackends = loadBalancer.backends(); + for (Backend lbBackend : lbBackends.values()) { + String backendId = loadBalancerId + "backendAddressPools" + "/" + lbBackend.name(); + for (SubResource subResource : ipConfig.loadBalancerBackendAddressPools()) { + if (subResource.id().equalsIgnoreCase(backendId)) { + attachedBackends.put(lbBackend.name(), lbBackend); + } + } + } + return attachedBackends; + } + + private static Map getInboundNatPoolsAssociatedWithIpConfiguration(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { + String loadBalancerId = loadBalancer.id(); + Map attachedInboundNatPools = new HashMap<>(); + Map lbInboundNatPools = loadBalancer.inboundNatPools(); + for (InboundNatPool lbInboundNatPool : lbInboundNatPools.values()) { + String inboundNatPoolId = loadBalancerId + "inboundNatPools" + "/" + lbInboundNatPool.name(); + for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { + if (subResource.id().equalsIgnoreCase(inboundNatPoolId)) { + attachedInboundNatPools.put(lbInboundNatPool.name(), lbInboundNatPool); + } + } + } + return attachedInboundNatPools; + } + + private static void associateLoadBalancerToIpConfiguration(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { Collection backends = loadBalancer.backends().values(); String[] backendNames = new String[backends.size()]; int i = 0; @@ -841,7 +987,7 @@ private void associateAllBackendsAndInboundNatPools(LoadBalancer loadBalancer, i++; } - this.associateBackEndsToIpConfiguration(loadBalancer.id(), + associateBackEndsToIpConfiguration(loadBalancer.id(), ipConfig, backendNames); @@ -853,13 +999,19 @@ private void associateAllBackendsAndInboundNatPools(LoadBalancer loadBalancer, i++; } - this.associateInboundNATPoolsToIpConfiguration(loadBalancer.id(), + associateInboundNATPoolsToIpConfiguration(loadBalancer.id(), ipConfig, natPoolNames); } - private void removeAllBackendsAssociation(LoadBalancer loadBalancer, - VirtualMachineScaleSetIPConfigurationInner ipConfig) { + private static void removeLoadBalancerAssociationFromIpConfiguration(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { + removeAllBackendAssociationFromIpConfiguration(loadBalancer, ipConfig); + removeAllInboundNatPoolAssociationFromIpConfiguration(loadBalancer, ipConfig); + } + + private static void removeAllBackendAssociationFromIpConfiguration(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { List toRemoveIndicies = new ArrayList<>(); int i = 0; for (SubResource subResource : ipConfig.loadBalancerBackendAddressPools()) { @@ -874,14 +1026,8 @@ private void removeAllBackendsAssociation(LoadBalancer loadBalancer, } } - private void removeLoadBalancerAssociation(LoadBalancer loadBalancer, - VirtualMachineScaleSetIPConfigurationInner ipConfig) { - this.removeAllBackendsAssociation(loadBalancer, ipConfig); - this.removeAllInboundNatPoolsAssociation(loadBalancer, ipConfig); - } - - private void removeAllInboundNatPoolsAssociation(LoadBalancer loadBalancer, - VirtualMachineScaleSetIPConfigurationInner ipConfig) { + private static void removeAllInboundNatPoolAssociationFromIpConfiguration(LoadBalancer loadBalancer, + VirtualMachineScaleSetIPConfigurationInner ipConfig) { List toRemoveIndicies = new ArrayList<>(); int i = 0; for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { @@ -896,9 +1042,51 @@ private void removeAllInboundNatPoolsAssociation(LoadBalancer loadBalancer, } } - private void refreshPrimaryLoadBalancersCache() throws IOException { - this.primaryInternetFacingLoadBalancer = null; - this.primaryInternalLoadBalancer = null; - this.setPrimaryLoadBalancersIfAvailable(); + private static void removeBackendsFromIpConfiguration(String loadBalancerId, + VirtualMachineScaleSetIPConfigurationInner ipConfig, + String... backendNames) { + List indices = new ArrayList<>(); + for (String backendName : backendNames) { + String backendPoolId = loadBalancerId + "/" + "backendAddressPools" + "/" + backendName; + int index = -1; + for (SubResource subResource : ipConfig.loadBalancerBackendAddressPools()) { + index++; + if (subResource.id().equalsIgnoreCase(backendPoolId)) { + indices.add(index); + break; + } + } + } + + for (Integer index : indices) { + ipConfig.loadBalancerBackendAddressPools().remove(index); + } + } + + private static void removeInboundNatPoolsFromIpConfiguration(String loadBalancerId, + VirtualMachineScaleSetIPConfigurationInner ipConfig, + String... inboundNatPoolNames) { + List indices = new ArrayList<>(); + for (String natPoolName : inboundNatPoolNames) { + String inboundNatPoolId = loadBalancerId + "/" + "inboundNatPools" + "/" + natPoolName; + int index = -1; + for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { + index++; + if (subResource.id().equalsIgnoreCase(inboundNatPoolId)) { + indices.add(index); + break; + } + } + } + + for (Integer index : indices) { + ipConfig.loadBalancerInboundNatPools().remove(index); + } + } + + private static void addToList(List list, T...items) { + for (T item : items) { + list.add(item); + } } } diff --git a/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java index db62c21beaf1..1978b05defad 100644 --- a/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java +++ b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java @@ -34,6 +34,29 @@ public void canCreateVirtualMachineScaleSet() throws Exception { .define(RG_NAME) .withRegion(LOCATION); + /** + LoadBalancer lb = this.networkManager + .loadBalancers() + .define("foo") + .withRegion(LOCATION) + .withNewResourceGroup(resourceGroupCreatable) + .definePublicFrontend("primary") + .withExistingPublicIpAddress("abc") + .attach() + .defineBackend("aa") + .attach() + .defineHttpProbe("httpProbe") + .withRequestPath("/") + .attach() + .defineLoadBalancingRule("") + .withProtocol(TransportProtocol.TCP) + .withFrontend("primary") + .withFrontendPort(80) + .withProbe("httpProbe") + .withBackend("aa") + .attach() + **/ + VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets() .define(VMSCALESETNAME) .withRegion(LOCATION) From 607688c54640a937990bbf449501fa5402df19b3 Mon Sep 17 00:00:00 2001 From: anuchan Date: Mon, 12 Sep 2016 17:10:02 -0700 Subject: [PATCH 10/10] implementing createInner --- .../compute/VirtualMachineScaleSet.java | 21 +- .../VirtualMachineScaleSetImpl.java | 454 ++++++++++++------ .../VirtualMachineScaleSetsImpl.java | 2 +- ...VirtualMachineScaleSetOperationsTests.java | 23 - 4 files changed, 324 insertions(+), 176 deletions(-) diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java index 2564dd694da6..16ee6da1876f 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/VirtualMachineScaleSet.java @@ -53,6 +53,11 @@ public interface VirtualMachineScaleSet extends */ CachingTypes osDiskCachingType(); + /** + * @return gets the name of the OS disk of virtual machines in the scale set. + */ + String osDiskName(); + /** * @return the upgradePolicy */ @@ -310,7 +315,7 @@ interface WithPrimaryInternetFacingLoadBalancerBackendOrNatPool extends WithPrim * @param backendNames the backend names * @return the next stage of the virtual machine scale set definition */ - WithPrimaryInternetFacingLoadBalancerNatPool withPrimaryInternetFacingLoadBalancerBackend(String ...backendNames); + WithPrimaryInternetFacingLoadBalancerNatPool withPrimaryInternetFacingLoadBalancerBackends(String ...backendNames); } /** @@ -326,7 +331,7 @@ interface WithPrimaryInternetFacingLoadBalancerNatPool extends WithPrimaryIntern * @param natPoolNames the inbound NAT pool names * @return the next stage of the virtual machine scale set definition */ - WithPrimaryInternalLoadBalancer withPrimaryInternetFacingLoadBalancerInboundNatPool(String ...natPoolNames); + WithPrimaryInternalLoadBalancer withPrimaryInternetFacingLoadBalancerInboundNatPools(String ...natPoolNames); } /** @@ -342,7 +347,7 @@ interface WithInternalLoadBalancerBackendOrNatPool extends WithCreate { * @param backendNames the backend names * @return the next stage of the virtual machine scale set definition */ - WithInternalInternalLoadBalancerNatPool withPrimaryInternalLoadBalancerBackend(String ...backendNames); + WithInternalInternalLoadBalancerNatPool withPrimaryInternalLoadBalancerBackends(String ...backendNames); } /** @@ -358,7 +363,7 @@ interface WithInternalInternalLoadBalancerNatPool extends WithOS { * @param natPoolNames inbound NAT pool names * @return the next stage of the virtual machine scale set definition */ - WithOS withPrimaryInternalLoadBalancerInboundNatPool(String ...natPoolNames); + WithOS withPrimaryInternalLoadBalancerInboundNatPools(String ...natPoolNames); } /** @@ -607,6 +612,14 @@ interface WithOsDiskSettings { * @return the stage representing creatable VM scale set definition */ WithCreate withOsDiskCaching(CachingTypes cachingType); + + /** + * Specifies the name for the OS Disk. + * + * @param name the OS Disk name. + * @return the stage representing creatable VM scale set definition + */ + WithCreate withOsDiskName(String name); } /** diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java index 86dc012215d0..c306f08ca66c 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetImpl.java @@ -3,6 +3,7 @@ import com.microsoft.azure.CloudException; import com.microsoft.azure.PagedList; import com.microsoft.azure.SubResource; +import com.microsoft.azure.management.compute.ApiEntityReference; import com.microsoft.azure.management.compute.CachingTypes; import com.microsoft.azure.management.compute.DiskCreateOptionTypes; import com.microsoft.azure.management.compute.ImageReference; @@ -29,6 +30,7 @@ import com.microsoft.azure.management.network.InboundNatPool; import com.microsoft.azure.management.network.LoadBalancer; import com.microsoft.azure.management.network.Network; +import com.microsoft.azure.management.network.Subnet; import com.microsoft.azure.management.network.implementation.NetworkManager; import com.microsoft.azure.management.resources.fluentcore.arm.ResourceUtils; import com.microsoft.azure.management.resources.fluentcore.arm.models.implementation.GroupableParentResourceImpl; @@ -38,6 +40,7 @@ import com.microsoft.azure.management.storage.StorageAccount; import com.microsoft.azure.management.storage.implementation.StorageManager; import rx.Observable; +import rx.functions.Func1; import java.io.IOException; import java.util.ArrayList; @@ -64,10 +67,12 @@ public class VirtualMachineScaleSetImpl private final VirtualMachineScaleSetsInner client; private final StorageManager storageManager; private final NetworkManager networkManager; - // the name of the virtual machine scale set - private final String scaleSetName; // used to generate unique name for any dependency resources private final ResourceNamer namer; + // the name of the virtual machine scale set + private final String scaleSetName; + // Name of the container to store virtual machines disks + private String vhdContainerName; private boolean isMarketplaceLinuxImage = false; // reference to the primary internet facing load balancer private LoadBalancer primaryInternetFacingLoadBalancer; @@ -81,10 +86,10 @@ public class VirtualMachineScaleSetImpl private String existingSubnetNameToAssociate; // unique key of a creatable storage accounts to be used for virtual machines child resources that // requires storage [OS disk] - private List creatableStorageAccountKeys; + private List creatableStorageAccountKeys = new ArrayList<>(); // reference to an existing storage account to be used for virtual machines child resources that // requires storage [OS disk] - private List existingStorageAccountsToAssociate; + private List existingStorageAccountsToAssociate = new ArrayList<>(); private Map extensions; private PagedListConverter skuConverter; // Load balancer specific variables used during update @@ -93,14 +98,14 @@ public class VirtualMachineScaleSetImpl private boolean removePrimaryInternalLoadBalancerOnUpdate; private LoadBalancer primaryInternetFacingLoadBalancerToAttachOnUpdate; private LoadBalancer primaryInternalLoadBalancerToAttachOnUpdate; - private List primaryInternetFacingLBBackendsToRemoveOnUpdate; - private List primaryInternetFacingLBInboundNatPoolsToRemoveOnUpdate; - private List primaryInternalLBBackendsToRemoveOnUpdate; - private List primaryInternalLBInboundNatPoolsToRemoveOnUpdate; - private List primaryInternetFacingLBBackendsToAddOnUpdate; - private List primaryInternetFacingLBInboundNatPoolsToAddOnUpdate; - private List primaryInternalLBBackendsToAddOnUpdate; - private List primaryInternalLBInboundNatPoolsToAddOnUpdate; + private List primaryInternetFacingLBBackendsToRemoveOnUpdate = new ArrayList<>(); + private List primaryInternetFacingLBInboundNatPoolsToRemoveOnUpdate = new ArrayList<>(); + private List primaryInternalLBBackendsToRemoveOnUpdate = new ArrayList<>(); + private List primaryInternalLBInboundNatPoolsToRemoveOnUpdate = new ArrayList<>(); + private List primaryInternetFacingLBBackendsToAddOnUpdate = new ArrayList<>(); + private List primaryInternetFacingLBInboundNatPoolsToAddOnUpdate = new ArrayList<>(); + private List primaryInternalLBBackendsToAddOnUpdate = new ArrayList<>(); + private List primaryInternalLBInboundNatPoolsToAddOnUpdate = new ArrayList<>(); VirtualMachineScaleSetImpl(String name, @@ -115,10 +120,6 @@ public class VirtualMachineScaleSetImpl this.networkManager = networkManager; this.scaleSetName = name; this.namer = new ResourceNamer(this.scaleSetName); - - this.creatableStorageAccountKeys = new ArrayList<>(); - this.existingStorageAccountsToAssociate = new ArrayList<>(); - resetLoadBalancerStateVariables(); this.skuConverter = new PagedListConverter() { @Override public VirtualMachineScaleSetSku typeConvert(VirtualMachineScaleSetSkuInner inner) { @@ -127,6 +128,18 @@ public VirtualMachineScaleSetSku typeConvert(VirtualMachineScaleSetSkuInner inne }; } + @Override + protected void initializeChildrenFromInner() { + this.extensions = new HashMap<>(); + if (this.inner().virtualMachineProfile().extensionProfile() != null) { + if (this.inner().virtualMachineProfile().extensionProfile().extensions() != null) { + for (VirtualMachineScaleSetExtensionInner inner : this.inner().virtualMachineProfile().extensionProfile().extensions()) { + this.extensions.put(inner.name(), new VirtualMachineScaleSetExtensionImpl(inner, this)); + } + } + } + } + @Override public PagedList availableSkus() throws CloudException, IOException { return this.skuConverter.convert(this.client.listSkus(this.resourceGroupName(), this.name())); @@ -147,6 +160,11 @@ public CachingTypes osDiskCachingType() { return this.inner().virtualMachineProfile().storageProfile().osDisk().caching(); } + @Override + public String osDiskName() { + return this.inner().virtualMachineProfile().storageProfile().osDisk().name(); + } + @Override public UpgradePolicy upgradePolicy() { return this.inner().upgradePolicy(); @@ -229,7 +247,8 @@ public Map extensions() { @Override public VirtualMachineScaleSetImpl withSku(VirtualMachineScaleSetSkuTypes skuType) { - this.inner().withSku(skuType.sku()); + this.inner() + .withSku(skuType.sku()); return this; } @@ -288,7 +307,7 @@ public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancer(LoadBala } @Override - public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(String... backendNames) { + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackends(String... backendNames) { if (this.isInCreateMode()) { VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); removeAllBackendAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancer, defaultPrimaryIpConfig); @@ -302,7 +321,7 @@ public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerBackend(S } @Override - public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNatPool(String... natPoolNames) { + public VirtualMachineScaleSetImpl withPrimaryInternetFacingLoadBalancerInboundNatPools(String... natPoolNames) { if (this.isInCreateMode()) { VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); removeAllInboundNatPoolAssociationFromIpConfiguration(this.primaryInternetFacingLoadBalancer, @@ -332,7 +351,7 @@ public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancer(LoadBalancer l } @Override - public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String... backendNames) { + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackends(String... backendNames) { if (this.isInCreateMode()) { VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = primaryNicDefaultIPConfiguration(); this.removeAllBackendAssociationFromIpConfiguration(this.primaryInternalLoadBalancer, @@ -347,7 +366,7 @@ public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerBackend(String. } @Override - public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPool(String... natPoolNames) { + public VirtualMachineScaleSetImpl withPrimaryInternalLoadBalancerInboundNatPools(String... natPoolNames) { if (this.isInCreateMode()) { VirtualMachineScaleSetIPConfigurationInner defaultPrimaryIpConfig = this.primaryNicDefaultIPConfiguration(); this.removeAllInboundNatPoolAssociationFromIpConfiguration(this.primaryInternalLoadBalancer, @@ -492,7 +511,8 @@ public VirtualMachineScaleSetImpl withStoredLinuxImage(String imageUrl) { @Override public VirtualMachineScaleSetImpl withAdminUserName(String adminUserName) { - this.inner().virtualMachineProfile() + this.inner() + .virtualMachineProfile() .osProfile() .withAdminPassword(adminUserName); return this; @@ -578,9 +598,18 @@ public VirtualMachineScaleSetImpl withOsDiskCaching(CachingTypes cachingType) { return this; } + @Override + public VirtualMachineScaleSetImpl withOsDiskName(String name) { + this.inner() + .virtualMachineProfile() + .storageProfile().osDisk().withName(name); + return this; + } + @Override public VirtualMachineScaleSetImpl withOverProvision(boolean enabled) { - this.inner().withOverProvision(enabled); + this.inner() + .withOverProvision(enabled); return this; } @@ -596,7 +625,8 @@ public VirtualMachineScaleSetImpl withOverProvisionDisabled() { @Override public VirtualMachineScaleSetImpl withCapacity(long capacity) { - this.inner().sku().withCapacity(capacity); + this.inner() + .sku().withCapacity(capacity); return this; } @@ -633,32 +663,15 @@ public VirtualMachineScaleSetExtensionImpl defineNewExtension(String name) { return new VirtualMachineScaleSetExtensionImpl(new VirtualMachineScaleSetExtensionInner(), this); } - @Override - public Observable applyAsync() { - this.handlePrimaryIpConfigurationOnUpdate(); - return null; - } - - @Override - protected Observable createInner() { - return null; - } - - @Override - protected void initializeChildrenFromInner() { - this.extensions = new HashMap<>(); - if (this.inner().virtualMachineProfile().extensionProfile() != null) { - if (this.inner().virtualMachineProfile().extensionProfile().extensions() != null) { - for (VirtualMachineScaleSetExtensionInner inner : this.inner().virtualMachineProfile().extensionProfile().extensions()) { - this.extensions.put(inner.name(), new VirtualMachineScaleSetExtensionImpl(inner, this)); - } - } - } + protected VirtualMachineScaleSetImpl withExtension(VirtualMachineScaleSetExtensionImpl extension) { + this.extensions.put(extension.name(), extension); + return this; } + // Create Update specific methods + // @Override protected void beforeCreating() { - // Reset and update extensions if (this.extensions.size() > 0) { this.inner() .virtualMachineProfile() @@ -669,140 +682,185 @@ protected void beforeCreating() { } @Override - protected void afterCreating() { + protected Observable createInner() { + this.setOSDiskAndOSProfileDefaults(); + this.setPrimaryIpConfigurationSubnet(); + this.setPrimaryIpConfigurationBackendsAndInboundNatPools(); + return this.handleOSDiskContainersAsync() + .flatMap(new Func1>() { + @Override + public Observable call(Void aVoid) { + return client.createOrUpdateAsync(resourceGroupName(), scaleSetName, inner()); + } + }); + } + @Override + protected void afterCreating() { + this.clearCachedProperties(); + this.initializeChildrenFromInner(); } @Override - public Observable createResourceAsync() { - return null; + public Observable applyAsync() { + return this.createAsync(); } @Override public VirtualMachineScaleSetImpl refresh() throws Exception { VirtualMachineScaleSetInner inner = this.client.get(this.resourceGroupName(), this.name()); this.setInner(inner); - this.refreshPrimaryLoadBalancersCache(); - return this; - } - - VirtualMachineScaleSetImpl withExtension(VirtualMachineScaleSetExtensionImpl extension) { - this.extensions.put(extension.name(), extension); + this.clearCachedProperties(); + this.initializeChildrenFromInner(); return this; } // Helpers // - private void refreshPrimaryLoadBalancersCache() throws IOException { - this.primaryInternetFacingLoadBalancer = null; - this.primaryInternalLoadBalancer = null; - this.loadCurrentPrimaryLoadBalancersIfAvailable(); + private boolean isInUpdateMode() { + return !this.isInCreateMode(); } - private void loadCurrentPrimaryLoadBalancersIfAvailable() throws IOException { - if (this.primaryInternetFacingLoadBalancer != null && this.primaryInternalLoadBalancer != null) { + private void setOSDiskAndOSProfileDefaults() { + if (isInUpdateMode()) { return; } - String firstLoadBalancerId = null; - VirtualMachineScaleSetIPConfigurationInner ipConfig = primaryNicDefaultIPConfiguration(); - if (!ipConfig.loadBalancerBackendAddressPools().isEmpty()) { - firstLoadBalancerId = ResourceUtils - .parentResourcePathFromResourceId(ipConfig.loadBalancerBackendAddressPools().get(0).id()); + VirtualMachineScaleSetOSProfile osProfile = this.inner() + .virtualMachineProfile() + .osProfile(); + // linux image: Custom or marketplace linux image + if (this.osType() == OperatingSystemTypes.LINUX || this.isMarketplaceLinuxImage) { + if (osProfile.linuxConfiguration() == null) { + osProfile.withLinuxConfiguration(new LinuxConfiguration()); + } + osProfile + .linuxConfiguration() + .withDisablePasswordAuthentication(osProfile.adminPassword() == null); } - if (firstLoadBalancerId == null && !ipConfig.loadBalancerInboundNatPools().isEmpty()) { - firstLoadBalancerId = ResourceUtils - .parentResourcePathFromResourceId(ipConfig.loadBalancerInboundNatPools().get(0).id()); + if (this.osDiskCachingType() == null) { + withOsDiskCaching(CachingTypes.READ_WRITE); } - if (firstLoadBalancerId == null) { - return; + if (this.osDiskName() == null) { + withOsDiskName(this.scaleSetName + "-os-disk"); } - LoadBalancer loadBalancer1 = this.networkManager - .loadBalancers() - .getById(firstLoadBalancerId); - if (loadBalancer1.publicIpAddressIds() != null && loadBalancer1.publicIpAddressIds().size() > 0) { - this.primaryInternetFacingLoadBalancer = loadBalancer1; - } else { - this.primaryInternalLoadBalancer = loadBalancer1; + if (osProfile.computerNamePrefix() == null) { + // VM name cannot contain only numeric values and cannot exceed 15 chars + if (this.scaleSetName.matches("[0-9]+")) { + osProfile.withComputerNamePrefix(ResourceNamer.randomResourceName("vmss-vm", 12)); + } else if (this.scaleSetName.length() <= 12) { + osProfile.withComputerNamePrefix(this.scaleSetName + "-vm"); + } else { + osProfile.withComputerNamePrefix(ResourceNamer.randomResourceName("vmss-vm", 12)); + } } + } - String secondLoadBalancerId = null; - for (SubResource subResource: ipConfig.loadBalancerBackendAddressPools()) { - secondLoadBalancerId = ResourceUtils - .parentResourcePathFromResourceId(subResource.id()); - break; + private boolean isCustomImage(VirtualMachineScaleSetStorageProfile storageProfile) { + return storageProfile.osDisk().image() == null + || storageProfile.osDisk().image().uri() == null + || storageProfile.osDisk().image().uri() == ""; + } + + private Observable handleOSDiskContainersAsync() { + final VirtualMachineScaleSetStorageProfile storageProfile = inner() + .virtualMachineProfile() + .storageProfile(); + if (isCustomImage(storageProfile)) { + // There is a restriction currently that virtual machine's disk cannot be stored in multiple storage accounts + // if scale set is based on custom image. Remove this check once azure start supporting it. + storageProfile.osDisk() + .vhdContainers() + .clear(); + return Observable.just(null); } - if (secondLoadBalancerId == null) { - for (SubResource subResource: ipConfig.loadBalancerInboundNatPools()) { - if (!subResource.id().toLowerCase().startsWith(firstLoadBalancerId.toLowerCase())) { - secondLoadBalancerId = ResourceUtils - .parentResourcePathFromResourceId(subResource.id()); + if (this.isInCreateMode() + && this.creatableStorageAccountKeys.isEmpty() + && this.existingStorageAccountsToAssociate.isEmpty()) { + return this.storageManager.storageAccounts() + .define(this.namer.randomName("stg", 24)) + .withRegion(this.regionName()) + .withExistingResourceGroup(this.resourceGroupName()) + .createAsync() + .map(new Func1() { + @Override + public Void call(StorageAccount storageAccount) { + String containerName = vhdContainerName; + if (containerName == null) { + containerName = "vhds"; + } + storageProfile.osDisk() + .vhdContainers() + .add(storageAccount.endPoints().primary().blob() + "/" + containerName); + vhdContainerName = null; + return null; + } + }); + } else { + String containerName = this.vhdContainerName; + if (containerName == null) { + for (String containerUrl : storageProfile.osDisk().vhdContainers()) { + containerName = containerUrl.substring(containerUrl.lastIndexOf("/") + 1); break; } } - } - if (secondLoadBalancerId == null) { - return; - } - - LoadBalancer loadBalancer2 = this.networkManager - .loadBalancers() - .getById(secondLoadBalancerId); - if (loadBalancer2.publicIpAddressIds() != null && loadBalancer2.publicIpAddressIds().size() > 0) { - this.primaryInternetFacingLoadBalancer = loadBalancer2; - } else { - this.primaryInternalLoadBalancer = loadBalancer2; - } - } + if (containerName == null) { + containerName = "vhds"; + } - private VirtualMachineScaleSetIPConfigurationInner primaryNicDefaultIPConfiguration() { - List nicConfigurations = this.inner() - .virtualMachineProfile() - .networkProfile() - .networkInterfaceConfigurations(); + for (String storageAccountKey : this.creatableStorageAccountKeys) { + StorageAccount storageAccount = (StorageAccount) createdResource(storageAccountKey); + storageProfile.osDisk() + .vhdContainers() + .add(storageAccount.endPoints().primary().blob() + "/" + containerName); + } - for (VirtualMachineScaleSetNetworkConfigurationInner nicConfiguration : nicConfigurations) { - if (nicConfiguration.primary()) { - if (nicConfiguration.ipConfigurations().size() > 0) { - VirtualMachineScaleSetIPConfigurationInner ipConfig = nicConfiguration.ipConfigurations().get(0); - if (ipConfig.loadBalancerBackendAddressPools() == null) { - ipConfig.withLoadBalancerBackendAddressPools(new ArrayList()); - } - if (ipConfig.loadBalancerInboundNatPools() == null) { - ipConfig.withLoadBalancerInboundNatPools(new ArrayList()); - } - } + for (StorageAccount storageAccount : this.existingStorageAccountsToAssociate) { + storageProfile.osDisk() + .vhdContainers() + .add(storageAccount.endPoints().primary().blob() + "/" + containerName); } + + this.vhdContainerName = null; + this.creatableStorageAccountKeys.clear(); + this.existingStorageAccountsToAssociate.clear(); + return Observable.just(null); } - throw new RuntimeException("Could not find the primary nic configuration or an IP configuration in it"); } - private boolean isInUpdateMode() { - return !this.isInCreateMode(); - } + private void setPrimaryIpConfigurationSubnet() { + if (isInUpdateMode()) { + return; + } - private void resetLoadBalancerStateVariables() { - this.removePrimaryInternetFacingLoadBalancerOnUpdate = false; - this.removePrimaryInternalLoadBalancerOnUpdate = false; - this.primaryInternetFacingLoadBalancerToAttachOnUpdate = null; - this.primaryInternalLoadBalancerToAttachOnUpdate = null; - this.primaryInternetFacingLBBackendsToRemoveOnUpdate = new ArrayList<>(); - this.primaryInternetFacingLBInboundNatPoolsToRemoveOnUpdate = new ArrayList<>(); - this.primaryInternalLBBackendsToRemoveOnUpdate = new ArrayList<>(); - this.primaryInternalLBInboundNatPoolsToRemoveOnUpdate = new ArrayList<>(); - this.primaryInternetFacingLBBackendsToAddOnUpdate = new ArrayList<>(); - this.primaryInternetFacingLBInboundNatPoolsToAddOnUpdate = new ArrayList<>(); - this.primaryInternalLBBackendsToAddOnUpdate = new ArrayList<>(); - this.primaryInternalLBInboundNatPoolsToAddOnUpdate = new ArrayList<>(); + VirtualMachineScaleSetIPConfigurationInner ipConfig = this.primaryNicDefaultIPConfiguration(); + if (this.creatablePrimaryNetworkKey != null) { + Network primaryNetwork = (Network) this.createdResource(this.creatablePrimaryNetworkKey); + for (Subnet subnet : primaryNetwork.subnets().values()) { + ipConfig.withSubnet(new ApiEntityReference().withId(subnet.inner().id())); + } + } else if (this.existingPrimaryNetworkToAssociate != null) { + ipConfig.withSubnet(new ApiEntityReference().withId(this.existingPrimaryNetworkToAssociate.id() + + "/" + + "subnets" + + "/" + + existingSubnetNameToAssociate)); + } + this.creatablePrimaryNetworkKey = null; + this.existingPrimaryNetworkToAssociate = null; } - private void handlePrimaryIpConfigurationOnUpdate() { + private void setPrimaryIpConfigurationBackendsAndInboundNatPools() { + if (isInCreateMode()) { + return; + } + try { this.loadCurrentPrimaryLoadBalancersIfAvailable(); } catch (IOException ioException) { @@ -896,7 +954,107 @@ private void handlePrimaryIpConfigurationOnUpdate() { this.primaryInternalLBInboundNatPoolsToAddOnUpdate.toArray(new String[0])); } } - this.resetLoadBalancerStateVariables(); + + this.removePrimaryInternetFacingLoadBalancerOnUpdate = false; + this.removePrimaryInternalLoadBalancerOnUpdate = false; + this.primaryInternetFacingLoadBalancerToAttachOnUpdate = null; + this.primaryInternalLoadBalancerToAttachOnUpdate = null; + this.primaryInternetFacingLBBackendsToRemoveOnUpdate.clear(); + this.primaryInternetFacingLBInboundNatPoolsToRemoveOnUpdate.clear(); + this.primaryInternalLBBackendsToRemoveOnUpdate.clear(); + this.primaryInternalLBInboundNatPoolsToRemoveOnUpdate.clear(); + this.primaryInternetFacingLBBackendsToAddOnUpdate.clear(); + this.primaryInternetFacingLBInboundNatPoolsToAddOnUpdate.clear(); + this.primaryInternalLBBackendsToAddOnUpdate.clear(); + this.primaryInternalLBInboundNatPoolsToAddOnUpdate.clear(); + } + + private void clearCachedProperties() { + this.primaryInternetFacingLoadBalancer = null; + this.primaryInternalLoadBalancer = null; + } + + private void loadCurrentPrimaryLoadBalancersIfAvailable() throws IOException { + if (this.primaryInternetFacingLoadBalancer != null && this.primaryInternalLoadBalancer != null) { + return; + } + + String firstLoadBalancerId = null; + VirtualMachineScaleSetIPConfigurationInner ipConfig = primaryNicDefaultIPConfiguration(); + if (!ipConfig.loadBalancerBackendAddressPools().isEmpty()) { + firstLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(ipConfig.loadBalancerBackendAddressPools().get(0).id()); + } + + if (firstLoadBalancerId == null && !ipConfig.loadBalancerInboundNatPools().isEmpty()) { + firstLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(ipConfig.loadBalancerInboundNatPools().get(0).id()); + } + + if (firstLoadBalancerId == null) { + return; + } + + LoadBalancer loadBalancer1 = this.networkManager + .loadBalancers() + .getById(firstLoadBalancerId); + if (loadBalancer1.publicIpAddressIds() != null && loadBalancer1.publicIpAddressIds().size() > 0) { + this.primaryInternetFacingLoadBalancer = loadBalancer1; + } else { + this.primaryInternalLoadBalancer = loadBalancer1; + } + + String secondLoadBalancerId = null; + for (SubResource subResource: ipConfig.loadBalancerBackendAddressPools()) { + secondLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(subResource.id()); + break; + } + + if (secondLoadBalancerId == null) { + for (SubResource subResource: ipConfig.loadBalancerInboundNatPools()) { + if (!subResource.id().toLowerCase().startsWith(firstLoadBalancerId.toLowerCase())) { + secondLoadBalancerId = ResourceUtils + .parentResourcePathFromResourceId(subResource.id()); + break; + } + } + } + + if (secondLoadBalancerId == null) { + return; + } + + LoadBalancer loadBalancer2 = this.networkManager + .loadBalancers() + .getById(secondLoadBalancerId); + if (loadBalancer2.publicIpAddressIds() != null && loadBalancer2.publicIpAddressIds().size() > 0) { + this.primaryInternetFacingLoadBalancer = loadBalancer2; + } else { + this.primaryInternalLoadBalancer = loadBalancer2; + } + } + + private VirtualMachineScaleSetIPConfigurationInner primaryNicDefaultIPConfiguration() { + List nicConfigurations = this.inner() + .virtualMachineProfile() + .networkProfile() + .networkInterfaceConfigurations(); + + for (VirtualMachineScaleSetNetworkConfigurationInner nicConfiguration : nicConfigurations) { + if (nicConfiguration.primary()) { + if (nicConfiguration.ipConfigurations().size() > 0) { + VirtualMachineScaleSetIPConfigurationInner ipConfig = nicConfiguration.ipConfigurations().get(0); + if (ipConfig.loadBalancerBackendAddressPools() == null) { + ipConfig.withLoadBalancerBackendAddressPools(new ArrayList()); + } + if (ipConfig.loadBalancerInboundNatPools() == null) { + ipConfig.withLoadBalancerInboundNatPools(new ArrayList()); + } + } + } + } + throw new RuntimeException("Could not find the primary nic configuration or an IP configuration in it"); } private static void associateBackEndsToIpConfiguration(String loadBalancerId, @@ -951,7 +1109,7 @@ private static Map getBackendsAssociatedWithIpConfiguration(Loa Map attachedBackends = new HashMap<>(); Map lbBackends = loadBalancer.backends(); for (Backend lbBackend : lbBackends.values()) { - String backendId = loadBalancerId + "backendAddressPools" + "/" + lbBackend.name(); + String backendId = loadBalancerId + "/" + "backendAddressPools" + "/" + lbBackend.name(); for (SubResource subResource : ipConfig.loadBalancerBackendAddressPools()) { if (subResource.id().equalsIgnoreCase(backendId)) { attachedBackends.put(lbBackend.name(), lbBackend); @@ -967,7 +1125,7 @@ private static Map getInboundNatPoolsAssociatedWithIpCon Map attachedInboundNatPools = new HashMap<>(); Map lbInboundNatPools = loadBalancer.inboundNatPools(); for (InboundNatPool lbInboundNatPool : lbInboundNatPools.values()) { - String inboundNatPoolId = loadBalancerId + "inboundNatPools" + "/" + lbInboundNatPool.name(); + String inboundNatPoolId = loadBalancerId + "/" + "inboundNatPools" + "/" + lbInboundNatPool.name(); for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { if (subResource.id().equalsIgnoreCase(inboundNatPoolId)) { attachedInboundNatPools.put(lbInboundNatPool.name(), lbInboundNatPool); @@ -1028,16 +1186,16 @@ private static void removeAllBackendAssociationFromIpConfiguration(LoadBalancer private static void removeAllInboundNatPoolAssociationFromIpConfiguration(LoadBalancer loadBalancer, VirtualMachineScaleSetIPConfigurationInner ipConfig) { - List toRemoveIndicies = new ArrayList<>(); + List toRemoveIndices = new ArrayList<>(); int i = 0; for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { if (subResource.id().toLowerCase().startsWith(loadBalancer.id().toLowerCase() + "/")) { - toRemoveIndicies.add(i); + toRemoveIndices.add(i); } i++; } - for (Integer index : toRemoveIndicies) { + for (Integer index : toRemoveIndices) { ipConfig.loadBalancerInboundNatPools().remove(index); } } @@ -1045,20 +1203,20 @@ private static void removeAllInboundNatPoolAssociationFromIpConfiguration(LoadBa private static void removeBackendsFromIpConfiguration(String loadBalancerId, VirtualMachineScaleSetIPConfigurationInner ipConfig, String... backendNames) { - List indices = new ArrayList<>(); + List toRemoveIndices = new ArrayList<>(); for (String backendName : backendNames) { String backendPoolId = loadBalancerId + "/" + "backendAddressPools" + "/" + backendName; int index = -1; for (SubResource subResource : ipConfig.loadBalancerBackendAddressPools()) { index++; if (subResource.id().equalsIgnoreCase(backendPoolId)) { - indices.add(index); + toRemoveIndices.add(index); break; } } } - for (Integer index : indices) { + for (Integer index : toRemoveIndices) { ipConfig.loadBalancerBackendAddressPools().remove(index); } } @@ -1066,20 +1224,20 @@ private static void removeBackendsFromIpConfiguration(String loadBalancerId, private static void removeInboundNatPoolsFromIpConfiguration(String loadBalancerId, VirtualMachineScaleSetIPConfigurationInner ipConfig, String... inboundNatPoolNames) { - List indices = new ArrayList<>(); + List toRemoveIndices = new ArrayList<>(); for (String natPoolName : inboundNatPoolNames) { String inboundNatPoolId = loadBalancerId + "/" + "inboundNatPools" + "/" + natPoolName; int index = -1; for (SubResource subResource : ipConfig.loadBalancerInboundNatPools()) { index++; if (subResource.id().equalsIgnoreCase(inboundNatPoolId)) { - indices.add(index); + toRemoveIndices.add(index); break; } } } - for (Integer index : indices) { + for (Integer index : toRemoveIndices) { ipConfig.loadBalancerInboundNatPools().remove(index); } } diff --git a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java index 5ad3006099bd..eb7bfb2bd4f9 100644 --- a/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java +++ b/azure-mgmt-compute/src/main/java/com/microsoft/azure/management/compute/implementation/VirtualMachineScaleSetsImpl.java @@ -69,7 +69,7 @@ protected VirtualMachineScaleSetImpl wrapModel(String name) { VirtualMachineScaleSetNetworkConfigurationInner primaryNetworkInterfaceConfiguration = new VirtualMachineScaleSetNetworkConfigurationInner() .withPrimary(true) - .withName("default") + .withName("primary-nic-cfg") .withIpConfigurations(new ArrayList()); primaryNetworkInterfaceConfiguration .ipConfigurations() diff --git a/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java index 1978b05defad..db62c21beaf1 100644 --- a/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java +++ b/azure-mgmt-compute/src/test/java/com/microsoft/azure/management/compute/VirtualMachineScaleSetOperationsTests.java @@ -34,29 +34,6 @@ public void canCreateVirtualMachineScaleSet() throws Exception { .define(RG_NAME) .withRegion(LOCATION); - /** - LoadBalancer lb = this.networkManager - .loadBalancers() - .define("foo") - .withRegion(LOCATION) - .withNewResourceGroup(resourceGroupCreatable) - .definePublicFrontend("primary") - .withExistingPublicIpAddress("abc") - .attach() - .defineBackend("aa") - .attach() - .defineHttpProbe("httpProbe") - .withRequestPath("/") - .attach() - .defineLoadBalancingRule("") - .withProtocol(TransportProtocol.TCP) - .withFrontend("primary") - .withFrontendPort(80) - .withProbe("httpProbe") - .withBackend("aa") - .attach() - **/ - VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets() .define(VMSCALESETNAME) .withRegion(LOCATION)