Skip to content

Commit

Permalink
added nb_instances to produce multiple names with index.
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Belval committed Oct 30, 2019
1 parent 5c1a037 commit 9f4f054
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
11 changes: 11 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,14 @@ locals {
suffix = "${join(var.separator, var.suffixes)}"
separated_name = "${var.separator}${var.name}${var.separator}"
}

data "null_data_source" "names" {
count = var.nb_instances
inputs = {
result = var.nb_instances > 1 ? substr("${local.prefix}${local.separated_name}${count.index}${var.separator}${local.suffix}", 0, var.max_length) : substr("${local.prefix}${var.separator}${var.name}${var.separator}${local.suffix}", 0, var.max_length)
}
}

locals {
results = data.null_data_source.names.*.outputs.result
}
7 changes: 6 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
output "result" {
description = "The generated resource name."
value = substr("${local.prefix}${local.separated_name}${local.suffix}", 0, var.max_length)
value = local.results[0]
}

output "results" {
description = "The generated resource names."
value = local.results
}
4 changes: 4 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ variable "name" {
type = string
}

variable "nb_instances" {
default = 1
}

variable "prefixes" {
description = "List of prefixes to append in front of the resource name."
type = list(string)
Expand Down

0 comments on commit 9f4f054

Please sign in to comment.