2

How to get image that is captured from an azure VM created using ARM so that I can use it as a base image for all my subsequent VM creations with azure java sdk?

Will Shao - MSFT
  • 1,189
  • 7
  • 14
Dat
  • 73
  • 5

2 Answers2

1

There is an offical blog can help you getting start with Azure Java SDK for Service Manage. Please refer to https://azure.microsoft.com/en-us/blog/getting-started-with-the-azure-java-management-libraries/.

For implementing this needs, you need to add some maven packages into your Java project. Please see the dependencies below.

<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-svc-mgmt</artifactId>
    <version>0.9.0</version>
</dependency>
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure-svc-mgmt-compute</artifactId>
    <version>0.9.0</version>
</dependency>

You can modify some code below to implement for listing custom images instead of the code for the section Calling the Azure API to Get a List of Regions at the blog.

VirtualMachineVMImageOperations virtualMachineVMImageOperations = client.getVirtualMachineVMImagesOperations();
VirtualMachineVMImageListResponse virtualMachineVMImageListResponse = virtualMachineVMImageOperations.list();
List<VirtualMachineVMImage> list = virtualMachineVMImageListResponse.getVMImages();
for(VirtualMachineVMImage virtualMachineVMImage: list) {
    String vmImageName = virtualMachineVMImage.getName();
    System.out.println(vmImageName);
}
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • HI @Peter Pan - MSFT. Thanks for the reply.. But my question is to get the images using ARM.. the above example is using Service Management API... not Resource Management API.. – Dat Dec 15 '15 at 08:13
  • @Sam You can list images using ASM, not ARM. Please refer to my answer for the thread http://stackoverflow.com/questions/33996642/use-microsoft-azure-java-sdk-resource-management-api-or-service-management-api. – Peter Pan Dec 15 '15 at 13:12
0

Recently azure has released Java SDK 1.0.0 as LTS version.

Please refer below code to create a vm using custom image..

       VirtualMachineCustomImage customImage = azure.virtualMachineCustomImages().getByResourceGroup("resource_gr_name", "image_name");

       Creatable<VirtualMachine> linuxVM = azure.virtualMachines().define(vmName)
               .withRegion(Region.US_WEST)
               .withExistingResourceGroup("rishi")
               .withExistingPrimaryNetwork(network)
               .withSubnet("default") // Referencing the default subnet name when no name specified at creation
               .withPrimaryPrivateIPAddressDynamic()
               .withoutPrimaryPublicIPAddress()
               .withLinuxCustomImage(customImage.id())
               .withRootUsername("centos")
               .withRootPassword("mdfxrJ68")
               .withNewDataDisk(19)
               .withDataDiskDefaultCachingType(CachingTypes.READ_WRITE)
               .withDataDiskDefaultStorageAccountType(StorageAccountTypes.PREMIUM_LRS)
               .withExistingStorageAccount(storageAccount)
               .withOSDiskSizeInGB(10)
               .withExistingStorageAccount(storageAccount)
               .withSize(VirtualMachineSizeTypes.STANDARD_DS1_V2);


        azure.virtualMachines().create(linuxVM);
Rishi Anand
  • 280
  • 1
  • 4
  • 15