{
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "newStorageAccountName": {
      "type": "string",
      "metadata": {
        "description": "Name of the of the storage account for VM OS Disk"
      }
    },
    "publicDomainName": {
      "type": "string",
      "metadata": {
        "description": "Domain name associated with the load balancer public IP"
      }
    },
    "dnsPrefixNameForPublicIP": {
        "type": "string",
        "metadata": {
            "description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
        }
    },
    "vmSize": {
      "type": "string",
      "defaultValue": "Standard_A3",
      "allowedValues": [
        "Standard_A0",
        "Standard_A1",
        "Standard_A2",
        "Standard_A3",
        "Standard_A4",
        "Standard_D1",
        "Standard_DS1",
        "Standard_D2",
        "Standard_DS2",
        "Standard_D3",
        "Standard_DS3",
        "Standard_D4",
        "Standard_DS4",
        "Standard_D11",
        "Standard_DS11",
        "Standard_D12",
        "Standard_DS12",
        "Standard_D13",
        "Standard_DS13",
        "Standard_D14",
        "Standard_DS14"
      ],
      "metadata": {
        "description": "Instance size for the VMs"
      }
    },
    "adminUserName": {
      "type": "string",
      "metadata": {
        "description": "Username to login to the VMs"
      }
    },
    "sshKeyData": {
      "type": "string",
      "metadata": {
        "description": "Public key for SSH authentication"
      }
    },
    "customData": {
      "type": "string",
      "metadata": {
        "description": "Base64-encoded cloud-config.yaml file to deploy and start fleet"
      }
    },
    "numberOfNodes": {
      "type": "int",
      "metadata": {
        "description": "Number of member nodes"
      },
      "defaultValue": "3"
    },
    "dockerVolumeSize": {
      "type": "int",
      "metadata": {
        "description": "Size in GB of the Docker volume"
      },
      "defaultValue": "100"
    },
    "coreosVersion": {
      "type": "string",
      "metadata": {
        "description": "Version of CoreOS to deploy"
      }
    },
    "storageAccountType": {
      "type": "string",
      "metadata": {
        "description": "Storage account type"
      },
      "allowedValues": [
        "Standard_LRS",
        "Premium_LRS"
      ],
      "defaultValue": "Premium_LRS"
    }
  },
  "variables": {
    "addressPrefix": "10.0.0.0/16",
    "subnet1Name": "Subnet-1",
    "subnet1Prefix": "10.0.0.0/24",
    "publicIPAddressType": "Dynamic",
    "imagePublisher": "CoreOS",
    "imageOffer": "CoreOS",
    "imageSKU": "Stable",
    "vmNamePrefix": "deisNode",
    "virtualNetworkName": "deisvNet",
    "availabilitySetName": "deisAvailabilitySet",
    "loadBalancerName": "loadBalancer",
    "loadBalancerAPIRuleName": "loadBalancerAPIRule",
    "loadBalancerBuilderRuleName": "loadBalancerBuildRule",
    "loadBalancerPublicIPName": "loadBalancerIP",
    "loadBalancerIPConfigName": "loadBalancerIPConfig",
    "lbBackendAddressPoolName": "lbBackendAddressPool",
    "apiProbeName": "apiProbe",
    "builderProbeName": "builderProbe",
    "sshKeyPath": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]",
    "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
    "subnet1Ref": "[concat(variables('vnetID'),'/subnets/',variables('subnet1Name'))]",
    "lbID": "[resourceId('Microsoft.Network/loadBalancers',variables('loadBalancerName'))]",
    "lbAPIRuleID": "[concat(variables('lbID'),'/loadBalancingRules/',variables('loadBalancerAPIRuleName'))]",
    "lbBuilderRuleID": "[concat(variables('lbID'),'/loadBalancingRules/',variables('loadBalancerBuilderRuleName'))]",
    "lbIPConfig": "[concat(variables('lbID'),'/frontendIPConfigurations/',variables('loadBalancerIPConfigName'))]",
    "apiProbeID": "[concat(variables('lbID'),'/probes/',variables('apiProbeName'))]",
    "builderProbeID": "[concat(variables('lbID'),'/probes/',variables('builderProbeName'))]",
    "lbPoolID": "[concat(variables('lbID'),'/backendAddressPools/',variables('lbBackendAddressPoolName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Compute/availabilitySets",
      "name": "[variables('availabilitySetName')]",
      "apiVersion": "2015-05-01-preview",
      "location": "[resourceGroup().location]",
      "properties": {}
    },
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[parameters('newStorageAccountName')]",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-05-01-preview",
      "properties": {
        "accountType": "[parameters('storageAccountType')]"
      }
    },
    {
      "apiVersion": "2015-05-01-preview",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('loadBalancerPublicIPName')]",
      "location": "[resourceGroup().location]",
      "properties": {
        "publicIPAllocationMethod": "Dynamic",
        "dnsSettings": {
          "domainNameLabel": "[parameters('publicDomainName')]"
        }
      }
    },
    {
        "apiVersion": "2015-05-01-preview",
        "type": "Microsoft.Network/publicIPAddresses",
        "name": "[concat(parameters('dnsPrefixNameForPublicIP'),copyIndex())]",
        "location": "[resourceGroup().location]",
        "tags": {
            "displayName": "PublicIPAddress"
        },
        "properties": {
          "publicIPAllocationMethod": "Dynamic",
            "dnsSettings": {
                "domainNameLabel": "[concat(parameters('dnsPrefixNameForPublicIP'),copyIndex())]"
            }
        },
        "copy": {
            "name": "publicIpCopy",
            "count": "[parameters('numberOfNodes')]"
        }
    },
    {
      "apiVersion": "2015-05-01-preview",
      "type": "Microsoft.Network/loadBalancers",
      "name": "[variables('loadBalancerName')]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', variables('loadBalancerPublicIPName'))]"
      ],
      "properties": {
        "frontendIPConfigurations": [
          {
            "name": "[variables('loadBalancerIPConfigName')]",
            "properties": {
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('loadBalancerPublicIPName'))]"
              }
            }
          }
        ],
        "backendAddressPools": [
          {
            "name": "[variables('lbBackendAddressPoolName')]",
            "properties": {
              "loadBalancingRules": [
                {
                  "id": "[variables('lbAPIRuleID')]"
                },
                {
                  "id": "[variables('lbBuilderRuleID')]"
                }
              ]
            }
          }
        ],
        "loadBalancingRules": [
          {
            "name": "[variables('loadBalancerAPIRuleName')]",
            "dependsOn": [
              "[variables('lbIPConfig')]"
            ],
            "properties": {
              "frontendIPConfiguration": {
                "id": "[variables('lbIPConfig')]"
              },
              "backendAddressPool": {
                "id": "[variables('lbPoolID')]"
              },
              "protocol": "TCP",
              "frontendPort": "80",
              "backendPort": "80",
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": "10",
              "probe": {
                "id": "[variables('apiProbeID')]"
              }
            }
          },
          {
            "name": "[variables('loadBalancerBuilderRuleName')]",
            "dependsOn": [
              "[variables('lbIPConfig')]"
            ],
            "properties": {
              "frontendIPConfiguration": {
                "id": "[variables('lbIPConfig')]"
              },
              "backendAddressPool": {
                "id": "[variables('lbPoolID')]"
              },
              "protocol": "TCP",
              "frontendPort": "2222",
              "backendPort": "2222",
              "enableFloatingIP": false,
              "idleTimeoutInMinutes": "10"
            }
          }
        ],
        "probes": [
          {
            "name": "[variables('apiProbeName')]",
            "properties": {
              "protocol": "HTTP",
              "port": "80",
              "intervalInSeconds": "5",
              "numberOfProbes": "2",
              "requestPath": "/health-check"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[variables('virtualNetworkName')]",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-05-01-preview",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[variables('addressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[variables('subnet1Name')]",
            "properties": {
              "addressPrefix": "[variables('subnet1Prefix')]"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/networkInterfaces",
      "name": "[concat('nic', copyindex())]",
      "copy": {
        "name": "nicLoop",
        "count": "[parameters('numberOfNodes')]"
      },
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Network/publicIPAddresses/', concat(parameters('dnsPrefixNameForPublicIP'),copyIndex()))]",
        "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]",
        "[variables('lbID')]"
      ],
      "apiVersion": "2015-05-01-preview",
      "properties": {
        "ipConfigurations": [
          {
            "name": "ipconfig1",
            "properties": {
              "privateIPAllocationMethod": "Dynamic",
              "publicIPAddress": {
                "id": "[resourceId('Microsoft.Network/publicIPAddresses',concat(parameters('dnsPrefixNameForPublicIP'),copyIndex()))]"
              },
              "subnet": {
                "id": "[variables('subnet1Ref')]"
              },
              "loadBalancerBackendAddressPools": [
                {
                  "id": "[variables('lbPoolID')]"
                }
              ]
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "name": "[concat(variables('vmNamePrefix'), copyindex())]",
      "copy": {
        "name": "virtualMachineLoop",
        "count": "[parameters('numberOfNodes')]"
      },
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.Storage/storageAccounts/', parameters('newStorageAccountName'))]",
        "[concat('Microsoft.Network/networkInterfaces/', 'nic', copyindex())]",
        "[concat('Microsoft.Compute/availabilitySets/', variables('availabilitySetName'))]"
      ],
      "apiVersion": "2015-05-01-preview",
      "properties": {
        "availabilitySet": {
          "id": "[resourceId('Microsoft.Compute/availabilitySets',variables('availabilitySetName'))]"
        },
        "hardwareProfile": {
          "vmSize": "[parameters('vmSize')]"
        },
        "osProfile": {
          "computername": "[concat(variables('vmNamePrefix'), copyindex())]",
          "adminUsername": "[parameters('adminUsername')]",
          "customData": "[parameters('customData')]",
          "linuxConfiguration": {
            "disablePasswordAuthentication": "true",
            "ssh": {
              "publicKeys": [
                {
                  "path": "[variables('sshKeyPath')]",
                  "keyData": "[parameters('sshKeyData')]"
                }
              ]
            }
          }
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "[variables('imagePublisher')]",
            "offer": "[variables('imageOffer')]",
            "sku": "[variables('imageSKU')]",
            "version": "[parameters('coreosVersion')]"
          },
          "dataDisks": [
            {
              "name": "dockerdisk",
              "diskSizeGB": "[parameters('dockerVolumeSize')]",
              "lun": 0,
              "vhd": {
                "uri": "[concat('http://',parameters('newStorageAccountName'),'.blob.core.windows.net/vhds/','datadisk', copyindex(),'.vhd')]"
              },
              "createOption": "Empty"
            }
          ],
          "osDisk": {
            "name": "osdisk",
            "vhd": {
              "uri": "[concat('http://',parameters('newStorageAccountName'),'.blob.core.windows.net/vhds/','osdisk', copyindex(),'.vhd')]"
            },
            "caching": "ReadWrite",
            "createOption": "FromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces',concat('nic',copyindex()))]"
            }
          ]
        }
      }
    }
  ]
}
