From 855aa888f2703b01a199c5ca794c2fd3b9d91e79 Mon Sep 17 00:00:00 2001 From: Matt Trachier Date: Mon, 4 Dec 2023 18:27:57 -0600 Subject: [PATCH] fix: deploy a basic server to test selecting (#32) Signed-off-by: matttrach --- examples/overrides/select_server/main.tf | 55 +++++++++++++++++-- examples/overrides/select_server/variables.tf | 11 ++++ tests/overrides_test.go | 13 ++--- 3 files changed, 68 insertions(+), 11 deletions(-) create mode 100644 examples/overrides/select_server/variables.tf diff --git a/examples/overrides/select_server/main.tf b/examples/overrides/select_server/main.tf index cf4f65e..9a41575 100644 --- a/examples/overrides/select_server/main.tf +++ b/examples/overrides/select_server/main.tf @@ -1,5 +1,52 @@ -module "TestSelectServer" { - source = "../../../" - image_id = "ami-09b2a1e33ce552e68" # this must be an image in your region, it should be the image used to create the server_id - id = "i-05d05c6c07c007054" # this must be an instance in your region +locals { + identifier = var.identifier # this is a random unique string that can be used to identify resources in the cloud provider + category = "basic" + example = "basic" + email = "terraform-ci@suse.com" + name = "tf-aws-server-${local.category}-${local.example}-${local.identifier}" + username = "tf-${local.identifier}" + key_name = var.key_name + image = "sles-15" + public_ssh_key = var.key # I don't normally recommend this, but it allows tests to supply their own key + # 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 +} + +# selecting the vpc, subnet, and ssh key pair, generating a security group specific to the runner +module "aws_access" { + source = "rancher/access/aws" + version = "v0.1.1" + owner = local.email + vpc_name = "default" + subnet_name = "default" + security_group_name = local.name + security_group_type = "specific" + ssh_key_name = local.key_name +} + +module "basic" { + depends_on = [ + module.aws_access, + ] + source = "../../../" # change this to "rancher/server/aws" per https://registry.terraform.io/modules/rancher/server/aws/latest + # version = "v0.0.15" # when using this example you will need to set the version + image = local.image + owner = local.email + name = local.name + type = "small" + user = local.username + ssh_key = module.aws_access.ssh_key.public_key + ssh_key_name = local.key_name + subnet_name = "default" + security_group_name = local.name # WARNING: security_group.name isn't the same as security_group->tags->Name +} + +module "TestSelectServer" { + depends_on = [ + module.aws_access, + module.basic, + ] + source = "../../../" + image_id = module.basic.ami # this must be an image in your region, it should be the image used to create the server_id + id = module.basic.id # this must be an instance in your region } diff --git a/examples/overrides/select_server/variables.tf b/examples/overrides/select_server/variables.tf new file mode 100644 index 0000000..d125822 --- /dev/null +++ b/examples/overrides/select_server/variables.tf @@ -0,0 +1,11 @@ +variable "key" { + type = string + +} +variable "key_name" { + type = string + +} +variable "identifier" { + type = string +} diff --git a/tests/overrides_test.go b/tests/overrides_test.go index 8703e85..5291347 100644 --- a/tests/overrides_test.go +++ b/tests/overrides_test.go @@ -48,14 +48,13 @@ func TestSelectServer(t *testing.T) { category := "overrides" directory := "select_server" region := "us-west-1" - terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ - TerraformDir: fmt.Sprintf("../examples/%s/%s", category, directory), - // Environment variables to set when running Terraform - EnvVars: map[string]string{ - "AWS_DEFAULT_REGION": region, - }, - }) + owner := "terraform-ci@suse.com" + terraformOptions, keyPair := setup(t, category, directory, region, owner) + sshAgent := ssh.SshAgentWithKeyPair(t, keyPair.KeyPair) + defer sshAgent.Stop() + terraformOptions.SshAgent = sshAgent + defer teardown(t, category, directory, keyPair) defer terraform.Destroy(t, terraformOptions) terraform.InitAndApply(t, terraformOptions) }