{
    "if": {
        "allOf": [
            {
                "field": "type",
                "equals": "Microsoft.KeyVault/vaults"
            },
            {
                "field": "location",
                "in": "[parameters('azureRegions')]"
            }
        ]
    },
    "then": {
        "effect": "deployIfNotExists",
        "details": {
            "type": "Microsoft.Insights/diagnosticSettings",
            "existenceCondition": {
                "allOf": [
                    {
                        "field": "Microsoft.Insights/diagnosticSettings/logs.enabled",
                        "equals": "[parameters('logsEnabled')]"
                    },
                    {
                        "field": "Microsoft.Insights/diagnosticSettings/metrics.enabled",
                        "equals": "[parameters('metricsEnabled')]"
                    },
                    {
                        "field": "Microsoft.Insights/diagnosticSettings/eventHubName",
                        "equals": "[parameters('eventHubName')]"
                    }
                ]
            },
            "roleDefinitionIds": [
                "/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c"
            ],
            "deployment": {
                "properties": {
                    "mode": "incremental",
                    "template": {
                        "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                            "vaultName": {
                                "type": "string"
                            },
                            "location": {
                                "type": "string"
                            },
                            "eventHubName": {
                                "type": "string"
                            },
                            "eventHubRuleId": {
                                "type": "string"
                            },
                            "metricsEnabled": {
                                "type": "string"
                            },
                            "logsEnabled": {
                                "type": "string"
                            },
                            "profileName": {
                                "type": "string"
                            }
                        },
                        "variables": {},
                        "resources": [
                            {
                                "type": "Microsoft.KeyVault/vaults/providers/diagnosticSettings",
                                "apiVersion": "2017-05-01-preview",
                                "name": "[concat(parameters('vaultName'), '/', 'Microsoft.Insights/', parameters('profileName'))]",
                                "location": "[parameters('location')]",
                                "dependsOn": [],
                                "properties": {
                                    "eventHubName": "[parameters('eventHubName')]",
                                    "eventHubAuthorizationRuleId": "[parameters('eventHubRuleId')]",
                                    "metrics": [
                                        {
                                            "category": "AllMetrics",
                                            "enabled": "[parameters('metricsEnabled')]",
                                            "retentionPolicy": {
                                                "enabled": false,
                                                "days": 0
                                            }
                                        }
                                    ],
                                    "logs": [
                                        {
                                            "category": "AuditEvent",
                                            "enabled": "[parameters('logsEnabled')]"
                                        }
                                    ]
                                }
                            }
                        ],
                        "outputs": {
                            "policy": {
                                "type": "string",
                                "value": "[concat(parameters('eventHubName'), 'configured for diagnostic logs for ', ': ', parameters('name'))]"
                            }
                        }
                    },
                    "parameters": {
                        "EventHubName": {
                            "value": "[parameters('eventHubName')]"
                        },
                        "location": {
                            "value": "[field('location')]"
                        },
                        "vaultName": {
                            "value": "[field('name')]"
                        },
                        "EventHubRuleID": {
                            "value": "[parameters('eventHubRuleId')]"
                        },
                        "MetricsEnabled": {
                            "value": "[parameters('metricsEnabled')]"
                        },
                        "LogsEnabled": {
                            "value": "[parameters('logsEnabled')]"
                        },
                        "ProfileName": {
                            "value": "[parameters('profileName')]"
                        }
                    }
                }
            }
        }
    }
}