{
  "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "description": "ShiftZero by Byte Logic: temporary Azure Lighthouse access for one managed Azure SQL move. Deploy once in EACH subscription involved (the one the SQL server is in, and the one it moves to). Built-in roles only. Remove it at any time in the Azure portal: Service providers > Delegations. Based on Microsoft's Azure-Lighthouse-samples multi-rg.json."
  },
  "parameters": {
    "resourceGroupNames": {
      "type": "array",
      "metadata": {
        "description": "Resource groups in THIS subscription that Byte Logic may change: the group holding the SQL server (source subscription) or the group it moves into (target subscription). They must already exist. Example: [\"rg-sql-prod\"]"
      }
    },
    "grantSubscriptionReader": {
      "type": "bool",
      "defaultValue": true,
      "metadata": {
        "description": "Read-only access to this whole subscription, so Byte Logic can check the move beforehand and follow it to completion (Azure keeps move status at subscription level). No changes are possible with it."
      }
    },
    "offerName": {
      "type": "string",
      "defaultValue": "ShiftZero by Byte Logic (temporary)",
      "metadata": {
        "description": "Name shown to the customer under Service providers."
      }
    },
    "managedByTenantId": {
      "type": "string",
      "defaultValue": "23ec1e57-60b1-48c9-b345-7687a3b8d9d0",
      "metadata": {
        "description": "Byte Logic's Microsoft Entra tenant."
      }
    },
    "operatorPrincipalId": {
      "type": "string",
      "defaultValue": "4c558ce5-8554-4ce4-bb71-e1907e0cc16a",
      "metadata": {
        "description": "The Byte Logic engineer's object ID in Byte Logic's tenant."
      }
    },
    "operatorDisplayName": {
      "type": "string",
      "defaultValue": "Byte Logic engineer"
    }
  },
  "variables": {
    "contributor": "b24988ac-6180-42a0-ab88-20f7382dd24c",
    "reader": "acdd72a7-3385-48ef-bd42-f606fba81ae7",
    "deleteOwnAccess": "91c1777a-f3dc-4fae-b103-61d183457e46",
    "rgAuthorizations": [
      {
        "principalId": "[parameters('operatorPrincipalId')]",
        "principalIdDisplayName": "[concat(parameters('operatorDisplayName'), ' - ShiftZero move')]",
        "roleDefinitionId": "[variables('contributor')]"
      },
      {
        "principalId": "[parameters('operatorPrincipalId')]",
        "principalIdDisplayName": "[concat(parameters('operatorDisplayName'), ' - can remove this access')]",
        "roleDefinitionId": "[variables('deleteOwnAccess')]"
      }
    ],
    "subAuthorizations": [
      {
        "principalId": "[parameters('operatorPrincipalId')]",
        "principalIdDisplayName": "[concat(parameters('operatorDisplayName'), ' - read only')]",
        "roleDefinitionId": "[variables('reader')]"
      },
      {
        "principalId": "[parameters('operatorPrincipalId')]",
        "principalIdDisplayName": "[concat(parameters('operatorDisplayName'), ' - can remove this access')]",
        "roleDefinitionId": "[variables('deleteOwnAccess')]"
      }
    ],
    "subDefinitionName": "[guid(subscription().subscriptionId, parameters('offerName'), 'subscription-reader')]"
  },
  "resources": [
    {
      "type": "Microsoft.ManagedServices/registrationDefinitions",
      "apiVersion": "2022-10-01",
      "name": "[guid(subscription().subscriptionId, parameters('offerName'), parameters('resourceGroupNames')[copyIndex()])]",
      "copy": {
        "name": "rgDefinitions",
        "count": "[length(parameters('resourceGroupNames'))]"
      },
      "properties": {
        "registrationDefinitionName": "[concat(parameters('offerName'), ' - ', parameters('resourceGroupNames')[copyIndex()])]",
        "description": "[concat('Contributor on resource group ', parameters('resourceGroupNames')[copyIndex()], ' for a managed ShiftZero move by Byte Logic. Temporary.')]",
        "managedByTenantId": "[parameters('managedByTenantId')]",
        "authorizations": "[variables('rgAuthorizations')]"
      }
    },
    {
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2021-04-01",
      "name": "[concat('shiftzero-rg', copyIndex())]",
      "resourceGroup": "[parameters('resourceGroupNames')[copyIndex()]]",
      "copy": {
        "name": "rgAssignments",
        "count": "[length(parameters('resourceGroupNames'))]"
      },
      "dependsOn": [
        "rgDefinitions"
      ],
      "properties": {
        "mode": "Incremental",
        "template": {
          "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
          "contentVersion": "1.0.0.0",
          "resources": [
            {
              "type": "Microsoft.ManagedServices/registrationAssignments",
              "apiVersion": "2022-10-01",
              "name": "[guid(subscription().subscriptionId, parameters('offerName'), parameters('resourceGroupNames')[copyIndex()])]",
              "properties": {
                "registrationDefinitionId": "[subscriptionResourceId('Microsoft.ManagedServices/registrationDefinitions', guid(subscription().subscriptionId, parameters('offerName'), parameters('resourceGroupNames')[copyIndex()]))]"
              }
            }
          ]
        }
      }
    },
    {
      "condition": "[parameters('grantSubscriptionReader')]",
      "type": "Microsoft.ManagedServices/registrationDefinitions",
      "apiVersion": "2022-10-01",
      "name": "[variables('subDefinitionName')]",
      "properties": {
        "registrationDefinitionName": "[concat(parameters('offerName'), ' - read only')]",
        "description": "Read-only access to this subscription so Byte Logic can check and follow the ShiftZero move. Temporary.",
        "managedByTenantId": "[parameters('managedByTenantId')]",
        "authorizations": "[variables('subAuthorizations')]"
      }
    },
    {
      "condition": "[parameters('grantSubscriptionReader')]",
      "type": "Microsoft.ManagedServices/registrationAssignments",
      "apiVersion": "2022-10-01",
      "name": "[variables('subDefinitionName')]",
      "dependsOn": [
        "[subscriptionResourceId('Microsoft.ManagedServices/registrationDefinitions', variables('subDefinitionName'))]"
      ],
      "properties": {
        "registrationDefinitionId": "[subscriptionResourceId('Microsoft.ManagedServices/registrationDefinitions', variables('subDefinitionName'))]"
      }
    }
  ],
  "outputs": {
    "delegatedResourceGroups": {
      "type": "array",
      "value": "[parameters('resourceGroupNames')]"
    },
    "subscriptionReader": {
      "type": "bool",
      "value": "[parameters('grantSubscriptionReader')]"
    },
    "toRemove": {
      "type": "string",
      "value": "Azure portal > Service providers > Delegations > remove the 'ShiftZero by Byte Logic' entries"
    }
  }
}