{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "admin-username": {
            "type": "string"
        },
        "ssh-public-key": {
            "type": "string"
        }
    },
    "variables": {
        "ssh-keypath": "[concat('/home/', parameters('admin-username'), '/.ssh/authorized_keys')]",
        "unique-prefix": "[concat(replace(resourceGroup().name,'-',''), substring(uniquestring(resourceGroup().name), 0, 5))]",
        "storage-name": "[variables('unique-prefix')]",
        "vnet-name": "[concat(resourceGroup().name, '-vnet')]",
        "ip-prefix": "[concat(resourceGroup().name, '-ip-')]",
        "nsg-prefix": "[concat(resourceGroup().name, '-nsg')]",
        "nic-prefix": "[concat(resourceGroup().name, '-nic-')]",
        "access-nic-internal": "[concat(resourceGroup().name, '-access-nic-internal')]",
        "access-nic-external": "[concat(resourceGroup().name, '-access-nic-external')]",
        "vm-prefix": "[concat(resourceGroup().name, '-vm-')]",
        "names": [ "alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta", "theta", "iota", "kappa", "lambda", "mu", "nu", "xi", "omicron", "pi", "rho", "sigma", "tau", "upsilon", "phi", "chi", "psi", "omega" ],
        "private-vm-size": "Standard_DS1_v2",
        "vnet-address-space":  "10.16.0.0/12",
        "vnet-subnet01-octet": 17,
        "vnet-subnet01-octet": 18
    },
    "resources": [
        {
            "comments": "",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[concat(variables('ip-prefix'), variables('names')[0])]",
            "apiVersion": "2017-03-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "publicIPAllocationMethod": "Dynamic",
                "idleTimeoutInMinutes": 4,
                "dnsSettings": {
                    "domainNameLabel": "[concat(variables('unique-prefix'), '-', variables('names')[0])]"
                }
            },
            "resources": [],
            "dependsOn": []
        },
        {
            "comments": "",
            "type": "Microsoft.Network/publicIPAddresses",
            "name": "[concat(variables('ip-prefix'), variables('names')[1])]",
            "apiVersion": "2017-03-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "publicIPAllocationMethod": "Dynamic",
                "idleTimeoutInMinutes": 4,
                "dnsSettings": {
                    "domainNameLabel": "[concat(variables('unique-prefix'), '-', variables('names')[1])]"
                }
            },
            "resources": [],
            "dependsOn": []
        },
        {
            "comments": "",
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[concat(variables('vm-prefix'), variables('names')[0])]",
            "apiVersion": "2016-04-30-preview",
            "location": "[resourceGroup().location]",
            "properties": {
                "hardwareProfile": {
                    "vmSize": "Standard_DS2_v2"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "OpenLogic",
                        "offer": "CentOS",
                        "sku": "7.2",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[concat(variables('vm-prefix'), variables('names')[0], '-boot')]",
                        "createOption": "FromImage",
                        "vhd": {
                            "uri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storage-name')), '2015-06-15').primaryEndpoints.blob, 'vhds/', variables('names')[0], '-boot.vhd')]"
                        },
                        "caching": "ReadWrite"
                    }
                },
                "osProfile": {
                    "computerName": "[concat(variables('vm-prefix'), variables('names')[0])]",
                    "adminUsername": "[parameters('admin-username')]",
                    "linuxConfiguration": {
                        "disablePasswordAuthentication": true,
                        "ssh": {
                            "publicKeys": [
                              {
                                "path": "[variables('ssh-keypath')]",
                                "keyData": "[parameters('ssh-public-key')]"
                              }
                            ]
                        }
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nic-prefix'), variables('names')[0]))]"
                        }
                    ]
                }
            },
            "resources": [],
            "dependsOn": [
                "[resourceId('Microsoft.Storage/storageAccounts', variables('storage-name'))]",
                "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('nic-prefix'), variables('names')[0]))]"
            ]
        },
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "name": "[concat(variables('vm-prefix'), variables('names')[0], '/script')]",
            "apiVersion": "2015-05-01-preview",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[concat(variables('vm-prefix'), variables('names')[0])]"
            ],
            "properties": {
                "publisher": "Microsoft.Azure.Extensions",
                "type": "CustomScript",
                "typeHandlerVersion": "2.0",
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "fileUris": [
                        "https://linux.azure.david.betz.space/raw/postfix/service-install.sh"
                    ],
                    "commandToExecute": "sh service-install.sh"
                }
            }
        },
        {
            "comments": "",
            "type": "Microsoft.Compute/virtualMachines",
            "name": "[concat(variables('vm-prefix'), variables('names')[1])]",
            "apiVersion": "2016-04-30-preview",
            "location": "[resourceGroup().location]",
            "properties": {
                "hardwareProfile": {
                    "vmSize": "[variables('private-vm-size')]"
                },
                "storageProfile": {
                    "imageReference": {
                        "publisher": "OpenLogic",
                        "offer": "CentOS",
                        "sku": "7.2",
                        "version": "latest"
                    },
                    "osDisk": {
                        "name": "[concat(concat(variables('vm-prefix'), variables('names')[1]), '-boot')]",
                        "createOption": "FromImage",
                        "vhd": {
                            "uri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('storage-name')), '2015-06-15').primaryEndpoints.blob, 'vhds/', variables('names')[1], '-boot.vhd')]"
                        },
                        "caching": "ReadWrite"
                    }
                },
                "osProfile": {
                    "computerName": "[concat(variables('vm-prefix'), variables('names')[1])]",
                    "adminUsername": "[parameters('admin-username')]",
                    "linuxConfiguration": {
                        "disablePasswordAuthentication": true,
                      "ssh": {
                        "publicKeys": [
                          {
                            "path": "[variables('ssh-keypath')]",
                            "keyData": "[parameters('ssh-public-key')]"
                          }
                        ]
                      }
                    }
                },
                "networkProfile": {
                    "networkInterfaces": [
                        {
                            "id": "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vm-prefix'), variables('names')[1]))]"
                        }
                    ]
                }
            },
            "resources": [],
            "dependsOn": [
                "[resourceId('Microsoft.Storage/storageAccounts', variables('storage-name'))]",
                "[resourceId('Microsoft.Network/networkInterfaces', concat(variables('vm-prefix'), variables('names')[1]))]"
            ]
        },
        {
            "type": "Microsoft.Compute/virtualMachines/extensions",
            "name": "[concat(variables('vm-prefix'), variables('names')[1], '/script')]",
            "apiVersion": "2015-05-01-preview",
            "location": "[resourceGroup().location]",
            "dependsOn": [
                "[concat(variables('vm-prefix'), variables('names')[1])]"
            ],
            "properties": {
                "publisher": "Microsoft.Azure.Extensions",
                "type": "CustomScript",
                "typeHandlerVersion": "2.0",
                "autoUpgradeMinorVersion": true,
                "settings": {
                    "fileUris": [
                        "https://linux.azure.david.betz.space/raw/postfix/client-install.sh"
                    ],
                    "commandToExecute": "sh client-install.sh"
                }
            }
        },
        {
            "comments": "",
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[concat(variables('nic-prefix'), variables('names')[0])]",
            "apiVersion": "2017-03-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ifconfig1",
                        "properties": {
                            "privateIPAddress": "[concat('10.', variables('vnet-subnet01-octet'), '.0.5')]",
                            "privateIPAllocationMethod": "Static",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('ip-prefix'), variables('names')[0]))]"
                            },
                            "subnet": {
                                "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', variables('vnet-name')), '/subnets/subnet01')]"
                            }
                        }
                    }
                ],
                "dnsSettings": {
                    "dnsServers": []
                },
                "enableIPForwarding": false,
                "networkSecurityGroup": {
                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', concat(variables('nsg-prefix'), variables('names')[0]))]"
                }
            },
            "resources": [],
            "dependsOn": [
                "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('ip-prefix'), variables('names')[0]))]",
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnet-name'))]",
                "[resourceId('Microsoft.Network/networkSecurityGroups', concat(variables('nsg-prefix'), variables('names')[0]))]"
            ]
        },
        {
            "comments": "",
            "type": "Microsoft.Network/networkInterfaces",
            "name": "[concat(variables('vm-prefix'), variables('names')[1])]",
            "apiVersion": "2017-03-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "ipConfigurations": [
                    {
                        "name": "ifconfig",
                        "properties": {
                            "privateIPAddress": "[concat('10.', variables('vnet-subnet01-octet'), '.0.6')]",
                            "privateIPAllocationMethod": "Static",
                            "publicIPAddress": {
                                "id": "[resourceId('Microsoft.Network/publicIPAddresses', concat(variables('ip-prefix'), variables('names')[1]))]"
                            },
                            "subnet": {
                                "id": "[concat(resourceId('Microsoft.Network/virtualNetworks', variables('vnet-name')), '/subnets/subnet01')]"
                            }
                        }
                    }
                ],
                "dnsSettings": {
                    "dnsServers": []
                },
                "enableIPForwarding": false,
                "networkSecurityGroup": {
                    "id": "[resourceId('Microsoft.Network/networkSecurityGroups', concat(variables('nsg-prefix'), variables('names')[1]))]"
                }
            },
            "resources": [],
            "dependsOn": [
                "[resourceId('Microsoft.Network/virtualNetworks', variables('vnet-name'))]",
                "[resourceId('Microsoft.Network/networkSecurityGroups', concat(variables('nsg-prefix'), variables('names')[1]))]"
            ]
        },
        {
            "comments": "",
            "type": "Microsoft.Network/virtualNetworks",
            "name": "[variables('vnet-name')]",
            "apiVersion": "2017-03-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "addressSpace": {
                    "addressPrefixes": [
                        "[variables('vnet-address-space')]"
                    ]
                },
                "subnets": [
                    {
                        "name": "subnet01",
                        "properties": {
                            "addressPrefix": "[concat('10.', variables('vnet-subnet01-octet'), '.0.0/16')]"
                        }
                    }
                ]
            },
            "resources": [],
            "dependsOn": []
        },
        {
            "comments": "",
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "[concat(variables('nsg-prefix'), variables('names')[0])]",
            "apiVersion": "2017-03-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "securityRules": [
                    {
                        "name": "default-allow-ssh",
                        "properties": {
                            "protocol": "Tcp",
                            "sourcePortRange": "*",
                            "destinationPortRange": "22",
                            "sourceAddressPrefix": "*",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 1000,
                            "direction": "Inbound"
                        }
                    },
                    {
                        "name": "dns-udp",
                        "properties": {
                            "protocol": "Udp",
                            "sourcePortRange": "*",
                            "destinationPortRange": "53",
                            "sourceAddressPrefix": "*",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 1100,
                            "direction": "Inbound"
                        }
                    },
                    {
                        "name": "dns-tcp",
                        "properties": {
                            "protocol": "Tcp",
                            "sourcePortRange": "*",
                            "destinationPortRange": "53",
                            "sourceAddressPrefix": "*",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 1200,
                            "direction": "Inbound"
                        }
                    }
                ]
            },
            "resources": [],
            "dependsOn": []
        },
        {
            "comments": "",
            "type": "Microsoft.Network/networkSecurityGroups",
            "name": "[concat(variables('nsg-prefix'), variables('names')[1])]",
            "apiVersion": "2017-03-01",
            "location": "[resourceGroup().location]",
            "properties": {
                "securityRules": [
                    {
                        "name": "default-allow-ssh",
                        "properties": {
                            "protocol": "Tcp",
                            "sourcePortRange": "*",
                            "destinationPortRange": "22",
                            "sourceAddressPrefix": "*",
                            "destinationAddressPrefix": "*",
                            "access": "Allow",
                            "priority": 1000,
                            "direction": "Inbound"
                        }
                    }
                ]
            },
            "resources": [],
            "dependsOn": []
        },
        {
            "comments": "",
            "type": "Microsoft.Storage/storageAccounts",
            "sku": {
                "name": "Standard_LRS",
                "tier": "Standard"
            },
            "kind": "Storage",
            "name": "[variables('storage-name')]",
            "apiVersion": "2016-01-01",
            "location": "[resourceGroup().location]",
            "tags": {},
            "properties": { },
            "resources": [],
            "dependsOn": []
        }
    ],
    "outputs": {
        "alpha": {
            "type": "string",
            "value": "[concat('ssh ', variables('unique-prefix'), '-', variables('names')[0], '.', resourceGroup().location, '.cloudapp.azure.com')]"
        },
        "beta": {
            "type": "string",
            "value": "[concat('ssh ', variables('unique-prefix'), '-', variables('names')[1], '.', resourceGroup().location, '.cloudapp.azure.com')]"
        }
    }
}

