Skip to content

Commit 23c2542

Browse files
authored
feat: added the ability to optionally add a prefix to all resources created by the DA using the new prefix input variable (#99)
1 parent 4593f02 commit 23c2542

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

solutions/standard/main.tf

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,17 @@
55
module "resource_group" {
66
source = "terraform-ibm-modules/resource-group/ibm"
77
version = "1.1.4"
8-
resource_group_name = var.existing_resource_group == false ? var.resource_group_name : null
8+
resource_group_name = var.existing_resource_group == false ? (var.prefix != null ? "${var.prefix}-${var.resource_group_name}" : var.resource_group_name) : null
99
existing_resource_group_name = var.existing_resource_group == true ? var.resource_group_name : null
1010
}
1111

1212
#######################################################################################################################
1313
# KMS Key
1414
#######################################################################################################################
1515
locals {
16-
kms_key_crn = var.existing_sm_kms_key_crn != null ? var.existing_sm_kms_key_crn : module.kms[0].keys[format("%s.%s", var.kms_key_ring_name, var.kms_key_name)].crn
16+
kms_key_crn = var.existing_sm_kms_key_crn != null ? var.existing_sm_kms_key_crn : module.kms[0].keys[format("%s.%s", local.kms_key_ring_name, local.kms_key_name)].crn
17+
kms_key_ring_name = var.prefix != null ? "${var.prefix}-${var.kms_key_ring_name}" : var.kms_key_ring_name
18+
kms_key_name = var.prefix != null ? "${var.prefix}-${var.kms_key_name}" : var.kms_key_name
1719
}
1820
# KMS root key for Secrets Manager secret encryption
1921
module "kms" {
@@ -30,12 +32,12 @@ module "kms" {
3032
key_endpoint_type = var.kms_endpoint_type
3133
keys = [
3234
{
33-
key_ring_name = var.kms_key_ring_name
35+
key_ring_name = local.kms_key_ring_name
3436
existing_key_ring = false
3537
force_delete_key_ring = true
3638
keys = [
3739
{
38-
key_name = var.kms_key_name
40+
key_name = local.kms_key_name
3941
standard_key = false
4042
rotation_interval_month = 3
4143
dual_auth_delete_enabled = false
@@ -54,7 +56,7 @@ module "secrets_manager" {
5456
source = "../.."
5557
resource_group_id = module.resource_group.resource_group_id
5658
region = var.region
57-
secrets_manager_name = var.secrets_manager_instance_name
59+
secrets_manager_name = var.prefix != null ? "${var.prefix}-${var.secrets_manager_instance_name}" : var.secrets_manager_instance_name
5860
sm_service_plan = var.service_plan
5961
allowed_network = var.allowed_network
6062
sm_tags = var.secret_manager_tags
@@ -76,7 +78,7 @@ module "iam_secrets_engine" {
7678
source = "terraform-ibm-modules/secrets-manager-iam-engine/ibm"
7779
version = "1.1.0"
7880
region = var.region
79-
iam_engine_name = var.iam_engine_name
81+
iam_engine_name = var.prefix != null ? "${var.prefix}-${var.iam_engine_name}" : var.iam_engine_name
8082
secrets_manager_guid = module.secrets_manager.secrets_manager_guid
8183
endpoint_type = var.allowed_network == "private-only" ? "private" : "public"
8284
}

solutions/standard/variables.tf

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ variable "existing_resource_group" {
1616

1717
variable "resource_group_name" {
1818
type = string
19-
description = "The name of a new or an existing resource group in which to provision Secrets Manager resources to."
19+
description = "The name of a new or an existing resource group in which to provision Secrets Manager resources to. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
2020
}
2121

2222
variable "region" {
@@ -25,13 +25,19 @@ variable "region" {
2525
default = "us-south"
2626
}
2727

28+
variable "prefix" {
29+
type = string
30+
description = "(Optional) Prefix to append to all resources created by this solution."
31+
default = null
32+
}
33+
2834
########################################################################################################################
2935
# Secrets Manager
3036
########################################################################################################################
3137

3238
variable "secrets_manager_instance_name" {
3339
type = string
34-
description = "The name to give the Secrets Manager instance that will be provisioned by this solution."
40+
description = "The name to give the Secrets Manager instance that will be provisioned by this solution. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
3541
default = "base-security-services-sm"
3642
}
3743

@@ -69,7 +75,7 @@ variable "iam_engine_enabled" {
6975

7076
variable "iam_engine_name" {
7177
type = string
72-
description = "The name of the IAM Engine used to configure an IBM Secrets Manager IAM credentials engine."
78+
description = "The name of the IAM Engine used to configure an IBM Secrets Manager IAM credentials engine. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
7379
default = "base-sm-iam-engine"
7480
}
7581

@@ -118,13 +124,13 @@ variable "kms_endpoint_type" {
118124
variable "kms_key_ring_name" {
119125
type = string
120126
default = "sm-cos-key-ring"
121-
description = "The name to give to the new KMS key ring that will be used to store the KMS key to enable advanced, customer-managed encryption for your Secrets Manager secrets. Only required if not supplying an existing KMS root key CRN."
127+
description = "The name to give to the new KMS key ring that will be used to store the KMS key to enable advanced, customer-managed encryption for your Secrets Manager secrets. Only required if not supplying an existing KMS root key CRN. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
122128
}
123129

124130
variable "kms_key_name" {
125131
type = string
126132
default = "sm-cos-key"
127-
description = "The name to give to the new KMS root key that will be used to enable advanced, customer-managed encryption for your Secrets Manager secrets. Only required if not supplying an existing KMS root key CRN."
133+
description = "The name to give to the new KMS root key that will be used to enable advanced, customer-managed encryption for your Secrets Manager secrets. Only required if not supplying an existing KMS root key CRN. If prefix input variable is passed then it will get prefixed infront of the value in the format of '<prefix>-value'"
128134
}
129135

130136
########################################################################################################################

tests/pr_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ func TestRunDASolutionSchematics(t *testing.T) {
128128

129129
options.TerraformVars = []testschematic.TestSchematicTerraformVar{
130130
{Name: "ibmcloud_api_key", Value: options.RequiredEnvironmentVars["TF_VAR_ibmcloud_api_key"], DataType: "string", Secure: true},
131+
{Name: "prefix", Value: options.Prefix, DataType: "string"},
131132
{Name: "region", Value: options.Region, DataType: "string"},
132133
{Name: "resource_group_name", Value: options.Prefix, DataType: "string"},
133134
{Name: "service_plan", Value: "trial", DataType: "string"},

0 commit comments

Comments
 (0)