Deploying Azure VM in Avilability Zone with Java SDK not working

98 views Asked by At

I have been trying to deploy an Azure VM to an availability zone like the following link https://azure.microsoft.com/en-us/blog/java-manage-availability-zones-and-more/, but I keep on getting the following error.

cannot find symbol 
symbol:   method withAvailabilityZone(AvailabilityZoneId)
  location: interface WithCreate

It seems as if Java can't find a withAvailabilityZone method, but in the link above it seems to work fine. When I look in the Azure documentation, the only withAvailabilityZone method is in the withManagedCreate class, so I'm not sure how to alter the following code to match that:

VM_1 = azure.virtualMachines().define(name)
                    .withRegion(reg)
                    .withExistingResourceGroup(rg)
                    .withExistingPrimaryNetworkInterface(nI)
                    .withPopularLinuxImage(pli) 
                    .withExistingDataDisk(dd)
                    .withSize(type_1)
                    .withPriority(priority_var)
                    .withAvailabilityZone(availabilityZone) //error occurs here
                    .create();
1

There are 1 answers

0
Jim Xu On

If you want to create Azure VM in Avilability Zone with java, please refer to the following steps

  1. SDK
<dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-identity</artifactId>
      <version>1.2.4</version>
    </dependency>
    
    <dependency>
      <groupId>com.azure.resourcemanager</groupId>
      <artifactId>azure-resourcemanager</artifactId>
      <version>2.2.0</version>
    </dependency>

2.code

 String clientId="";
        String clientSecret="";
        String tenant="";
        String subId="";
        AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
        TokenCredential credential = new ClientSecretCredentialBuilder()
                .authorityHost(profile.getEnvironment().getActiveDirectoryEndpoint())
                .tenantId(tenant)
                .clientId(clientId)
                .clientSecret(clientSecret)
                .build();
        AzureResourceManager azureResourceManager = AzureResourceManager
                .configure()
                .withLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
                .authenticate(credential,profile)
                .withSubscription(subId);


        VirtualMachine virtualMachine1 = azureResourceManager.virtualMachines()
                .define(vmName1)
                .withRegion(region)
                .withNewResourceGroup(rgName)
                .withNewPrimaryNetwork("10.0.0.0/28")
                .withPrimaryPrivateIPAddressDynamic()
                .withNewPrimaryPublicIPAddress(pipName1)
                .withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS)
                .withRootUsername(userName)
                .withRootPassword(password)
                // Optional
                .withAvailabilityZone(AvailabilityZoneId.ZONE_1)
                .withSize(VirtualMachineSizeTypes.fromString("Standard_D2a_v4"))
                // Create VM
                .create();
        System.out.println("Created a zoned virtual machine: " + virtualMachine1.id());

enter image description here