Skip to content

Commit 25e36fb

Browse files
authored
feat: enable_log_archive variable has been replace by log_analysis_enable_archive and activity_tracker_enable_archive variables<br> - fixed some bugs related to using existing resources (#93)
1 parent 53d37fe commit 25e36fb

File tree

9 files changed

+248
-13
lines changed

9 files changed

+248
-13
lines changed

solutions/instances/main.tf

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ locals {
1818
at_cos_target_bucket_name = var.prefix != null ? "${var.prefix}-${var.at_cos_target_bucket_name}" : var.at_cos_target_bucket_name
1919

2020
cos_instance_crn = var.existing_cos_instance_crn != null ? var.existing_cos_instance_crn : module.cos_instance[0].cos_instance_crn
21-
existing_kms_guid = var.existing_kms_instance_crn != null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 3) : length(local.bucket_config_map) == 2 ? null : tobool("The CRN of the existing KMS is not provided.")
21+
existing_kms_guid = (var.existing_log_archive_cos_bucket_name != null && var.existing_at_cos_target_bucket_name != null) || (var.log_analysis_provision == false && var.enable_at_event_routing_to_cos_bucket == false) ? null : 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.")
2222
cos_instance_guid = var.existing_cos_instance_crn == null ? module.cos_instance[0].cos_instance_guid : element(split(":", var.existing_cos_instance_crn), length(split(":", var.existing_cos_instance_crn)) - 3)
2323
archive_cos_bucket_name = var.existing_log_archive_cos_bucket_name != null ? var.existing_log_archive_cos_bucket_name : module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].bucket_name
2424
archive_cos_bucket_endpoint = var.existing_log_archive_cos_bucket_endpoint != null ? var.existing_log_archive_cos_bucket_endpoint : module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].s3_endpoint_private
@@ -68,7 +68,7 @@ locals {
6868
)
6969
) : null
7070

71-
kms_region = (length(local.bucket_config_map) != 0) ? (var.existing_cos_kms_key_crn == null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 5) : null) : null
71+
kms_region = (length(coalesce(local.bucket_config_map, [])) != 0) ? (var.existing_cos_kms_key_crn == null ? element(split(":", var.existing_kms_instance_crn), length(split(":", var.existing_kms_instance_crn)) - 5) : null) : null
7272
at_cos_route = var.enable_at_event_routing_to_cos_bucket ? [{
7373
route_name = "at-cos-route"
7474
locations = ["*", "global"]
@@ -102,15 +102,16 @@ module "resource_group" {
102102

103103
module "observability_instance" {
104104
source = "terraform-ibm-modules/observability-instances/ibm"
105-
version = "2.12.2"
105+
version = "2.13.2"
106106
providers = {
107107
logdna.at = logdna.at
108108
logdna.ld = logdna.ld
109109
}
110-
region = var.region
111-
resource_group_id = module.resource_group.resource_group_id
112-
enable_archive = var.enable_log_archive
113-
ibmcloud_api_key = local.archive_api_key
110+
region = var.region
111+
resource_group_id = module.resource_group.resource_group_id
112+
log_analysis_enable_archive = var.log_analysis_enable_archive
113+
activity_tracker_enable_archive = var.activity_tracker_enable_archive
114+
ibmcloud_api_key = local.archive_api_key
114115
# Log Analysis
115116
log_analysis_provision = var.log_analysis_provision
116117
log_analysis_instance_name = var.prefix != null ? "${var.prefix}-${var.log_analysis_instance_name}" : var.log_analysis_instance_name
@@ -164,7 +165,7 @@ module "kms" {
164165
providers = {
165166
ibm = ibm.kms
166167
}
167-
count = (var.existing_cos_kms_key_crn != null || (length(local.bucket_config_map) == 0)) ? 0 : 1 # no need to create any KMS resources if passing an existing key, or bucket
168+
count = (var.existing_cos_kms_key_crn != null || (length(coalesce(local.bucket_config_map, [])) == 0)) ? 0 : 1 # no need to create any KMS resources if passing an existing key, or bucket
168169
source = "terraform-ibm-modules/kms-all-inclusive/ibm"
169170
version = "4.13.2"
170171
create_key_protect_instance = false
@@ -205,7 +206,7 @@ resource "time_sleep" "wait_for_authorization_policy" {
205206

206207
# Create IAM Authorization Policy to allow COS to access KMS for the encryption key
207208
resource "ibm_iam_authorization_policy" "policy" {
208-
count = (var.skip_cos_kms_auth_policy || (length(local.bucket_config_map) == 0)) ? 0 : 1
209+
count = (var.skip_cos_kms_auth_policy || (length(coalesce(local.bucket_config_map, [])) == 0)) ? 0 : 1
209210
source_service_name = "cloud-object-storage"
210211
source_resource_instance_id = local.cos_instance_guid
211212
target_service_name = local.kms_service
@@ -218,7 +219,7 @@ module "cos_instance" {
218219
providers = {
219220
ibm = ibm.cos
220221
}
221-
count = (var.existing_cos_instance_crn == null) && length(local.bucket_config_map) != 0 ? 1 : 0 # no need to call COS module if consumer is using existing COS instance
222+
count = (var.existing_cos_instance_crn == null) && length(coalesce(local.bucket_config_map, [])) != 0 ? 1 : 0 # no need to call COS module if consumer is using existing COS instance
222223
source = "terraform-ibm-modules/cos/ibm//modules/fscloud"
223224
version = "8.3.2"
224225
resource_group_id = module.resource_group.resource_group_id
@@ -235,7 +236,7 @@ module "cos_bucket" {
235236
providers = {
236237
ibm = ibm.cos
237238
}
238-
count = (length(local.bucket_config_map) != 0) ? 1 : 0 # no need to call COS module if consumer is using existing COS bucket
239+
count = (length(coalesce(local.bucket_config_map, [])) != 0) ? 1 : 0 # no need to call COS module if consumer is using existing COS bucket
239240
source = "terraform-ibm-modules/cos/ibm//modules/buckets"
240241
version = "8.3.2"
241242
bucket_configs = [

solutions/instances/variables.tf

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@ variable "log_analysis_tags" {
7979
default = []
8080
}
8181

82-
variable "enable_log_archive" {
82+
variable "log_analysis_enable_archive" {
8383
type = bool
84-
description = "Enable the archive file for the IBM Log Analysis instance."
84+
description = "Enable archive on log analysis instances"
85+
default = true
86+
}
87+
88+
variable "activity_tracker_enable_archive" {
89+
type = bool
90+
description = "Enable archive on activity tracker instances"
8591
default = true
8692
}
8793

tests/pr_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,111 @@ func TestAgentsSolutionInSchematics(t *testing.T) {
207207
logger.Log(t, "END: Destroy (existing resources)")
208208
}
209209
}
210+
211+
func TestRunExistingResourcesInstances(t *testing.T) {
212+
t.Parallel()
213+
214+
// ------------------------------------------------------------------------------------
215+
// Provision COS first
216+
// ------------------------------------------------------------------------------------
217+
218+
prefix := fmt.Sprintf("obs-exist-%s", strings.ToLower(random.UniqueId()))
219+
realTerraformDir := "./resources/existing-resources"
220+
tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId())))
221+
tags := common.GetTagsFromTravis()
222+
region := "us-south"
223+
224+
// Verify ibmcloud_api_key variable is set
225+
checkVariable := "TF_VAR_ibmcloud_api_key"
226+
val, present := os.LookupEnv(checkVariable)
227+
require.True(t, present, checkVariable+" environment variable not set")
228+
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
229+
230+
logger.Log(t, "Tempdir: ", tempTerraformDir)
231+
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
232+
TerraformDir: tempTerraformDir,
233+
Vars: map[string]interface{}{
234+
"prefix": prefix,
235+
"region": region,
236+
"resource_tags": tags,
237+
},
238+
// Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
239+
// This is the same as setting the -upgrade=true flag with terraform.
240+
Upgrade: true,
241+
})
242+
243+
terraform.WorkspaceSelectOrNew(t, existingTerraformOptions, prefix)
244+
_, existErr := terraform.InitAndApplyE(t, existingTerraformOptions)
245+
if existErr != nil {
246+
assert.True(t, existErr == nil, "Init and Apply of temp existing resource failed")
247+
} else {
248+
249+
// ------------------------------------------------------------------------------------
250+
// Deploy Observability instances DA passing in existing COS instance, and bucket details
251+
// ------------------------------------------------------------------------------------
252+
253+
options := testhelper.TestOptionsDefault(&testhelper.TestOptions{
254+
Testing: t,
255+
TerraformDir: solutionInstanceDADir,
256+
// Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur
257+
ImplicitRequired: false,
258+
Region: region,
259+
TerraformVars: map[string]interface{}{
260+
"cos_region": region,
261+
"resource_group_name": terraform.Output(t, existingTerraformOptions, "resource_group_name"),
262+
"use_existing_resource_group": true,
263+
"existing_log_archive_cos_bucket_name": terraform.Output(t, existingTerraformOptions, "bucket_name"),
264+
"existing_at_cos_target_bucket_name": terraform.Output(t, existingTerraformOptions, "bucket_name_at"),
265+
"existing_log_archive_cos_bucket_endpoint": terraform.Output(t, existingTerraformOptions, "bucket_endpoint"),
266+
"existing_at_cos_target_bucket_endpoint": terraform.Output(t, existingTerraformOptions, "bucket_endpoint_at"),
267+
"existing_cos_instance_crn": terraform.Output(t, existingTerraformOptions, "cos_crn"),
268+
"management_endpoint_type_for_bucket": "public",
269+
"log_analysis_service_endpoints": "public",
270+
"enable_platform_metrics": "false",
271+
},
272+
})
273+
274+
output, err := options.RunTestConsistency()
275+
assert.Nil(t, err, "This should not have errored")
276+
assert.NotNil(t, output, "Expected some output")
277+
278+
// ------------------------------------------------------------------------------------
279+
// Deploy Observability instance DA passing in existing COS instance (not bucket), and KMS key
280+
// ------------------------------------------------------------------------------------
281+
282+
options2 := testhelper.TestOptionsDefault(&testhelper.TestOptions{
283+
Testing: t,
284+
TerraformDir: solutionInstanceDADir,
285+
// Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur
286+
ImplicitRequired: false,
287+
TerraformVars: map[string]interface{}{
288+
"cos_region": region,
289+
"resource_group_name": terraform.Output(t, existingTerraformOptions, "resource_group_name"),
290+
"use_existing_resource_group": true,
291+
"existing_kms_instance_crn": permanentResources["hpcs_south_crn"],
292+
"kms_endpoint_type": "public",
293+
"existing_cos_instance_crn": terraform.Output(t, existingTerraformOptions, "cos_crn"),
294+
"management_endpoint_type_for_bucket": "public",
295+
"log_analysis_service_endpoints": "public",
296+
"enable_platform_metrics": "false",
297+
},
298+
})
299+
300+
output2, err := options2.RunTestConsistency()
301+
assert.Nil(t, err, "This should not have errored")
302+
assert.NotNil(t, output2, "Expected some output")
303+
304+
}
305+
306+
// Check if "DO_NOT_DESTROY_ON_FAILURE" is set
307+
envVal, _ := os.LookupEnv("DO_NOT_DESTROY_ON_FAILURE")
308+
// Destroy the temporary existing resources if required
309+
if t.Failed() && strings.ToLower(envVal) == "true" {
310+
fmt.Println("Terratest failed. Debug the test and delete resources manually.")
311+
} else {
312+
logger.Log(t, "START: Destroy (existing resources)")
313+
terraform.Destroy(t, existingTerraformOptions)
314+
terraform.WorkspaceDelete(t, existingTerraformOptions, prefix)
315+
logger.Log(t, "END: Destroy (existing resources)")
316+
}
317+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The terraform code in this directory is used by the existing resource test in tests/pr_test.go
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
##############################################################################
2+
# Resource Group
3+
##############################################################################
4+
5+
module "resource_group" {
6+
source = "terraform-ibm-modules/resource-group/ibm"
7+
version = "1.1.5"
8+
# if an existing resource group is not set (null) create a new one using prefix
9+
resource_group_name = var.resource_group == null ? "${var.prefix}-resource-group" : null
10+
existing_resource_group_name = var.resource_group
11+
}
12+
13+
##############################################################################
14+
# Create Cloud Object Storage instance and buckets
15+
##############################################################################
16+
17+
module "cos" {
18+
source = "terraform-ibm-modules/cos/ibm"
19+
version = "8.2.13"
20+
resource_group_id = module.resource_group.resource_group_id
21+
region = var.region
22+
cos_instance_name = "${var.prefix}-cos"
23+
cos_tags = var.resource_tags
24+
bucket_name = "${var.prefix}-bucket"
25+
retention_enabled = false # disable retention for test environments - enable for stage/prod
26+
kms_encryption_enabled = false
27+
}
28+
29+
module "additional_cos_bucket" {
30+
source = "terraform-ibm-modules/cos/ibm"
31+
version = "8.2.13"
32+
region = var.region
33+
create_cos_instance = false
34+
existing_cos_instance_id = module.cos.cos_instance_id
35+
bucket_name = "${var.prefix}-bucket-at"
36+
kms_encryption_enabled = false
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
##############################################################################
2+
# Outputs
3+
##############################################################################
4+
5+
output "resource_group_name" {
6+
description = "Resource group name"
7+
value = module.resource_group.resource_group_name
8+
}
9+
10+
output "prefix" {
11+
description = "Prefix"
12+
value = var.prefix
13+
}
14+
15+
output "cos_crn" {
16+
description = "COS CRN"
17+
value = module.cos.cos_instance_crn
18+
}
19+
20+
output "bucket_name" {
21+
description = "Log Archive bucket name"
22+
value = module.cos.bucket_name
23+
}
24+
25+
output "bucket_name_at" {
26+
description = "Activity Tracker bucket name"
27+
value = module.additional_cos_bucket.bucket_name
28+
}
29+
30+
output "bucket_endpoint" {
31+
description = "Log Archive bucket endpoint"
32+
value = module.cos.s3_endpoint_public
33+
}
34+
35+
output "bucket_endpoint_at" {
36+
description = "Activity Tracker bucket endpoint"
37+
value = module.additional_cos_bucket.s3_endpoint_public
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
provider "ibm" {
2+
ibmcloud_api_key = var.ibmcloud_api_key
3+
region = var.region
4+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
##############################################################################
2+
# Input variables
3+
##############################################################################
4+
5+
variable "ibmcloud_api_key" {
6+
type = string
7+
description = "The IBM Cloud API Key"
8+
sensitive = true
9+
}
10+
11+
variable "region" {
12+
type = string
13+
description = "Region"
14+
}
15+
16+
variable "prefix" {
17+
type = string
18+
description = "The prefix to add to all resources."
19+
}
20+
21+
variable "resource_group" {
22+
type = string
23+
description = "The name of an existing resource group to provision resources in. If not specified, a new resource group is created with the `prefix` variable."
24+
default = null
25+
}
26+
27+
variable "resource_tags" {
28+
type = list(string)
29+
description = "The tags to add to the created resources."
30+
default = []
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.3.0"
3+
required_providers {
4+
ibm = {
5+
source = "ibm-cloud/ibm"
6+
version = ">= 1.51.0"
7+
}
8+
}
9+
}

0 commit comments

Comments
 (0)