{
    "properties": {
        "displayName": "Deploy associations for a managed application",
        "policyType": "BuiltIn",
        "mode": "Indexed",
        "description": "Deploys an association resource that associates selected resource types to the specified managed application.  This policy deployment does not support nested resource types.",
        "metadata": {
            "version": "1.0.0",
            "category": "Managed Application"
        },
        "parameters": {
            "targetManagedApplicationId": {
                "type": "String",
                "metadata": {
                    "displayName": "Managed application ID",
                    "description": "Resource ID of the managed application to which resources need to be associated."
                }
            },
            "resourceTypesToAssociate": {
                "type": "Array",
                "metadata": {
                    "displayName": "Resource types to associate",
                    "description": "The list of resource types to be associated to the managed application.",
                    "strongType": "resourceTypes"
                }
            },
            "associationNamePrefix": {
                "type": "String",
                "metadata": {
                    "displayName": "Association name prefix",
                    "description": "Prefix to be added to the name of the association resource being created."
                },
                "defaultValue": "DeployedByPolicy"
            }
        },
        "policyRule": {
            "if": {
                "field": "type",
                "in": "[parameters('resourceTypesToAssociate')]"
            },
            "then": {
                "effect": "deployIfNotExists",
                "details": {
                    "type": "Microsoft.CustomProviders/Associations",
                    "name": "[concat(parameters('associationNamePrefix'), '-', uniqueString(parameters('targetManagedApplicationId')))]",
                    "roleDefinitionIds": [
                        "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
                    ],
                    "deployment": {
                        "properties": {
                            "mode": "incremental",
                            "template": {
                                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                                "contentVersion": "1.0.0.0",
                                "parameters": {
                                    "associatedResourceName": {
                                        "type": "string"
                                    },
                                    "resourceTypesToAssociate": {
                                        "type": "string"
                                    },
                                    "targetManagedApplicationId": {
                                        "type": "string"
                                    },
                                    "associationNamePrefix": {
                                        "type": "string"
                                    }
                                },
                                "variables": {
                                    "resourceType": "[concat(parameters('resourceTypesToAssociate'), '/providers/associations')]",
                                    "resourceName": "[concat(parameters('associatedResourceName'), '/microsoft.customproviders/', parameters('associationNamePrefix'), '-', uniqueString(parameters('targetManagedApplicationId')))]"
                                },
                                "resources": [
                                    {
                                        "type": "Microsoft.Resources/deployments",
                                        "apiVersion": "2017-05-10",
                                        "name": "[concat(deployment().Name, '-2')]",
                                        "properties": {
                                            "mode": "Incremental",
                                            "template": {
                                                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                                                "contentVersion": "1.0.0.0",
                                                "resources": [
                                                    {
                                                        "type": "[variables('resourceType')]",
                                                        "name": "[variables('resourceName')]",
                                                        "apiVersion": "2018-09-01-preview",
                                                        "properties": {
                                                            "targetResourceId": "[parameters('targetManagedApplicationId')]"
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    }
                                ]
                            },
                            "parameters": {
                                "resourceTypesToAssociate": {
                                    "value": "[field('type')]"
                                },
                                "associatedResourceName": {
                                    "value": "[field('name')]"
                                },
                                "targetManagedApplicationId": {
                                    "value": "[parameters('targetManagedApplicationId')]"
                                },
                                "associationNamePrefix": {
                                    "value": "[parameters('associationNamePrefix')]"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "id": "/providers/Microsoft.Authorization/policyDefinitions/17763ad9-70c0-4794-9397-53d765932634",
    "name": "17763ad9-70c0-4794-9397-53d765932634"
}