Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ You need the following permissions to run this module.

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.70.0, <2.0.0 |
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1, < 4.0.0 |
| <a name="requirement_time"></a> [time](#requirement\_time) | >= 0.9.1 |
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/version.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.9.0"
required_providers {
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
ibm = {
Expand Down
2 changes: 1 addition & 1 deletion examples/complete/version.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.9.0"
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
required_providers {
ibm = {
Expand Down
2 changes: 1 addition & 1 deletion examples/fscloud/version.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.9.0"
required_providers {
# Use latest version of provider in non-basic examples to verify latest version works with module
ibm = {
Expand Down
2 changes: 1 addition & 1 deletion modules/fscloud/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The IBM Cloud Framework for Financial Services mandates the application of an in

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.70.0, <2.0.0 |

### Modules
Expand Down
2 changes: 1 addition & 1 deletion modules/fscloud/version.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.9.0"
required_providers {
# The below tflint-ignore is required because although the below provider is not directly required by this submodule,
# it is required by consuming modules, and if not set here, the top level module calling this module will not be
Expand Down
2 changes: 1 addition & 1 deletion solutions/standard/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ module "es_instance_crn_parser" {

# Existing instance local vars
locals {
existing_elasticsearch_guid = var.existing_db_instance_crn != null ? module.es_instance_crn_parser[0].guid : null
existing_elasticsearch_guid = var.existing_db_instance_crn != null ? module.es_instance_crn_parser[0].service_instance : null
existing_elasticsearch_region = var.existing_db_instance_crn != null ? module.es_instance_crn_parser[0].region : null

# Validate the region input matches region detected in existing instance CRN (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
Expand Down
2 changes: 1 addition & 1 deletion solutions/standard/version.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.9.0"

# Lock DA into an exact provider version - renovate automation will keep it updated
required_providers {
Expand Down
84 changes: 84 additions & 0 deletions tests/pr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import (
"fmt"
"log"
"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/cloudinfo"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/common"
"github.com/terraform-ibm-modules/ibmcloud-terratest-wrapper/testhelper"
Expand Down Expand Up @@ -211,3 +216,82 @@ func TestRunStandardSolutionIBMKeys(t *testing.T) {
assert.Nil(t, err, "This should not have errored")
assert.NotNil(t, output, "Expected some output")
}

func TestRunExistingESSolution(t *testing.T) {
t.Parallel()

prefix := fmt.Sprintf("existing-es-%s", strings.ToLower(random.UniqueId()))
// realTerraformDir := "./resources"
realTerraformDir := ".."
tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId())))
resourcesDir := tempTerraformDir + "/tests/resources"
tags := common.GetTagsFromTravis()

// 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")

// Programmatically determine region to use based on availability
region, _ := testhelper.GetBestVpcRegion(val, "../common-dev-assets/common-go-assets/cloudinfo-region-vpc-gen2-prefs.yaml", "eu-de")

logger.Log(t, "Tempdir: ", tempTerraformDir)
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
TerraformDir: resourcesDir,
Vars: map[string]interface{}{
"prefix": prefix,
"region": region,
"resource_tags": tags,
},
// 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 {
options := testschematic.TestSchematicOptionsDefault(&testschematic.TestSchematicOptions{
Testing: t,
TarIncludePatterns: []string{
"*.tf",
fmt.Sprintf("%s/*.tf", standardSolutionTerraformDir),
fmt.Sprintf("%s/*.tf", fscloudExampleTerraformDir),
fmt.Sprintf("%s/*.tf", "modules/fscloud"),
fmt.Sprintf("%s/*.sh", "scripts"),
},
TemplateFolder: standardSolutionTerraformDir,
BestRegionYAMLPath: regionSelectionPath,
Prefix: "els-upg-da",
ResourceGroup: resourceGroup,
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: "existing_db_instance_crn", Value: terraform.Output(t, existingTerraformOptions, "id"), DataType: "string"},
{Name: "resource_group_name", Value: fmt.Sprintf("%s-resource-group", prefix), DataType: "string"},
{Name: "region", Value: region, DataType: "string"},
{Name: "use_existing_resource_group", Value: true, DataType: "bool"},
{Name: "provider_visibility", Value: "public", DataType: "string"},
}
err := options.RunSchematicTest()
assert.Nil(t, err, "This should not have errored")
}

// 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)")
}
}
1 change: 1 addition & 0 deletions tests/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 tests to provision a Elasticsearch which is required to test the DA.
31 changes: 31 additions & 0 deletions tests/resources/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
##############################################################################
# Resource Group
##############################################################################

module "resource_group" {
source = "git::https://github.com/terraform-ibm-modules/terraform-ibm-resource-group.git?ref=v1.1.6"
# if an existing resource group is not set (null) create a new one using prefix
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
existing_resource_group_name = var.resource_group
}

##############################################################################
# Elasticsearch Instance
##############################################################################

module "icd_elasticsearch" {
source = "../../"
resource_group_id = module.resource_group.resource_group_id
name = "${var.prefix}-elasticsearch"
region = var.region
elasticsearch_version = var.elasticsearch_version
tags = var.resource_tags
access_tags = var.access_tags
service_endpoints = "private"
service_credential_names = {
"elasticsearch_admin" : "Administrator",
"elasticsearch_operator" : "Operator",
"elasticsearch_viewer" : "Viewer",
"elasticsearch_editor" : "Editor",
}
}
33 changes: 33 additions & 0 deletions tests/resources/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
##############################################################################
# Outputs
##############################################################################
output "id" {
description = "Elasticsearch id"
value = module.icd_elasticsearch.id
}

output "version" {
description = "Enterprise DB instance version"
value = module.icd_elasticsearch.version
}

output "adminuser" {
description = "Database admin user name"
value = module.icd_elasticsearch.adminuser
}

output "hostname" {
description = "Database connection hostname"
value = module.icd_elasticsearch.hostname
}

output "port" {
description = "Database connection port"
value = module.icd_elasticsearch.port
}

output "certificate_base64" {
description = "Database connection certificate"
value = module.icd_elasticsearch.certificate_base64
sensitive = true
}
11 changes: 11 additions & 0 deletions tests/resources/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
provider "ibm" {
ibmcloud_api_key = var.ibmcloud_api_key
region = var.region
}

provider "elasticsearch" {
username = module.icd_elasticsearch.service_credentials_object.credentials["elasticsearch_admin"].username
password = module.icd_elasticsearch.service_credentials_object.credentials["elasticsearch_admin"].password
url = "https://${module.icd_elasticsearch.service_credentials_object.hostname}:${module.icd_elasticsearch.service_credentials_object.port}"
cacert_file = base64decode(module.icd_elasticsearch.service_credentials_object.certificate)
}
41 changes: 41 additions & 0 deletions tests/resources/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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 = "elastic"
}

variable "resource_group" {
type = string
description = "An existing resource group name to use for this example, if unset a new resource group will be created"
default = null
}

variable "elasticsearch_version" {
type = string
description = "Version of elasticsearch to deploy"
default = null
}

variable "access_tags" {
type = list(string)
description = "A list of access tags to apply to the Elasticsearch instance created by the module, see https://cloud.ibm.com/docs/account?topic=account-access-tags-tutorial for more details"
default = []
}

variable "resource_tags" {
type = list(string)
description = "Optional list of tags to be added to created resources"
default = []
}
15 changes: 15 additions & 0 deletions tests/resources/version.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_version = ">= 1.9.0"
required_providers {
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
ibm = {
source = "IBM-Cloud/ibm"
version = "1.70.0"
}
# The elasticsearch provider is not actually required by the module itself, just this example, so OK to use ">=" here instead of locking into a version
elasticsearch = {
source = "phillbaker/elasticsearch"
version = ">= 2.0.7"
}
}
}
2 changes: 1 addition & 1 deletion version.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = ">= 1.3.0"
required_version = ">= 1.9.0"
# Use "greater than or equal to" range in modules
required_providers {
ibm = {
Expand Down