Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add context #25

Merged
merged 23 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions examples/complete/main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
module "label" {
module "label1" {
source = "../../"
namespace = "Namespace"
stage = "Stage"
name = "Name"
name = "Name1"
attributes = ["1", "2", "3", ""]
tags = "${map("Key", "Value")}"

tags = {
"SomeKey" = "SomeValue"
}
}

module "label2" {
source = "../../"
context = "${module.label1.context}"
name = "Name2"
}
56 changes: 44 additions & 12 deletions examples/complete/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,23 +1,55 @@
output "id" {
value = "${module.label.id}"
output "label1_id" {
value = "${module.label1.id}"
}

output "name" {
value = "${module.label.name}"
output "label1_name" {
value = "${module.label1.name}"
}

output "namespace" {
value = "${module.label.namespace}"
output "label1_namespace" {
value = "${module.label1.namespace}"
}

output "stage" {
value = "${module.label.stage}"
output "label1_stage" {
value = "${module.label1.stage}"
}

output "attributes" {
value = "${module.label.attributes}"
output "label1_attributes" {
value = "${module.label1.attributes}"
}

output "tags" {
value = "${module.label.tags}"
output "label1_tags" {
value = "${module.label1.tags}"
}

output "label1_context" {
value = "${module.label1.context}"
}

output "label2_id" {
value = "${module.label2.id}"
}

output "label2_name" {
value = "${module.label2.name}"
}

output "label2_namespace" {
value = "${module.label2.namespace}"
}

output "label2_stage" {
value = "${module.label2.stage}"
}

output "label2_attributes" {
value = "${module.label2.attributes}"
}

output "label2_tags" {
value = "${module.label2.tags}"
}

output "label2_context" {
value = "${module.label2.context}"
}
46 changes: 30 additions & 16 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
locals {
enabled = "${var.enabled == "true" ? true : false }"
id = "${local.enabled ? lower(join(var.delimiter, compact(concat(list(var.namespace, var.stage, var.name), var.attributes)))) : ""}"
name = "${local.enabled ? lower(format("%v", var.name)) : ""}"
namespace = "${local.enabled ? lower(format("%v", var.namespace)) : ""}"
stage = "${local.enabled ? lower(format("%v", var.stage)) : ""}"
attributes = "${local.enabled ? lower(format("%v", join(var.delimiter, compact(var.attributes)))) : ""}"

tags = "${
merge(
map(
"Name", "${local.id}",
"Namespace", "${local.namespace}",
"Stage", "${local.stage}"
), var.tags
)
}"
enabled = "${var.enabled == "true" ? true : false }"

id = "${lower(join(var.delimiter, compact(concat(list(local.namespace, local.stage, local.name, local.attributes)))))}"
name = "${var.name == "" ? lookup(var.context, "name", "") : lower(format("%v", var.name))}"
namespace = "${var.namespace == "" ? lookup(var.context, "namespace", "") : lower(format("%v", var.namespace))}"
stage = "${var.stage == "" ? lookup(var.context, "stage", "") : lower(format("%v", var.stage))}"
attributes = "${length(var.attributes) == 0 ? lookup(var.context, "attributes", "") : lower(format("%v", join(var.delimiter, compact(var.attributes))))}"

generated_tags = {
"Name" = "${local.enabled ? local.id : ""}"
"Namespace" = "${local.enabled ? local.namespace : ""}"
"Stage" = "${local.enabled ? local.stage : ""}"
}

tags = "${merge(local.generated_tags, var.tags)}"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to incorporate the var.context["tags"] here some how

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it doesn't seem possible :(


tags_as_list_of_maps = ["${null_resource.tags_as_list_of_maps.*.triggers}"]

null_tags = {
Name = ""
Namespace = ""
Stage = ""
}

context = {
name = "${local.name}"
namespace = "${local.namespace}"
stage = "${local.stage}"
attributes = "${local.attributes}"
tags = "${local.tags}"
delimiter = "${var.delimiter}"
}
}

resource "null_resource" "tags_as_list_of_maps" {
Expand Down
15 changes: 10 additions & 5 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
output "id" {
value = "${local.id}"
value = "${local.enabled ? local.id : ""}"
description = "Disambiguated ID"
}

output "name" {
value = "${local.name}"
value = "${local.enabled ? local.name : ""}"
description = "Normalized name"
}

output "namespace" {
value = "${local.namespace}"
value = "${local.enabled ? local.namespace : ""}"
description = "Normalized namespace"
}

output "stage" {
value = "${local.stage}"
value = "${local.enabled ? local.stage : ""}"
description = "Normalized stage"
}

output "attributes" {
value = "${local.attributes}"
value = "${local.enabled ? local.attributes : ""}"
description = "Normalized attributes"
}

Expand All @@ -32,3 +32,8 @@ output "tags_as_list_of_maps" {
value = ["${local.tags_as_list_of_maps}"]
description = "Additional tags as a list of maps, which can be used in several AWS resources"
}

output "context" {
value = "${local.context}"
description = "Context of this module to pass between other modules"
}
9 changes: 9 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
variable "namespace" {
description = "Namespace, which could be your organization name, e.g. 'cp' or 'cloudposse'"
default = ""
}

variable "stage" {
description = "Stage, e.g. 'prod', 'staging', 'dev', or 'test'"
default = ""
}

variable "name" {
description = "Solution name, e.g. 'app' or 'jenkins'"
default = ""
}

variable "enabled" {
Expand Down Expand Up @@ -38,3 +41,9 @@ variable "additional_tag_map" {
default = {}
description = "Additional tags for appending to each tag map."
}

variable "context" {
type = "map"
default = {}
description = "Default context to use for passing state between label invocations"
}