Search code examples
javaazureazure-java-sdkazure-management

Azure Java: cannot add plan information while creating VM


Version: 1.1.0

I am creating a VM using a market place image. My code looks something like this

VirtualMachine linuxVM = azure.virtualMachines().define(name)
                .withRegion(Region.US_WEST)
                .withExistingResourceGroup(myRg)
                .withExistingPrimaryNetwork(network)
                .withSubnet("subnet1")
                .withPrimaryPrivateIPAddressDynamic()
                .withNewPrimaryPublicIPAddress("ip-" + name)
                .withLatestLinuxImage("publisher", "offer", "sku")
                .withRootUsername("root")
                .withRootPassword("some password")
                .withSize(VirtualMachineSizeTypes.BASIC_A0)
                .create();

I get an error as follows.

Async operation failed with provisioning state: Failed: Creating a virtual machine from Marketplace image requires Plan information in the request. OS disk name is '<name>'

How do I add plan information?


Solution

  • It looks like plan information can be added to the VM create, after spending some time with the source code. The following code works with 1.1.0.

    PurchasePlan plan = new PurchasePlan();
    plan.withName("name");
    plan.withPublisher("publisher");
    plan.withProduct("prodcut");
    
    VirtualMachine linuxVM = azure.virtualMachines().define(name)
                    .withRegion(Region.US_WEST)
                    .withExistingResourceGroup(myRg)
                    .withExistingPrimaryNetwork(network)
                    .withSubnet("subnet1")
                    .withPrimaryPrivateIPAddressDynamic()
                    .withNewPrimaryPublicIPAddress("ip-" + name)
                    .withLatestLinuxImage("publisher", "offer", "sku")
                    .withRootUsername("root")
                    .withRootPassword("some password")
                    .withSize(VirtualMachineSizeTypes.BASIC_A0)
                    .withPlan(plan)
                    .create();