{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "metadata": {
    "_generator": {
      "name": "bicep",
      "version": "0.46.1.21595",
      "templateHash": "5854626971821916783"
    },
    "VeloDBAzureBYOCTemplate": {
      "version": "2024-10-30",
      "description": "During deployment, two user identities will be created with relevant permissions.\n  A virtual machine will be deployed in your virtual network to install agent for management.\n  We'll also establish a private link to velodb virtual network for commands and metrics.\n  Rest assured, data always stays in your environment.\n  Find more for BYOC guidance: https://docs.velodb.io/cloud/management-guide/warehouse-management/create-byoc-warehouse/azure\n"
    }
  },
  "parameters": {
    "Token": {
      "type": "string",
      "metadata": {
        "source": "velodb",
        "description": "The token generated by VeloDB Cloud uniquely identifies the your VirtualNetwork. Please do not modify it."
      }
    },
    "VirtualNetwork": {
      "type": "string",
      "metadata": {
        "source": "user",
        "description": "Please enter the full resource id of the virtual network where the BYOC warehouse is deployed, e.g. /subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Network/virtualNetworks/{vnet-name}."
      }
    },
    "Subnet": {
      "type": "string",
      "metadata": {
        "source": "user",
        "description": "Please enter the full resource id of the subnet where the BYOC warehouse is deployed, e.g. /subscriptions/{subscription-id}/resourceGroups/{resource-group}/providers/Microsoft.Network/virtualNetworks/{vnet-name}/subnets/{subnet-name}."
      }
    }
  },
  "variables": {
    "RegionMap": {
      "westus3": {
        "ScriptUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure.sh",
        "TemplateUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure-byoc-v1.json"
      },
      "centralindia": {
        "ScriptUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure.sh",
        "TemplateUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure-byoc-v1.json"
      },
      "eastus2": {
        "ScriptUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure.sh",
        "TemplateUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure-byoc-v1.json"
      },
      "germanywestcentral": {
        "ScriptUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure.sh",
        "TemplateUrl": "https://onlinevelodbwestus3.blob.core.windows.net/velodb-cloud-online-westus3/public/azure-byoc-v1.json"
      }
    },
    "location": "[resourceGroup().location]",
    "lastIndex": "[lastIndexOf(deployment().name, '-')]",
    "StackName": "[if(equals(variables('lastIndex'), -1), deployment().name, substring(deployment().name, 0, variables('lastIndex')))]",
    "virtualNetworkParts": "[split(parameters('VirtualNetwork'), '/')]",
    "virtualNetworkResourceGroup": "[variables('virtualNetworkParts')[4]]",
    "virtualNetworkName": "[variables('virtualNetworkParts')[8]]",
    "subnetName": "[last(split(parameters('Subnet'), '/'))]",
    "scriptArguments": [
      "[format('\"{0}\"', replace(parameters('Token'), '\"', '\\\"'))]",
      "[format('\"{0}\"', replace(variables('location'), '\"', '\\\"'))]",
      "[format('\"{0}\"', replace(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, variables('virtualNetworkResourceGroup')), 'Microsoft.Network/virtualNetworks', variables('virtualNetworkName')), '\"', '\\\"'))]",
      "[format('\"{0}\"', replace(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, variables('virtualNetworkResourceGroup')), 'Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName')), '\"', '\\\"'))]"
    ]
  },
  "resources": [
    {
      "type": "Microsoft.Resources/deploymentScripts",
      "apiVersion": "2023-08-01",
      "name": "[format('velodb-vpc-check-script-{0}', variables('StackName'))]",
      "location": "[variables('location')]",
      "tags": {
        "resource-created-by": "velodb",
        "cloud-resource-profile": "online",
        "resource-used-by-app": "BYOC"
      },
      "kind": "AzureCLI",
      "properties": {
        "primaryScriptUri": "[variables('RegionMap')[variables('location')].ScriptUrl]",
        "azCliVersion": "2.50.0",
        "arguments": "[join(variables('scriptArguments'), ' ')]",
        "timeout": "PT30M",
        "cleanupPreference": "OnSuccess",
        "retentionInterval": "PT1H"
      }
    },
    {
      "type": "Microsoft.Resources/deploymentStacks",
      "apiVersion": "2024-03-01",
      "name": "[format('velodb-deployment-stack-{0}', variables('StackName'))]",
      "tags": {
        "resource-created-by": "velodb",
        "cloud-resource-profile": "online",
        "resource-used-by-app": "BYOC"
      },
      "properties": {
        "actionOnUnmanage": {
          "resources": "delete"
        },
        "denySettings": {
          "mode": "none"
        },
        "description": "This nested deployment stack will create resources to build byoc base environment.",
        "parameters": {
          "Token": {
            "value": "[parameters('Token')]"
          },
          "VirtualNetwork": {
            "value": "[variables('virtualNetworkName')]"
          },
          "VirtualNetworkResourceGroup": {
            "value": "[variables('virtualNetworkResourceGroup')]"
          },
          "Subnet": {
            "value": "[variables('subnetName')]"
          },
          "SkipStack": {
            "value": "[or(or(equals(reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.invalidToken, true()), equals(reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.hasBeenUsedByOtherOrg, true())), equals(reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.hasBeenUsed, true()))]"
          },
          "InstanceType": {
            "value": "[reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.instanceType]"
          }
        },
        "templateLink": {
          "contentVersion": "1.0.0.0",
          "uri": "[variables('RegionMap')[variables('location')].TemplateUrl]"
        }
      },
      "dependsOn": [
        "[resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName')))]"
      ]
    }
  ],
  "outputs": {
    "VeloDBStackName": {
      "type": "string",
      "metadata": {
        "description": "The name of the deployment stack"
      },
      "value": "[variables('StackName')]"
    },
    "VeloDBScriptLogs": {
      "type": "array",
      "metadata": {
        "description": "The logs of the deployment script"
      },
      "value": "[split(reference(resourceId('Microsoft.Resources/deploymentScripts/logs', format('velodb-vpc-check-script-{0}', variables('StackName')), 'default'), '2020-10-01').log, '\n')]"
    },
    "SkipStack": {
      "type": "bool",
      "metadata": {
        "description": "Whether the nested deployment stack should be skipped"
      },
      "value": "[or(or(equals(reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.invalidToken, true()), equals(reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.hasBeenUsedByOtherOrg, true())), equals(reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.hasBeenUsed, true()))]"
    },
    "InstanceType": {
      "type": "string",
      "metadata": {
        "description": "The instance type of agent"
      },
      "value": "[reference(resourceId('Microsoft.Resources/deploymentScripts', format('velodb-vpc-check-script-{0}', variables('StackName'))), '2023-08-01').outputs.instanceType]"
    }
  }
}