|
| 1 | +# Copyright (c) 2023, Oracle and/or its affiliates. |
| 2 | +# Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 3 | + |
| 4 | +# If any of the existing stacks is the same stack compartment is already using the same "service_name" as the current stack, then fail the validation. |
| 5 | +# To pass the validation, recreate the stack with a different service name. |
| 6 | +# This is to prevent trying to create another compute instance using the same prefix, which will lead to DNS issues. |
| 7 | +# This validation is applicable only for resource manager stacks. Not applicable for terraform CLI mode. |
| 8 | + |
| 9 | +data "oci_resourcemanager_stacks" "all_stacks_in_the_compartment" { |
| 10 | + compartment_id = var.compartment_id |
| 11 | +} |
| 12 | + |
| 13 | +# collect id of each stack |
| 14 | +locals { |
| 15 | + stack_list = data.oci_resourcemanager_stacks.all_stacks_in_the_compartment.stacks |
| 16 | + num_stacks = length(local.stack_list) |
| 17 | + stack_ids = [for stack in local.stack_list : { id = stack.id }] |
| 18 | +} |
| 19 | + |
| 20 | +# get details of each stack from the list of stack_ids |
| 21 | +data "oci_resourcemanager_stack" "all_stacks" { |
| 22 | + count = local.num_stacks |
| 23 | + #Required |
| 24 | + stack_id = local.stack_ids[count.index].id |
| 25 | +} |
| 26 | + |
| 27 | +locals { |
| 28 | + stack_variables = [for stack in data.oci_resourcemanager_stack.all_stacks : { variables = stack.variables }] |
| 29 | + service_names_used_by_existing_stacks = [for stack_variables in local.stack_variables : lookup(stack_variables.variables, "service_name", "?_not_found_?")] |
| 30 | + duplicate_service_names_list = [for service_name in local.service_names_used_by_existing_stacks : service_name if service_name == var.service_name] |
| 31 | + # There will be always one entry for the name of the current stack. Set duplicate to true if there are more than one entries. |
| 32 | + service_name_already_exists = length(local.duplicate_service_names_list) > 1 ? true : false |
| 33 | + service_name_already_exists_msg = "WLSC-ERROR: Another stack with the service_name [${var.service_name}] already exisits in the stack compartment. Try again with a different service name." |
| 34 | + validate_service_name_is_not_already_used = local.service_name_already_exists ? local.validators_msg_map[local.service_name_already_exists_msg] : null |
| 35 | +} |
0 commit comments