Skip to content

Commit

Permalink
Enhancements
Browse files Browse the repository at this point in the history
 - Fixing creating random_string resources if `prefix_length_limit` = 0
  • Loading branch information
ahaffar committed Aug 4, 2022
1 parent 5554f90 commit 7b9355d
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions example/example1.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module "label1" {
environment = "dev"
project_name = "obytes"
region = "me-south-1"
delimiter = "+"
delimiter = "_"
attributes = ["private"]
enabled = true
prefix_length_limit = 10
}

2 changes: 1 addition & 1 deletion example/example2.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module "label3" {
environment = "prd"
region = "us-east-1"
delimiter = "-"
random_string = module.label1.random_string
prefix_length_limit = 12
}
7 changes: 3 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,8 @@ locals {

id_truncated_length_limit = local.prefix_length_limit - (local.delimiter_length + local.random_length)
id_truncated = local.id_truncated_length_limit <= 0 ? "" : "${trimsuffix(substr(local.id_full, 0, local.id_truncated_length_limit), local.delimiter)}${local.delimiter}"
random_string = local.input.random_string == null ? random_string.this.result : local.input.random_string
id_random_case = local.tag_value_case == "none" ? local.random_string : local.tag_value_case == "upper" ? upper(local.random_string) : local.tag_value_case == "title" ? title(local.random_string) : lower(local.random_string)
id_short = substr("${local.id_truncated}${local.id_random_case}", 0, local.prefix_length_limit)
random_string = local.input.random_string == null && local.prefix_length_limit != null ? try(element(random_string.this.*.result,0 ), null ) : local.input.random_string
id_short = local.random_string == null ? substr(local.id_truncated, 0, local.prefix_length_limit) : substr("${local.id_truncated}${local.random_string}", 0, local.prefix_length_limit)
id = local.prefix_length_limit != 0 && length(local.id_full) > local.prefix_length_limit ? local.id_short : local.id_full

# Context of this label to pass to other label modules
Expand All @@ -113,6 +112,6 @@ locals {
regex_substitute_chars = local.regex_substitute_chars
tag_key_case = local.tag_key_case
tag_value_case = local.tag_value_case
random_string = local.id_random_case
random_string = local.random_string
}
}
1 change: 1 addition & 0 deletions random.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
resource "random_string" "this" {
count = var.prefix_length_limit != null ? 1 : 0
length = 12
special = false
min_numeric = 2
Expand Down

0 comments on commit 7b9355d

Please sign in to comment.