Azure ARM模板:有没有办法把两层嵌套数组扩展成一层?

72qzrwbm  于 2023-06-30  发布在  其他
关注(0)|答案(1)|浏览(107)

我有一个参数如下,它是一个数组与嵌套的内部数组

"parameters": {
  "input": {
    "type": "array",
    "defaultValue": [
      {
        "clusterName": "clusterNameA",
        "databaseName": "databaseNameA",
        "innerArray": {
          "value": [
            {
              "principalId": "id1",
              "role": "Viewer"
            },
            {
              "principalId": "id2",
              "role": "Ingestor"
            }
          ]
        }
      },
      {
        "clusterName": "clusterNameB",
        "databaseName": "databaseNameB",
        "innerArray": {
          "value": [
            {
              "principalId": "id3",
              "role": "Viewer"
            },
            {
              "principalId": "id4",
              "role": "Ingestor"
            }
          ]
        }
      }
    ]
  }
}

是否可以通过展开innerArrays来创建一个数组变量,使其看起来像下面这样?这背后的背景是ARM似乎不直接支持嵌套复制,所以我试图通过扩展来解决它。

[
  {
    "clusterName": "clusterNameA",
    "databaseName": "databaseNameA",
    "principalId": "id1",
    "role": "Viewer"
  },
  {
    "clusterName": "clusterNameA",
    "databaseName": "databaseNameA",
    "principalId": "id2",
    "role": "Ingestor"
  },
  {
    "clusterName": "clusterNameB",
    "databaseName": "databaseNameB",
    "principalId": "id3",
    "role": "Viewer"
  },
  {
    "clusterName": "clusterNameB",
    "databaseName": "databaseNameB",
    "principalId": "id4",
    "role": "Ingestor"
  }
]

我不知道怎么做

zaqlnxep

zaqlnxep1#

因为你已经用azure-bicep标记了这个问题,我觉得你可以使用这些函数来帮助你解决你的问题:

  • Map:

将自定义Map函数应用于数组的每个元素。

  • 展平:

接受数组的数组,并按原始顺序返回子数组元素的数组。子阵列只被展平一次,而不是递归地展平。
这对我很有效:

// main.bicep
param input array

output flatten array = flatten(map(input, cluster => map(cluster.innerArray.value, inner => {
  clusterName: cluster.clusterName
  databaseName: cluster.databaseName
  principalId: inner.principalId
  role: inner.role
})))

等效的ARM(az bicep build --file main.bicep)看起来像这样:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "input": {
      "type": "array"
    }
  },
  "resources": [],
  "outputs": {
    "flatten": {
      "type": "array",
      "value": "[flatten(map(parameters('input'), lambda('cluster', map(lambdaVariables('cluster').innerArray.value, lambda('inner', createObject('clusterName', lambdaVariables('cluster').clusterName, 'databaseName', lambdaVariables('cluster').databaseName, 'principalId', lambdaVariables('inner').principalId, 'role', lambdaVariables('inner').role))))))]"
    }
  }
}

并给予这个结果:

[
  {
    "clusterName": "clusterNameA",
    "databaseName": "databaseNameA",
    "principalId": "id1",
    "role": "Viewer"
  },
  {
    "clusterName": "clusterNameA",
    "databaseName": "databaseNameA",
    "principalId": "id2",
    "role": "Ingestor"
  },
  {
    "clusterName": "clusterNameB",
    "databaseName": "databaseNameB",
    "principalId": "id3",
    "role": "Viewer"
  },
  {
    "clusterName": "clusterNameB",
    "databaseName": "databaseNameB",
    "principalId": "id4",
    "role": "Ingestor"
  }
]

相关问题