From 293e6068f95470d758dd533631a980f03906d5a0 Mon Sep 17 00:00:00 2001 From: matttrach Date: Wed, 10 Jan 2024 12:20:52 -0600 Subject: [PATCH 1/2] fix: only chomp if necessary Signed-off-by: matttrach --- main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.tf b/main.tf index b4ed95a..559e738 100644 --- a/main.tf +++ b/main.tf @@ -16,7 +16,7 @@ locals { ssh_key_name = var.ssh_key_name public_ssh_key = var.public_ssh_key # create when public key is given, otherwise select with name - ipinfo_ip = chomp(data.http.my_public_ip[0].response_body) + ipinfo_ip = (can(chomp(data.http.my_public_ip[0].response_body)) ? chomp(data.http.my_public_ip[0].response_body) : "") ip = (local.security_group_ip == "" ? local.ipinfo_ip : local.security_group_ip) } From 4f6e286684e9fe8b6d8da47e5dab96b577b16814 Mon Sep 17 00:00:00 2001 From: matttrach Date: Wed, 10 Jan 2024 12:45:23 -0600 Subject: [PATCH 2/2] fix: add test for specifying ip Signed-off-by: matttrach --- examples/specifyip/main.tf | 21 +++++++++++++++ examples/specifyip/outputs.tf | 19 ++++++++++++++ examples/specifyip/variables.tf | 9 +++++++ examples/specifyip/versions.tf | 17 ++++++++++++ tests/specifyip_test.go | 46 +++++++++++++++++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 examples/specifyip/main.tf create mode 100644 examples/specifyip/outputs.tf create mode 100644 examples/specifyip/variables.tf create mode 100644 examples/specifyip/versions.tf create mode 100644 tests/specifyip_test.go diff --git a/examples/specifyip/main.tf b/examples/specifyip/main.tf new file mode 100644 index 0000000..acdd4c0 --- /dev/null +++ b/examples/specifyip/main.tf @@ -0,0 +1,21 @@ +# this is given for reference, in most cases you will want to set the region using environment variables +# provider "aws" { +# region = "us-west-1" +# } + +# AWS reserves the first four IP addresses and the last IP address in any CIDR block for its own use (cumulatively) +module "TestBasic" { + source = "../../" + owner = "terraform-ci@suse.com" + vpc_name = "terraform-aws-access-test-basic" + vpc_cidr = "10.0.255.0/24" # gives 256 usable addresses from .1 to .254, but AWS reserves .1 to .4 and .255, leaving .5 to .254 + subnet_name = "terraform-aws-access-test-basic" + subnet_cidr = "10.0.255.224/28" # gives 14 usable addresses from .225 to .238, but AWS reserves .225 to .227 and .238, leaving .227 to .237 + availability_zone = "us-west-1b" # check what availability zones are available in your region before setting this + security_group_name = "terraform-aws-access-test-basic" + security_group_type = "egress" + security_group_ip = chomp(var.ip) + public_ssh_key = var.key # I don't normally recommend this, but it allows tests to supply their own key + ssh_key_name = var.key_name # A lot of troubleshooting during critical times can be saved by hard coding variables in root modules + # root modules should be secured properly (including the state), and should represent your running infrastructure +} diff --git a/examples/specifyip/outputs.tf b/examples/specifyip/outputs.tf new file mode 100644 index 0000000..6ba84f6 --- /dev/null +++ b/examples/specifyip/outputs.tf @@ -0,0 +1,19 @@ +output "vpc" { + value = module.TestBasic.vpc +} + +output "subnet" { + value = module.TestBasic.subnet +} + +output "cidr" { + value = module.TestBasic.cidr +} + +output "security_group" { + value = module.TestBasic.security_group +} + +output "ssh_key" { + value = module.TestBasic.ssh_key +} diff --git a/examples/specifyip/variables.tf b/examples/specifyip/variables.tf new file mode 100644 index 0000000..18c5125 --- /dev/null +++ b/examples/specifyip/variables.tf @@ -0,0 +1,9 @@ +variable "key" { + type = string +} +variable "key_name" { + type = string +} +variable "ip" { + type = string +} \ No newline at end of file diff --git a/examples/specifyip/versions.tf b/examples/specifyip/versions.tf new file mode 100644 index 0000000..43a2f01 --- /dev/null +++ b/examples/specifyip/versions.tf @@ -0,0 +1,17 @@ +terraform { + required_version = ">= 1.2.0" + required_providers { + local = { + source = "hashicorp/local" + version = ">= 2.4" + } + aws = { + source = "hashicorp/aws" + version = ">= 5.11" + } + http = { + source = "hashicorp/http" + version = ">= 3.4" + } + } +} \ No newline at end of file diff --git a/tests/specifyip_test.go b/tests/specifyip_test.go new file mode 100644 index 0000000..d62ba2f --- /dev/null +++ b/tests/specifyip_test.go @@ -0,0 +1,46 @@ +package test + +import ( + "fmt" + "log" + "net" + "testing" + + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/ssh" + "github.com/gruntwork-io/terratest/modules/terraform" +) + +// this test generates all objects, no overrides +func TestIp(t *testing.T) { + t.Parallel() + uniqueID := random.UniqueId() + directory := "specifyip" + region := "us-west-1" + ip := GetOutboundIP().String() + + keyPair := ssh.GenerateRSAKeyPair(t, 2048) + keyPairName := fmt.Sprintf("terraform-aws-access-test-%s-%s", directory, uniqueID) + terraformVars := map[string]interface{}{ + "key_name": keyPairName, + "key": keyPair.PublicKey, + "ip": ip, + } + terraformOptions := setup(t, directory, region, terraformVars) + defer teardown(t, directory) + defer terraform.Destroy(t, terraformOptions) + terraform.InitAndApply(t, terraformOptions) +} + +// Get preferred outbound ip of this machine +func GetOutboundIP() net.IP { + conn, err := net.Dial("udp", "8.8.8.8:80") + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + localAddr := conn.LocalAddr().(*net.UDPAddr) + + return localAddr.IP +}