如何通过 API 而不是参数文件传递 ARM 模板参数?

How can I pass ARM template parameters through the API instead of a parameter file?

我正在尝试使用 Azure 资源管理器 .NET 库在 Azure 中自动创建一些资源。我能够创建资源组并将我的 ARM 模板放在 blob 存储上的可访问位置;但是,我希望能够将参数传递给代码中的请求,而不是在存储中的某个地方暂存一个 JSON 文件。

看来这应该是可以的。例如,在 Deployment.Properties 对象上,它同时具有 ParametersParametersLink,但我找不到任何关于其用法的文档,下面是抛出一个异常,说明没有指定值模板中的参数:

deployment.Properties = new DeploymentProperties
{
    Mode = DeploymentMode.Incremental,
    TemplateLink = new TemplateLink("link-to-my-template-json-in-storage"),
    Parameters = new
    {
        diskStorageAccountName = "value",
        imageVhdPath = "value",
        virtualNetworkName = "value",
        virtualNetworkSubnetName = "value",
        vmName = value,
        vmAdminUserName = "value",
        vmAdminPassword = "value"
    }
};

这会产生以下错误:

An unhandled exception of type 'Microsoft.Rest.Azure.CloudException' occurred in mscorlib.dll

Additional information: Deployment template validation failed: 'The value for the template parameter 'diskStorageAccountName' at line '5' and column '32' is not provided. Please see http://aka.ms/arm-deploy/#parameter-file for usage details.'.

我做错了什么吗? DeploymentProperties.Parameters 只是一个 Object 所以我假设它会被序列化并正确传递——这个假设不正确吗?

编辑:MSDN article 也不是很有帮助。

编辑 2:我想知道这是否是自动生成代码中的错误。请在此处查看第 700 行:

https://github.com/Azure/azure-sdk-for-net/blob/master/src/ResourceManagement/Resource/ResourceManagement/Generated/DeploymentOperations.cs

看起来它正在尝试 JObject.Parse

编辑 3:打开了 issue on GitHub

根据 source code testing,它的布局有点奇怪...

@"{ 'siteName': {'value': 'mctest0101'},'hostingPlanName': {'value': 'mctest0101'},'siteMode': {'value': 'Limited'},'computeMode': {'value': 'Shared'},'siteLocation': {'value': 'North Europe'},'sku': {'value': 'Free'},'workerSize': {'value': '0'}}",

还有一个issue提出了类似的问题

我目前没有时间测试这个!所以如果它不起作用让我知道,我会删除这个答案。

对于 Deployment Properties Parameters,您应该使用 中的 JObject 类型Newtonsoft.Json.Linq 命名空间。

例如

using Newtonsoft.Json.Linq;
// paramJsonString is a string type object
Parameters = JObject.Parse(paramJsonString);

注意:Microsoft.Azure.Management.Resources nuget 包将被弃用。

Strongly recommend to use Microsoft.Azure.ResourceManager 1.0.0-preview Microsoft.Azure.ResourceManager for your development related to Azure Resource Manager.

希望对您有所帮助!