{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "vmSku": {
      "type": "string",
      "defaultValue": "Standard_D4s_v3",
      "metadata": {
        "description": "Size of VMs in the VM Scale Set."
      }
    },
    "clusterName": {
      "type": "string",
      "maxLength": 61,
      "metadata": {
        "description": "String used as a base for naming resources. Must be 3-61 characters in length and globally unique across Azure. A hash is prepended to this string for some resources, and resource-specific information is appended."
      }
    },
    "db_type": {
      "type": "string",
      "defaultValue": "Postgresql_deploy.json",
      "allowedValues": [
        "Postgresql_deploy.json",
        "MSSQL_deploy.json",
        "Postgresql_existing.json",
        "MSSQL_existing.json"
      ],
      "metadata": {
        "description": "Deploy new Postgresql, MSSQL or use existing DB"
      }
    },
    "manual_db_url": {
      "type": "string",
      "metadata": {
        "description": "DB server URL, if existing DB server is used instead of a new deployment (jdbc:sqlserver://.. or jdbc:postgresql://..)"
      }
    },
    "db_server": {
      "type": "string",
      "metadata": {
        "description": "DB server name, if pre-existing DB is used"
      }
    },
    "db_name": {
      "type": "string",
      "minLength": 1,
      "metadata": {
        "description": "MSSQL Database name"
      }
    },
    "db_edition": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "Basic",
      "allowedValues": [
        "Basic",
        "Standard",
        "Premium"
      ],
      "metadata": {
        "description": "MSSQL Database Edition"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for the resources."
      }
    },
    "nodeCount": {
      "type": "int",
      "defaultValue": 2,
      "minValue": 1,
      "maxValue": 100,
      "metadata": {
        "description": "Number of VM instances (100 or less) for member artifactory nodes. Minimum 1 node."
      }
    },
    "artifactoryVersion": {
      "type": "string",
      "defaultValue": "0.0.42",
      "allowedValues": [
        "0.0.8",
        "0.0.9",
        "0.0.10",
        "0.0.11",
        "0.0.12",
        "0.0.14",
        "0.0.15",
        "0.0.18",
        "0.0.19",
        "0.0.21",
        "0.0.22",
        "0.0.23",
        "0.0.25",
        "0.0.27",
        "0.0.28",
        "0.0.29",
        "0.0.30",
        "0.0.32",
        "0.0.33",
        "0.0.34",
        "0.0.35",
        "0.0.37",
        "0.0.38",
        "0.0.39",
        "0.0.40",
        "0.0.41",
        "0.0.42"
      ],
      "metadata": {
        "description": "Artifactory-vm image version to deploy."
      }
    },
    "masterKey": {
      "type": "securestring",
      "maxLength": 64,
      "metadata": {
        "description": "Master key for Artifactory cluster. Generate master.key using command '$openssl rand -hex 32'"
      }
    },
    "joinKey": {
      "type": "securestring",
      "maxLength": 64,
      "metadata": {
        "description": "Join key for Artifactory cluster. Generate join.key using command '$openssl rand -hex 32'"
      }
    },
    "adminUsername": {
      "type": "string",
      "metadata": {
        "description": "Admin username on all VMs. Follow conventions for azure VM admin user name."
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "description": "Admin password on all VMs. Follow conventions for azure VM admin password rules."
      }
    },
    "artifactoryLicense1": {
      "type": "string",
      "metadata": {
        "description": "Artifactory Enterprise License. Providing one license is mandatory"
      }
    },
    "artifactoryLicense2": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "Artifactory Enterprise License"
      }
    },
    "artifactoryLicense3": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "Artifactory Enterprise License"
      }
    },
    "artifactoryLicense4": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "Artifactory Enterprise License"
      }
    },
    "artifactoryLicense5": {
      "type": "string",
      "defaultValue": "",
      "metadata": {
        "description": "Artifactory Enterprise License"
      }
    },
    "certificate": {
      "type": "securestring",
      "metadata": {
        "description": "To use Artifactory as docker registry you need to provide wild card valid Certificate. Provide your SSL Certificate."
      }
    },
    "certificateKey": {
      "type": "securestring",
      "metadata": {
        "description": "Provide your SSL Certificate key"
      }
    },
    "certificateDomain": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "artifactory",
      "metadata": {
        "description": "Provide your Certificate Domain Name. For e.g jfrog.team for certificate with *.jfrog.team"
      }
    },
    "artifactoryServerName": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "artifactory",
      "metadata": {
        "description": "Provide artifactory server name to be used in Nginx. e.g artifactory for artifactory.jfrog.team"
      }
    },
    "extraJavaOptions": {
      "type": "string",
      "minLength": 1,
      "defaultValue": "-server -Xms2g -Xmx8g -Xss256k -XX:+UseG1GC -XX:OnOutOfMemoryError=\\\"kill -9 %p\\\"",
      "metadata": {
        "description": "Setting Java Memory Parameters for Artifactory. Learn about system requirements for Artifactory https://www.jfrog.com/confluence/display/RTF/System+Requirements#SystemRequirements-RecommendedHardware."
      }
    },
    "db_user": {
      "type": "string",
      "minLength": 1,
      "metadata": {
        "description": "Database Admin user name"
      }
    },
    "db_password": {
      "type": "securestring",
      "minLength": 1,
      "metadata": {
        "description": "Database Admin password"
      }
    },
    "databases": {
      "type": "object",
      "defaultValue": {
        "properties": [
          {
            "name": "artdb",
            "charset": "UTF8",
            "collation": "English_United States.1252"
          }
        ]
      }
    },
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_ZRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "_artifactsLocation": {
      "type": "string",
      "metadata": {
        "description": "The base URI where artifacts required by this template are located. When the template is deployed using the accompanying scripts, a private location in the subscription will be used and this value will be automatically generated."
      },
      "defaultValue": "https://raw.githubusercontent.com/jfrog/JFrog-Cloud-Installers/master/AzureResourceManager/Artifactory/"
    },
    "_artifactsLocationSasToken": {
      "type": "securestring",
      "metadata": {
        "description": "The sasToken required to access _artifactsLocation.  When the template is deployed using the accompanying scripts, a sasToken will be automatically generated."
      },
      "defaultValue": ""
    }
  },
  "variables": {
    "namingInfix": "[toLower(substring(concat(parameters('clusterName'), uniqueString(resourceGroup().id)), 0, 9))]",
    "addressPrefix": "10.0.0.0/16",
    "subnetPrimaryPrefix": "10.0.1.0/24",
    "subnetMemberPrefix": "10.0.2.0/24",
    "virtualNetworkName": "[concat(variables('namingInfix'), 'vnet')]",
    "subnetPrimaryName": "[concat(variables('namingInfix'), 'primarySubnet')]",
    "subnetMemberName": "[concat(variables('namingInfix'), 'memberSubnet')]",
    "scaleSetPrimaryName": "[concat(variables('namingInfix'), 'primaryScaleset')]",
    "scaleSetMemberName": "[concat(variables('namingInfix'), 'memberScaleset')]",
    "lbName": "[concat(variables('namingInfix'), 'lb')]",
    "bepoolPrimaryName": "[concat(variables('lbName'), 'primaryBepool')]",
    "bepoolMemberName": "[concat(variables('lbName'), 'memberBepool')]",
    "fepoolPrimaryName": "[concat(variables('lbName'), 'primaryFepool')]",
    "fepoolMemberName": "[concat(variables('lbName'), 'memberFepool')]",
    "bepoolPrimaryID": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('bepoolPrimaryName'))]",
    "bepoolMemberID": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('bepoolMemberName'))]",
    "feIpConfigPrimaryName": "[concat(variables('fepoolPrimaryName'), 'primaryIpConfig')]",
    "feIpConfigMemberName": "[concat(variables('fepoolMemberName'), 'memberIpConfig')]",
    "feIpConfigPrimaryId": "[resourceId('Microsoft.Network/loadBalancers/frontendIpConfigurations', variables('lbName'), variables('feIpConfigPrimaryName'))]",
    "feIpConfigMemberId": "[resourceId('Microsoft.Network/loadBalancers/frontendIpConfigurations', variables('lbName'), variables('feIpConfigMemberName'))]",
    "pipPrimaryName": "[concat(variables('namingInfix'), 'primaryPip')]",
    "pipMemberName": "[concat(variables('namingInfix'), 'memberPip')]",
    "nicPrimaryName": "[concat(variables('namingInfix'), 'primaryNic')]",
    "nicMemberName": "[concat(variables('namingInfix'), 'memberNic')]",
    "natPoolPrimaryName": "[concat(variables('lbName'), 'primaryNatpool')]",
    "natPoolMemberName": "[concat(variables('lbName'), 'memberNatpool')]",
    "ipConfigName": "[concat(variables('namingInfix'), 'ipconfig')]",
    "httpProbePrimaryName": "primaryHttpProbe",
    "httpsProbePrimaryName": "primaryHttpsProbe",
    "httpProbeMemberName": "memberHttpProbe",
    "httpsProbeMemberName": "memberHttpsProbe",
    "storageAccountName": "[concat(variables('namingInfix'), 'storage')]",
    "vmStorageAccountContainerName": "filestore",
    "azureSqlServerName": "[if(or(equals(parameters('db_type'), 'MSSQL_existing.json'),equals(parameters('db_type'),'Postgresql_existing.json')), parameters('db_server'), concat(variables('namingInfix'), 'sqlsrv'))]",
    "artDBname": "[parameters('databases').properties[0].name]",
    "postgres_db_user": "[concat(parameters('db_user'), '@', variables('azureSqlServerName'))]",
    "db_user": "[if(or(equals(parameters('db_type'), 'Postgresql_deploy.json'),equals(parameters('db_type'),'Postgresql_existing.json')), variables('postgres_db_user'), parameters('db_user'))]",
    "db_password": "[parameters('db_password')]",
    "db_location": "[parameters('location')]",
    "db_name": "[parameters('db_name')]",
    "db_edition": "[parameters('db_edition')]",
    "masterKey": "[parameters('masterKey')]",
    "joinKey": "[parameters('joinKey')]",
    "certificate": "[parameters('certificate')]",
    "certificateKey": "[parameters('certificateKey')]",
    "certificateDomain": "[parameters('certificateDomain')]",
    "artifactoryServerName": "[parameters('artifactoryServerName')]",
    "extraJavaOptions": "[parameters('extraJavaOptions')]",
    "artifactoryLicense1": "[parameters('artifactoryLicense1')]",
    "artifactoryLicense2": "[parameters('artifactoryLicense2')]",
    "artifactoryLicense3": "[parameters('artifactoryLicense3')]",
    "artifactoryLicense4": "[parameters('artifactoryLicense4')]",
    "artifactoryLicense5": "[parameters('artifactoryLicense5')]",
    "osType": {
      "publisher": "jfrog",
      "offer": "artifactory-vm",
      "sku": "artifactory-vm",
      "version": "[parameters('artifactoryVersion')]"
    },
    "imageReference": "[variables('osType')]",
    "dbTemplate": "[parameters('db_type')]",
    "dbTemplateLocation": "[uri(parameters('_artifactsLocation'), concat('nested/', variables('dbTemplate'), parameters('_artifactsLocationSasToken')))]",
    "nsgName": "[concat(variables('namingInfix'), 'nsg')]"
  },
  "resources": [
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "location": "[parameters('location')]",
      "name": "[variables('nsgName')]",
      "apiVersion": "2020-03-01",
      "properties": {
        "securityRules": [
          {
            "name": "ssh-rule",
            "properties": {
              "description": "Allow SSH",
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "22",
              "sourceAddressPrefix": "Internet",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 100,
              "direction": "Inbound"
            }
          },
          {
            "name": "http-artifactory-rule",
            "properties": {
              "description": "Allow HTTP",
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "8081",
              "sourceAddressPrefix": "Internet",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 101,
              "direction": "Inbound"
            }
          },
          {
            "name": "http-nginx-rule",
            "properties": {
              "description": "Allow HTTP",
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "80",
              "sourceAddressPrefix": "Internet",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 102,
              "direction": "Inbound"
            }
          },
          {
            "name": "https-nginx-rule",
            "properties": {
              "description": "Allow HTTP",
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "443",
              "sourceAddressPrefix": "Internet",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 103,
              "direction": "Inbound"
            }
          },
          {
            "name": "membership-rule",
            "properties": {
              "description": "Allow HTTP",
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "10001",
              "sourceAddressPrefix": "Internet",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 105,
              "direction": "Inbound"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[variables('virtualNetworkName')]",
      "location": "[parameters('location')]",
      "apiVersion": "2020-03-01",
      "dependsOn": [
        "[resourceId('Microsoft.Network/networkSecurityGroups/', variables('nsgName'))]"
      ],
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('addressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[variables('subnetPrimaryName')]",
            "properties": {
              "addressPrefix": "[variables('subnetPrimaryPrefix')]",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"
              }
            }
          },
          {
            "name": "[variables('subnetMemberName')]",
            "properties": {
              "addressPrefix": "[variables('subnetMemberPrefix')]",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"
              }
            }
          }
        ]
      }
    },
    {
      "apiVersion": "2020-03-01",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[variables('nicPrimaryName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/publicIPAddresses/', variables('pipPrimaryName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
        "[resourceId('Microsoft.Network/networkSecurityGroups/', variables('nsgName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetPrimaryName'))]"
              }
            }
          }
        ],
        "networkSecurityGroup": {
          "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"
        }
      }
    },
    {
      "apiVersion": "2020-03-01",
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[variables('nicMemberName')]",
      "location": "[parameters('location')]",
      "dependsOn": [
        "[resourceId('Microsoft.Network/publicIPAddresses/', variables('pipMemberName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
        "[resourceId('Microsoft.Network/networkSecurityGroups/', variables('nsgName'))]"
      ],
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "subnet": {
                "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetMemberName'))]"
              }
            }
          }
        ],
        "networkSecurityGroup": {
          "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgName'))]"
        }
      }
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('pipPrimaryName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard"
      },
      "apiVersion": "2020-03-01",
      "properties": {
        "publicIPAllocationMethod": "Static",
        "dnsSettings": {
          "domainNameLabel": "[variables('namingInfix')]"
        }
      }
    },
    {
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('pipMemberName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard"
      },
      "apiVersion": "2020-03-01",
      "properties": {
        "publicIPAllocationMethod": "Static",
        "dnsSettings": {
          "domainNameLabel": "[concat(variables('namingInfix'),'2')]"
        }
      }
    },
    {
      "type": "Microsoft.Network/loadBalancers",
      "name": "[variables('lbName')]",
      "location": "[parameters('location')]",
      "apiVersion": "2020-03-01",
      "sku": {
        "name": "Standard"
      },
      "dependsOn": [
        "[resourceId('Microsoft.Network/publicIPAddresses/', variables('pipPrimaryName'))]",
        "[resourceId('Microsoft.Network/publicIPAddresses/', variables('pipMemberName'))]"
      ],
      "tags": {
        "displayName": "Load Balancer"
      },
      "properties": {
        "frontendIPConfigurations": [
          {
            "name": "[variables('feIpConfigPrimaryName')]",
            "properties": {
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('pipPrimaryName'))]"
              }
            }
          },
          {
            "name": "[variables('feIpConfigMemberName')]",
            "properties": {
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('pipMemberName'))]"
              }
            }
          }
        ],
        "backendAddressPools": [
          {
            "name": "[variables('bepoolPrimaryName')]"
          },
          {
            "name": "[variables('bepoolMemberName')]"
          }
        ],
        "inboundNatPools": [
          {
            "name": "[variables('natPoolPrimaryName')]",
            "properties": {
              "frontendIPConfiguration": {
                "id": "[variables('feIpConfigPrimaryId')]"
              },
              "protocol": "tcp",
              "frontendPortRangeStart": 10022,
              "frontendPortRangeEnd": 11022,
              "backendPort": 22
            }
          },
          {
            "name": "[variables('natPoolMemberName')]",
            "properties": {
              "frontendIPConfiguration": {
                "id": "[variables('feIpConfigMemberId')]"
              },
              "protocol": "tcp",
              "frontendPortRangeStart": 20022,
              "frontendPortRangeEnd": 21022,
              "backendPort": 22
            }
          }
        ],
        "loadBalancingRules": [
          {
            "name": "HTTPRulePrimary",
            "properties": {
              "loadDistribution": "Default",
              "frontendIPConfiguration": {
                "id": "[variables('feIpConfigPrimaryId')]"
              },
              "backendAddressPool": {
                "id": "[variables('bepoolPrimaryID')]"
              },
              "protocol": "Tcp",
              "frontendPort": 80,
              "backendPort": 80,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 5,
              "probe": {
                "id": "[resourceId('Microsoft.Network/loadBalancers/probes', variables('lbName'), variables('httpProbePrimaryName'))]"
              }
            }
          },
          {
            "name": "HTTPSRulePrimary",
            "properties": {
              "loadDistribution": "Default",
              "frontendIPConfiguration": {
                "id": "[variables('feIpConfigPrimaryId')]"
              },
              "backendAddressPool": {
                "id": "[variables('bepoolPrimaryID')]"
              },
              "protocol": "Tcp",
              "frontendPort": 443,
              "backendPort": 443,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 5,
              "probe": {
                "id": "[resourceId('Microsoft.Network/loadBalancers/probes', variables('lbName'), variables('httpsProbePrimaryName'))]"
              }
            }
          },
          {
            "name": "HTTPRuleMember",
            "properties": {
              "loadDistribution": "Default",
              "frontendIPConfiguration": {
                "id": "[variables('feIpConfigMemberId')]"
              },
              "backendAddressPool": {
                "id": "[variables('bepoolMemberID')]"
              },
              "protocol": "Tcp",
              "frontendPort": 80,
              "backendPort": 80,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 5,
              "probe": {
                "id": "[resourceId('Microsoft.Network/loadBalancers/probes', variables('lbName'), variables('httpProbeMemberName'))]"
              }
            }
          },
          {
            "name": "HTTPSRuleMember",
            "properties": {
              "loadDistribution": "Default",
              "frontendIPConfiguration": {
                "id": "[variables('feIpConfigMemberId')]"
              },
              "backendAddressPool": {
                "id": "[variables('bepoolMemberID')]"
              },
              "protocol": "Tcp",
              "frontendPort": 443,
              "backendPort": 443,
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": 5,
              "probe": {
                "id": "[resourceId('Microsoft.Network/loadBalancers/probes', variables('lbName'), variables('httpsProbeMemberName'))]"
              }
            }
          }
        ],
        "probes": [
          {
            "name": "[variables('httpProbePrimaryName')]",
            "properties": {
              "protocol": "Http",
              "requestPath": "/artifactory/api/system/ping",
              "port": 80,
              "intervalInSeconds": 60,
              "numberOfProbes": 5
            }
          },
          {
            "name": "[variables('httpsProbePrimaryName')]",
            "properties": {
              "protocol": "Tcp",
              "port": 443,
              "intervalInSeconds": 60,
              "numberOfProbes": 5
            }
          },
          {
            "name": "[variables('httpProbeMemberName')]",
            "properties": {
              "protocol": "Http",
              "requestPath": "/artifactory/api/system/ping",
              "port": 80,
              "intervalInSeconds": 60,
              "numberOfProbes": 5
            }
          },
          {
            "name": "[variables('httpsProbeMemberName')]",
            "properties": {
              "protocol": "Tcp",
              "port": 443,
              "intervalInSeconds": 60,
              "numberOfProbes": 5
            }
          }
        ]
      }
    },
    {
      "apiVersion": "2019-06-01",
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "tags": {
        "displayName": "Artifactory Storage Account"
      },
      "kind": "Storage",
      "properties": {}
    },
    {
      "type": "Microsoft.Resources/deployments",
      "name": "deploySQLDB",
      "apiVersion": "2019-09-01",
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[variables('dbTemplateLocation')]",
          "contentVersion": "1.0.0.0"
        },
        "parameters": {
          "db_user": {
            "value": "[parameters('db_user')]"
          },
          "db_password": {
            "value": "[variables('db_password')]"
          },
          "db_server": {
            "value": "[variables('azureSqlServerName')]"
          },
          "db_location": {
            "value": "[variables('db_location')]"
          },
          "databases": {
            "value": "[parameters('databases')]"
          },
          "db_name": {
            "value": "[variables('db_name')]"
          },
          "db_edition": {
            "value": "[variables('db_edition')]"
          },
          "manual_db_url": {
            "value": "[parameters('manual_db_url')]"
          }
        }
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachineScaleSets",
      "name": "[variables('scaleSetPrimaryName')]",
      "location": "[parameters('location')]",
      "apiVersion": "2020-06-01",
      "dependsOn": [
        "[resourceId('Microsoft.Network/loadBalancers/', variables('lbName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
        "[resourceId('Microsoft.Resources/deployments/', 'deploySQLDB')]",
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]"
      ],
      "plan": {
        "name": "artifactory-vm",
        "publisher": "jfrog",
        "product": "artifactory-vm"
      },
      "sku": {
        "name": "[parameters('vmSku')]",
        "tier": "Standard",
        "capacity": "1"
      },
      "properties": {
        "singlePlacementGroup": true,
        "overprovision": "false",
        "upgradePolicy": {
          "mode": "Manual"
        },
        "virtualMachineProfile": {
          "storageProfile": {
            "osDisk": {
              "caching": "ReadWrite",
              "diskSizeGB": "250",
              "createOption": "FromImage"
            },
            "imageReference": "[variables('imageReference')]"
          },
          "osProfile": {
            "computerNamePrefix": "[variables('namingInfix')]",
            "adminUsername": "[parameters('adminUsername')]",
            "adminPassword": "[parameters('adminPassword')]",
            "customData": "[base64(concat('#INSTALL SCRIPT INPUTS\nARTIFACTORY_VERSION=', parameters('artifactoryVersion'),'\nCERTIFICATE_KEY=',variables('certificateKey'),'\nCERTIFICATE=', variables('certificate'),'\nCERTIFICATE_DOMAIN=',variables('certificateDomain'),'\nDB_TYPE=',parameters('db_type'),'\nARTIFACTORY_SERVER_NAME=',variables('artifactoryServerName'),'\nEXTRA_JAVA_OPTS=',variables('extraJavaOptions'),'\nJDBC_STR=',reference('Microsoft.Resources/deployments/deploySQLDB').outputs.jdbcConnString.value,'\nDB_NAME=',variables('artDBname'),'\nDB_ADMIN_USER=',variables('db_user'),'\nDB_ADMIN_PASSWD=',variables('db_password'),'\nSTO_ACT_NAME=',variables('storageAccountName'),'\nSTO_ACT_ENDPOINT=',reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob,'\nSTO_CTR_NAME=',variables('vmStorageAccountContainerName'),'\nSTO_ACT_KEY=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2018-07-01').keys[0].value,'\nMASTER_KEY=',variables('masterKey'),'\nJOIN_KEY=',variables('joinKey'),'\nIS_PRIMARY=','true','\nLOCATION=',parameters('location'),'\nLICENSE1=',variables('artifactoryLicense1'),'\nLICENSE2=',variables('artifactoryLicense2'),'\nLICENSE3=',variables('artifactoryLicense3'),'\nLICENSE4=',variables('artifactoryLicense4'),'\nLICENSE5=',variables('artifactoryLicense5'),'\n'))]"
          },
          "networkProfile": {
            "networkInterfaceConfigurations": [
              {
                "name": "[variables('nicPrimaryName')]",
                "properties": {
                  "primary": true,
                  "ipConfigurations": [
                    {
                      "name": "[concat(variables('ipConfigName'),'1')]",
                      "properties": {
                        "subnet": {
                          "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetPrimaryName'))]"
                        },
                        "loadBalancerBackendAddressPools": [
                          {
                            "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('bePoolPrimaryName'))]"
                          }
                        ],
                        "loadBalancerInboundNatPools": [
                          {
                            "id": "[resourceId('Microsoft.Network/loadBalancers/inboundNatPools', variables('lbName'), variables('natPoolPrimaryName'))]"
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          },
          "extensionProfile": {
            "extensions": [
              {
                "name": "extension1",
                "properties": {
                  "publisher": "Microsoft.Azure.Extensions",
                  "type": "CustomScript",
                  "typeHandlerVersion": "2.0",
                  "autoUpgradeMinorVersion": false,
                  "settings": {
                    "fileUris": [
                      "[uri(parameters('_artifactsLocation'), concat('scripts/install_artifactory7.sh', parameters('_artifactsLocationSasToken')))]"
                    ]
                  },
                  "protectedSettings": {
                    "commandToExecute": "bash install_artifactory7.sh && ls -al && pwd >> /var/lib/cloud/instance/user-data.txt"
                  }
                }
              }
            ]
          }
        }
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachineScaleSets",
      "name": "[variables('scaleSetMemberName')]",
      "location": "[parameters('location')]",
      "apiVersion": "2020-06-01",
      "dependsOn": [
        "[resourceId('Microsoft.Network/loadBalancers/', variables('lbName'))]",
        "[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
        "[resourceId('Microsoft.Compute/virtualMachineScaleSets/', variables('scaleSetPrimaryName'))]",
        "[resourceId('Microsoft.Resources/deployments/', 'deploySQLDB')]",
        "[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]"
      ],
      "plan": {
        "name": "artifactory-vm",
        "publisher": "jfrog",
        "product": "artifactory-vm"
      },
      "sku": {
        "name": "[parameters('vmSku')]",
        "tier": "Standard",
        "capacity": "[parameters('nodeCount')]"
      },
      "properties": {
        "singlePlacementGroup": true,
        "overprovision": "false",
        "upgradePolicy": {
          "mode": "Manual"
        },
        "virtualMachineProfile": {
          "storageProfile": {
            "osDisk": {
              "caching": "ReadWrite",
              "diskSizeGB": "250",
              "createOption": "FromImage"
            },
            "imageReference": "[variables('imageReference')]"
          },
          "osProfile": {
            "computerNamePrefix": "[variables('namingInfix')]",
            "adminUsername": "[parameters('adminUsername')]",
            "adminPassword": "[parameters('adminPassword')]",
            "customData": "[base64(concat('#INSTALL SCRIPT INPUTS\nARTIFACTORY_VERSION=', parameters('artifactoryVersion'),'\nCERTIFICATE_KEY=',variables('certificateKey'),'\nCERTIFICATE=', variables('certificate'),'\nCERTIFICATE_DOMAIN=',variables('certificateDomain'),'\nDB_TYPE=',parameters('db_type'),'\nARTIFACTORY_SERVER_NAME=',variables('artifactoryServerName'),'\nEXTRA_JAVA_OPTS=',variables('extraJavaOptions'),'\nJDBC_STR=',reference('Microsoft.Resources/deployments/deploySQLDB').outputs.jdbcConnString.value,'\nDB_NAME=',variables('artDBname'),'\nDB_ADMIN_USER=',variables('db_user'),'\nDB_ADMIN_PASSWD=',variables('db_password'),'\nSTO_ACT_NAME=',variables('storageAccountName'),'\nSTO_ACT_ENDPOINT=',reference(resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))).primaryEndpoints.blob,'\nSTO_CTR_NAME=',variables('vmStorageAccountContainerName'),'\nSTO_ACT_KEY=',listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2018-07-01').keys[0].value,'\nMASTER_KEY=',variables('masterKey'),'\nJOIN_KEY=',variables('joinKey'),'\nIS_PRIMARY=','false','\nLOCATION=',parameters('location'),'\nLICENSE1=',variables('artifactoryLicense1'),'\nLICENSE2=',variables('artifactoryLicense2'),'\nLICENSE3=',variables('artifactoryLicense3'),'\nLICENSE4=',variables('artifactoryLicense4'),'\nLICENSE5=',variables('artifactoryLicense5'),'\n'))]"
          },
          "networkProfile": {
            "networkInterfaceConfigurations": [
              {
                "name": "[variables('nicMemberName')]",
                "properties": {
                  "primary": true,
                  "ipConfigurations": [
                    {
                      "name": "[concat(variables('ipConfigName'),'1')]",
                      "properties": {
                        "subnet": {
                          "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetMemberName'))]"
                        },
                        "loadBalancerBackendAddressPools": [
                          {
                            "id": "[resourceId('Microsoft.Network/loadBalancers/backendAddressPools', variables('lbName'), variables('bePoolMemberName'))]"
                          }
                        ],
                        "loadBalancerInboundNatPools": [
                          {
                            "id": "[resourceId('Microsoft.Network/loadBalancers/inboundNatPools', variables('lbName'), variables('natPoolMemberName'))]"
                          }
                        ]
                      }
                    }
                  ]
                }
              }
            ]
          },
          "extensionProfile": {
            "extensions": [
              {
                "name": "extension1",
                "properties": {
                  "publisher": "Microsoft.Azure.Extensions",
                  "type": "CustomScript",
                  "typeHandlerVersion": "2.0",
                  "autoUpgradeMinorVersion": false,
                  "settings": {
                    "fileUris": [
                      "[uri(parameters('_artifactsLocation'), concat('scripts/install_artifactory7.sh', parameters('_artifactsLocationSasToken')))]"
                    ]
                  },
                  "protectedSettings": {
                    "commandToExecute": "bash install_artifactory7.sh && ls -al && pwd >> /var/lib/cloud/instance/user-data.txt"
                  }
                }
              }
            ]
          }
        }
      }
    }
  ],
  "outputs": {
    "fqdn": {
      "value": "[reference(resourceId('Microsoft.Network/publicIPAddresses',variables('pipMemberName')),'2018-07-01').dnsSettings.fqdn]",
      "type": "string"
    }
  }
}
