{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "baseName": {
      "type": "string",
      "defaultValue": "[resourceGroup().name]",
      "metadata": {
        "description": "The base resource name."
      }
    },
    "subscriptionId": {
      "type": "string",
      "defaultValue": "[subscription().subscriptionId]",
      "metadata": {
        "description": "The subscription ID to which the application and resources belong."
      }
    },
    "tenantId": {
      "type": "string",
      "defaultValue": "[subscription().tenantId]",
      "metadata": {
        "description": "The tenant ID to which the application and resources belong."
      }
    },
    "testApplicationOid": {
      "type": "string",
      "metadata": {
        "description": "The client OID to grant access to test resources."
      }
    },
    "testApplicationId": {
      "type": "string",
      "metadata": {
        "description": "The application client ID used to run tests."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "The location of the resources. By default, this is the same as the resource group."
      }
    },
    "storageEndpointSuffix": {
      "type": "string",
      "defaultValue": "core.windows.net",
      "metadata": {
        "description": "The url suffix to use when creating storage connection strings."
      }
    },
    "perTestExecutionLimitMinutes": {
      "type": "string",
      "defaultValue": "15",
      "metadata": {
        "description": "The maximum duration, in minutes, that a single test is permitted to run before it is considered at-risk for being hung."
      }
    }
  },
  "variables": {
    "apiVersion": "2017-04-01",
    "eventHubName": "testEventHub",
    "consumerGroup": "defaultGroup",
    "contributorRoleId": "b24988ac-6180-42a0-ab88-20f7382dd24c",
    "eventHubsDataOwnerRoleId": "f526a384-b230-433a-b45c-95f59c4a2dec",
    "storageDataOwnerRoleId": "b7e6dc6d-f1e8-4753-8033-0f276bb0955b",
    "eventHubsNamespace": "[concat('eh-', parameters('baseName'))]",
    "storageAccount": "[concat('blb', parameters('baseName'))]",
    "defaultSASKeyName": "RootManageSharedAccessKey",
    "eventHubsAuthRuleResourceId": "[resourceId('Microsoft.EventHub/namespaces/authorizationRules', variables('eventHubsNamespace'), variables('defaultSASKeyName'))]",
    "storageAccountId": "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccount'))]",
    "authorizationName": "[concat(variables('eventHubsNamespace'), '/RootManageSharedAccessKey')]"
  },
  "resources": [
    {
      "type": "Microsoft.EventHub/Namespaces",
      "apiVersion": "[variables('apiVersion')]",
      "name": "[variables('eventHubsNamespace')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard",
        "tier": "Standard",
        "capacity": 5
      }
    },
    {
      "type": "Microsoft.EventHub/Namespaces/AuthorizationRules",
      "apiVersion": "[variables('apiVersion')]",
      "name": "[variables('authorizationName')]",
      "location": "[parameters('location')]",
      "dependsOn": [ "[resourceId('Microsoft.EventHub/namespaces', variables('eventHubsNamespace'))]" ],
      "properties": {
        "rights": [ "Listen", "Manage", "Send" ]
      }
    },
    {
      "type": "Microsoft.EventHub/Namespaces/EventHubs",
      "apiVersion": "[variables('apiVersion')]",
      "name": "[concat(variables('eventHubsNamespace'), '/', variables('eventHubName'))]",
      "location": "[parameters('location')]",
      "dependsOn": [ "[resourceId('Microsoft.EventHub/Namespaces', variables('eventHubsNamespace'))]" ],
      "properties": {
        "messageRetentionInDays": 1,
        "partitionCount": 4
      }
    },
    {
      "type": "Microsoft.EventHub/Namespaces/NetworkRuleSets",
      "apiVersion": "[variables('apiVersion')]",
      "name": "[concat(variables('eventHubsNamespace'), '/default')]",
      "location": "[parameters('location')]",
      "dependsOn": [ "[resourceId('Microsoft.EventHub/Namespaces', variables('eventHubsNamespace'))]" ],
      "properties": {
        "defaultAction": "Deny",
        "virtualNetworkRules": [],
        "ipRules": []
      }
    },
    {
      "type": "Microsoft.EventHub/Namespaces/EventHubs/ConsumerGroups",
      "apiVersion": "[variables('apiVersion')]",
      "name": "[concat(variables('eventHubsNamespace'), '/', variables('eventHubName'), '/', variables('consumerGroup'))]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.EventHub/namespaces/eventhubs', variables('eventHubsNamespace'), variables('eventHubName'))]",
        "[resourceId('Microsoft.EventHub/namespaces', variables('eventHubsNamespace'))]"
      ],
      "properties": {}
    },
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2019-04-01",
      "name": "[variables('storageAccount')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS",
        "tier": "Standard"
      },
      "kind": "BlobStorage",
      "properties": {
        "networkAcls": {
          "bypass": "AzureServices",
          "virtualNetworkRules": [],
          "ipRules": [],
          "defaultAction": "Allow"
        },
        "supportsHttpsTrafficOnly": true,
        "encryption": {
          "services": {
            "file": {
              "enabled": true
            },
            "blob": {
              "enabled": true
            }
          },
          "keySource": "Microsoft.Storage"
        },
        "accessTier": "Hot"
      }
    },
    {
      "type": "Microsoft.Storage/storageAccounts/blobServices",
      "apiVersion": "2019-04-01",
      "name": "[concat(variables('storageAccount'), '/default')]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccount'))]"
      ],
      "properties": {
        "cors": {
          "corsRules": []
        },
        "deleteRetentionPolicy": {
          "enabled": false
        }
      }
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2019-04-01-preview",
      "name": "[guid(resourceGroup().id, parameters('testApplicationOid'), variables('eventHubsDataOwnerRoleId'))]",
      "dependsOn": [
        "[resourceId('Microsoft.EventHub/Namespaces', variables('eventHubsNamespace'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccount'))]"
      ],
      "properties": {
        "roleDefinitionId": "[resourceId('Microsoft.Authorization/roleDefinitions', variables('eventHubsDataOwnerRoleId'))]",
        "principalId": "[parameters('testApplicationOid')]",
        "scope": "[resourceGroup().id]"
      }
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2019-04-01-preview",
      "name": "[guid(resourceGroup().id, parameters('testApplicationOid'), variables('contributorRoleId'))]",
      "dependsOn": [
        "[resourceId('Microsoft.EventHub/Namespaces', variables('eventHubsNamespace'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccount'))]"
      ],
      "properties": {
        "roleDefinitionId": "[resourceId('Microsoft.Authorization/roleDefinitions', variables('contributorRoleId'))]",
        "principalId": "[parameters('testApplicationOid')]",
        "scope": "[resourceGroup().id]"
      }
    },
    {
      "type": "Microsoft.Authorization/roleAssignments",
      "apiVersion": "2019-04-01-preview",
      "name": "[guid(resourceGroup().id, parameters('testApplicationOid'), variables('storageDataOwnerRoleId'))]",
      "dependsOn": [
        "[resourceId('Microsoft.EventHub/Namespaces', variables('eventHubsNamespace'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccount'))]"
      ],
      "properties": {
        "roleDefinitionId": "[resourceId('Microsoft.Authorization/roleDefinitions', variables('storageDataOwnerRoleId'))]",
        "principalId": "[parameters('testApplicationOid')]",
        "scope": "[resourceGroup().id]"
      }
    }
  ],
  "outputs": {
    "EVENTHUB_NAME": {
      "type": "string",
      "value": "[variables('eventHubName')]"
    },
    "EVENTHUBS_HOST": {
      "type": "string",
      "value": "[concat(variables('eventHubsNamespace'), '.servicebus.windows.net')]"
    },
    "EVENTHUBS_NAMESPACE": {
      "type": "string",
      "value": "[variables('eventHubsNamespace')]"
    },
    "EVENTHUB_STORAGE_ENDPOINT_SUFFIX": {
      "type": "string",
      "value": "[parameters('storageEndpointSuffix')]"
    },
    "EVENTHUB_PER_TEST_LIMIT_MINUTES": {
      "type": "string",
      "value": "[parameters('perTestExecutionLimitMinutes')]"
    },
    "EVENTHUB_CONNECTION_STRING": {
      "type": "string",
      "value": "[listkeys(variables('eventHubsAuthRuleResourceId'), '2015-08-01').primaryConnectionString]"
    },
    "EVENTHUB_CONSUMER_GROUP": {
      "type": "string",
      "value": "[variables('consumerGroup')]"
    },
    "CHECKPOINTSTORE_STORAGE_URL": {
      "type": "string",
      "value": "[concat('https://', variables('storageAccount'), '.blob.', parameters('storageEndpointSuffix'))]"
    },
    "CHECKPOINTSTORE_STORAGE_CONNECTION_STRING": {
      "type": "string",
      "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccount'), ';AccountKey=', listKeys(variables('storageAccountId'), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value, ';EndpointSuffix=', parameters('storageEndpointSuffix'))]"
    },
    "AZURE_SUBSCRIPTION_ID": {
      "type": "string",
      "value": "[subscription().subscriptionId]"
    },
    "AZURE_TENANT_ID": {
      "type": "string",
      "value": "[parameters('tenantId')]"
    },
    "AZURE_CLIENT_ID": {
      "type": "string",
      "value": "[parameters('testApplicationId')]"
    }
  }
}
