2016-11-11 149 views
0
 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帐户中创建虚拟机吗?创建虚拟机部署Azure REST API

+0

我想问问你试图使用原始REST API创建Azure资源的目的是什么?你有没有尝试过一些替代品? –

+0

@MilanNankov我正在计划一些能够让用户在我的网站下创建帐户后自动在Azure上创建虚拟主机的功能。有什么选择?你能提供一些步骤吗?谢谢 –

回答

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工作的烦恼,并使用一些包装代替。

让我知道你的想法。

+0

好的,谢谢你的帮助。我会尽快回到这里。给我一些时间来开发和弄清楚。我很感激你可以在这里给我一些建议。 =) –

+0

我现在可以使用Azure CLI创建一个虚拟机。谢谢。现在我有另一个问题,vm是通过使用azure CLI手动创建的。是否有任何步骤使用Azure CLI自动创建虚拟机? –

+0

云,你对你最后的评论详细说明一下。 “自动”是什么意思?针对某些事件或?你提到过你的网站,所以你可能想在用户注册时创建虚拟机。您网站是ASP.NET应用程序还是您正在使用其他技术? –