Как преобразовать названия стран в значения стандарта ISO 3166-1 alpha-2 в шаблоне arm

#azure #azure-resource-manager #arm-template #country-codes #iso-3166

#azure #azure-resource-manager #arm-template #коды стран #iso-3166

Вопрос:

У меня есть ARM-шаблон, и я хотел бы преобразовать название страны в «Соединенные Штаты», и я хотел бы получить код ISO 3166-1 alpha 2 типа «US». Это преобразованное значение я бы использовал для имени группы ресурсов. Я попытался использовать условие «if», но эти параметры я могу использовать в случае, когда параметр «CountryString» содержит только две страны. Я не могу найти решения для параметра «CountryObject», который содержит более двух стран. Есть ли способ сделать это?

 {
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
        "CountryString": {
        "type": "string",
        "metadata": { "Description": "Select a country from the list." },
        "defaultValue": "United States",
        "allowedValues": [ "United States", "Germany"]
    },
    "CountryObject": {
        "type": "object",
        "defaultValue": {
            "United States": "US",
            "Germany": "DE",
            "United Kingdom": "GB"
        }
    }
},
"variables": {
     "OutputString": {
        "type": "string",
        "value": "[if(equals('United States', parameters('CountryString')), 'US','DE')]"
    },
      "Outputobject": {
        "type": "string",
        "value": "[if(equals('United States', parameters('CountryObject')), 'US','DE')]"
    },
     "rgName": "[concat('rg-',variables('Outputobject').value, '-rgname')]"
},

   "resources": [
    {
        "type": "Microsoft.Resources/resourceGroups",
        "apiVersion": "2019-08-01",
        "location": "East Asia",
        "name": "[variables('rgName')]",
        "properties": {}
    }],
"outputs": {
    "OutputString": {
        "type": "string",
        "value": "[variables('OutputString').value]"
    },
        "Outputobject": {
        "type": "string",
        "value": "[variables('Outputobject').value]"
    }
}}
  

Развертывание в azure

Шаблон здесь

Комментарии:

1. Это элегантное решение. Спасибо, мистер Стрингфеллоу

Ответ №1:

Вместо того, чтобы использовать if оператор, обработайте CountryObject его как хэш-таблицу.

       "value": "[parameters('CountryObject')[parameters('CountryString')]]"

  

Все это.

 {
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.1",
  "parameters": {
    "CountryString": {
      "type": "string",
      "metadata": { "Description": "Select a country from the list." },
      "defaultValue": "United States",
      "allowedValues": [ "United States", "Germany" ]
    },
    "CountryObject": {
      "type": "object",
      "defaultValue": {
        "United States": "US",
        "Germany": "DE",
        "United Kingdom": "GB"
      }
    }
  },
  "variables": {
    "OutputString": {
      "type": "string",
      "value": "[parameters('CountryObject')[parameters('CountryString')]]"
    }  },

  "resources": [],
  "outputs": {
    "OutputString": {
      "type": "string",
      "value": "[variables('OutputString').value]"
    }
  }
}
  

Ответ №2:

Окончательное решение, которое я использовал: параметры «CountryObject» заменены на переменную «CountryObject»

 {
"$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
"contentVersion": "1.0.0.1",
"parameters": {
    "CountryString": {
        "type": "string",
        "metadata": { "Description": "Select a country from the list." },
        "allowedValues": [
            "United States",
            "Germany",
            "United Kingdom"
        ]
    }
},
"variables": {
    "CountryObject": {
        "type": "object",
        "value": {
            "United States": "US",
            "Germany": "DE",
            "United Kingdom": "GB"

        }
    },
    "OutputString": {
        "type": "object",
        "value": "[variables('CountryObject').value[parameters('CountryString')]]"
    }
},

"resources": [],
"outputs": {
    "OutputString": {
        "type": "string",
        "value": "[variables('OutputString').value]"
    }
}}
  

Шаблон с полным списком стран.