Azure Resource Manager 提供一致的管理层,用于管理通过 Azure PowerShell、Azure CLI、Azure 门户、REST API 和开发工具执行的任务,所有工具使用一组通用操作,这就意味着Resource Manager 提供了一种新方法来部署和管理解决方案。下图显示各种工具如何与同一 Azure 资源管理器 API 交互, API 将请求传递给 Resource Manager 服务,后者对请求进行身份验证和授权,随后将请求路由到相应的资源提供程序。
本文以创建虚拟机为例,主要描述了通过Java调用ARM API来创建虚拟机的过程。
1. 创建以下资源:
- 资源组:IaaSUserManualDemo
- 虚拟网络:ContosoVNet(10.1.0.0/22)
- 子网:Web-Subnet(10.1.1.0/24)和App-Subnet(10.1.2.0/24)
- 安全组:Web-NSG和App-NSG
- 可用集:Web-availSet和App-availSet
2. 创建虚拟机(这里以用资源管理器部署的方式创建完整的虚拟机)
创建结果:创建虚拟机ContosoWeb01,自动分配动态publicIP和privateIP, 指定配置到ContosoVNet和Web-Subnet和Web-availSet和App-NSG,自动创建网络接口contosoWeb01-nic1
PUT https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/Microsoft.Resources/deployments/{deploymentName}?api-version=2017-08-01
例子:
https://management.chinacloudapi.cn/subscriptions/d0a61681-0f6a-4e42-a7c4-739bd7b821f7/resourcegroups/IaaSUserManualDemo/providers/Microsoft.Resources/deployments/vmCreation20171125-001?api-version=2017-08-01
Headers:
Content-Type: application/json
Authorization: Bearer {获取到的令牌}
以下是请求的Body部分:(在当前资源组下申请新的虚拟机,您只需要更改JSON尾部标黄的部分,这部分对应了您在门户中创建虚拟机时提供的相关参数)。
{
"properties": {
"template": {
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"location": {
"type": "string"
},
"virtualMachineName": {
"type": "string"
},
"virtualMachineSize": {
"type": "string"
},
"adminUsername": {
"type": "string"
},
"virtualNetworkName": {
"type": "string"
},
"networkInterfaceName": {
"type": "string"
},
"networkSecurityGroupName": {
"type": "string"
},
"adminPassword": {
"type": "securestring"
},
"availabilitySetName": {
"type": "string"
},
"diagnosticsStorageAccountName": {
"type": "string"
},
"diagnosticsStorageAccountId": {
"type": "string"
},
"diagnosticsStorageAccountType": {
"type": "string"
},
"subnetName": {
"type": "string"
},
"publicIpAddressName": {
"type": "string"
},
"publicIpAddressType": {
"type": "string"
},
"publicIpAddressSku": {
"type": "string"
}
},
"variables": {
"vnetId": "[resourceId('IaaSUserManualDemo','Microsoft.Network/virtualNetworks', parameters('virtualNetworkName'))]",
"subnetRef": "[concat(variables('vnetId'), '/subnets/', parameters('subnetName'))]"
},
"resources": [
{
"name": "[parameters('virtualMachineName')]",
"type": "Microsoft.Compute/virtualMachines",
"apiVersion": "2016-04-30-preview",
"location": "[parameters('location')]",
"tags": {
"costCenter": "Finance"
},
"dependsOn": [
"[concat('Microsoft.Network/networkInterfaces/', parameters('networkInterfaceName'))]",
"[concat('Microsoft.Storage/storageAccounts/', parameters('diagnosticsStorageAccountName'))]"
],
"properties": {
"osProfile": {
"computerName": "[parameters('virtualMachineName')]",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]",
"windowsConfiguration": {
"provisionVmAgent": "true"
}
},
"hardwareProfile": {
"vmSize": "[parameters('virtualMachineSize')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2016-Datacenter",
"version": "latest"
},
"osDisk": {
"createOption": "fromImage",
"managedDisk": {
"storageAccountType": "Standard_LRS"
}
},
"dataDisks": []
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces', parameters('networkInterfaceName'))]"
}
]
},
"diagnosticsProfile": {
"bootDiagnostics": {
"enabled": true,
"storageUri": "[reference(resourceId('IaaSUserManualDemo', 'Microsoft.Storage/storageAccounts', parameters('diagnosticsStorageAccountName')), '2015-06-15').primaryEndpoints['blob']]"
}
},
"availabilitySet": {
"id": "[resourceId('Microsoft.Compute/availabilitySets', parameters('availabilitySetName'))]"
}
}
},
{
"name": "[parameters('diagnosticsStorageAccountName')]",
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2015-06-15",
"location": "[parameters('location')]",
"properties": {
"accountType": "[parameters('diagnosticsStorageAccountType')]"
}
},
{
"name": "[parameters('networkInterfaceName')]",
"type": "Microsoft.Network/networkInterfaces",
"apiVersion": "2016-09-01",
"location": "[parameters('location')]",
"dependsOn": [
"[concat('Microsoft.Network/publicIpAddresses/', parameters('publicIpAddressName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"subnet": {
"id": "[variables('subnetRef')]"
},
"privateIPAllocationMethod": "Dynamic",
"publicIpAddress": {
"id": "[resourceId('IaaSUserManualDemo','Microsoft.Network/publicIpAddresses', parameters('publicIpAddressName'))]"
}
}
}
],
"networkSecurityGroup": {
"id": "[resourceId('IaaSUserManualDemo', 'Microsoft.Network/networkSecurityGroups', parameters('networkSecurityGroupName'))]"
}
}
},
{
"name": "[parameters('publicIpAddressName')]",
"type": "Microsoft.Network/publicIpAddresses",
"apiVersion": "2017-08-01",
"location": "[parameters('location')]",
"properties": {
"publicIpAllocationMethod": "[parameters('publicIpAddressType')]"
},
"sku": {
"name": "[parameters('publicIpAddressSku')]"
}
}
],
"outputs": {
"adminUsername": {
"type": "string",
"value": "[parameters('adminUsername')]"
}
}
},
"parameters": {
"location": {
"value": "chinanorth"
},
"virtualMachineName": {
"value": "ContosoWeb01"
},
"virtualMachineSize": {
"value": "Standard_A1_v2"
},
"adminUsername": {
"value": "wangfeng"
},
"virtualNetworkName": {
"value": "ContosoVNet"
},
"networkInterfaceName": {
"value": "contosoWeb01-nic1"
},
"networkSecurityGroupName": {
"value": "Web-NSG"
},
"adminPassword": {
"value": "Happywangfeng1234"
},
"availabilitySetName": {
"value": "Web-AvailSet"
},
"diagnosticsStorageAccountName": {
"value": "iaasusermanualdemo393"
},
"diagnosticsStorageAccountType": {
"value": "Standard_LRS"
},
"diagnosticsStorageAccountId": {
"value": "Microsoft.Storage/storageAccounts/iaasusermanualdemo393"
},
"subnetName": {
"value": "Web-Subnet"
},
"publicIpAddressName": {
"value": "ContosoAD02-ip"
},
"publicIpAddressType": {
"value": "Dynamic"
},
"publicIpAddressSku": {
"value": "Basic"
}
},
"mode": "Incremental"
}
}
返回结果如图所示:获取操作状态的URL在返回的头部,图中以红框标出
查询执行结果状态:执行配置中
查询执行结果状态:执行成功