Skip to content

Commit 282f63f

Browse files
authored
fix: fixed bug in validation when passing existing SM instance (#173)
1 parent 7c0c8b3 commit 282f63f

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

main.tf

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
locals {
77
# Validation (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
88
# tflint-ignore: terraform_unused_declarations
9-
validate_kms_values = (!var.kms_encryption_enabled && var.kms_key_crn != null) ? tobool("When passing values for var.kms_key_crn, you must set var.kms_encryption_enabled to true. Otherwise unset them to use default encryption") : (!var.kms_encryption_enabled && var.existing_kms_instance_guid != null) ? tobool("When passing values for var.existing_kms_instance_guid, you must set var.kms_encryption_enabled to true. Otherwise unset them to use default encryption") : true
9+
validate_kms_values = (!var.kms_encryption_enabled && var.kms_key_crn != null && var.existing_sm_instance_crn == null) ? tobool("When passing values for var.kms_key_crn, you must set 'kms_encryption_enabled' to true. Otherwise set 'kms_encryption_enabled' to false to use default encryption") : (!var.kms_encryption_enabled && var.existing_kms_instance_guid != null) ? tobool("When passing values for var.existing_kms_instance_guid, you must set var.kms_encryption_enabled to true. Otherwise unset them to use default encryption") : true
1010
# tflint-ignore: terraform_unused_declarations
11-
validate_kms_vars = var.kms_encryption_enabled && var.kms_key_crn == null ? tobool("When setting var.kms_encryption_enabled to true, a value must be passed for var.kms_key_crn") : true
11+
validate_kms_vars = var.kms_encryption_enabled && var.kms_key_crn == null && var.existing_sm_instance_crn == null ? tobool("When setting var.kms_encryption_enabled to true, a value must be passed for var.kms_key_crn") : true
1212
# tflint-ignore: terraform_unused_declarations
13-
validate_auth_policy = var.kms_encryption_enabled && var.skip_kms_iam_authorization_policy == false && var.existing_kms_instance_guid == null ? tobool("When var.skip_kms_iam_authorization_policy is set to false, and var.kms_encryption_enabled to true, a value must be passed for var.existing_kms_instance_guid in order to create the auth policy.") : true
13+
validate_auth_policy = var.kms_encryption_enabled && var.skip_kms_iam_authorization_policy == false && var.existing_kms_instance_guid == null && var.existing_sm_instance_crn == null ? tobool("When var.skip_kms_iam_authorization_policy is set to false, and var.kms_encryption_enabled to true, a value must be passed for var.existing_kms_instance_guid in order to create the auth policy.") : true
1414
# tflint-ignore: terraform_unused_declarations
1515
validate_event_notification = var.enable_event_notification && var.existing_en_instance_crn == null ? tobool("When setting var.enable_event_notification to true, a value must be passed for var.existing_en_instance_crn") : true
1616
# tflint-ignore: terraform_unused_declarations
17-
validate_endpoint = var.enable_event_notification && var.endpoint_type == "public" && var.allowed_network == "private-only" ? tobool("It is not allowed to have conflicting var.endpoint_type and var.allowed_network values.") : true
17+
validate_endpoint = var.enable_event_notification && var.endpoint_type == "public" && var.allowed_network == "private-only" && var.existing_sm_instance_crn == null ? tobool("It is not allowed to have conflicting var.endpoint_type and var.allowed_network values.") : true
1818
# tflint-ignore: terraform_unused_declarations
1919
validate_region = var.existing_sm_instance_crn == null && var.region == null ? tobool("When existing_sm_instance_crn is null, a value must be passed for var.region") : true
2020
}
@@ -62,7 +62,7 @@ locals {
6262
}
6363

6464
resource "ibm_iam_authorization_policy" "kms_policy" {
65-
count = var.kms_encryption_enabled && !var.skip_kms_iam_authorization_policy ? 1 : 0
65+
count = var.kms_encryption_enabled && !var.skip_kms_iam_authorization_policy && var.existing_sm_instance_crn == null ? 1 : 0
6666
source_service_name = "secrets-manager"
6767
source_resource_group_id = var.resource_group_id
6868
target_service_name = local.kms_service_name

tests/pr_test.go

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,38 @@ func TestRunExistingResourcesInstances(t *testing.T) {
180180
if existErr != nil {
181181
assert.True(t, existErr == nil, "Init and Apply of temp existing resource failed")
182182
} else {
183+
184+
// ------------------------------------------------------------------------------------
185+
// Test passing an existing SM, RG, EN
186+
// ------------------------------------------------------------------------------------
187+
183188
options := testhelper.TestOptionsDefault(&testhelper.TestOptions{
189+
Testing: t,
190+
TerraformDir: solutionsTerraformDir,
191+
// Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur
192+
ImplicitRequired: false,
193+
TerraformVars: map[string]interface{}{
194+
"ibmcloud_api_key": os.Getenv("TF_VAR_ibmcloud_api_key"),
195+
"region": region,
196+
"resource_group_name": terraform.Output(t, existingTerraformOptions, "resource_group_name"),
197+
"use_existing_resource_group": true,
198+
"existing_event_notification_instance_crn": terraform.Output(t, existingTerraformOptions, "event_notification_instance_crn"),
199+
"existing_secrets_manager_crn": terraform.Output(t, existingTerraformOptions, "secrets_manager_instance_crn"),
200+
"iam_engine_enabled": true,
201+
"private_engine_enabled": true,
202+
"existing_secrets_endpoint_type": "public",
203+
},
204+
})
205+
206+
output, err := options.RunTestConsistency()
207+
assert.Nil(t, err, "This should not have errored")
208+
assert.NotNil(t, output, "Expected some output")
209+
210+
// ------------------------------------------------------------------------------------
211+
// Test passing existing RG, EN, and KMS key
212+
// ------------------------------------------------------------------------------------
213+
214+
options2 := testhelper.TestOptionsDefault(&testhelper.TestOptions{
184215
Testing: t,
185216
TerraformDir: solutionsTerraformDir,
186217
// Do not hard fail the test if the implicit destroy steps fail to allow a full destroy of resource to occur
@@ -194,17 +225,16 @@ func TestRunExistingResourcesInstances(t *testing.T) {
194225
"existing_secrets_manager_kms_key_crn": terraform.Output(t, existingTerraformOptions, "secrets_manager_kms_key_crn"),
195226
"existing_kms_instance_crn": terraform.Output(t, existingTerraformOptions, "secrets_manager_kms_instance_crn"),
196227
"service_plan": "trial",
197-
"existing_secrets_manager_crn": terraform.Output(t, existingTerraformOptions, "secrets_manager_instance_crn"),
198228
"iam_engine_enabled": true,
199229
"private_engine_enabled": true,
200230
"existing_secrets_endpoint_type": "public",
201231
"allowed_network": "public-and-private",
202232
},
203233
})
204234

205-
output, err := options.RunTestConsistency()
235+
output2, err := options2.RunTestConsistency()
206236
assert.Nil(t, err, "This should not have errored")
207-
assert.NotNil(t, output, "Expected some output")
237+
assert.NotNil(t, output2, "Expected some output")
208238

209239
}
210240

0 commit comments

Comments
 (0)