From 960fdd0a61f1d23341058fe5675453e873ce4d75 Mon Sep 17 00:00:00 2001 From: Mike Yeaney Date: Fri, 25 Sep 2020 17:10:11 -0400 Subject: [PATCH] Fixes per PR feedback --- .../terraform-azure-nsg-example/README.md | 4 +-- .../azure/terraform-azure-nsg-example/main.tf | 25 ++++++++++++++++--- .../terraform-azure-nsg-example/outputs.tf | 4 +-- .../terraform-azure-nsg-example/variables.tf | 1 - modules/azure/nsg.go | 2 +- modules/azure/nsg_test.go | 6 ++--- .../azure/terraform_azure_nsg_example_test.go | 5 +--- 7 files changed, 30 insertions(+), 17 deletions(-) diff --git a/examples/azure/terraform-azure-nsg-example/README.md b/examples/azure/terraform-azure-nsg-example/README.md index 86992110a..1cee389c3 100644 --- a/examples/azure/terraform-azure-nsg-example/README.md +++ b/examples/azure/terraform-azure-nsg-example/README.md @@ -3,8 +3,8 @@ This folder contains a simple Terraform module that deploys resources in [Azure](https://azure.microsoft.com/) to demonstrate how you can use Terratest to write automated tests for your Azure Terraform code. This module deploys the following: * A [Virtual Machine](https://azure.microsoft.com/en-us/services/virtual-machines/) that gives the module the following: - * [Virtual Machine](https://docs.microsoft.com/en-us/azure/virtual-machines/) with the value specified in the `vm_name` variable. - * A [Network Security Group](https://docs.microsoft.com/en-us/azure/virtual-network/network-security-groups-overview) created with a single custom rule to allow SSH (port 22) with the nsg name specified in the `nsg_name` variable. + * [Virtual Machine](https://docs.microsoft.com/en-us/azure/virtual-machines/) with the value specified in the `vm_name` variable along with a random value for the `postfix` variable (set from test code). + * A [Network Security Group](https://docs.microsoft.com/en-us/azure/virtual-network/network-security-groups-overview) created with a single custom rule to allow SSH (port 22) with the nsg name specified in the `nsg_name` variable along with a random value for the `postfix` variable (set from test code). Check out [test/azure/terraform_azure_nsg_example_test.go](/test/azure/terraform_azure_nsg_example_test.go) to see how you can write automated tests for this module. diff --git a/examples/azure/terraform-azure-nsg-example/main.tf b/examples/azure/terraform-azure-nsg-example/main.tf index 26b1cbf18..04f39841d 100644 --- a/examples/azure/terraform-azure-nsg-example/main.tf +++ b/examples/azure/terraform-azure-nsg-example/main.tf @@ -1,3 +1,11 @@ +# --------------------------------------------------------------------------------------------------------------------- +# DEPLOY AN AZURE VM ALONG WITH AN EXAMPLE NETWORK SECURITY GROUP (NSG) +# This is an example of how to deploy an NSG along with the minimum networking resources +# to support a basic virtual machine. +# --------------------------------------------------------------------------------------------------------------------- +# See test/azure/terraform_azure_nsg_example_test.go for how to write automated tests for this code. +# --------------------------------------------------------------------------------------------------------------------- + provider "azurerm" { version = "~>2.20" features {} @@ -14,7 +22,7 @@ terraform { # --------------------------------------------------------------------------------------------------------------------- # DEPLOY A RESOURCE GROUP -# See test/terraform_azure_example_test.go for how to write automated tests for this code. +# See test/terraform_azure_nsg_example_test.go for how to write automated tests for this code. # --------------------------------------------------------------------------------------------------------------------- resource "azurerm_resource_group" "nsg_rg" { @@ -63,7 +71,7 @@ resource "azurerm_network_interface_security_group_association" "main" { network_security_group_id = azurerm_network_security_group.nsg_example.id } -resource "azurerm_network_security_rule" "allowSSH" { +resource "azurerm_network_security_rule" "allow_ssh" { name = "${var.nsg_ssh_rule_name}-${var.postfix}" description = "${var.nsg_ssh_rule_name}-${var.postfix}" priority = 100 @@ -78,7 +86,7 @@ resource "azurerm_network_security_rule" "allowSSH" { network_security_group_name = azurerm_network_security_group.nsg_example.name } -resource "azurerm_network_security_rule" "blockHTTP" { +resource "azurerm_network_security_rule" "block_http" { name = "${var.nsg_http_rule_name}-${var.postfix}" description = "${var.nsg_http_rule_name}-${var.postfix}" priority = 200 @@ -124,7 +132,7 @@ resource "azurerm_virtual_machine" "vm_example" { os_profile { computer_name = var.hostname admin_username = var.username - admin_password = var.password + admin_password = random_password.nsg.result } os_profile_linux_config { @@ -137,3 +145,12 @@ resource "azurerm_virtual_machine" "vm_example" { ] } +resource "random_password" "nsg" { + length = 16 + override_special = "-_%@" + min_upper = "1" + min_lower = "1" + min_numeric = "1" + min_special = "1" +} + diff --git a/examples/azure/terraform-azure-nsg-example/outputs.tf b/examples/azure/terraform-azure-nsg-example/outputs.tf index d96abf0be..f69279fea 100644 --- a/examples/azure/terraform-azure-nsg-example/outputs.tf +++ b/examples/azure/terraform-azure-nsg-example/outputs.tf @@ -11,9 +11,9 @@ output "nsg_name" { } output "ssh_rule_name" { - value = azurerm_network_security_rule.allowSSH.name + value = azurerm_network_security_rule.allow_ssh.name } output "http_rule_name" { - value = azurerm_network_security_rule.blockHTTP.name + value = azurerm_network_security_rule.block_http.name } diff --git a/examples/azure/terraform-azure-nsg-example/variables.tf b/examples/azure/terraform-azure-nsg-example/variables.tf index f65383766..1f52eb3ec 100644 --- a/examples/azure/terraform-azure-nsg-example/variables.tf +++ b/examples/azure/terraform-azure-nsg-example/variables.tf @@ -24,7 +24,6 @@ variable "postfix" { default = "qwefgt" } - variable "resource_group_name" { description = "Name for the resource group holding resources for this example" type = string diff --git a/modules/azure/nsg.go b/modules/azure/nsg.go index c0d6f06a2..934456337 100644 --- a/modules/azure/nsg.go +++ b/modules/azure/nsg.go @@ -165,7 +165,7 @@ func bindRuleList(source network.SecurityRuleListResultIterator) ([]NsgRuleSumma return rules, nil } -// convertToNsgRuleSummary converst the raw SDK security rule type into a summarized struct, flattening the +// convertToNsgRuleSummary converts the raw SDK security rule type into a summarized struct, flattening the // rules properties and name into a single, string-based struct. func convertToNsgRuleSummary(name *string, rule *network.SecurityRulePropertiesFormat) NsgRuleSummary { summary := NsgRuleSummary{} diff --git a/modules/azure/nsg_test.go b/modules/azure/nsg_test.go index 0e61f2e55..a65d7f065 100644 --- a/modules/azure/nsg_test.go +++ b/modules/azure/nsg_test.go @@ -42,10 +42,10 @@ func TestPortRangeParsing(t *testing.T) { } } -func TestNsgRuleSummaryConverstion(t *testing.T) { +func TestNsgRuleSummaryConversion(t *testing.T) { // Quick test to make sure the safe nil handling is working - var name = "test name" - var sdkStruct = network.SecurityRulePropertiesFormat{} + name := "test name" + sdkStruct := network.SecurityRulePropertiesFormat{} // Verify the nil values were correctly defaulted to "" without a panic result := convertToNsgRuleSummary(&name, &sdkStruct) diff --git a/test/azure/terraform_azure_nsg_example_test.go b/test/azure/terraform_azure_nsg_example_test.go index e7fb02bc5..cb641ba86 100644 --- a/test/azure/terraform_azure_nsg_example_test.go +++ b/test/azure/terraform_azure_nsg_example_test.go @@ -6,7 +6,6 @@ package test import ( - "fmt" "testing" "github.com/gruntwork-io/terratest/modules/azure" @@ -19,15 +18,13 @@ func TestTerraformAzureNsgExample(t *testing.T) { t.Parallel() randomPostfixValue := random.UniqueId() - vmPassword := fmt.Sprintf("%s@#$%s", random.UniqueId(), random.UniqueId()) // Construct options for TF apply terraformOptions := &terraform.Options{ // The path to where our Terraform code is located TerraformDir: "../../examples/azure/terraform-azure-nsg-example", Vars: map[string]interface{}{ - "postfix": randomPostfixValue, - "password": vmPassword, + "postfix": randomPostfixValue, }, }