{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "db_user": {
      "type": "string",
      "defaultValue": "xray",
      "minLength": 1
    },
    "db_password": {
      "type": "securestring"
    },
    "db_server": {
      "type": "string",
      "defaultValue": "xraypostgressrv",
      "minLength": 1
    },
    "db_name": {
      "type": "string",
      "defaultValue": "artrtdb",
      "minLength": 1
    },
    "db_location": {
      "type": "string"
    },
    "skuCapacity": {
      "type": "int",
      "defaultValue": 2
    },
    "skuFamily": {
      "type": "string",
      "defaultValue": "Gen5"
    },
    "skuName": {
      "type": "string",
      "defaultValue": "GP_Gen5_2"
    },
    "skuSizeMB": {
      "type": "int",
      "defaultValue": 102400
    },
    "skuTier": {
      "type": "string",
      "defaultValue": "GeneralPurpose"
    },
    "version": {
      "type": "string",
      "defaultValue": "11"
    },
    "backupRetentionDays": {
      "type": "int",
      "defaultValue": 7
    },
    "geoRedundantBackup": {
      "type": "string",
      "defaultValue": "Disabled"
    },
    "databases": {
      "type": "object"
    },
    "sslEnforcement": {
      "type": "string",
      "allowedValues": [
        "Enabled",
        "Disabled"
      ],
      "defaultValue": "Disabled",
      "metadata": {
        "description": "SSL Enforcement"
      }
    },
    "publicNetworkAccess": {
      "type": "string",
      "allowedValues": [
        "Enabled",
        "Disabled"
      ],
      "defaultValue": "Enabled",
      "metadata": {
        "description": "Public Network Access"
      }
    },
    "db_edition": {
      "type": "string",
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ]
    },
    "manual_db_url": {
      "type": "string"
    }
  },
  "variables": {
  },
  "resources": [
    {
      "type": "Microsoft.DBforPostgreSQL/servers",
      "apiVersion": "2017-12-01",
      "location": "[parameters('db_location')]",
      "name": "[parameters('db_server')]",
      "properties": {
        "version": "[parameters('version')]",
        "administratorLogin": "[parameters('db_user')]",
        "administratorLoginPassword": "[parameters('db_password')]",
        "publicNetworkAccess": "[parameters('publicNetworkAccess')]",
        "sslEnforcement": "[parameters('sslEnforcement')]",
        "storageProfile": {
          "storageMB": "[parameters('skuSizeMB')]",
          "backupRetentionDays": "[parameters('backupRetentionDays')]",
          "geoRedundantBackup": "[parameters('geoRedundantBackup')]"
        }
      },
      "sku": {
        "name": "[parameters('skuName')]",
        "tier": "[parameters('skuTier')]",
        "capacity": "[parameters('skuCapacity')]",
        "size": "[parameters('skuSizeMB')]",
        "family": "[parameters('skuFamily')]"
      },
      "resources": [
        {
          "name": "[uniqueString(parameters('db_server'), 'AllowAllWindowsAzureIps' )]",
          "type": "firewallRules",
          "apiVersion": "2017-12-01",
          "location": "[parameters('db_location')]",
          "dependsOn": [
            "[resourceId('Microsoft.DBforPostgreSQL/servers', parameters('db_server'))]"
          ],
          "properties": {
            "startIpAddress": "0.0.0.0",
            "endIpAddress": "0.0.0.0"
          }
        },
        {
          "type": "Microsoft.DBforPostgreSQL/servers/databases",
          "apiversion": "2017-12-01",
          "name": "[concat(parameters('db_server'), '/', parameters('databases').properties[0].name)]",
          "dependsOn": [
            "[resourceId('Microsoft.DBforPostgreSQL/servers', parameters('db_server'))]"
          ],
          "properties": {
            "charset": "[parameters('databases').properties[0].charset]",
            "collation": "[parameters('databases').properties[0].collation]"
          }
        }
      ]
    }
  ],
  "outputs": {
    "jdbcConnString": {
      "type": "string",
      "value": "[concat('jdbc:postgresql://', reference(resourceId('Microsoft.DBforPostgreSQL/servers/', parameters('db_server'))).fullyQualifiedDomainName, ':5432')]"
    },
    "dbServerName": {
      "type": "string",
      "value": "[parameters('db_server')]"
    },
    "xrayConnString": {
      "type": "string",
      "value": "[concat('postgres://', reference(resourceId('Microsoft.DBforPostgreSQL/servers/', parameters('db_server'))).fullyQualifiedDomainName, ':5432')]"
    },
    "db_name": {
      "type": "string",
      "value": "[parameters('db_name')]"
    },
    "db_edition": {
      "type": "string",
      "value": "[parameters('db_edition')]"
    },
    "dbUrl": {
      "type": "string",
      "value": "[parameters('manual_db_url')]"
    }
  }
}
