|
| 1 | +################################################### |
| 2 | +# Copyright (C) IBM Corp. 2023 All Rights Reserved. |
| 3 | +# Licensed under the Apache License v2.0 |
| 4 | +################################################### |
| 5 | + |
| 6 | +# This file contains the complete information on all the validations performed from the code during the generate plan process |
| 7 | +# Validations are performed to make sure, the appropriate error messages are displayed to user in-order to provide required input parameter |
| 8 | + |
| 9 | +locals { |
| 10 | + # validation for the boot volume encryption toggling. |
| 11 | + validate_enable_customer_managed_encryption = anytrue([alltrue([var.kms_key_name != null, var.kms_instance_name != null]), (var.kms_key_name == null), (var.key_management != "key_protect")]) |
| 12 | + validate_enable_customer_managed_encryption_msg = "Please make sure you are passing the kms_instance_name if you are passing kms_key_name." |
| 13 | + # tflint-ignore: terraform_unused_declarations |
| 14 | + validate_enable_customer_managed_encryption_chk = regex( |
| 15 | + "^${local.validate_enable_customer_managed_encryption_msg}$", |
| 16 | + (local.validate_enable_customer_managed_encryption ? local.validate_enable_customer_managed_encryption_msg : "")) |
| 17 | + |
| 18 | + # validation for the boot volume encryption toggling. |
| 19 | + validate_null_customer_managed_encryption = anytrue([alltrue([var.kms_instance_name == null, var.key_management != "key_protect"]), (var.key_management == "key_protect")]) |
| 20 | + validate_null_customer_managed_encryption_msg = "Please make sure you are setting key_management as key_protect if you are passing kms_instance_name, kms_key_name." |
| 21 | + # tflint-ignore: terraform_unused_declarations |
| 22 | + validate_null_customer_managed_encryption_chk = regex( |
| 23 | + "^${local.validate_null_customer_managed_encryption_msg}$", |
| 24 | + (local.validate_null_customer_managed_encryption ? local.validate_null_customer_managed_encryption_msg : "")) |
| 25 | + |
| 26 | + # Validate existing packer subnet should be the subset of vpc_name entered |
| 27 | + validate_subnet_id_vpc_msg = "Provided packer subnet should be within the vpc entered." |
| 28 | + validate_subnet_id_vpc = anytrue([var.subnet_id == null, var.subnet_id != null && var.vpc_name != null ? alltrue([for subnet_id in [var.subnet_id] : contains(data.ibm_is_vpc.existing_vpc[0].subnets[*].id, subnet_id)]) : false]) |
| 29 | + # tflint-ignore: terraform_unused_declarations |
| 30 | + validate_subnet_id_vpc_chk = regex("^${local.validate_subnet_id_vpc_msg}$", |
| 31 | + (local.validate_subnet_id_vpc ? local.validate_subnet_id_vpc_msg : "")) |
| 32 | + |
| 33 | + # Validate existing packer subnet should be in the appropriate zone. |
| 34 | + validate_subnet_id_zone_msg = "Provided packer subnet should be in appropriate zone." |
| 35 | + validate_subnet_id_zone = anytrue([var.subnet_id == null, var.subnet_id != null && var.vpc_name != null ? alltrue([data.ibm_is_subnet.existing_subnet[0].zone == var.zones[0]]) : false]) |
| 36 | + # tflint-ignore: terraform_unused_declarations |
| 37 | + validate_subnet_id_zone_chk = regex("^${local.validate_subnet_id_zone_msg}$", |
| 38 | + (local.validate_subnet_id_zone ? local.validate_subnet_id_zone_msg : "")) |
| 39 | + |
| 40 | + # Validate existing packer subnet public gateways |
| 41 | + validate_subnet_name_pg_msg = "Provided existing packer subnet should have public gateway attached." |
| 42 | + validate_subnet_name_pg = anytrue([var.subnet_id == null, var.subnet_id != null && var.vpc_name != null ? (data.ibm_is_subnet.existing_subnet[0].public_gateway != "") : false]) |
| 43 | + # tflint-ignore: terraform_unused_declarations |
| 44 | + validate_subnet_name_pg_chk = regex("^${local.validate_subnet_name_pg_msg}$", |
| 45 | + (local.validate_subnet_name_pg ? local.validate_subnet_name_pg_msg : "")) |
| 46 | + |
| 47 | + # Validate existing vpc public gateways |
| 48 | + validate_existing_vpc_pgw_msg = "Provided existing vpc should have the public gateways created in the provided zones." |
| 49 | + validate_existing_vpc_pgw = anytrue([(var.vpc_name == null), alltrue([var.vpc_name != null, var.subnet_id != null]), alltrue([var.vpc_name != null, var.subnet_id == null, length(local.zone_1_pgw_ids) > 0])]) |
| 50 | + # tflint-ignore: terraform_unused_declarations |
| 51 | + validate_existing_vpc_pgw_chk = regex("^${local.validate_existing_vpc_pgw_msg}$", |
| 52 | + (local.validate_existing_vpc_pgw ? local.validate_existing_vpc_pgw_msg : "")) |
| 53 | + |
| 54 | + # Validate the subnet_id user input value |
| 55 | + validate_subnet_id_msg = "If the packer subnet_id is provided, the user should also provide the vpc_name." |
| 56 | + validate_subnet_id = anytrue([var.vpc_name != null && var.subnet_id != null, var.subnet_id == null]) |
| 57 | + # tflint-ignore: terraform_unused_declarations |
| 58 | + validate_subnet_id_chk = regex("^${local.validate_subnet_id_msg}$", |
| 59 | + (local.validate_subnet_id ? local.validate_subnet_id_msg : "")) |
| 60 | + |
| 61 | + # Validate security_group_id user input value |
| 62 | + validate_security_group_id_msg = "If existing security_group_id is provided, the user should also specify vpc_name that has that security group ID." |
| 63 | + validate_security_group_id = anytrue([var.vpc_name != null && var.security_group_id != "", var.security_group_id == ""]) |
| 64 | + # tflint-ignore: terraform_unused_declarations |
| 65 | + validate_security_group_id_chk = regex("^${local.validate_security_group_id_msg}$", |
| 66 | + (local.validate_security_group_id ? local.validate_security_group_id_msg : "")) |
| 67 | + |
| 68 | +} |
0 commit comments