创建虚拟机部署Azure REST API

时间:2016-11-11 02:08:09

标签: rest api azure

        String cloudServiceDeployment = "imagevmtest";
        url = String.format("https://management.core.windows.net/%s/services/hostedservices/%s/deployments", subscriptionId, cloudServiceDeployment);
        String requestBody2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Deployment xmlns=\"http://schemas.microsoft.com/windowsazure\"><Name>190bed4a</Name><DeploymentSlot>Production</DeploymentSlot><Label>190bed4a</Label><RoleList><Role><RoleName>SuseOpenVm_rolec8fc</RoleName><RoleType>PersistentVMRole</RoleType><ConfigurationSets><ConfigurationSet><ConfigurationSetType>LinuxProvisioningConfiguration</ConfigurationSetType><HostName>SuseOpenVm_rolec8fc</HostName><UserName>anandsrinivasan</UserName><UserPassword>Cloud360</UserPassword><DisableSshPasswordAuthentication>false</DisableSshPasswordAuthentication></ConfigurationSet><ConfigurationSet><ConfigurationSetType>NetworkConfiguration</ConfigurationSetType><DisableSshPasswordAuthentication>false</DisableSshPasswordAuthentication><InputEndpoints><InputEndpoint><LocalPort>22</LocalPort><Name>SSH</Name><Port>22</Port><Protocol>TCP</Protocol></InputEndpoint></InputEndpoints></ConfigurationSet></ConfigurationSets><OSVirtualHardDisk><MediaLink>https://2xportalvhds7jsf9ncknn4s.blob.core.windows.net/vhds/imagevmtest-imagevmtest-2016-11-10.vhd</MediaLink><SourceImageName>imagevmtest-imagevmtest-2016-11-10.vhd</SourceImageName></OSVirtualHardDisk><RoleSize>Small</RoleSize></Role></RoleList><VirtualNetworkName>anand360NW</VirtualNetworkName></Deployment>";

        int deployResponseCode = processPostRequest(new URL(url),requestBody2.getBytes(), "application/xml", keyStorePath, keyStorePassword);
        System.out.println(deployResponseCode);

我正在使用上面的代码并尝试在Azure中创建虚拟机。但仍然得到错误400.你们可以指导我如何通过REST API在我的Azure帐户中创建一个虚拟机吗?

1 个答案:

答案 0 :(得分:0)

现在这是您问题的直接解决方案,但我会提供一些可能更适合您的方案的替代方案。

必须处理生成整个XML非常困难且容易出错。 REST API与Azure上的级别一样低。我的建议是尝试一些构建在REST API顶部的API /工具,如PowerShellAzure CLI

如果您使用.NET,最好的选择是使用Management Libraries for .NET。最初的管理库使用起来有点麻烦,但是使用流畅的API有一个新的和改进的版本,从我到目前为止看到的,它真棒!以下是介绍性博客文章 - https://azure.microsoft.com/en-us/blog/simpler-azure-management-libraries-for-net/

总而言之,我认为最好是省去使用原始REST API并使用一些包装器的麻烦。

让我知道你的想法。

相关问题