Skip to content

Commit bbc8288

Browse files
authored
fix: skip cos creation when log_analysis_archive and at_event_routing is false (#136)
* feat: skip cos creation when archive and routing is false * precommit changes * change map creation logic * update logic
1 parent 8c5caca commit bbc8288

File tree

3 files changed

+20
-31
lines changed

3 files changed

+20
-31
lines changed

solutions/instances/main.tf

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@ locals {
1717
log_archive_cos_bucket_name = var.prefix != null ? "${var.prefix}-${var.log_archive_cos_bucket_name}" : var.log_archive_cos_bucket_name
1818
at_cos_target_bucket_name = var.prefix != null ? "${var.prefix}-${var.at_cos_target_bucket_name}" : var.at_cos_target_bucket_name
1919

20-
cos_instance_crn = var.existing_cos_instance_crn != null ? var.existing_cos_instance_crn : module.cos_instance[0].cos_instance_crn
20+
cos_instance_crn = var.existing_cos_instance_crn != null ? var.existing_cos_instance_crn : length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_crn : null
2121
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.")
22-
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)
23-
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
24-
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
22+
cos_instance_guid = var.existing_cos_instance_crn == null ? length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_guid : null : element(split(":", var.existing_cos_instance_crn), length(split(":", var.existing_cos_instance_crn)) - 3)
23+
archive_cos_bucket_name = var.existing_log_archive_cos_bucket_name != null ? var.existing_log_archive_cos_bucket_name : var.log_analysis_enable_archive ? module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].bucket_name : null
24+
archive_cos_bucket_endpoint = var.existing_log_archive_cos_bucket_endpoint != null ? var.existing_log_archive_cos_bucket_endpoint : var.log_analysis_enable_archive ? module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].s3_endpoint_private : null
2525
cos_kms_key_crn = (var.existing_log_archive_cos_bucket_name != null && var.existing_at_cos_target_bucket_name != null) ? null : var.existing_cos_kms_key_crn != null ? var.existing_cos_kms_key_crn : module.kms[0].keys[format("%s.%s", local.cos_key_ring_name, local.cos_key_name)].crn
2626

27-
cos_target_bucket_name = var.existing_at_cos_target_bucket_name != null ? var.existing_at_cos_target_bucket_name : module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].bucket_name
28-
cos_target_bucket_endpoint = var.existing_at_cos_target_bucket_endpoint != null ? var.existing_at_cos_target_bucket_endpoint : module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].s3_endpoint_private
27+
cos_target_bucket_name = var.existing_at_cos_target_bucket_name != null ? var.existing_at_cos_target_bucket_name : var.enable_at_event_routing_to_cos_bucket ? module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].bucket_name : null
28+
cos_target_bucket_endpoint = var.existing_at_cos_target_bucket_endpoint != null ? var.existing_at_cos_target_bucket_endpoint : var.enable_at_event_routing_to_cos_bucket ? module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].s3_endpoint_private : null
2929
cos_target_name = var.prefix != null ? "${var.prefix}-cos-target" : "cos-target"
3030
log_analysis_target_name = var.prefix != null ? "${var.prefix}-log-analysis-target" : "log-analysis-target"
3131
at_cos_route_name = var.prefix != null ? "${var.prefix}-at-cos-route" : "at-cos-route"
3232
at_log_analysis_route_name = var.prefix != null ? "${var.prefix}-at-log-analysis-route" : "at-log-analysis-route"
3333

34-
bucket_config_1 = var.existing_log_archive_cos_bucket_name == null && var.log_analysis_provision == true ? {
34+
bucket_config_1 = var.existing_log_archive_cos_bucket_name == null && var.log_analysis_provision == true && var.log_analysis_enable_archive == true ? {
3535
class = var.log_archive_cos_bucket_class
3636
name = local.log_archive_cos_bucket_name
3737
tag = var.archive_bucket_access_tags
@@ -43,11 +43,7 @@ locals {
4343
tag = var.at_cos_bucket_access_tags
4444
} : null
4545

46-
bucket_config_map = var.existing_log_archive_cos_bucket_name == null ? (
47-
var.existing_at_cos_target_bucket_name == null ? [local.bucket_config_1, local.bucket_config_2] : [local.bucket_config_1]
48-
) : (
49-
var.existing_at_cos_target_bucket_name == null ? [local.bucket_config_2] : null
50-
)
46+
bucket_config_map = local.bucket_config_1 != null && local.bucket_config_2 != null ? [local.bucket_config_1, local.bucket_config_2] : local.bucket_config_1 != null ? [local.bucket_config_1] : local.bucket_config_2 != null ? [local.bucket_config_2] : null
5147

5248
archive_rule = (var.existing_log_archive_cos_bucket_name == null || var.existing_at_cos_target_bucket_name == null) ? {
5349
enable = true
@@ -107,11 +103,10 @@ module "observability_instance" {
107103
logdna.at = logdna.at
108104
logdna.ld = logdna.ld
109105
}
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
106+
region = var.region
107+
resource_group_id = module.resource_group.resource_group_id
108+
log_analysis_enable_archive = var.log_analysis_enable_archive
109+
ibmcloud_api_key = local.archive_api_key
115110
# Log Analysis
116111
log_analysis_provision = var.log_analysis_provision
117112
log_analysis_instance_name = var.prefix != null ? "${var.prefix}-${var.log_analysis_instance_name}" : var.log_analysis_instance_name
@@ -227,7 +222,7 @@ module "cos_instance" {
227222
providers = {
228223
ibm = ibm.cos
229224
}
230-
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
225+
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
231226
source = "terraform-ibm-modules/cos/ibm//modules/fscloud"
232227
version = "8.6.2"
233228
resource_group_id = module.resource_group.resource_group_id
@@ -244,7 +239,7 @@ module "cos_bucket" {
244239
providers = {
245240
ibm = ibm.cos
246241
}
247-
count = (length(coalesce(local.bucket_config_map, [])) != 0) ? 1 : 0 # no need to call COS module if consumer is using existing COS bucket
242+
count = length(coalesce(local.bucket_config_map, [])) != 0 ? 1 : 0 # no need to call COS module if consumer is using existing COS bucket
248243
source = "terraform-ibm-modules/cos/ibm//modules/buckets"
249244
version = "8.6.2"
250245
bucket_configs = [

solutions/instances/outputs.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,32 +49,32 @@ output "cloud_monitoring_access_key" {
4949
## COS Instance
5050
output "cos_instance_id" {
5151
description = "COS instance id"
52-
value = var.existing_cos_instance_crn == null ? module.cos_instance[0].cos_instance_id : null
52+
value = var.existing_cos_instance_crn == null ? length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_id : null : null
5353
}
5454

5555
output "cos_instance_guid" {
5656
description = "COS instance guid"
57-
value = var.existing_cos_instance_crn == null ? module.cos_instance[0].cos_instance_guid : null
57+
value = var.existing_cos_instance_crn == null ? length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_guid : null : null
5858
}
5959

6060
output "cos_instance_name" {
6161
description = "COS instance name"
62-
value = var.existing_cos_instance_crn == null ? module.cos_instance[0].cos_instance_name : null
62+
value = var.existing_cos_instance_crn == null ? length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_name : null : null
6363
}
6464

6565
output "cos_instance_crn" {
6666
description = "COS instance crn"
67-
value = var.existing_cos_instance_crn == null ? module.cos_instance[0].cos_instance_crn : null
67+
value = var.existing_cos_instance_crn == null ? length(module.cos_instance) != 0 ? module.cos_instance[0].cos_instance_crn : null : null
6868
}
6969

7070
## COS Buckets
7171
output "log_archive_cos_bucket_name" {
72-
value = var.existing_log_archive_cos_bucket_name == null ? module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].bucket_name : var.existing_log_archive_cos_bucket_name
72+
value = var.existing_log_archive_cos_bucket_name == null ? var.log_analysis_enable_archive ? module.cos_bucket[0].buckets[local.log_archive_cos_bucket_name].bucket_name : null : var.existing_log_archive_cos_bucket_name
7373
description = "The name of log archive COS bucket"
7474
}
7575

7676
output "at_cos_target_bucket_name" {
77-
value = var.existing_at_cos_target_bucket_name == null ? module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].bucket_name : var.existing_at_cos_target_bucket_name
77+
value = var.existing_at_cos_target_bucket_name == null ? var.enable_at_event_routing_to_cos_bucket ? module.cos_bucket[0].buckets[local.at_cos_target_bucket_name].bucket_name : null : var.existing_at_cos_target_bucket_name
7878
description = "The name of the AT target COS bucket"
7979
}
8080

solutions/instances/variables.tf

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ variable "log_analysis_enable_archive" {
9292
default = true
9393
}
9494

95-
variable "activity_tracker_enable_archive" {
96-
type = bool
97-
description = "Whether to enable archiving on Activity Tracker."
98-
default = true
99-
}
100-
10195
variable "log_archive_api_key" {
10296
type = string
10397
description = "The API key to use to configure archiving from Log Analysis to Object Storage. If not specified, the API key value in ibmcloud_api_key is used."

0 commit comments

Comments
 (0)