Skip to content

Commit 9d01af7

Browse files
feat: improved user experience for validating input variable values <br>* updated required terraform to be >= 1.9.0 (#460)
1 parent f59ae9b commit 9d01af7

File tree

12 files changed

+122
-42
lines changed

12 files changed

+122
-42
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ You need the following permissions to run this module.
5959

6060
| Name | Version |
6161
|------|---------|
62-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
62+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
6363
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.70.0, <2.0.0 |
6464
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 3.2.1, < 4.0.0 |
6565
| <a name="requirement_time"></a> [time](#requirement\_time) | >= 0.9.1 |

examples/backup-restore/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
required_providers {
44
# Ensure that there is always 1 example locked into the lowest provider version of the range defined in the main
55
# module's version.tf (basic example), and 1 example that will always use the latest provider version (complete example).

examples/basic/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
required_providers {
44
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
55
ibm = {

examples/complete/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
# Pin to the lowest provider version of the range defined in the main module's version.tf to ensure lowest version still works
44
required_providers {
55
ibm = {

examples/fscloud/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
required_providers {
44
# Use latest version of provider in non-basic examples to verify latest version works with module
55
ibm = {

main.tf

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@
55
# TODO: Replace with terraform cross variable validation: https://github.ibm.com/GoldenEye/issues/issues/10836
66
########################################################################################################################
77

8-
locals {
9-
# tflint-ignore: terraform_unused_declarations
10-
validate_kms_values = var.use_ibm_owned_encryption_key && (var.kms_key_crn != null || var.backup_encryption_key_crn != null) ? tobool("When passing values for 'kms_key_crn' or 'backup_encryption_key_crn', you must set 'use_ibm_owned_encryption_key' to false. Otherwise unset them to use default encryption.") : true
11-
# tflint-ignore: terraform_unused_declarations
12-
validate_kms_vars = !var.use_ibm_owned_encryption_key && var.kms_key_crn == null ? tobool("When setting 'use_ibm_owned_encryption_key' to false, a value must be passed for 'kms_key_crn'.") : true
13-
# tflint-ignore: terraform_unused_declarations
14-
validate_backup_key = !var.use_ibm_owned_encryption_key && var.backup_encryption_key_crn != null && (var.use_default_backup_encryption_key || var.use_same_kms_key_for_backups) ? tobool("When passing a value for 'backup_encryption_key_crn' you cannot set 'use_default_backup_encryption_key' to true or 'use_ibm_owned_encryption_key' to false.") : true
15-
# tflint-ignore: terraform_unused_declarations
16-
validate_backup_key_2 = !var.use_ibm_owned_encryption_key && var.backup_encryption_key_crn == null && !var.use_same_kms_key_for_backups ? tobool("When 'use_same_kms_key_for_backups' is set to false, a value needs to be passed for 'backup_encryption_key_crn'.") : true
17-
# tflint-ignore: terraform_unused_declarations
18-
validate_plan = var.enable_elser_model && var.plan != "platinum" ? tobool("When 'enable_elser_model' is set to true, the 'plan' must be set to 'platinum' in order to enable ELSER model.") : true
19-
# tflint-ignore: terraform_unused_declarations
20-
validate_es_user = var.enable_elser_model && !((length(var.service_credential_names) > 0 && length([for k, v in var.service_credential_names : k if v == "Administrator"]) > 0) || var.admin_pass != null) ? tobool("When 'enable_elser_model' is set to true, an Administrator role user must be created using the 'service_credential_names' input, or by passing a value for the 'admin_pass' input.") : true
21-
}
22-
238
########################################################################################################################
249
# Locals
2510
########################################################################################################################

modules/fscloud/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The IBM Cloud Framework for Financial Services mandates the application of an in
1313

1414
| Name | Version |
1515
|------|---------|
16-
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.3.0 |
16+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.9.0 |
1717
| <a name="requirement_ibm"></a> [ibm](#requirement\_ibm) | >= 1.70.0, <2.0.0 |
1818

1919
### Modules

modules/fscloud/version.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
terraform {
2-
required_version = ">= 1.3.0"
2+
required_version = ">= 1.9.0"
33
required_providers {
44
# The below tflint-ignore is required because although the below provider is not directly required by this submodule,
55
# it is required by consuming modules, and if not set here, the top level module calling this module will not be

solutions/standard/main.tf

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ module "resource_group" {
1616
# TODO: Replace with terraform cross variable validation: https://github.ibm.com/GoldenEye/issues/issues/10836
1717
#######################################################################################################################
1818

19-
locals {
20-
# tflint-ignore: terraform_unused_declarations
21-
validate_kms_1 = var.existing_elasticsearch_instance_crn != null ? true : var.use_ibm_owned_encryption_key && (var.existing_kms_instance_crn != null || var.existing_kms_key_crn != null || var.existing_backup_kms_key_crn != null) ? tobool("When setting values for 'existing_kms_instance_crn', 'existing_kms_key_crn' or 'existing_backup_kms_key_crn', the 'use_ibm_owned_encryption_key' input must be set to false.") : true
22-
# tflint-ignore: terraform_unused_declarations
23-
validate_kms_2 = var.existing_elasticsearch_instance_crn != null ? true : !var.use_ibm_owned_encryption_key && (var.existing_kms_instance_crn == null && var.existing_kms_key_crn == null) ? tobool("When 'use_ibm_owned_encryption_key' is false, a value is required for either 'existing_kms_instance_crn' (to create a new key), or 'existing_kms_key_crn' to use an existing key.") : true
24-
}
25-
2619
#######################################################################################################################
2720
# KMS encryption key
2821
#######################################################################################################################
@@ -254,10 +247,6 @@ module "es_instance_crn_parser" {
254247
locals {
255248
existing_elasticsearch_guid = var.existing_elasticsearch_instance_crn != null ? module.es_instance_crn_parser[0].service_instance : null
256249
existing_elasticsearch_region = var.existing_elasticsearch_instance_crn != null ? module.es_instance_crn_parser[0].region : null
257-
258-
# Validate the region input matches region detected in existing instance CRN (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
259-
# tflint-ignore: terraform_unused_declarations
260-
validate_existing_instance_region = var.existing_elasticsearch_instance_crn != null && var.region != local.existing_elasticsearch_region ? tobool("The region detected in the 'existing_elasticsearch_instance_crn' value must match the value of the 'region' input variable when passing an existing instance.") : true
261250
}
262251

263252
# Do a data lookup on the resource GUID to get more info that is needed for the 'ibm_database' data lookup below
@@ -333,14 +322,6 @@ locals {
333322
#######################################################################################################################
334323

335324
locals {
336-
## Variable validation (approach based on https://github.com/hashicorp/terraform/issues/25609#issuecomment-1057614400)
337-
# tflint-ignore: terraform_unused_declarations
338-
validate_sm_crn = length(local.service_credential_secrets) > 0 && var.existing_secrets_manager_instance_crn == null ? tobool("`existing_secrets_manager_instance_crn` is required when adding service credentials to a secrets manager secret.") : false
339-
# tflint-ignore: terraform_unused_declarations
340-
validate_sm_sg = var.existing_secrets_manager_instance_crn != null && var.admin_pass_secrets_manager_secret_group == null ? tobool("`admin_pass_secrets_manager_secret_group` is required when `existing_secrets_manager_instance_crn` is set.") : false
341-
# tflint-ignore: terraform_unused_declarations
342-
validate_sm_sn = var.existing_secrets_manager_instance_crn != null && var.admin_pass_secrets_manager_secret_name == null ? tobool("`admin_pass_secrets_manager_secret_name` is required when `existing_secrets_manager_instance_crn` is set.") : false
343-
344325
create_sm_auth_policy = var.skip_elasticsearch_to_secrets_manager_auth_policy || var.existing_secrets_manager_instance_crn == null ? 0 : 1
345326
}
346327

solutions/standard/variables.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,25 @@ variable "backup_crn" {
5656
type = string
5757
description = "The CRN of a backup resource to restore from. The backup is created by a database deployment with the same service ID. The backup is loaded after provisioning and the new deployment starts up that uses that data. A backup CRN is in the format crn:v1:<…>:backup:. If omitted, the database is provisioned empty."
5858
default = null
59+
60+
validation {
61+
condition = anytrue([
62+
var.backup_crn == null,
63+
can(regex("^crn:.*:backup:", var.backup_crn))
64+
])
65+
error_message = "backup_crn must be null OR starts with 'crn:' and contains ':backup:'"
66+
}
5967
}
6068

6169
variable "region" {
6270
type = string
6371
description = "The region where you want to deploy your instance, or the region in which your existing instance is in."
6472
default = "us-south"
73+
74+
validation {
75+
condition = var.existing_elasticsearch_instance_crn != null && var.region != local.existing_elasticsearch_region ? false : true
76+
error_message = "The region detected in the 'existing_elasticsearch_instance_crn' value must match the value of the 'region' input variable when passing an existing instance."
77+
}
6578
}
6679

6780
variable "plan" {
@@ -209,6 +222,30 @@ variable "use_ibm_owned_encryption_key" {
209222
type = bool
210223
description = "IBM Cloud Databases will secure your deployment's data at rest automatically with an encryption key that IBM hold. Alternatively, you may select your own Key Management System instance and encryption key (Key Protect or Hyper Protect Crypto Services) by setting this to false. If setting to false, a value must be passed for `existing_kms_instance_crn` to create a new key, or `existing_kms_key_crn` and/or `existing_backup_kms_key_crn` to use an existing key."
211224
default = false
225+
226+
# this validation ensures IBM-owned key is not used when KMS details are provided
227+
validation {
228+
condition = (
229+
var.existing_elasticsearch_instance_crn != null ||
230+
!(var.use_ibm_owned_encryption_key && (
231+
var.existing_kms_instance_crn != null ||
232+
var.existing_kms_key_crn != null ||
233+
var.existing_backup_kms_key_crn != null
234+
))
235+
)
236+
error_message = "When setting values for 'existing_kms_instance_crn', 'existing_kms_key_crn' or 'existing_backup_kms_key_crn', the 'use_ibm_owned_encryption_key' input must be set to false."
237+
}
238+
239+
# this validation ensures key info is provided when IBM-owned key is disabled and no Elasticsearch instance is given
240+
validation {
241+
condition = !(
242+
var.existing_elasticsearch_instance_crn == null &&
243+
var.use_ibm_owned_encryption_key == false &&
244+
var.existing_kms_instance_crn == null &&
245+
var.existing_kms_key_crn == null
246+
)
247+
error_message = "When 'use_ibm_owned_encryption_key' is false, you must provide either 'existing_kms_instance_crn' (to create a new key) or 'existing_kms_key_crn' (to use an existing key)."
248+
}
212249
}
213250

214251
variable "existing_kms_instance_crn" {
@@ -320,6 +357,14 @@ variable "service_credential_secrets" {
320357
])
321358
error_message = "service_credentials_source_service_role_crn must be a serviceRole CRN. See https://cloud.ibm.com/iam/roles"
322359
}
360+
361+
validation {
362+
condition = (
363+
length(var.service_credential_secrets) == 0 ||
364+
var.existing_secrets_manager_instance_crn != null
365+
)
366+
error_message = "`existing_secrets_manager_instance_crn` is required when adding service credentials to a secrets manager secret."
367+
}
323368
}
324369

325370
variable "skip_elasticsearch_to_secrets_manager_auth_policy" {
@@ -332,6 +377,14 @@ variable "admin_pass_secrets_manager_secret_group" {
332377
type = string
333378
description = "The name of a new or existing secrets manager secret group for admin password. To use existing secret group, `use_existing_admin_pass_secrets_manager_secret_group` must be set to `true`. If a prefix input variable is specified, the prefix is added to the name in the `<prefix>-<name>` format."
334379
default = "elasticsearch-secrets"
380+
381+
validation {
382+
condition = (
383+
var.existing_secrets_manager_instance_crn == null ||
384+
var.admin_pass_secrets_manager_secret_group != null
385+
)
386+
error_message = "`admin_pass_secrets_manager_secret_group` is required when `existing_secrets_manager_instance_crn` is set."
387+
}
335388
}
336389

337390
variable "use_existing_admin_pass_secrets_manager_secret_group" {
@@ -344,6 +397,14 @@ variable "admin_pass_secrets_manager_secret_name" {
344397
type = string
345398
description = "The name of a new elasticsearch administrator secret. If a prefix input variable is specified, the prefix is added to the name in the `<prefix>-<name>` format."
346399
default = "elasticsearch-admin-password"
400+
401+
validation {
402+
condition = (
403+
var.existing_secrets_manager_instance_crn == null ||
404+
var.admin_pass_secrets_manager_secret_name != null
405+
)
406+
error_message = "`admin_pass_secrets_manager_secret_name` is required when `existing_secrets_manager_instance_crn` is set."
407+
}
347408
}
348409

349410
##############################################################

0 commit comments

Comments
 (0)