How to convert an array into properties of one object in an ARM template?

冷暖自知 提交于 2020-07-08 03:41:36

问题


I am looking for a way to convert an array (e.g. of strings) into one object, where the properties are generated from the array values.

Use case: I want to generate a tags object with links to resources, based on a list of resource names. I need to do this, to link App Service resources to an Application Insights resource.

The list of resources could be supplied using a parameter:

"parameters": {
  "appServices": {
    "type": "array",
    "metadata": {
      "description": "Names of app services to link this application insights resource to via hidden tags"
    }
  }
}

Sample input:

['appName1', 'appName2', 'appName3']

Sample output:

"tags":
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName1'))]": "Resource",
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName2'))]": "Resource",
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName3'))]": "Resource"
}

I know you can use copy to loop over arrays but that will create an array of objects and not a single object (which is required for tags), for example:

[
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName1'))]": "Resource"
},
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName2'))]": "Resource"
},
{
  "[concat('hidden-link:', resourceId('Microsoft.Web/sites/', 'appName3'))]": "Resource"
}
]

It would be possible to use union to merge those objects again, but that function requires you to hardcode the objects you want to merge, so it does not work when you have an input with variable length.

What I am looking for is a way to do this in a dynamic way.


回答1:


I'm not sure if this is the best approach to this problem.

Tags are supposed to be metadata about a specific object/service. Wouldn't it make more sense to apply a tag (say your system name, environment, etc..) and then run a query against azure on that tag?

This should achieve the same result pulling back all related resources.



来源:https://stackoverflow.com/questions/62714060/how-to-convert-an-array-into-properties-of-one-object-in-an-arm-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!