Skip to content

Commit e99420d

Browse files
fix: added extra KMS validation logic (#223)
1 parent 43d1837 commit e99420d

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

main.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ locals {
6262
# tflint-ignore: terraform_unused_declarations
6363
validate_encryption_inputs = var.enable_cos_kms_encryption && (var.cos_kms_crn == null || var.cos_kms_crn == "") ? tobool("A value must be passed for 'cos_kms_crn' when 'enable_cos_kms_encryption' is set to true") : true
6464

65+
# tflint-ignore: terraform_unused_declarations
66+
validate_enable_cos_kms_encryption = (var.cos_kms_crn != null || var.cos_kms_key_crn != null) && var.enable_cos_kms_encryption == false ? tobool("If a value for 'cos_kms_crn' or 'cos_kms_key_crn' is passed then 'enable_cos_kms_encryption' must be set to true") : true
6567
}
6668

6769
data "ibm_iam_auth_token" "restapi" {

tests/pr_test.go

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,12 @@ func TestWithExistingKP(t *testing.T) {
152152
},
153153
},
154154
TerraformVars: map[string]interface{}{
155-
"location": validRegions[rand.Intn(len(validRegions))],
156-
"resource_group_name": prefix,
157-
"provider_visibility": "public",
158-
"cos_kms_crn": terraform.Output(t, existingTerraformOptions, "key_protect_crn"),
159-
"cos_kms_key_crn": terraform.Output(t, existingTerraformOptions, "kms_key_crn"),
155+
"location": validRegions[rand.Intn(len(validRegions))],
156+
"resource_group_name": prefix,
157+
"provider_visibility": "public",
158+
"enable_cos_kms_encryption": true,
159+
"cos_kms_crn": terraform.Output(t, existingTerraformOptions, "key_protect_crn"),
160+
"cos_kms_key_crn": terraform.Output(t, existingTerraformOptions, "kms_key_crn"),
160161
},
161162
})
162163

@@ -178,3 +179,89 @@ func TestWithExistingKP(t *testing.T) {
178179
}
179180

180181
}
182+
183+
func TestRunUpgradeExistingKP(t *testing.T) {
184+
t.Parallel()
185+
186+
// ------------------------------------------------------------------------------------
187+
// Provision KP first
188+
// ------------------------------------------------------------------------------------
189+
190+
prefix := fmt.Sprintf("kp-ut-%s", strings.ToLower(random.UniqueId()))
191+
realTerraformDir := "./resources/kp-instance"
192+
tempTerraformDir, _ := files.CopyTerraformFolderToTemp(realTerraformDir, fmt.Sprintf(prefix+"-%s", strings.ToLower(random.UniqueId())))
193+
region := "us-south"
194+
195+
// Verify ibmcloud_api_key variable is set
196+
checkVariable := "TF_VAR_ibmcloud_api_key"
197+
val, present := os.LookupEnv(checkVariable)
198+
require.True(t, present, checkVariable+" environment variable not set")
199+
require.NotEqual(t, "", val, checkVariable+" environment variable is empty")
200+
201+
logger.Log(t, "Tempdir: ", tempTerraformDir)
202+
existingTerraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
203+
TerraformDir: tempTerraformDir,
204+
Vars: map[string]interface{}{
205+
"prefix": prefix,
206+
"region": region,
207+
},
208+
// Set Upgrade to true to ensure latest version of providers and modules are used by terratest.
209+
// This is the same as setting the -upgrade=true flag with terraform.
210+
Upgrade: true,
211+
})
212+
213+
terraform.WorkspaceSelectOrNew(t, existingTerraformOptions, prefix)
214+
_, existErr := terraform.InitAndApplyE(t, existingTerraformOptions)
215+
if existErr != nil {
216+
assert.True(t, existErr == nil, "Init and Apply of temp existing resource failed")
217+
} else {
218+
219+
// ------------------------------------------------------------------------------------
220+
// Upgrade test for watsonx DA passing in existing KP details
221+
// ------------------------------------------------------------------------------------
222+
223+
options := testhelper.TestOptionsDefault(&testhelper.TestOptions{
224+
Testing: t,
225+
TerraformDir: rootDaDir,
226+
Prefix: "existing-kp-upg",
227+
IgnoreDestroys: testhelper.Exemptions{ // Ignore for consistency check
228+
List: []string{
229+
"module.configure_user.null_resource.configure_user",
230+
"module.configure_user.null_resource.restrict_access",
231+
},
232+
},
233+
IgnoreUpdates: testhelper.Exemptions{ // Ignore for consistency check
234+
List: []string{
235+
"module.configure_user.null_resource.configure_user",
236+
"module.configure_user.null_resource.restrict_access",
237+
},
238+
},
239+
TerraformVars: map[string]interface{}{
240+
"location": validRegions[rand.Intn(len(validRegions))],
241+
"resource_group_name": prefix,
242+
"provider_visibility": "public",
243+
"enable_cos_kms_encryption": true,
244+
"cos_kms_crn": terraform.Output(t, existingTerraformOptions, "key_protect_crn"),
245+
},
246+
})
247+
248+
output, err := options.RunTestUpgrade()
249+
if !options.UpgradeTestSkipped {
250+
assert.Nil(t, err, "This should not have errored")
251+
assert.NotNil(t, output, "Expected some output")
252+
}
253+
}
254+
255+
// Check if "DO_NOT_DESTROY_ON_FAILURE" is set
256+
envVal, _ := os.LookupEnv("DO_NOT_DESTROY_ON_FAILURE")
257+
// Destroy the temporary existing resources if required
258+
if t.Failed() && strings.ToLower(envVal) == "true" {
259+
fmt.Println("Terratest failed. Debug the test and delete resources manually.")
260+
} else {
261+
logger.Log(t, "START: Destroy (existing resources)")
262+
terraform.Destroy(t, existingTerraformOptions)
263+
terraform.WorkspaceDelete(t, existingTerraformOptions, prefix)
264+
logger.Log(t, "END: Destroy (existing resources)")
265+
}
266+
267+
}

0 commit comments

Comments
 (0)