{
  "properties": {
    "displayName": "Metric alert rules should be configured on Batch accounts",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Audit configuration of metric alert rules on Batch account to enable the required metric",
    "metadata": {
      "version": "1.0.0",
      "category": "Batch"
    },
    "parameters": {
      "effect": {
        "type": "string",
        "defaultValue": "AuditIfNotExists",
        "allowedValues": [
          "AuditIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      },
      "metricName": {
        "type": "String",
        "metadata": {
          "displayName": "Metric name",
          "description": "The metric name that an alert rule must be enabled on"
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.Batch/batchAccounts"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.Insights/alertRules",
          "existenceScope": "Subscription",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.Insights/alertRules/isEnabled",
                "equals": "true"
              },
              {
                "field": "Microsoft.Insights/alertRules/condition.dataSource.metricName",
                "equals": "[parameters('metricName')]"
              },
              {
                "field": "Microsoft.Insights/alertRules/condition.dataSource.resourceUri",
                "equals": "[concat('/subscriptions/', subscription().subscriptionId, '/resourcegroups/', resourceGroup().name, '/providers/Microsoft.Batch/batchAccounts/', field('name'))]"
              }
            ]
          }
        }
      }
    }
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/26ee67a2-f81a-4ba8-b9ce-8550bd5ee1a7",
  "name": "26ee67a2-f81a-4ba8-b9ce-8550bd5ee1a7"
}