Skip to content

Commit 9b7f860

Browse files
add validation to check existing stack prefix before provisioning (#205)
Issue Fixed: - [JCS-13837](https://jira.oraclecorp.com/jira/browse/JCS-13837) Enh 35251378 - Script to check existing stack prefix before provisioning Validation: - The validation fails if another stack with the service name is already present in the same stack compartment Limitations: - Only the stacks in the current stack compartment are checked. Other compartments are not checked. - Validation is done for UI mode only. No validation is done for CLI mode. Tests PASS: - PASS: create second stack with the same prefix in the same compartment, and verify the apply fails with the validation error. ![image](https://github.com/oracle-quickstart/oci-weblogic-server/assets/101137416/ad92e2c7-1892-47c8-98f3-7aebb6afd111) - PASS: validation succeeds if there is no other stack with the same service_name in the same compartment - PASS: destroy fails
2 parents 62af40c + 8ff225a commit 9b7f860

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

builds/build_cli.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ mkdir -p ${SCRIPT_DIR}/binaries/tmpbuild
6969
create_cli_bundle()
7070
{
7171
cp -Rf ${SCRIPT_DIR}/../terraform/modules ${SCRIPT_DIR}/../terraform/edition.tf ${SCRIPT_DIR}/../terraform/*.tf ${SCRIPT_DIR}/../terraform/images ${TMP_BUILD}
72+
rm ${TMP_BUILD}/modules/validators/stack_validators.tf
7273
replace_variables
7374
(cd ${TMP_BUILD}; zip -r ${SCRIPT_DIR}/binaries/wlsoci-terraform.zip *; rm -Rf ${TMP_BUILD}/*)
7475
}

terraform/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ module "vcn-peering" {
350350
module "validators" {
351351
#depends_on = [module.network-validation]
352352
source = "./modules/validators"
353-
353+
compartment_id = var.compartment_ocid
354354
service_name = var.service_name
355355
wls_ms_port = var.wls_ms_extern_port
356356
wls_ms_ssl_port = var.wls_ms_extern_ssl_port
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

terraform/modules/validators/variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Copyright (c) 2023, Oracle and/or its affiliates.
22
# Licensed under the Universal Permissive License v1.0 as shown at https://oss.oracle.com/licenses/upl.
33

4+
variable "compartment_id" {
5+
type = string
6+
description = "The OCID of the compartment where the stack resources are created"
7+
}
8+
49
variable "service_name" {
510
type = string
611
description = "Prefix for stack resources"

0 commit comments

Comments
 (0)