如何使用azure java sdk列出自定义映像(从ARM VM捕获)

时间:2015-12-09 10:03:48

标签: java azure azure-java-sdk azureportal

如何获取从使用ARM创建的azure VM捕获的图像,以便我可以将其用作使用azure java sdk进行的所有后续VM创建的基本映像?

2 个答案:

答案 0 :(得分:1)

有一个官方博客可以帮助您开始使用Azure Java SDK for Service Manage。请参阅https://azure.microsoft.com/en-us/blog/getting-started-with-the-azure-java-management-libraries/

为了实现这一需求,您需要在Java项目中添加一些maven包。请参阅下面的依赖项。

<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>

您可以修改下面的一些代码来实现列出自定义图片,而不是博客中Calling the Azure API to Get a List of Regions部分的代码。

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);
}

答案 1 :(得分:0)

最近,azure发布了Java SDK 1.0.0作为LTS版本。

请参考下面的代码,使用自定义图片创建vm ..

       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);