2 回答
TA贡献1890条经验 获得超9个赞
我能够通过构造基于参数类型的类来解决该问题,并制定了一种将参数值映射到 ARM 参数类型的方法。
ARM模板参数类摘录:
namespace FuncApp.MSAzure.ARMTemplates.ARMParaneterTypes
{
public class ParameterValueString
{
public string Value { get; set; }
}
public class ParameterValueArray
{
public string[] Value { get; set; }
}
public class ParameterBoolValue
{
public bool Value { get; set; }
}
}
映射类方法摘录:
public static AzNewVmRequestArmParameters ToArmParameters(
this AzNewVmRequest requestContent,
string adminUsername,
string adminPassword
)
{
return new AzNewVmRequestArmParameters
{
location = new ParameterValueString {
Value = requestContent.Location
},
adminUsername = new ParameterValueString
{
Value = adminUsername
},
adminPassword = new ParameterValueString
{
Value = adminPassword
},
};
}
“AzNewVmRequestArmParameters”模型类摘录:
namespace FuncApp.MSAzure.VirtualMachines
{
public class AzNewVmRequestArmParameters
{
public ParameterValueString location { get; set; }
public ParameterValueString adminUsername { get; set; }
public ParameterValueString adminPassword { get; set; }
}
}
有了这些,我就可以运行下面的代码(简化版)来制定一个有效的 jObject 变量,其中包含 API 可以准备的参数:
var parameters = azureVmDeploymentRequest.ToArmParameters(
adminUsername: "azurevmadmin",
adminPassword: "P@ssword123!"
);
var jParameters = JObject.FromObject(parameters);
TA贡献1871条经验 获得超13个赞
根据我的测试,如果要使用动态变量来制定对象,我们需要创建一个新的JObject。例如我的 template.json
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": { "type": "string" },
"adminPassword": { "type": "securestring" }
},
"variables": {
"vnetID": "[resourceId('Microsoft.Network/virtualNetworks','myVNet123456')]",
"subnetRef": "[concat(variables('vnetID'),'/subnets/mySubnet')]"
},
"resources": [
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/publicIPAddresses",
"name": "myPublicIPAddress123456",
"location": "[resourceGroup().location]",
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "myresourcegroupdns15896"
}
}
},
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/virtualNetworks",
"name": "myVNet123456",
"location": "[resourceGroup().location]",
"properties": {
"addressSpace": { "addressPrefixes": [ "10.0.0.0/16" ] },
"subnets": [
{
"name": "mySubnet",
"properties": { "addressPrefix": "10.0.0.0/24" }
}
]
}
},
{
"apiVersion": "2016-03-30",
"type": "Microsoft.Network/networkInterfaces",
"name": "myNic562354",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses/', 'myPublicIPAddress123456')]",
"[resourceId('Microsoft.Network/virtualNetworks/', 'myVNet')]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": { "id": "[resourceId('Microsoft.Network/publicIPAddresses','myPublicIPAddress123456')]" },
"subnet": { "id": "[variables('subnetRef')]" }
}
}
]
}
},
{
"apiVersion": "2016-04-30-preview",
"type": "Microsoft.Compute/virtualMachines",
"name": "myVM",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Network/networkInterfaces/', 'myNic562354')]"
],
"properties": {
"hardwareProfile": { "vmSize": "Standard_DS1" },
"osProfile": {
"computerName": "myVM",
"adminUsername": "[parameters('adminUsername')]",
"adminPassword": "[parameters('adminPassword')]"
},
"storageProfile": {
"imageReference": {
"publisher": "MicrosoftWindowsServer",
"offer": "WindowsServer",
"sku": "2012-R2-Datacenter",
"version": "latest"
},
"osDisk": {
"name": "myManagedOSDisk",
"caching": "ReadWrite",
"createOption": "FromImage"
}
},
"networkProfile": {
"networkInterfaces": [
{
"id": "[resourceId('Microsoft.Network/networkInterfaces','myNic562354')]"
}
]
}
}
}
]
}
我的代码
JObject parametesObjectv1 = new JObject(
new JProperty("adminUsername",
new JObject(
new JProperty("value", "azureuser")
)
),
new JProperty("adminPassword",
new JObject(
new JProperty("value", "Azure12345678")
)
)
);
var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(clientId, clientSecret, tenantId, AzureEnvironment.AzureGlobalCloud);
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithSubscription(subscriptionId);
if (azure.ResourceGroups.Contain(resourceGroupName) == false)
{
var resourceGroup = azure.ResourceGroups.Define(resourceGroupName)
.WithRegion(resourceGroupLocation)
.Create();
}
Console.WriteLine("start");
var deployment = azure.Deployments.Define(deploymentName)
.WithExistingResourceGroup(resourceGroupName)
.WithTemplateLink(pathToTemplateFile, "1.0.0.0")
.WithParameters(parametesObjectv1)
.WithMode(Microsoft.Azure.Management.ResourceManager.Fluent.Models.DeploymentMode.Incremental)
.Create();
另外,如果你不想使用动态变量,你可以直接提供parameter.json和template.json的url来创建资源
var deployment = azure.Deployments.Define(deploymentName)
.WithExistingResourceGroup(resourceGroupName)
.WithTemplateLink(pathToTemplateFile, "1.0.0.0")
.WithParametersLink(pathToJsonFile, "1.0.0.0")
.WithMode(Microsoft.Azure.Management.ResourceManager.Fluent.Models.DeploymentMode.Incremental)
.Create();
- 2 回答
- 0 关注
- 98 浏览
添加回答
举报