Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ARM template transform array of strings into array of objects

Not sure if this functionality exists. I'm trying to transform a list of comma separated IP addresses from the Azure DevOps build parameters into an array of objects. So far it's only splitting a comma separated list into an array of strings, but the template needs an array of objects.

The parameter value is a comma separated list of IP Addresses. e.g. "192.168.0.1,192.168.0.2/32,127.0.0.1"

The ARM template would look like:

"variables": {
  "ipaddresses": "[split(parameters('ipaddresses'), ',')]"
},
"resources": [
  ...
    "ipRestrictions": "[stringArrToObjArr(variables('ipaddresses'))]" <--
  ...
]

And ideally function with the arrow above would yield a value for ipRestictions would be something like:

[
  {
    "ipAddress": "192.168.0.1"
  },
  {
    "ipAddress": "192.168.0.2/32"
  },
  {
    "ipAddress": "127.0.0.1"
  },
]
like image 324
Brian Avatar asked Jul 03 '19 19:07

Brian


1 Answers

you can use copy() function to do that:

"variables": {
  "ipaddresses": "[split(parameters('ipaddresses'), ',')]"
  "copy": [
    {
      "name": "myVariable",
      "count": "[length(variables('ipaddresses'))]",
      "input": {
        "ipAddress": "[variables('ipaddresses')[copyIndex('myVariable')]]"
      }
    }
  ]
},

this would return the desired object into a variable called myVariable. if you want to rename it >> don't forget to rename it inside copyIndex() as well

like image 135
4c74356b41 Avatar answered Oct 11 '22 13:10

4c74356b41