Skip to content

Commit

Permalink
Fixes per PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Yeaney committed Sep 25, 2020
1 parent 71b68ec commit 960fdd0
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions examples/azure/terraform-azure-nsg-example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 21 additions & 4 deletions examples/azure/terraform-azure-nsg-example/main.tf
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -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" {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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"
}

4 changes: 2 additions & 2 deletions examples/azure/terraform-azure-nsg-example/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
1 change: 0 additions & 1 deletion examples/azure/terraform-azure-nsg-example/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/azure/nsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
6 changes: 3 additions & 3 deletions modules/azure/nsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions test/azure/terraform_azure_nsg_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package test

import (
"fmt"
"testing"

"github.com/gruntwork-io/terratest/modules/azure"
Expand All @@ -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,
},
}

Expand Down

0 comments on commit 960fdd0

Please sign in to comment.