Reputation: 65
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);
I am using the above code and trying to create a virtual machine in my Azure. But still getting error 400. Can you guys guide me how to create a vm in my Azure account via REST API?
Upvotes: 0
Views: 800
Reputation: 1481
This is now a direct anwer to your question but I will offer some alternatives that might be better for your scenario.
Having to deal with generating the whole XML is quite difficult and error prone. The REST API is as low level as it gets on Azure. My suggestion is to try some of the API/Tools that are built ontop of the REST API like PowerShell and Azure CLI.
The best option, if you are using .NET, is to use Management Libraries for .NET. The original Management Libraries are a bit cumbersome to use but there is a new and improved version in the works using fluent API and from what I have seen so far, it is awesome! Here is the introductory blog post - https://azure.microsoft.com/en-us/blog/simpler-azure-management-libraries-for-net/
To summarize, I think that it is best if you could spare yourself the trouble of working with the raw REST API and use some of the wrappers instead.
Let me know that you think.
Upvotes: 1