Skip to content

Commit ec1d80d

Browse files
Khuzaima05Khuzaima-Shakeel
andauthored
feat: improved user experience for validating input variable values <br> - update required terraform to be >= 1.9.0 (#958)
* feat: updated code to use cross-object referencing for validations * resolve review comments * resolve review comments --------- Co-authored-by: Khuzaima-Shakeel <[email protected]>
1 parent 89214a9 commit ec1d80d

File tree

2 files changed

+70
-40
lines changed

2 files changed

+70
-40
lines changed

main.tf

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,6 @@
11
##############################################################################
22
# terraform-ibm-landing-zone-vpc
3-
##############################################################################
4-
locals {
5-
# input variable validation
6-
# tflint-ignore: terraform_unused_declarations
7-
validate_default_secgroup_rules = var.clean_default_sg_acl && (var.security_group_rules != null && length(var.security_group_rules) > 0) ? tobool("var.clean_default_sg_acl is true and var.security_group_rules are not empty, which are in direct conflict of each other. If you would like the default VPC Security Group to be empty, you must remove default rules from var.security_group_rules.") : true
8-
9-
# tflint-ignore: terraform_unused_declarations
10-
validate_existing_vpc_id = !var.create_vpc && var.existing_vpc_id == null ? tobool("If var.create_vpc is false, then provide a value for var.existing_vpc_id to create vpc.") : true
11-
12-
# tflint-ignore: terraform_unused_declarations
13-
validate_existing_subnet_id = !var.create_subnets && length(var.existing_subnets) == 0 ? tobool("If var.create_subnet is false, then provide a value for var.existing_subnets to create subnets.") : true
14-
# tflint-ignore: terraform_unused_declarations
15-
validate_existing_vpc_and_subnet = var.create_vpc == true && var.create_subnets == false ? tobool("If user is not providing a vpc then they should also not be providing a subnet") : true
16-
17-
# tflint-ignore: terraform_unused_declarations
18-
validate_hub_vpc_input = (var.hub_vpc_id != null && var.hub_vpc_crn != null) ? tobool("var.hub_vpc_id and var.hub_vpc_crn are mutually exclusive. Hence cannot have values at the same time.") : true
19-
20-
# tflint-ignore: terraform_unused_declarations
21-
validate_hub_vpc_id_input = (var.enable_hub_vpc_id && var.hub_vpc_id == null) ? tobool("var.hub_vpc_id must be passed when var.enable_hub_vpc_id is True.") : true
22-
23-
# tflint-ignore: terraform_unused_declarations
24-
validate_enable_hub_vpc_id_input = (!var.enable_hub_vpc_id && var.hub_vpc_id != null) ? tobool("var.enable_hub_vpc_id must be true when var.hub_vpc_id is not null.") : true
253

26-
# tflint-ignore: terraform_unused_declarations
27-
validate_hub_vpc_crn_input = (var.enable_hub_vpc_crn && var.hub_vpc_crn == null) ? tobool("var.hub_vpc_crn must be passed when var.enable_hub_vpc_crn is True.") : true
28-
29-
# tflint-ignore: terraform_unused_declarations
30-
validate_enable_hub_vpc_crn_input = (!var.enable_hub_vpc_crn && var.hub_vpc_crn != null) ? tobool("var.enable_hub_vpc_crn must be true when var.hub_vpc_crn is not null.") : true
31-
32-
# tflint-ignore: terraform_unused_declarations
33-
validate_manual_servers_input = (var.resolver_type == "manual" && length(var.manual_servers) == 0) ? tobool("var.manual_servers must be set when var.resolver_type is manual") : true
34-
35-
# tflint-ignore: terraform_unused_declarations
36-
validate_resolver_type_input = (var.resolver_type != null && var.update_delegated_resolver == true) ? tobool("var.resolver_type cannot be set if var.update_delegated_resolver is set to true. Only one type of resolver can be created by VPC.") : true
37-
38-
# tflint-ignore: terraform_unused_declarations
39-
validate_vpc_flow_logs_inputs = (var.enable_vpc_flow_logs) ? ((var.create_authorization_policy_vpc_to_cos) ? ((var.existing_cos_instance_guid != null && var.existing_storage_bucket_name != null) ? true : tobool("Please provide COS instance & bucket name to create flow logs collector.")) : ((var.existing_storage_bucket_name != null) ? true : tobool("Please provide COS bucket name to create flow logs collector"))) : false
40-
41-
# tflint-ignore: terraform_unused_declarations
42-
validate_skip_spoke_auth_policy_input = (var.hub_account_id == null && !var.skip_spoke_auth_policy && !var.enable_hub && (var.enable_hub_vpc_id || var.enable_hub_vpc_crn)) ? tobool("var.hub_account_id must be set when var.skip_spoke_auth_policy is False and either var.enable_hub_vpc_id or var.enable_hub_vpc_crn is true.") : true
43-
}
444

455
##############################################################################
466
# Check if existing vpc id is passed

variables.tf

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ variable "create_vpc" {
66
description = "Indicates whether user wants to use an existing vpc or create a new one. Set it to true to create a new vpc"
77
type = bool
88
default = true
9+
10+
validation {
11+
condition = !(var.create_vpc == false && var.existing_vpc_id == null)
12+
error_message = "You must either enable 'create_vpc' or provide 'existing_vpc_id', but not both or neither."
13+
}
14+
15+
validation {
16+
condition = !(var.create_vpc == false && var.create_subnets == true)
17+
error_message = "You cannot create subnets without creating a VPC. Hence if 'create_vpc' is false, then 'create_subnets' can not be true."
18+
}
919
}
1020

1121
variable "existing_vpc_id" {
@@ -393,6 +403,14 @@ variable "existing_subnets" {
393403
}))
394404
default = []
395405
nullable = false
406+
407+
validation {
408+
condition = (
409+
(var.create_subnets && length(var.existing_subnets) == 0) ||
410+
(!var.create_subnets && length(var.existing_subnets) > 0)
411+
)
412+
error_message = "You must either set 'create_subnets' to true and leave 'existing_subnets' empty, or set 'create_subnets' to false and provide a non-empty list for 'existing_subnets'."
413+
}
396414
}
397415

398416
##############################################################################
@@ -454,6 +472,11 @@ variable "security_group_rules" {
454472
])
455473
)) == 0
456474
}
475+
476+
validation {
477+
condition = !(var.clean_default_sg_acl && var.security_group_rules != null && length(var.security_group_rules) > 0)
478+
error_message = "var.clean_default_sg_acl is true and var.security_group_rules are not empty, which are in direct conflict. If you want to clean the default VPC Security Group, you must not pass security_group_rules."
479+
}
457480
}
458481

459482
variable "clean_default_sg_acl" {
@@ -501,6 +524,18 @@ variable "enable_vpc_flow_logs" {
501524
description = "Flag to enable vpc flow logs. If true, flow log collector will be created"
502525
type = bool
503526
default = false
527+
528+
validation {
529+
condition = (
530+
!var.enable_vpc_flow_logs ||
531+
(
532+
var.create_authorization_policy_vpc_to_cos
533+
? (var.existing_cos_instance_guid != null && var.existing_storage_bucket_name != null)
534+
: (var.existing_storage_bucket_name != null)
535+
)
536+
)
537+
error_message = "To enable VPC Flow Logs, provide COS Bucket name. If you're creating an authorization policy then also provide COS instance GUID."
538+
}
504539
}
505540

506541
variable "create_authorization_policy_vpc_to_cos" {
@@ -543,6 +578,16 @@ variable "skip_spoke_auth_policy" {
543578
description = "Set to true to skip the creation of an authorization policy between the DNS resolution spoke and hub, only enable this if a policy already exists between these two VPCs. See https://cloud.ibm.com/docs/vpc?topic=vpc-vpe-dns-sharing-s2s-auth&interface=ui for more details."
544579
type = bool
545580
default = false
581+
582+
validation {
583+
condition = (
584+
var.hub_account_id != null ||
585+
var.skip_spoke_auth_policy ||
586+
var.enable_hub ||
587+
!(var.enable_hub_vpc_id || var.enable_hub_vpc_crn)
588+
)
589+
error_message = "var.hub_account_id must be set when var.skip_spoke_auth_policy is false and either var.enable_hub_vpc_id or var.enable_hub_vpc_crn is true and enable_hub is false."
590+
}
546591
}
547592

548593
variable "hub_account_id" {
@@ -561,6 +606,16 @@ variable "hub_vpc_id" {
561606
description = "Indicates the id of the hub VPC for DNS resolution. See https://cloud.ibm.com/docs/vpc?topic=vpc-hub-spoke-model. Mutually exclusive with hub_vpc_crn."
562607
type = string
563608
default = null
609+
610+
validation {
611+
condition = !(var.hub_vpc_id != null && var.hub_vpc_crn != null)
612+
error_message = "The inputs 'hub_vpc_id' and 'hub_vpc_crn' are mutually exclusive. Only one of them can be set at a time."
613+
}
614+
615+
validation {
616+
condition = !(var.enable_hub_vpc_id && var.hub_vpc_id == null)
617+
error_message = "The input 'hub_vpc_id' must be provided when 'enable_hub_vpc_id' is set to true."
618+
}
564619
}
565620

566621
variable "enable_hub_vpc_crn" {
@@ -573,12 +628,22 @@ variable "hub_vpc_crn" {
573628
description = "Indicates the crn of the hub VPC for DNS resolution. See https://cloud.ibm.com/docs/vpc?topic=vpc-hub-spoke-model. Mutually exclusive with hub_vpc_id."
574629
type = string
575630
default = null
631+
632+
validation {
633+
condition = !(var.enable_hub_vpc_crn && var.hub_vpc_crn == null)
634+
error_message = "The input 'hub_vpc_crn' must be provided when 'enable_hub_vpc_crn' is set to true."
635+
}
576636
}
577637

578638
variable "update_delegated_resolver" {
579639
description = "If set to true, and if the vpc is configured to be a spoke for DNS resolution (enable_hub_vpc_crn or enable_hub_vpc_id set), then the spoke VPC resolver will be updated to a delegated resolver."
580640
type = bool
581641
default = false
642+
643+
validation {
644+
condition = !(var.update_delegated_resolver == true && var.resolver_type != null)
645+
error_message = "var.resolver_type cannot be set if var.update_delegated_resolver is true. Only one type of resolver can be created by VPC."
646+
}
582647
}
583648

584649
variable "skip_custom_resolver_hub_creation" {
@@ -620,6 +685,11 @@ variable "manual_servers" {
620685
zone_affinity = optional(string)
621686
}))
622687
default = []
688+
689+
validation {
690+
condition = !(var.resolver_type == "manual" && length(var.manual_servers) == 0)
691+
error_message = "The input 'manual_servers' must be set when 'resolver_type' is 'manual'."
692+
}
623693
}
624694

625695
variable "dns_location" {

0 commit comments

Comments
 (0)