Java: Manage availability zones and more

Data di pubblicazione: 5 ottobre, 2017

Principal Program Manager, Azure Developer Experience

We released 1.3 of the Azure Management Libraries for Java. This release adds support for availability zones (in preview).

https://github.com/Azure/azure-sdk-for-java

Getting started

Add the following dependency fragment to your Maven POM file to use 1.3 version of the libraries:

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.3.0</version>
</dependency>

Create virtual machine in availability zone

You can create a virtual machine instance in an availability zone by using a define() … create() method chain.

VirtualMachine = azure.virtualMachines()
    .define("Linux-VM-in-Availability-Zone")
    .withRegion(Region.US_EAST2)
    .withNewResourceGroup(rgName)
    .withNewPrimaryNetwork("10.0.0.0/28")
    .withPrimaryPrivateIPAddressDynamic()
    .withNewPrimaryPublicIPAddress("linux-vm-with-high-availability")
    .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
    .withRootUsername("tirekicker")
    .withSsh(sshKey)
    // Optional
    .withAvailabilityZone(AvailabilityZoneId.ZONE_1)
    .withSize(VirtualMachineSizeTypes.STANDARD_D3_V2)
    // Create VM
    .create();

Create virtual machine scale set in availability zone

You can create a virtual machine scale set instance in multiple availability zones by using another define() … create() method chain.

VirtualMachineScaleSet virtualMachineScaleSet1 = azure.virtualMachineScaleSets()
    .define("VM-Scale-Set-in-Availability-Zone")
    .withRegion(Region.US_EAST2)
    .withExistingResourceGroup(rgName)
    .withSku(VirtualMachineScaleSetSkuTypes.STANDARD_D3_V2)
    .withExistingPrimaryNetworkSubnet(network, "front-end")
    .withExistingPrimaryInternetFacingLoadBalancer(loadBalancer)
    .withPrimaryInternetFacingLoadBalancerBackends(backends.get(0))
    .withPrimaryInternetFacingLoadBalancerInboundNatPools(natpools.get(0))
    .withoutPrimaryInternalLoadBalancer()
    .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
    .withRootUsername("tirekicker")
    .withSsh(sshKey)
    // Optional 
    .withAvailabilityZone(AvailabilityZoneId.ZONE_1)
    // Create VM
        .create();

Support for more Azure services

1.3 also adds support for Network Peering, Virtual Network Gateway and Azure Container Instances.

Peer two virtual networks

You can peer two virtual networks using another define() … create() method chain.

NetworkPeering peeringAB = networkA.peerings()
   .define(peeringABName)
   .withRemoteNetwork(networkB)
   .create();

You can use network watcher to check connectivity between virtual machines in peered virtual networks.

Create Virtual Private Network (VPN) using site-to-site connection

You can create a virtual private network site-to-site connection using another define() … create() method chain.

virtualNetworkGateway.connections()
    .define("My-Connection")
    .withSiteToSite()
    .withLocalNetworkGateway(localNetworkGateway)
    .withSharedKey("MySecretKey")
    .create();

You can create a virtual network to virtual network VPN connection.

Create container group in Azure Container Instances

You can create a container group with multiple container instances using another define() … create() method chain.

ContainerGroup containerGroup = azure.containerGroups().define("azureContainerInstance")
    .withRegion(Region.US_WEST)
    .withNewResourceGroup(rgName)
    .withLinux()
    .withPublicImageRegistryOnly()
    .withoutVolume()
    .defineContainerInstance(aciName + "-1")
        .withImage(containerImageName1)
        .withExternalTcpPort(80)
        .withCpuCoreCount(.5)
        .withMemorySizeInGB(.75)
        .attach()
    .defineContainerInstance(aciName + "-2")
        .withImage(containerImageName2)
        .withoutPorts()
        .withCpuCoreCount(.5)
        .withMemorySizeInGB(.75)
        .attach()
    .create();

Similarly, you can:

Try it

You can get more samples from our GitHub repo. Give it a try and let us know what you think (via e-mail or comments below).
 
You can find plenty of additional info about Java on Azure at http://azure.com/java.