|
1 | 1 | # Copyright (c) 2023, Oracle and/or its affiliates. |
2 | 2 | # Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl. |
| 3 | +# This validation is applicable only for resource manager stacks. Not applicable for terraform CLI mode. |
3 | 4 |
|
4 | | -# The validation fails if any running/stopped compute instances with the display name "<service_name>-wls-0" are present in the stack compartment. |
5 | | - |
6 | | -data "oci_core_instances" "wls_running_instances_in_stack_compartment" { |
| 5 | +# list all stacks in any state (active, deleted etc) |
| 6 | +data "oci_resourcemanager_stacks" "all_stacks_in_the_compartment" { |
7 | 7 | compartment_id = var.compartment_id |
8 | | - display_name = local.instance_name_to_match |
9 | | - state = "RUNNING" |
10 | 8 | } |
11 | 9 |
|
12 | | -data "oci_core_instances" "wls_stopped_instances_in_stack_compartment" { |
13 | | - compartment_id = var.compartment_id |
14 | | - display_name = local.instance_name_to_match |
15 | | - state = "STOPPED" |
| 10 | +# collect id of each stack |
| 11 | +locals { |
| 12 | + stack_list = data.oci_resourcemanager_stacks.all_stacks_in_the_compartment.stacks |
| 13 | + num_stacks = length(local.stack_list) |
| 14 | + stack_ids = [for stack in local.stack_list : { id = stack.id }] |
| 15 | +} |
| 16 | + |
| 17 | +# get details of each stack from the list of stack_ids |
| 18 | +data "oci_resourcemanager_stack" "all_stacks" { |
| 19 | + count = local.num_stacks |
| 20 | + #Required |
| 21 | + stack_id = local.stack_ids[count.index].id |
16 | 22 | } |
17 | 23 |
|
18 | 24 | locals { |
19 | | - vnic_prefix = "wls" |
20 | | - resource_name_prefix = var.service_name |
21 | | - # The host_label value below should match with the host_label of the module wls_compute |
22 | | - host_label = "${local.resource_name_prefix}-${local.vnic_prefix}" |
23 | | - instance_name_to_match = "${local.host_label}-0" |
24 | | - num_running_instances = length(data.oci_core_instances.wls_running_instances_in_stack_compartment.instances) |
25 | | - num_stopped_instances = length(data.oci_core_instances.wls_stopped_instances_in_stack_compartment.instances) |
26 | | - num_instances = local.num_running_instances + local.num_stopped_instances |
27 | | - service_name_already_exists = local.num_instances > 0 ? true : false |
28 | | - service_name_already_exists_msg = "WLSC-ERROR: Another compute instance with the name [${local.instance_name_to_match}] already exisits in the stack compartment. Try again with a different service name." |
| 25 | + stack_variables = [for stack in data.oci_resourcemanager_stack.all_stacks : { variables = stack.variables }] |
| 26 | + service_names_used_by_existing_stacks = [for stack_variables in local.stack_variables : lookup(stack_variables.variables, "service_name", "?_not_found_?")] |
| 27 | + duplicate_service_names_list = [for service_name in local.service_names_used_by_existing_stacks : service_name if service_name == var.service_name] |
| 28 | + # There will be always one entry for the name of the current stack. Set duplicate to true if there are more than one entries. |
| 29 | + service_name_already_exists = length(local.duplicate_service_names_list) > 1 ? true : false |
| 30 | + 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." |
29 | 31 | validate_service_name_is_not_already_used = local.service_name_already_exists ? local.validators_msg_map[local.service_name_already_exists_msg] : null |
30 | 32 | } |
0 commit comments