Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions solutions/standard/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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" : (
Expand Down
1 change: 1 addition & 0 deletions tests/existing-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The terraform code in this directory is used by the existing resource test in tests/pr_test.go
17 changes: 17 additions & 0 deletions tests/existing-resources/main.tf
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions tests/existing-resources/outputs.tf
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 4 additions & 0 deletions tests/existing-resources/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = var.region
}
33 changes: 33 additions & 0 deletions tests/existing-resources/variables.tf
Original file line number Diff line number Diff line change
@@ -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 = []
}
9 changes: 9 additions & 0 deletions tests/existing-resources/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
required_version = ">= 1.3.0"
required_providers {
ibm = {
source = "ibm-cloud/ibm"
version = ">= 1.64.1"
}
}
}
90 changes: 89 additions & 1 deletion tests/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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",
Expand Down Expand Up @@ -183,3 +191,83 @@ 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 := ".."
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)")
}
}