From 244a969130193519d15864fb7e9ae322f68b88af Mon Sep 17 00:00:00 2001 From: "akocbek@ie.ibm.com" Date: Thu, 26 Sep 2024 14:30:54 +0100 Subject: [PATCH 1/3] fix: use existing notification instance crn --- solutions/standard/main.tf | 4 +- tests/existing-resources copy/README.md | 1 + tests/existing-resources copy/main.tf | 17 ++++ tests/existing-resources copy/outputs.tf | 28 +++++++ tests/existing-resources copy/provider.tf | 4 + tests/existing-resources copy/variables.tf | 33 ++++++++ tests/existing-resources copy/version.tf | 9 +++ tests/existing-resources/README.md | 1 + tests/existing-resources/main.tf | 17 ++++ tests/existing-resources/outputs.tf | 28 +++++++ tests/existing-resources/provider.tf | 4 + tests/existing-resources/variables.tf | 33 ++++++++ tests/existing-resources/version.tf | 9 +++ tests/pr_test.go | 93 +++++++++++++++++++++- 14 files changed, 278 insertions(+), 3 deletions(-) create mode 100644 tests/existing-resources copy/README.md create mode 100644 tests/existing-resources copy/main.tf create mode 100644 tests/existing-resources copy/outputs.tf create mode 100644 tests/existing-resources copy/provider.tf create mode 100644 tests/existing-resources copy/variables.tf create mode 100644 tests/existing-resources copy/version.tf create mode 100644 tests/existing-resources/README.md create mode 100644 tests/existing-resources/main.tf create mode 100644 tests/existing-resources/outputs.tf create mode 100644 tests/existing-resources/provider.tf create mode 100644 tests/existing-resources/variables.tf create mode 100644 tests/existing-resources/version.tf diff --git a/solutions/standard/main.tf b/solutions/standard/main.tf index 850d1249..24caae9a 100644 --- a/solutions/standard/main.tf +++ b/solutions/standard/main.tf @@ -26,11 +26,11 @@ locals { existing_kms_guid = var.existing_kms_instance_crn != null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 3) : tobool("The CRN of the existing KMS is not provided.") en_key_name = var.prefix != null ? "${var.prefix}-${var.en_key_name}" : var.en_key_name en_key_ring_name = var.prefix != null ? "${var.prefix}-${var.en_key_ring_name}" : var.en_key_ring_name - en_kms_key_id = local.existing_kms_root_key_id != null ? local.existing_kms_root_key_id : module.kms[0].keys[format("%s.%s", local.en_key_ring_name, local.en_key_name)].key_id + en_kms_key_id = local.existing_kms_root_key_id != null ? local.existing_kms_root_key_id : var.existing_en_instance_crn == null ? module.kms[0].keys[format("%s.%s", local.en_key_ring_name, local.en_key_name)].key_id : null cos_key_name = var.prefix != null ? "${var.prefix}-${var.cos_key_name}" : var.cos_key_name cos_key_ring_name = var.prefix != null ? "${var.prefix}-${var.cos_key_ring_name}" : var.cos_key_ring_name cos_instance_guid = var.existing_cos_instance_crn != null ? element(split(":", var.existing_cos_instance_crn), length(split(":", var.existing_cos_instance_crn)) - 3) : null - cos_kms_key_crn = var.existing_cos_bucket_name != null ? null : var.existing_kms_root_key_crn != null ? var.existing_kms_root_key_crn : module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn + cos_kms_key_crn = var.existing_cos_bucket_name != null ? null : var.existing_kms_root_key_crn != null ? var.existing_kms_root_key_crn : var.existing_en_instance_crn == null ? module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn : null kms_service_name = var.existing_kms_instance_crn != null ? ( can(regex(".*kms.*", var.existing_kms_instance_crn)) ? "kms" : ( diff --git a/tests/existing-resources copy/README.md b/tests/existing-resources copy/README.md new file mode 100644 index 00000000..4bb3621d --- /dev/null +++ b/tests/existing-resources copy/README.md @@ -0,0 +1 @@ +The terraform code in this directory is used by the existing resource test in tests/pr_test.go diff --git a/tests/existing-resources copy/main.tf b/tests/existing-resources copy/main.tf new file mode 100644 index 00000000..f57c16b5 --- /dev/null +++ b/tests/existing-resources copy/main.tf @@ -0,0 +1,17 @@ +module "resource_group" { + source = "terraform-ibm-modules/resource-group/ibm" + version = "1.1.6" + resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null + existing_resource_group_name = var.resource_group +} + +module "event_notification" { + source = "../../" + resource_group_id = module.resource_group.resource_group_id + name = "${var.prefix}-en" + tags = var.resource_tags + plan = "lite" + service_endpoints = "public" + region = var.region + cos_integration_enabled = false +} diff --git a/tests/existing-resources copy/outputs.tf b/tests/existing-resources copy/outputs.tf new file mode 100644 index 00000000..738779c3 --- /dev/null +++ b/tests/existing-resources copy/outputs.tf @@ -0,0 +1,28 @@ +######################################################################################################################## +# Outputs +######################################################################################################################## + +output "resource_group_id" { + description = "The id of the resource group where resources are created" + value = module.resource_group.resource_group_id +} + +output "resource_group_name" { + description = "The name of the resource group where resources are created" + value = module.resource_group.resource_group_name +} + +output "event_notification_instance_name" { + description = "Event Notification name" + value = module.event_notification.event_notification_instance_name +} + +output "event_notification_instance_crn" { + description = "Event notification instance crn" + value = module.event_notification.crn +} + +output "event_notification_instance_guid" { + description = "Event Notification guid" + value = module.event_notification.guid +} diff --git a/tests/existing-resources copy/provider.tf b/tests/existing-resources copy/provider.tf new file mode 100644 index 00000000..df45ef50 --- /dev/null +++ b/tests/existing-resources copy/provider.tf @@ -0,0 +1,4 @@ +provider "ibm" { + ibmcloud_api_key = var.ibmcloud_api_key + region = var.region +} diff --git a/tests/existing-resources copy/variables.tf b/tests/existing-resources copy/variables.tf new file mode 100644 index 00000000..a237825c --- /dev/null +++ b/tests/existing-resources copy/variables.tf @@ -0,0 +1,33 @@ +############################################################################## +# Input variables +############################################################################## + +variable "ibmcloud_api_key" { + type = string + description = "The IBM Cloud API Key" + sensitive = true +} + +variable "region" { + type = string + description = "Region to provision all resources created by this example" + default = "us-south" +} + +variable "prefix" { + type = string + description = "Prefix to append to all resources created by this example" + default = "en-ext-res" +} + +variable "resource_group" { + type = string + description = "The name of an existing resource group to provision resources in to. If not set a new resource group will be created using the prefix variable" + default = null +} + +variable "resource_tags" { + type = list(string) + description = "Optional list of tags to be added to created resources" + default = [] +} diff --git a/tests/existing-resources copy/version.tf b/tests/existing-resources copy/version.tf new file mode 100644 index 00000000..97b20938 --- /dev/null +++ b/tests/existing-resources copy/version.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.3.0" + required_providers { + ibm = { + source = "ibm-cloud/ibm" + version = ">= 1.64.1" + } + } +} diff --git a/tests/existing-resources/README.md b/tests/existing-resources/README.md new file mode 100644 index 00000000..4bb3621d --- /dev/null +++ b/tests/existing-resources/README.md @@ -0,0 +1 @@ +The terraform code in this directory is used by the existing resource test in tests/pr_test.go diff --git a/tests/existing-resources/main.tf b/tests/existing-resources/main.tf new file mode 100644 index 00000000..f57c16b5 --- /dev/null +++ b/tests/existing-resources/main.tf @@ -0,0 +1,17 @@ +module "resource_group" { + source = "terraform-ibm-modules/resource-group/ibm" + version = "1.1.6" + resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null + existing_resource_group_name = var.resource_group +} + +module "event_notification" { + source = "../../" + resource_group_id = module.resource_group.resource_group_id + name = "${var.prefix}-en" + tags = var.resource_tags + plan = "lite" + service_endpoints = "public" + region = var.region + cos_integration_enabled = false +} diff --git a/tests/existing-resources/outputs.tf b/tests/existing-resources/outputs.tf new file mode 100644 index 00000000..738779c3 --- /dev/null +++ b/tests/existing-resources/outputs.tf @@ -0,0 +1,28 @@ +######################################################################################################################## +# Outputs +######################################################################################################################## + +output "resource_group_id" { + description = "The id of the resource group where resources are created" + value = module.resource_group.resource_group_id +} + +output "resource_group_name" { + description = "The name of the resource group where resources are created" + value = module.resource_group.resource_group_name +} + +output "event_notification_instance_name" { + description = "Event Notification name" + value = module.event_notification.event_notification_instance_name +} + +output "event_notification_instance_crn" { + description = "Event notification instance crn" + value = module.event_notification.crn +} + +output "event_notification_instance_guid" { + description = "Event Notification guid" + value = module.event_notification.guid +} diff --git a/tests/existing-resources/provider.tf b/tests/existing-resources/provider.tf new file mode 100644 index 00000000..df45ef50 --- /dev/null +++ b/tests/existing-resources/provider.tf @@ -0,0 +1,4 @@ +provider "ibm" { + ibmcloud_api_key = var.ibmcloud_api_key + region = var.region +} diff --git a/tests/existing-resources/variables.tf b/tests/existing-resources/variables.tf new file mode 100644 index 00000000..a237825c --- /dev/null +++ b/tests/existing-resources/variables.tf @@ -0,0 +1,33 @@ +############################################################################## +# Input variables +############################################################################## + +variable "ibmcloud_api_key" { + type = string + description = "The IBM Cloud API Key" + sensitive = true +} + +variable "region" { + type = string + description = "Region to provision all resources created by this example" + default = "us-south" +} + +variable "prefix" { + type = string + description = "Prefix to append to all resources created by this example" + default = "en-ext-res" +} + +variable "resource_group" { + type = string + description = "The name of an existing resource group to provision resources in to. If not set a new resource group will be created using the prefix variable" + default = null +} + +variable "resource_tags" { + type = list(string) + description = "Optional list of tags to be added to created resources" + default = [] +} diff --git a/tests/existing-resources/version.tf b/tests/existing-resources/version.tf new file mode 100644 index 00000000..97b20938 --- /dev/null +++ b/tests/existing-resources/version.tf @@ -0,0 +1,9 @@ +terraform { + required_version = ">= 1.3.0" + required_providers { + ibm = { + source = "ibm-cloud/ibm" + version = ">= 1.64.1" + } + } +} diff --git a/tests/pr_test.go b/tests/pr_test.go index a79466ac..97ccf3b2 100644 --- a/tests/pr_test.go +++ b/tests/pr_test.go @@ -2,12 +2,20 @@ package test import ( + "fmt" "log" "math/rand" "os" + "strings" "testing" + "github.com/gruntwork-io/terratest/modules/files" + "github.com/gruntwork-io/terratest/modules/logger" + "github.com/gruntwork-io/terratest/modules/random" + "github.com/gruntwork-io/terratest/modules/terraform" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common" "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper" "github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testschematic" @@ -22,7 +30,7 @@ const resourceGroup = "geretain-test-event-notifications" const yamlLocation = "../common-dev-assets/common-go-assets/common-permanent-resources.yaml" -// Current supported SCC region +// Current supported EN region var validRegions = []string{ "us-south", "eu-de", @@ -183,3 +191,86 @@ func TestRunUpgradeDASolution(t *testing.T) { assert.NotNil(t, output, "Expected some output") } } + +func TestRunExistingResourcesInstances(t *testing.T) { + t.Parallel() + + // ------------------------------------------------------------------------------------ + // Provision RG, EN + // ------------------------------------------------------------------------------------ + + prefix := fmt.Sprintf("en-existing-%s", strings.ToLower(random.UniqueId())) + // realTerraformDir := "./existing-resources" + // tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId()))) + + realTerraformDir := ".." + tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId()))) + existingRes := realTerraformDir + "/tests/existing-resources" + + // Verify ibmcloud_api_key variable is set + checkVariable := "TF_VAR_ibmcloud_api_key" + val, present := os.LookupEnv(checkVariable) + require.True(t, present, checkVariable+" environment variable not set") + require.NotEqual(t, "", val, checkVariable+" environment variable is empty") + logger.Log(t, "Tempdir: ", tempTerraformDir) + existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ + TerraformDir: existingRes, + Vars: map[string]interface{}{ + "prefix": prefix, + "region": validRegions[rand.Intn(len(validRegions))], + }, + // Set Upgrade to true to ensure latest version of providers and modules are used by terratest. + // This is the same as setting the -upgrade=true flag with terraform. + Upgrade: true, + }) + terraform.WorkspaceSelectOrNew(t, existingTerraformOptions, prefix) + _, existErr := terraform.InitAndApplyE(t, existingTerraformOptions) + if existErr != nil { + assert.True(t, existErr == nil, "Init and Apply of temp existing resource failed") + } else { + + var region = validRegions[rand.Intn(len(validRegions))] + + options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{ + Testing: t, + Prefix: "en-exs-res", + TarIncludePatterns: []string{ + "*.tf", + solutionDADir + "/*.tf", + }, + TemplateFolder: solutionDADir, + Tags: []string{"test-schematic"}, + DeleteWorkspaceOnFail: false, + WaitJobCompleteMinutes: 60, + }) + + options.TerraformVars = []testschematic.TestSchematicTerraformVar{ + {Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true}, + {Name: "region", Value: region, DataType: "string"}, + {Name: "resource_group_name", Value: terraform.Output(t, existingTerraformOptions, "resource_group_name"), DataType: "string"}, + {Name: "existing_kms_instance_crn", Value: permanentResources["hpcs_south_crn"], DataType: "string"}, + {Name: "existing_en_instance_crn", Value: terraform.Output(t, existingTerraformOptions, "event_notification_instance_crn"), DataType: "string"}, + {Name: "kms_endpoint_url", Value: permanentResources["hpcs_south_private_endpoint"], DataType: "string"}, + } + + err := options.RunSchematicTest() + + if assert.NoError(t, err) { + t.Log("TestRunExistingResourcesInstances Passed") + } else { + t.Error("TestRunExistingResourcesInstances Failed") + } + } + + // Check if "DO_NOT_DESTROY_ON_FAILURE" is set + envVal, _ := os.LookupEnv("DO_NOT_DESTROY_ON_FAILURE") + // Destroy the temporary existing resources if required + if t.Failed() && strings.ToLower(envVal) == "true" { + fmt.Println("Terratest failed. Debug the test and delete resources manually.") + } else { + logger.Log(t, "START: Destroy (existing resources)") + terraform.Destroy(t, existingTerraformOptions) + terraform.WorkspaceDelete(t, existingTerraformOptions, prefix) + logger.Log(t, "END: Destroy (existing resources)") + } +} From 013dc92c375691acfff720d184e6315e7b66fe51 Mon Sep 17 00:00:00 2001 From: "akocbek@ie.ibm.com" Date: Thu, 26 Sep 2024 15:01:16 +0100 Subject: [PATCH 2/3] fix: use existing notification instance crn --- tests/existing-resources copy/README.md | 1 - tests/existing-resources copy/main.tf | 17 ----------- tests/existing-resources copy/outputs.tf | 28 ------------------ tests/existing-resources copy/provider.tf | 4 --- tests/existing-resources copy/variables.tf | 33 ---------------------- tests/existing-resources copy/version.tf | 9 ------ 6 files changed, 92 deletions(-) delete mode 100644 tests/existing-resources copy/README.md delete mode 100644 tests/existing-resources copy/main.tf delete mode 100644 tests/existing-resources copy/outputs.tf delete mode 100644 tests/existing-resources copy/provider.tf delete mode 100644 tests/existing-resources copy/variables.tf delete mode 100644 tests/existing-resources copy/version.tf diff --git a/tests/existing-resources copy/README.md b/tests/existing-resources copy/README.md deleted file mode 100644 index 4bb3621d..00000000 --- a/tests/existing-resources copy/README.md +++ /dev/null @@ -1 +0,0 @@ -The terraform code in this directory is used by the existing resource test in tests/pr_test.go diff --git a/tests/existing-resources copy/main.tf b/tests/existing-resources copy/main.tf deleted file mode 100644 index f57c16b5..00000000 --- a/tests/existing-resources copy/main.tf +++ /dev/null @@ -1,17 +0,0 @@ -module "resource_group" { - source = "terraform-ibm-modules/resource-group/ibm" - version = "1.1.6" - resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null - existing_resource_group_name = var.resource_group -} - -module "event_notification" { - source = "../../" - resource_group_id = module.resource_group.resource_group_id - name = "${var.prefix}-en" - tags = var.resource_tags - plan = "lite" - service_endpoints = "public" - region = var.region - cos_integration_enabled = false -} diff --git a/tests/existing-resources copy/outputs.tf b/tests/existing-resources copy/outputs.tf deleted file mode 100644 index 738779c3..00000000 --- a/tests/existing-resources copy/outputs.tf +++ /dev/null @@ -1,28 +0,0 @@ -######################################################################################################################## -# Outputs -######################################################################################################################## - -output "resource_group_id" { - description = "The id of the resource group where resources are created" - value = module.resource_group.resource_group_id -} - -output "resource_group_name" { - description = "The name of the resource group where resources are created" - value = module.resource_group.resource_group_name -} - -output "event_notification_instance_name" { - description = "Event Notification name" - value = module.event_notification.event_notification_instance_name -} - -output "event_notification_instance_crn" { - description = "Event notification instance crn" - value = module.event_notification.crn -} - -output "event_notification_instance_guid" { - description = "Event Notification guid" - value = module.event_notification.guid -} diff --git a/tests/existing-resources copy/provider.tf b/tests/existing-resources copy/provider.tf deleted file mode 100644 index df45ef50..00000000 --- a/tests/existing-resources copy/provider.tf +++ /dev/null @@ -1,4 +0,0 @@ -provider "ibm" { - ibmcloud_api_key = var.ibmcloud_api_key - region = var.region -} diff --git a/tests/existing-resources copy/variables.tf b/tests/existing-resources copy/variables.tf deleted file mode 100644 index a237825c..00000000 --- a/tests/existing-resources copy/variables.tf +++ /dev/null @@ -1,33 +0,0 @@ -############################################################################## -# Input variables -############################################################################## - -variable "ibmcloud_api_key" { - type = string - description = "The IBM Cloud API Key" - sensitive = true -} - -variable "region" { - type = string - description = "Region to provision all resources created by this example" - default = "us-south" -} - -variable "prefix" { - type = string - description = "Prefix to append to all resources created by this example" - default = "en-ext-res" -} - -variable "resource_group" { - type = string - description = "The name of an existing resource group to provision resources in to. If not set a new resource group will be created using the prefix variable" - default = null -} - -variable "resource_tags" { - type = list(string) - description = "Optional list of tags to be added to created resources" - default = [] -} diff --git a/tests/existing-resources copy/version.tf b/tests/existing-resources copy/version.tf deleted file mode 100644 index 97b20938..00000000 --- a/tests/existing-resources copy/version.tf +++ /dev/null @@ -1,9 +0,0 @@ -terraform { - required_version = ">= 1.3.0" - required_providers { - ibm = { - source = "ibm-cloud/ibm" - version = ">= 1.64.1" - } - } -} From 8de1d3ecdea68eb1e17abe9f192aa1b9fd4eb84e Mon Sep 17 00:00:00 2001 From: "akocbek@ie.ibm.com" Date: Thu, 26 Sep 2024 15:01:45 +0100 Subject: [PATCH 3/3] fix: use existing notification instance crn --- tests/pr_test.go | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/pr_test.go b/tests/pr_test.go index 97ccf3b2..e56edeb9 100644 --- a/tests/pr_test.go +++ b/tests/pr_test.go @@ -200,9 +200,6 @@ func TestRunExistingResourcesInstances(t *testing.T) { // ------------------------------------------------------------------------------------ prefix := fmt.Sprintf("en-existing-%s", strings.ToLower(random.UniqueId())) - // realTerraformDir := "./existing-resources" - // tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId()))) - realTerraformDir := ".." tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId()))) existingRes := realTerraformDir + "/tests/existing-resources"