Azure ARM模板:如何使用Copy生成属性名称和属性值。

huangapple go评论52阅读模式
英文:

Azure ARM Template : How to use Copy to generate both property name and property value

问题

Sure, here's the translated code part you requested:

"parameters": {
    "input": {
        "type": "array",
        "defaultValue": [
            {
                "name": "**globalName1**",
                "value": "**globalValue1**"
            },
            {
                "name": "globalName2",
                "value": "globalValue2"
            }
        ]
    }
}
"globalParametersVar": {
    "**globalName1**": {
        "type": "string",
        "value": "**globalValue1**"
    },
    "globalName2": {
        "type": "string",
        "value": "globalValue2"
    }
}
"resources": [
    {
        "name": "[parameters('factoryNameTest')]",
        "apiVersion": "2018-06-01",
        "type": "Microsoft.DataFactory/factories",
        "properties": {
            "**globalParameters**": {
                "globalName1": {
                    "type": "string",
                    "value": "globalValue1"
                },
                "globalName2": {
                    "type": "string",
                    "value": "globalValue2"
                },
                "globalName3": {
                    "type": "string",
                    "value": "globalValue3"
                }
            }
        },
        "location": "west us2",
        "identity": {
            "type": "SystemAssigned"
        }
    }
]

I hope this helps!

英文:

I have a parameter as below:

"parameters": {
    "input":{
      "type": "array",
      "defaultValue":[
        {
          "name":"**globalName1**",
          "value": "**globalValue1**"
        },
        {
          "name": "globalName2",
          "value": "globalValue2"
        }
      ]
    },

Is it possible to create a variable globalParametersVar using Copy on Input so its value would be as below:

    "globalParametersVar": {
      	"**globalName1**": {
					"type": "string",
					"value": "**globalValue1**"
				},
      	"globalName2": {
					"type": "string",
					"value": "globalValue2"
				}  
    },

In our existing code, there're something similar as below. You could see the globalParameters section is hardcoded. I'm trying to create globalParameters for AzureDataFactory dynamically based on the name/value from the input array

	"resources": [
    {
      "name": "[parameters('factoryNameTest')]",
      "apiVersion": "2018-06-01",
      "type": "Microsoft.DataFactory/factories",
      "properties": {
        "**globalParameters**": {
          "globalName1": {
            "type": "string",
            "value": "globalValue1"
          },
          "globalName2": {
            "type": "string",
            "value": "globalValue2"
          },
          "globalName3": {
            "type": "string",
            "value": "globalValue3"
          }
        }
      },
      "location": "west us2",
      "identity": {
        "type": "SystemAssigned"
      }
    }   
	],

I tried to use Copy but it didn't work

答案1

得分: 2

你可以通过利用 reducelambda 来实现这一点。

我使用 bicep 编写了这段代码,因为它可以让 ARM 作者的体验更加迅速,同时仍然能够编译生成所需的 ARM(再加上使用 ARM json 编写代码已经过时了 🤪)。

以下是输出的内容:

"debugGlobObj": {
        "type": "Object",
        "value": {
          "globalName1": {
            "type": "string",
            "value": "globalValue1"
          },
          "globalName2": {
            "type": "string",
            "value": "globalValue2"
          }
        }
      }

bicep

param input array = [{name: 'globalName1', value: 'globalValue1'}, {name: 'globalName2', value: 'globalValue2'}]

var globalVar = [for i in input: {
  '${i.name}' : {
    type: 'string'
    value: i.value
  }
}]

output debugGlobArray array = globalVar
output debugGlobObj object = reduce(globalVar, {}, (cur, next) => union(cur, next))

ARM

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "input": {
      "type": "array",
      "defaultValue": [
        {
          "name": "globalName1",
          "value": "globalValue1"
        },
        {
          "name": "globalName2",
          "value": "globalValue2"
        }
      ]
    }
  },
  "variables": {
    "copy": [
      {
        "name": "globalVar",
        "count": "[length(parameters('input'))]",
        "input": {
          "[format('{0}', parameters('input')[copyIndex('globalVar')].name)]": {
            "type": "string",
            "value": "[parameters('input')[copyIndex('globalVar')].value]"
          }
        }
      }
    ]
  },
  "resources": [],
  "outputs": {
    "debugGlobArray": {
      "type": "array",
      "value": "[variables('globalVar')]"
    },
    "debugGlobObj": {
      "type": "object",
      "value": "[reduce(variables('globalVar'), createObject(), lambda('cur', 'next', union(lambdaVariables('cur'), lambdaVariables('next'))))]"
    }
  }
}

希望这对你有所帮助!

英文:

You can do this by leveraging reduce and lambda.

I've written this using bicep because it makes the ARM authoring experience a lot faster, whilst still compiling down the ARM you want. (plus the fact that writing in ARM json is so 2020 🤪)

Here's the output

"debugGlobObj": {
        "type": "Object",
        "value": {
          "globalName1": {
            "type": "string",
            "value": "globalValue1"
          },
          "globalName2": {
            "type": "string",
            "value": "globalValue2"
          }
        }
      }

bicep

param input array = [{name: 'globalName1', value: 'globalValue1'}, {name: 'globalName2', value: 'globalValue2'}]

var globalVar = [for i in input: {
  '${i.name}' : {
    type: 'string'
    value: i.value
  }
}]

output debugGlobArray array = globalVar
output debugGlobObj object = reduce(globalVar, {}, (cur, next) => union(cur, next))

ARM

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "input": {
      "type": "array",
      "defaultValue": [
        {
          "name": "globalName1",
          "value": "globalValue1"
        },
        {
          "name": "globalName2",
          "value": "globalValue2"
        }
      ]
    }
  },
  "variables": {
    "copy": [
      {
        "name": "globalVar",
        "count": "[length(parameters('input'))]",
        "input": {
          "[format('{0}', parameters('input')[copyIndex('globalVar')].name)]": {
            "type": "string",
            "value": "[parameters('input')[copyIndex('globalVar')].value]"
          }
        }
      }
    ]
  },
  "resources": [],
  "outputs": {
    "debugGlobArray": {
      "type": "array",
      "value": "[variables('globalVar')]"
    },
    "debugGlobObj": {
      "type": "object",
      "value": "[reduce(variables('globalVar'), createObject(), lambda('cur', 'next', union(lambdaVariables('cur'), lambdaVariables('next'))))]"
    }
  }
}

huangapple
  • 本文由 发表于 2023年6月12日 13:59:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定