Skip to content

Commit 6f6c12c

Browse files
add validation to check existing stack prefix before provisioning
1 parent 62af40c commit 6f6c12c

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
# list all stacks in all states (active, deleted etc)
5+
data "oci_resourcemanager_stacks" "all_stacks_in_the_compartment" {
6+
compartment_id = var.compartment_id
7+
}
8+
9+
locals {
10+
stack_list = data.oci_resourcemanager_stacks.all_stacks_in_the_compartment.stacks
11+
num_stacks = length(local.stack_list)
12+
stack_ids = [for stack in local.stack_list : { id = stack.id }]
13+
}
14+
15+
# get details of each stack
16+
data "oci_resourcemanager_stack" "all_stacks" {
17+
count = local.num_stacks
18+
#Required
19+
stack_id = local.stack_ids[count.index].id
20+
}
21+
22+
locals {
23+
stack_variables = [for stack in data.oci_resourcemanager_stack.all_stacks : { variables = stack.variables }]
24+
service_names_used_by_existing_stacks = [for stack_variables in local.stack_variables : lookup(stack_variables.variables, "service_name", "?service_name_attribute_not_found?")]
25+
service_name_already_exists = contains(local.service_names_used_by_existing_stacks, var.service_name) ? true : false
26+
service_name_already_exists_msg = "WLSC-ERROR: The stack with the service_name [${var.service_name}] already exisits in the stack compartment. Try again with a different service name."
27+
validate_service_name_is_not_already_used = local.service_name_already_exists ? local.validators_msg_map[local.service_name_already_exists_msg] : null
28+
}

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 = "Compartment for stack resources"
7+
}
8+
49
variable "service_name" {
510
type = string
611
description = "Prefix for stack resources"

0 commit comments

Comments
 (0)