From 8e7f3cc09de6f40f923a62c776dad72386fb0b20 Mon Sep 17 00:00:00 2001 From: Annie Hedgpeth Date: Thu, 4 May 2017 04:57:54 -0500 Subject: [PATCH] provider/azurerm: Add example of a VNET w/ Two Subnets (#14115) * merge master * added new constructs/naming for deploy scripts, etc. * suppress az login output * removed .tfvars and provider.tf; updated prev merge * reverted .travis.yml back to Hashicorp's * Reverting back to the Hashicorp travis file --- .travis.yml | 4 +- .../README.md | 2 +- .../deploy.ci.sh | 2 +- .../deploy.mac.sh | 2 +- .../outputs.tf | 2 +- .../variables.tf | 2 +- examples/azure-vnet-two-subnets/.gitignore | 3 ++ examples/azure-vnet-two-subnets/README.md | 18 ++++++++ examples/azure-vnet-two-subnets/deploy.ci.sh | 41 +++++++++++++++++++ examples/azure-vnet-two-subnets/deploy.mac.sh | 15 +++++++ examples/azure-vnet-two-subnets/main.tf | 32 +++++++++++++++ examples/azure-vnet-two-subnets/variables.tf | 8 ++++ 12 files changed, 124 insertions(+), 7 deletions(-) create mode 100644 examples/azure-vnet-two-subnets/.gitignore create mode 100644 examples/azure-vnet-two-subnets/README.md create mode 100755 examples/azure-vnet-two-subnets/deploy.ci.sh create mode 100755 examples/azure-vnet-two-subnets/deploy.mac.sh create mode 100644 examples/azure-vnet-two-subnets/main.tf create mode 100644 examples/azure-vnet-two-subnets/variables.tf diff --git a/.travis.yml b/.travis.yml index 1c60d74d5..7fe62879f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ dist: trusty sudo: false language: go go: -- 1.8.x +- 1.8 # add TF_CONSUL_TEST=1 to run consul tests # they were causing timouts in travis @@ -25,7 +25,7 @@ install: - bash scripts/gogetcookie.sh - go get github.com/kardianos/govendor script: -- make vendor-status test vet +- make vet vendor-status test - GOOS=windows go build branches: only: diff --git a/examples/azure-vm-simple-linux-managed-disk/README.md b/examples/azure-vm-simple-linux-managed-disk/README.md index b8d0706a1..cde5c458e 100644 --- a/examples/azure-vm-simple-linux-managed-disk/README.md +++ b/examples/azure-vm-simple-linux-managed-disk/README.md @@ -17,4 +17,4 @@ Azure requires that an application is added to Azure Active Directory to generat If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it. ## variables.tf -The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template. +The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template. \ No newline at end of file diff --git a/examples/azure-vm-simple-linux-managed-disk/deploy.ci.sh b/examples/azure-vm-simple-linux-managed-disk/deploy.ci.sh index 03e56e00a..71e407526 100755 --- a/examples/azure-vm-simple-linux-managed-disk/deploy.ci.sh +++ b/examples/azure-vm-simple-linux-managed-disk/deploy.ci.sh @@ -33,4 +33,4 @@ docker run --rm -it \ --workdir=/data \ --entrypoint "/bin/sh" \ hashicorp/terraform:light \ - -c "/bin/terraform destroy -force -var dns_name=$KEY -var hostname=$KEY -var resource_group=$KEY -var admin_password=$PASSWORD;" + -c "/bin/terraform destroy -force -var dns_name=$KEY -var hostname=$KEY -var resource_group=$KEY -var admin_password=$PASSWORD;" \ No newline at end of file diff --git a/examples/azure-vm-simple-linux-managed-disk/deploy.mac.sh b/examples/azure-vm-simple-linux-managed-disk/deploy.mac.sh index 9c6563f07..dfc34c2be 100755 --- a/examples/azure-vm-simple-linux-managed-disk/deploy.mac.sh +++ b/examples/azure-vm-simple-linux-managed-disk/deploy.mac.sh @@ -12,4 +12,4 @@ if docker -v; then else echo "Docker is used to run terraform commands, please install before run: https://docs.docker.com/docker-for-mac/install/" -fi +fi \ No newline at end of file diff --git a/examples/azure-vm-simple-linux-managed-disk/outputs.tf b/examples/azure-vm-simple-linux-managed-disk/outputs.tf index 13768e554..32c6294ce 100644 --- a/examples/azure-vm-simple-linux-managed-disk/outputs.tf +++ b/examples/azure-vm-simple-linux-managed-disk/outputs.tf @@ -8,4 +8,4 @@ output "vm_fqdn" { output "ssh_command" { value = "ssh ${var.admin_username}@${azurerm_public_ip.pip.fqdn}" -} +} \ No newline at end of file diff --git a/examples/azure-vm-simple-linux-managed-disk/variables.tf b/examples/azure-vm-simple-linux-managed-disk/variables.tf index 17fbe337d..91024000b 100644 --- a/examples/azure-vm-simple-linux-managed-disk/variables.tf +++ b/examples/azure-vm-simple-linux-managed-disk/variables.tf @@ -72,4 +72,4 @@ variable "admin_username" { variable "admin_password" { description = "administrator password (recommended to disable password auth)" -} +} \ No newline at end of file diff --git a/examples/azure-vnet-two-subnets/.gitignore b/examples/azure-vnet-two-subnets/.gitignore new file mode 100644 index 000000000..4893d38ff --- /dev/null +++ b/examples/azure-vnet-two-subnets/.gitignore @@ -0,0 +1,3 @@ +terraform.tfstate* +provider.tf +out.tfplan diff --git a/examples/azure-vnet-two-subnets/README.md b/examples/azure-vnet-two-subnets/README.md new file mode 100644 index 000000000..1f47743df --- /dev/null +++ b/examples/azure-vnet-two-subnets/README.md @@ -0,0 +1,18 @@ +# Virtual Network with Two Subnets + +This template allows you to create a Virtual Network with two subnets. + +## main.tf +The `main.tf` file contains the actual resources that will be deployed. It also contains the Azure Resource Group definition and any defined variables. + +## outputs.tf +This data is outputted when `terraform apply` is called, and can be queried using the `terraform output` command. + +## provider.tf +Azure requires that an application is added to Azure Active Directory to generate the `client_id`, `client_secret`, and `tenant_id` needed by Terraform (`subscription_id` can be recovered from your Azure account details). Please go [here](https://www.terraform.io/docs/providers/azurerm/) for full instructions on how to create this to populate your `provider.tf` file. + +## terraform.tfvars +If a `terraform.tfvars` file is present in the current directory, Terraform automatically loads it to populate variables. We don't recommend saving usernames and password to version control, but you can create a local secret variables file and use `-var-file` to load it. + +## variables.tf +The `variables.tf` file contains all of the input parameters that the user can specify when deploying this Terraform template. \ No newline at end of file diff --git a/examples/azure-vnet-two-subnets/deploy.ci.sh b/examples/azure-vnet-two-subnets/deploy.ci.sh new file mode 100755 index 000000000..49fb955c7 --- /dev/null +++ b/examples/azure-vnet-two-subnets/deploy.ci.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +set -o errexit -o nounset + +# generate a unique string for CI deployment +# KEY=$(cat /dev/urandom | tr -cd 'a-z' | head -c 12) +# PASSWORD=$KEY$(cat /dev/urandom | tr -cd 'A-Z' | head -c 2)$(cat /dev/urandom | tr -cd '0-9' | head -c 2) + +docker run --rm -it \ + -e ARM_CLIENT_ID \ + -e ARM_CLIENT_SECRET \ + -e ARM_SUBSCRIPTION_ID \ + -e ARM_TENANT_ID \ + -v $(pwd):/data \ + --workdir=/data \ + --entrypoint "/bin/sh" \ + hashicorp/terraform:light \ + -c "/bin/terraform get; \ + /bin/terraform validate; \ + /bin/terraform plan -out=out.tfplan -var resource_group=$KEY; \ + /bin/terraform apply out.tfplan; \ + /bin/terraform show;" + +# check that resources exist via azure cli +docker run --rm -it \ + azuresdk/azure-cli-python \ + sh -c "az login --service-principal -u $ARM_CLIENT_ID -p $ARM_CLIENT_SECRET --tenant $ARM_TENANT_ID > /dev/null; \ + az network vnet subnet show -n subnet1 -g $KEY --vnet-name '$KEY'vnet; \ + az network vnet subnet show -n subnet2 -g $KEY --vnet-name '$KEY'vnet;" + +# cleanup deployed azure resources via terraform +docker run --rm -it \ + -e ARM_CLIENT_ID \ + -e ARM_CLIENT_SECRET \ + -e ARM_SUBSCRIPTION_ID \ + -e ARM_TENANT_ID \ + -v $(pwd):/data \ + --workdir=/data \ + --entrypoint "/bin/sh" \ + hashicorp/terraform:light \ + -c "/bin/terraform destroy -force -var resource_group=$KEY;" \ No newline at end of file diff --git a/examples/azure-vnet-two-subnets/deploy.mac.sh b/examples/azure-vnet-two-subnets/deploy.mac.sh new file mode 100755 index 000000000..dfc34c2be --- /dev/null +++ b/examples/azure-vnet-two-subnets/deploy.mac.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -o errexit -o nounset + +if docker -v; then + + # generate a unique string for CI deployment + export KEY=$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'a-z' | head -c 12) + export PASSWORD=$KEY$(cat /dev/urandom | env LC_CTYPE=C tr -cd 'A-Z' | head -c 2)$(cat /dev/urandom | env LC_CTYPE=C tr -cd '0-9' | head -c 2) + + /bin/sh ./deploy.ci.sh + +else + echo "Docker is used to run terraform commands, please install before run: https://docs.docker.com/docker-for-mac/install/" +fi \ No newline at end of file diff --git a/examples/azure-vnet-two-subnets/main.tf b/examples/azure-vnet-two-subnets/main.tf new file mode 100644 index 000000000..aee3593f3 --- /dev/null +++ b/examples/azure-vnet-two-subnets/main.tf @@ -0,0 +1,32 @@ +# provider "azurerm" { +# subscription_id = "REPLACE-WITH-YOUR-SUBSCRIPTION-ID" +# client_id = "REPLACE-WITH-YOUR-CLIENT-ID" +# client_secret = "REPLACE-WITH-YOUR-CLIENT-SECRET" +# tenant_id = "REPLACE-WITH-YOUR-TENANT-ID" +# } + +resource "azurerm_resource_group" "rg" { + name = "${var.resource_group}" + location = "${var.location}" +} + +resource "azurerm_virtual_network" "vnet" { + name = "${var.resource_group}vnet" + location = "${var.location}" + address_space = ["10.0.0.0/16"] + resource_group_name = "${azurerm_resource_group.rg.name}" +} + +resource "azurerm_subnet" "subnet1" { + name = "subnet1" + virtual_network_name = "${azurerm_virtual_network.vnet.name}" + resource_group_name = "${azurerm_resource_group.rg.name}" + address_prefix = "10.0.0.0/24" +} + +resource "azurerm_subnet" "subnet2" { + name = "subnet2" + virtual_network_name = "${azurerm_virtual_network.vnet.name}" + resource_group_name = "${azurerm_resource_group.rg.name}" + address_prefix = "10.0.1.0/24" +} diff --git a/examples/azure-vnet-two-subnets/variables.tf b/examples/azure-vnet-two-subnets/variables.tf new file mode 100644 index 000000000..8d5dd4131 --- /dev/null +++ b/examples/azure-vnet-two-subnets/variables.tf @@ -0,0 +1,8 @@ +variable "resource_group" { + description = "The name of the resource group in which to create the virtual network." +} + +variable "location" { + description = "The location/region where the virtual network is created. Changing this forces a new resource to be created." + default = "southcentralus" +}