Evan Park
Evan Park

Reputation: 538

ARM Template Accessing Object Property through Parameter as property name

I'm searching for ways to access ARM template Object property through a parameter name.

in below example,

parameters: {
    "propertyName": {
        "type": "string"
    } 
}

variables: {
    "object": {
        "value": {
            "color": "red"
        }
    }
}

where [parameters("propertyName")] is color

does below work within ARM template deployment? or is there a way to achieve similar thing?

"[variables('object')].[parameters('propertyName')]"

I am expecting the output to be "red" for above line.

Any help is appreciated! :)

Upvotes: 5

Views: 3474

Answers (1)

Stringfellow
Stringfellow

Reputation: 2908

Here is how you can use a parameters value to select the property of an object in the variables.

"value": "[variables('objects')[parameters('propertyName')].color]"

A complete example:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "propertyName": {
      "type": "string"
    }
  },
  "variables": {
    "objects": {
      "property0": {
        "color": "red"
      },
      "property1": {
        "color": "green"
      },
      "property2": {
        "color": "blue"
      }
    }
  },
  "resources": [],
  "outputs": {
    "messageOutObject": {
      "type": "object",
      "value": "[variables('objects')]"
    },
    "messageOutObjectProperty": {
      "type": "string",
      "value": "[variables('objects')[parameters('propertyName')].color]"
    }
  }
}

Then you can pick the different properties by passing in different parameter values. For example:

New-AzResourceGroupDeployment -ResourceGroupName 'DeleteMe20190605' -TemplateFile .\azuredeploy.json -TemplateParameterObject @{propertyName = 'property1'}

OR

New-AzResourceGroupDeployment -ResourceGroupName 'DeleteMe20190605' -TemplateFile .\azuredeploy.json -TemplateParameterObject @{propertyName = 'property2'}

Results: enter image description here

Upvotes: 7

Related Questions