Create a virtual machine using the Azure REST API
Before you create a virtual machine using the REST API, first make sure that the cloud service and storage account are already built into your azure subscription. If not, you can use the Azure Management page, or the PowerShell command-line tool, or the rest API to create a cloud service and storage account.
Https://msdn.microsoft.com/library/gg441304.aspx
https://azure.microsoft.com/zh-cn/documentation/articles/storage-create-storage-account/
It is also important to note that because Azure in the country operates independently, the API is called when the Https://management.core.windows.net in the MSDN code needs to be
Replace with:
https://management.core.chinacloudapi.cn/
You then need to install the certificate on the client host that invokes the API and upload it to azure Portal.
Install and upload the certificate.
The steps are as follows:
1 using the Visual Studio 2013 command line, enter the following command: (you need to replace <CertificateName> in the command with your own certificate name)
Makecert-sky exchange-r-N "cn=<certificatename>"-pe-a sha1-len 2048-ss My "<certificatename>.cer"
2 run Certmgr.msc on the client host, locate the "personal" Certificate in Certificate Manager, and locate the certificate that you just created. Right-click the certificate, select Export, in the Export Certificate Wizard after, select "Do not export private key", and select Der/A in the certificate format.
3 Open the Azure Management portal page, find "settings" in the left navigation menu, click "Manage Certificates" in the Settings menu, and select upload the certificate with no private key that you just exported. The upload will see this certificate on the portal and the subscription ID and fingerprint are also listed. The subscription ID and fingerprint will be used in the following code.
The work of the certificate upload has been completed, and the following is the code development work to invoke the rest API.
Here, use VS2013 to create a new console application to do the demo. This document needs to be read in conjunction with the code in the Attachment Createvm_api.rar.
Invoke API
Open the project "Createvm_api" in VS2013, the main use of three files:
Program.cs This is the main program file for the project, which already includes reading the local certificate, replacing the "Certificatethumbprint" in the code with the fingerprint string you just saw on Azure portal. You will also need to replace some of the parameters in the URI string, as explained later.
Requestbody.xml This is the request body for the new deployment
Addvm.xml This is the request body used to increase the virtual machine in an existing deployment
It is necessary to add that the following steps are required when creating a virtual machine using the REST API:
Create a cloud service and storage account
Create a deployment slot under the cloud service
Adding a virtual machine to a deployment slot
The code uses two examples to illustrate the above steps, and here are some explanations for these two examples.
Example 1
Create a deployment slot and establish a virtual machine in a cloud service that does not have a virtual machine deployed
The following rest APIs need to be called
"Https://management.core.chinacloudapi.cn/{0}/services/hostedservices/{1}/deployments
Here is the URI defined in the main function of Program.cs.
Replace {0} with subscription ID
Replace {1} with the name of the cloud service
Replace the "Certificatethumbprint" in the code with the fingerprint string you just saw on Azure portal
We then need to modify the Requestbody.xml in the sample code accordingly.
<Name> name of the deployment slot </Name> adding a virtual machine to the deployment will use the corresponding red box as shown in the section
<Label> deployment slot label </Label> to identify the deployment slot, corresponding to the Blue box section
<RoleName> role name </RoleName> The name of the virtual machine you see in Azure Portal
<ComputerName> Computer name </ComputerName> computer name of the operating system inside the virtual machine
<AdminPassword> Administrator Password </AdminPassword> the administrator password for the operating system of the virtual machine requires compliance with the default password complexity rules for the operating system that is created. The administrator name defaults to administrator.
Then you need to process the port mappings for the virtual machines so that tools like remote desktops can access our virtual machines.
<LocalPort> Virtual machine ports </LocalPort> that is, intranet ports
<Name> name of the port mapping rule </Name> Specify a name for each virtual machine port mapping entry
<Port> Cloud service Port </Port> that is, the external network port
<Protocol> protocol type </Protocol> can only enter TCP or UDP
Then you need to select the operating system image of the virtual machine
<sourceimagename>55bc2b193643443bb879a78bda516fc8__windows-server-2012-r2-201502.01-zh.cn-127gb.vhd</ Sourceimagename>
The values here are provided by Azure and can be obtained through the rest API or PowerShell commands to obtain the current operating system image provided by Azure.
Https://msdn.microsoft.com/zh-cn/library/azure/jj157191.aspx
Finally select the path to save the image and need to save to an existing storage account
<MediaLink>https://{0}.blob.core.chinacloudapi.cn/vhds/{1}.vhd</MediaLink> Replace {0} with the storage account name and {1} Replace the name of the file you want to save, and try to use only lowercase captions and numbers to set the filename Chengxiang.
You can then run the code.
Example 2
Establish virtual machines in a cloud service where virtual machines are deployed
The following APIs need to be called:
Https://management.core.chinacloudapi.cn/{0}/services/hostedservices/{1}/deployments/{2}/roles
Here is the second URI defined in the main function of Program.cs.
Replace {0} with subscription ID
Replace {1} with the name of the cloud service
Replace {2} with the deployment name. The deployment name is the name of the <Name> deployment slot in Requestbody.xml </name> You can also find the deployment name in the dashboard of cloud services via Azure Portal.
Replace the "Certificatethumbprint" in the code with the fingerprint string you just saw on Azure portal
We then need to modify the Addvm.xml in the sample code to run the code accordingly. The content that needs to be modified is basically consistent with the requestbody.xml, but cannot have both <Name> and <Label> properties.
Create a virtual machine using the Azure REST API