Search code examples
pythonazurevalidationazure-rm-template

Trying to validate any ARM Template but getting: 'Error converting value <...MyTemplate...> to type '....Templates.Schema.Template'


I'm trying to validate an arm template - ANY arm template - but I always get the same error:

The request content was invalid and could not be deserialized: 'Error converting value "
{
...
}" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Templates.Schema.Template'. Path 'properties.template', line 1, position 1202.'.

I created the ARM template on the azure portal and plugged it in:

#!/usr/bin/env python3                                                                                                                                                                   

# https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-authoring-templates                                                                                       
t='''                                                                                                                                                                                    
{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1",
    "apiProfile": "",
    "parameters": {
        "testType": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues": [
                "Standard_LRS",
                "Standard_ZRS",
                "Standard_GRS",
                "Standard_RAGRS",
                "Premium_LRS"
            ]
        }
    },
    "variables": {
        "testName": "[concat('test', uniqueString(resourceGroup().id))]"
    },
    "functions": [],
    "resources": [
        {
            "name": "[variables('testName')]",
            "type": "Microsoft.Storage/storageAccounts",
            "location": "[resourceGroup().location]",
            "apiVersion": "2015-06-15",
            "dependsOn": [],
            "tags": {
                "displayName": "test"
            },
            "properties": {
                "accountType": "[parameters('testType')]"
            }
        }
    ],
    "outputs": {}
}    

'''
from azure.common.client_factory import get_client_from_cli_profile
from azure.mgmt.resource import ResourceManagementClient
c = get_client_from_cli_profile(ResourceManagementClient)

print(c.deployments.validate('PAWS_Resources', 'food', c.models().DeploymentProperties(mode='incremental', template=t)).error.message)

Solution

  • According to the docs:

    template object The template content. You use this element when you want to pass the template syntax directly in the request rather than link to an existing template. It can be a JObject or well-formed JSON string. Use either the templateLink property or the template property, but not both.

    However, it wants a python object, not a "well-formed JSON string".

    Removing the quotes from around the template data makes it work.