简体   繁体   English

ARM模板参数对象数组转换为模板数组

[英]ARM template parameter object array into template array

We're trying to create templates using objects for parameters so there's the option of having multiple values in different resources, ie deploy an Event Hub namespace that could have multiple authorization rules and eventhubs, but another object in the parameters for a second Event Hub namespace that may only have one of each. 我们正在尝试使用对象对象作为参数来创建模板,因此可以选择在不同资源中具有多个值,即部署可以具有多个授权规则和eventhub的Event Hub命名空间,但在参数中为第二个Event Hub命名空间部署另一个对象每个可能只有一个。

The template is like below: 模板如下所示:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "eventhubs": {
      "type": "object",
      "metadata": {
        "description": "JSON object that describes the deployment. see example parameters file"
      }
    }
  },
  "variables": {
      "resourceNamePrefix": "[substring(resourceGroup().name, 0, 8)]",
      "datacenterCode": "[substring(resourceGroup().name, 0, 3)]",
      "productCode": "[substring(resourceGroup().name, 3, 3)]",
      "environmentLevel": "[substring(resourceGroup().name, 6, 2)]"
  },
"resources": [
  {
    "type": "Microsoft.EventHub/namespaces",
    "name": "[concat(variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].name)]",
    "apiVersion": "2015-08-01",
    "location": "[resourceGroup().location]",
    "sku": {
      "name": "[concat(variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].sku.name)]",
      "tier": "[parameters('eventhubs').instances[copyIndex()].sku.tier]",
      "capacity": "[parameters('eventhubs').instances[copyIndex()].sku.capacity]"
    },
    "copy": {
      "name": "eventHubCopy",
      "count": "[length(parameters('eventhubs').instances)]"
    },
    "properties": {
      "serviceBusEndpoint": "[concat('https://',variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].name,'.servicebus.windows.net:443/')]",
      "enabled": "[parameters('eventhubs').instances[copyIndex()].properties.enabled]"
    },
    "resources": [
        *** PARAMETER OBJECT ***
    ]
    "dependsOn": []
      }
    ],
    "outputs": {}
  }

And the parameters file: 和参数文件:

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
  "parameters": {
    "eventhubs": {
      "value": {
        "instances": [
          {
            "name": "EVT001",
            "sku": {
              "name": "Standard",
              "tier": "Standard",
              "capacity": 4
            },
            "scale": null,
            "properties": {
              "enabled": "true"
            },
            "resources": [
              {
                "type": "AuthorizationRules",
                "name": "SendKey",
                "apiVersion": "2015-08-01",
                "properties": {
                  "rights": [
                    "Send"
                  ]
                }
              },
              {
                "type": "AuthorizationRules",
                "name": "ListenKey",
                "apiVersion": "2015-08-01",
                "properties": {
                  "rights": [
                    "Listen"
                  ]
                }
              },
              {
                "type": "EventHub",
                "name": "TestHub",
                "apiVersion": "2015-08-01",
                "properties": {
                  "messageRetentionInDays": 7,
                  "status": "Active",
                  "partitionCount": 4
                }
              }
            ]
          },
          {
            "name": "EVT002",
            "sku": {
              "name": "Standard",
              "tier": "Standard",
              "capacity": 4
            },
            "scale": null,
            "properties": {
              "enabled": "true"
            },
            "resources": [
              {
                "type": "AuthorizationRules",
                "name": "SendKey",
                "apiVersion": "2015-08-01",
                "properties": {
                  "rights": [
                    "Send"
                  ]
                }
              },
              {
                "type": "EventHub",
                "name": "TestHub",
                "apiVersion": "2015-08-01",
                "properties": {
                  "messageRetentionInDays": 7,
                  "status": "Active",
                  "partitionCount": 4
                }
              },
              {
                "type": "EventHub",
                "name": "SecondHub",
                "apiVersion": "2015-08-01",
                "properties": {
                  "messageRetentionInDays": 7,
                  "status": "Active",
                  "partitionCount": 4
                }
              }
            ]
          }
        ]
      }
    }
  }
}

What I'm attempting to do is move the content of the resources array in the parameters file into the nested resources array in the template file. 我试图做的是将参数文件中资源数组的内容移到模板文件中的嵌套资源数组中。 This is possible when moving an array into an object, but I'm facing the following problems with an array into an array: 将数组移到对象中时,这是可能的,但是我将数组移入数组时面临以下问题:

"resources": "[parameters('eventhubs').instances[copyIndex()].properties]", <--- value must be of type array
"resources": [ { "[parameters('eventhubs').instances[copyIndex()].properties]" } ],  <--- expecting a name and value as it's in an object 
 "resources": [ "[parameters('eventhubs').instances[copyIndex()].properties]" ], <--- value must be of the following types: object

Adding another set of square brackets around the object in the array in the parameters file doesn't help either. 在参数文件中数组对象周围添加另一组方括号也无济于事。

Same errors when using the createArray function. 使用createArray函数时出现相同的错误。

The workaround I have is to do 我要解决的方法是

    "resources": [
  {
    "type": "AuthorizationRules",
    "name": "[parameters('eventhubs').instances[copyIndex()].resources[0].name]",
    "apiversion": "[parameters('eventhubs').instances[copyIndex()].resources[0].apiversion]",
    "properties": "[parameters('eventhubs').instances[copyIndex()].resources[0].properties]",
    "dependsOn": [ "[concat(variables('resourceNamePrefix'), parameters('eventhubs').instances[copyIndex()].name)]" ]
  }
],

But the type property cannot be an expression so won't work for the way our templates will be consumed and used. 但是type属性不能是表达式,因此无法使用和使用我们的模板。

Is it possible to do what I'm attempting? 有可能做我想做的事吗?

this won't really fit into an answer (and i, sadly, dont have time to type out all the explanations), but you are basically looking to separate configuration and implementation (which is a wise thing to do), but ARM templates are not as straight forward as you would think. 这并不能完全满足答案(我很遗憾,我没有时间输入所有解释),但是您基本上是在寻求将配置和实现分开(这是明智的选择),但是ARM模板是不像您想的那样直接。 I will try to create a mcve. 我将尝试创建一个mcve。 It will be really abstract, but you only need the idea. 这将是非常抽象的,但是您只需要这个主意。

So the premise: you need to deploy X amount of resource with different properties. 因此,前提是:您需要部署X个具有不同属性的资源。

you config data looks like this: 您配置的数据如下所示:

"eventhubs": [
    {
        "name": "EVT001",
        "sku": skuobject,
        "scale": null,
        "properties": propertiesobject,
        "resources": [
            nestedobject,
            nestedobject,
            nestedobject
        ]
    },
    parentobject,
]

This object is nested object inside array inside parent object inside array (i suggest you drop the outermost object, as it is useless and does nothing except for adding complexity). 该对象是数组内部父对象内部数组中的嵌套对象(我建议您删除最外面的对象,因为它没有用,除了增加复杂性之外什么也不做)。 Your course of action, iterate outermost (parent object) array in a copy loop and attempt to iterate innermost (nested object) array in the properties of the resource. 您的操作过程是,在复制循环中迭代最外面的(父对象)数组,并尝试迭代资源属性中最里面的(嵌套的对象)数组。 which doesnt work for ARM templates. 这不适用于ARM模板。

With arm templates you can only iterate one level deep (with a loop, with hardcode you can go as deep as you want) so what you need to do is split your configuration object into objects that are 1 level deep. 使用arm模板,您只能迭代一个级别的深度(使用循环,可以使用所需的硬代码进行深度访问),因此您需要做的就是将配置对象拆分为1个级别的对象。 your object is 2 levels deep (array of parent object and array of nested objects). 您的对象深2层(父对象数组和嵌套对象数组)。 you can do that with a nested deployment. 您可以使用嵌套部署来做到这一点。

What you need to do is something like this: 您需要做的是这样的:

{
    "name": "[concat('nested-', copyIndex())]",
    "type": "Microsoft.Resources/deployments",
    "apiVersion": "2016-06-01",
    "copy": {
        "name": "eventHubLoop",
        "count": "[length(parameters('eventhubs'))]" (this should be an array)
    },
    "properties": {
        "mode": "Incremental",
        "templateLink": {
            "uri": "xxx",
            "contentVersion": "1.0.0.0"
        },
        "parameters": {
            "currentIteration": {
                "value": "[parameters('eventhubs')[copyIndex()]]"
            }
        }
    }
}

and your nested template should be parent resource (no copy, as it is always a single resource, we took care of that with the loop above) and properties\\resources loop. 并且您的嵌套模板应为父资源(无副本,因为它始终是单个资源,我们在上面的循环中已对此进行了处理)和properties \\ resources循环。 unfortunately i dont have an example handy (nda), but i hope you get the idea. 不幸的是,我没有方便的示例(nda),但我希望您能理解。 you need to create 2 distinct loops, that way you can handle your level of nestedness, if you have 3 levels of nestedness, you need 3 distinct loops (you can achieve that the same way) 您需要创建2个不同的循环,这样就可以处理嵌套级别,如果您具有3个嵌套的级别,则需要3个不同的循环(您可以以相同的方式实现)

you can of course make this much more complex and parametrize almost anything (location, different resource groups, different additional properties), but i feel like this level of complexity doesnt add anything to the table but rather makes you create a properties file exactly as a template (and this is the point where it gets useless if you ask me). 您当然可以使其复杂得多,几乎可以对任何参数(位置,不同的资源组,不同的其他属性)进行参数化,但是我觉得这种复杂程度不会向表中添加任何内容,而是使您完全像创建一个属性文件一样模板(如果您问我,这就是毫无用处的地方)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM