Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

Azure ARM template: expression as default value of parameter that is not string or that has allowed values

I am defining some parameters to configure my SQL Database elastic pool in my ARM template. I would like to specify defaults for each parameter, with the second and third parameters' default values being based on the first parameter's value. However, I'm getting errors whenever I attempt to specify expressions as the default values of parameters that have an allowed set of values: "This value is not one of the allowed values for parameter '…'." The same happens for parameters that have a type other than string: "Parameter '…' must be of type '…'.".

I can work around the issue by eliminating the allowed values, and by changing all parameter types to string. However, this would lose the validation for user-input values. Am I doing something wrong when defining the default values, or is this an ARM limitation?

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "sqlSkuName": {
      "type": "string",
      "allowedValues": [
        "BasicPool",
        "StandardPool",
        "PremiumPool"
      ],
      "defaultValue": "StandardPool"
    },
    "sqlSkuTier": {
      "type": "string",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ],
      "defaultValue": "[if(equals(parameters('sqlSkuName'), 'BasicPool'), 'Basic',
                        if(equals(parameters('sqlSkuName'), 'StandardPool'), 'Standard',
                        if(equals(parameters('sqlSkuName'), 'PremiumPool'), 'Premium',
                        '')))]"
    },
    "sqlDatabaseMaxCapacity": {
      "type": "int",
      "defaultValue": "[if(equals(parameters('sqlSkuName'), 'BasicPool'), 5, 50)]"
    },
  }
}

enter image description here

enter image description here

question from:https://stackoverflow.com/questions/65921721/azure-arm-template-expression-as-default-value-of-parameter-that-is-not-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to use the object variables to use the parameters conditionally, so you have to do something like this:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "sqlSkuName": {
        "type": "string",
        "allowedValues": [
            "BasicPool",
            "StandardPool",
            "PremiumPool"
        ],
        "defaultValue": "StandardPool"
    },
    "sqlSkuTier": {
        "type": "string",
        "allowedValues": [
            "Basic",
            "Standard",
            "Premium"
        ]
    },
    "sqlDatabaseMaxCapacity": {
        "type": "int",
        "defaultValue": 0
    }
},
"variables": {
    "sqlSkuTier-Var": "[if(equals(parameters('sqlSkuName'), 'BasicPool'), 'Basic',
                    if(equals(parameters('sqlSkuName'), 'StandardPool'), 'Standard',
                    if(equals(parameters('sqlSkuName'), 'PremiumPool'), 'Premium',
                    '')))]",
    "sqlDatabaseMaxCapacity-var": "[if(equals(parameters('sqlDatabaseMaxCapacity'),0 ),
                                        if(equals(parameters('sqlSkuName'), 'BasicPool'), 5,50),
                                        parameters('sqlDatabaseMaxCapacity'))]"
},
"resources": []

}

And then to use the value:

variables('sqlSkuTier-Var')

I recommend that you use the visual studio code plugin( https://marketplace.visualstudio.com/items?itemName=msazurermtools.azurerm-vscode-tools) to have IntelliSense, it helps a lot to work and see what you have wrong.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...