本文介绍了从 ARM 模板部署新的 Azure VM 时,无法为 .WithParameters() 格式化有效的 JObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我在使用 C# 编写的 Azure 函数从 ARM 模板部署 Azure VM 时遇到问题,同时使用来自 Newjonsoft.Json、Linq 库的 JObject 为新 VM 提供参数.

Currently I'm having trouble with deploying an Azure VM from an ARM template using an Azure Function which is written in C#, whilst using a JObject, from the Newjonsoft.Json,Linq library, to provide parameters for the new VM.

JObject.FromObject() 方法以 "{"paramName": "paramValue"}" 格式制定参数,但我认为它需要制定为 "{"paramName": { "value": "paramValue"}.我不确定是否还需要指定 'contentVersion' 和 '$schema' ARM 模板参数才能使其工作.

The JObject.FromObject() method formulates parameters in the format "{"paramName": "paramValue"}", however I believe that it needs to be formulated as "{"paramName": { "value": "paramValue"}. I'm not sure if 'contentVersion' and '$schema' ARM Template parameters also need to be specified for this to work.

到目前为止,我已经尝试使用动态变量来制定对象,然后将其转换为字符串并使用 JObject.Parse() 方法进行解析,但这只能产生与之前描述的相同的结果.

So far I have tried to formulate the object using a dynamic variable, which is then converted to string and parsed using JObject.Parse() method, however this only works to produce the same result as described before.

Azure 函数代码示例(并非所有代码):

Azure Function code sample (not all code):

using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage.Table;
using System.Threading.Tasks;
using System;
using Microsoft.Rest.Azure;
using Newtonsoft.Json.Linq;

// Authenticate with Azure

IAzure azure = await 
    Authentication.AuthenticateWithAzure(azureVmDeploymentRequest.SubscriptionId);

// Get current datetime
string Datetime = DateTime.Now.ToString("yyyy-MM-ddHHmmss");

log.LogInformation("Initiating VM ARM Template Deployment");
var parameters = azureVmDeploymentRequest.ToArmParameters(
        subscriptionId: azureVmDeploymentRequest.SubscriptionId,
        imageReferencePublisher: azureVmDeploymentRequest.ImageReferencePublisher
    );

// AzNewVmRequestArmParametersMain is a custom object containing the 
// parameters needed for the ARM template, constructed with GET SET

var parametersMain = new AzNewVmRequestArmParametersMain
{
    parameters = parameters
};

var jParameters = JObject.FromObject(parameters);

// Deploy VM from ARM template if request is valid
var vmArmTemplateParams = new ARMTemplateDeploymentRequest
{
    DeploymentName = "vmDeployTfLCP-" + Datetime,
    ParametersObject = jParameters,
    ResourceGroupName = azureVmDeploymentRequest.ResourceGroupName,
    TemplateUri = Environment.GetEnvironmentVariable("VM_ARMTEMPLATE_URI"),
    SasToken = Environment.GetEnvironmentVariable("STORAGE_ACCOUNT_SASTOKEN")
};

ARM 模板部署类代码示例(并非所有代码):

ARM Template Deployment class code sample (not all code):

using Microsoft.Azure.Management.Fluent;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using System;
using Microsoft.Extensions.Logging;
using Microsoft.Rest.Azure;


// Formulate ARM template URI
var ArmTemplatePath = ARMTemplateDeploymentRequest.TemplateUri + ARMTemplateDeploymentRequest.SasToken;

deployment = azure.Deployments.Define(ARMTemplateDeploymentRequest.DeploymentName)
    .WithExistingResourceGroup(ARMTemplateDeploymentRequest.ResourceGroupName)
    .WithTemplateLink(ArmTemplatePath, "1.0.0.0")
    .WithParameters(ARMTemplateDeploymentRequest.ParametersObject)
    .WithMode(Microsoft.Azure.Management.ResourceManager.Fluent.Models.DeploymentMode.Incremental)
    .Create();

作为预期的结果,我希望代码简单地将 ARM 模板部署启动到 Azure 资源组,但目前它失败并显示以下消息:

As an expected result, i'm expecting the code to simply initiate an ARM template deployment to a Azure Resource Group, however currently it is failing with the following message:

'请求内容无效,无法反序列化:'错误将值parameterValue"转换为类型'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Data.Definitions.DeploymentParameterDefinition'.路径 'properties.parameters.vNetResourceGroup',第 8 行,位置48.'.'

推荐答案

我能够通过构造基于参数类型的类来解决这个问题,并制定了一种将参数值映射到 ARM 参数类型的方法.

I was able to solve the problem by constructing parameter type based classes, and formulated a method to map out the parameter values to a ARM Parameter type.

ARM 模板参数类摘录:

ARM Template parameter classes extract:

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' 模型类摘录:

'AzNewVmRequestArmParameters' Model class extract:

namespace FuncApp.MSAzure.VirtualMachines
{
    public class AzNewVmRequestArmParameters
    {

        public ParameterValueString location { get; set; }

        public ParameterValueString adminUsername { get; set; }

        public ParameterValueString adminPassword { get; set; }

    }
}

有了这些,我就可以运行以下(简化的)代码,用 API 准备好的参数来制定一个有效的 jObject 变量:

With these, i'm able to run the following code below (simplified) to formulate a valid jObject variable with the parameters that can be ready by the API:

var parameters = azureVmDeploymentRequest.ToArmParameters(
   adminUsername: "azurevmadmin",
   adminPassword: "P@ssword123!"
);
var jParameters = JObject.FromObject(parameters);

这篇关于从 ARM 模板部署新的 Azure VM 时,无法为 .WithParameters() 格式化有效的 JObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 18:58