{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "description": "V19 change watch. Sends successful changes in this Azure subscription (resource writes, deletes and actions, from Event Grid) to V19, so V19 can flag production changes that did not go through it. No Function or forwarder. Deploy into any resource group of the subscription to watch."
  },
  "parameters": {
    "v19ApiKey": {
      "type": "securestring",
      "minLength": 20,
      "metadata": { "description": "A V19 API key with only the change:observe scope (create it under API keys in V19)." }
    },
    "v19Endpoint": {
      "type": "string",
      "defaultValue": "https://visionxixlabs.com/api/v1/changes/observed",
      "metadata": { "description": "Leave as is unless you run V19 yourself." }
    }
  },
  "variables": {
    "topicName": "[concat('v19-change-watch-', uniqueString(subscription().id))]"
  },
  "resources": [
    {
      "type": "Microsoft.EventGrid/systemTopics",
      "apiVersion": "2022-06-15",
      "name": "[variables('topicName')]",
      "location": "global",
      "properties": {
        "source": "[subscription().id]",
        "topicType": "Microsoft.Resources.Subscriptions"
      }
    },
    {
      "type": "Microsoft.EventGrid/systemTopics/eventSubscriptions",
      "apiVersion": "2022-06-15",
      "name": "[concat(variables('topicName'), '/v19')]",
      "dependsOn": [
        "[resourceId('Microsoft.EventGrid/systemTopics', variables('topicName'))]"
      ],
      "properties": {
        "eventDeliverySchema": "EventGridSchema",
        "filter": {
          "includedEventTypes": [
            "Microsoft.Resources.ResourceWriteSuccess",
            "Microsoft.Resources.ResourceDeleteSuccess",
            "Microsoft.Resources.ResourceActionSuccess"
          ]
        },
        "destination": {
          "endpointType": "WebHook",
          "properties": {
            "endpointUrl": "[parameters('v19Endpoint')]",
            "maxEventsPerBatch": 50,
            "preferredBatchSizeInKilobytes": 512,
            "deliveryAttributeMappings": [
              {
                "name": "Authorization",
                "type": "Static",
                "properties": {
                  "value": "[concat('Bearer ', parameters('v19ApiKey'))]",
                  "isSecret": true
                }
              }
            ]
          }
        },
        "retryPolicy": {
          "maxDeliveryAttempts": 30,
          "eventTimeToLiveInMinutes": 1440
        }
      }
    }
  ],
  "outputs": {
    "systemTopic": {
      "type": "string",
      "value": "[variables('topicName')]"
    }
  }
}
