-
-
Notifications
You must be signed in to change notification settings - Fork 318
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
Add context #25
Conversation
This is working and produces the following output. I just don't know how to inherit the additional tags from the parent's context.
|
main.tf
Outdated
"Stage" = "${local.enabled ? local.stage : ""}" | ||
} | ||
|
||
tags = "${merge(local.generated_tags, var.tags)}" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 :(
@osterman Hey, it's an awesome idea to have context. Unfortunately I also don't see how it's possible with HCL1. We should always try to remember that scalars such as maps/list have always to be flat. Basically scalars must:
These limitations will be hopefully resolved in 0.12 if it includes HCL2... In other words now all the variables and all the outputs should be flat and homogeneous. With the introduced context you break this rule. Regarding non-flat scalars, the only workaround possible now is leveraging locals for intermediate computation, like bellow: locals {
non_flat_map = {
map1 = "${var.map1}" # var.map1 should be flat
map2 = "${var.map2}" # var.map2 should be flat
}
target_map = "${local.non_flat_map[var.choice ? "map1": "map2"]}"
}
output "map" {
value = "${local.target_map}"
} |
This code:module "label1" {
source = "../../"
namespace = "CloudPosse"
stage = "Production"
name = "Winston"
attributes = ["fire", "water", "earth", "air"]
tags = {
"City" = "Dublin"
"Environment" = "Private"
}
}
module "label2" {
source = "../../"
context = "${module.label1.context}"
name = "Charlie"
tags = {
"City" = "London"
"Environment" = "Public"
}
}
output "label1" {
value = {
id = "${module.label1.id}"
name = "${module.label1.name}"
namespace = "${module.label1.namespace}"
stage = "${module.label1.stage}"
attributes = "${module.label1.attributes}"
}
}
output "label2" {
value = {
id = "${module.label2.id}"
name = "${module.label2.name}"
namespace = "${module.label2.namespace}"
stage = "${module.label2.stage}"
attributes = "${module.label2.attributes}"
}
}
output "label1_tags" {
value = "${module.label1.tags}"
}
output "label1_context" {
value = "${module.label1.context}"
}
output "label2_tags" {
value = "${module.label2.tags}"
}
output "label2_context" {
value = "${module.label2.context}"
} Makes this output nowOutputs:
label1 = {
attributes = [fire water earth air]
id = cloudposse-production-winston-fire-water-earth-air
name = winston
namespace = cloudposse
stage = production
}
label1_context = {
attributes = [fire water earth air]
delimiter = [-]
name = [winston]
namespace = [cloudposse]
stage = [production]
tags_keys = [City Environment Name Namespace Stage]
tags_values = [Dublin Private cloudposse-production-winston-fire-water-earth-air cloudposse production]
}
label1_tags = {
City = Dublin
Environment = Private
Name = cloudposse-production-winston-fire-water-earth-air
Namespace = cloudposse
Stage = production
}
label2 = {
attributes = [fire water earth air]
id = cloudposse-production-charlie-fire-water-earth-air
name = charlie
namespace = cloudposse
stage = production
}
label2_context = {
attributes = [fire water earth air]
delimiter = [-]
name = [charlie]
namespace = [cloudposse]
stage = [production]
tags_keys = [City Environment Name Namespace Stage]
tags_values = [London Public cloudposse-production-charlie-fire-water-earth-air cloudposse production]
}
label2_tags = {
City = London
Environment = Public
Name = cloudposse-production-charlie-fire-water-earth-air
Namespace = cloudposse
Stage = production
} |
@dennybaa I got it working |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
main.tf
Outdated
# Merge the map of empty values, with the variable context, so that context_local always contains all map keys | ||
context_local = "${merge(local.context_context, var.context)}" | ||
# Only maps that contain all the same attribute types can be merged, so they have been set to list | ||
context_context = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we rename this? maybe call it null_context
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm renaming it to context_struct
, since it is the structure of context with all variables.
main.tf
Outdated
selected_stage = ["${compact(concat(local.context_local["stage"], list(var.stage)))}"] | ||
stage = "${lower(join("", split(" ", local.selected_stage[0])))}" | ||
selected_attributes = ["${distinct(compact(concat(var.attributes, local.context_local["attributes"])))}"] | ||
attributes = "${split("~^~", lower(join("~^~", local.selected_attributes)))}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's call ~^~
something (E.g. unique_separator
) - either a variable
or a local
.
…bel into add-context * 'add-context' of github.com:cloudposse/terraform-null-label: Migrate to README.yaml format (#24)
@dennybaa - please have another look at this. It's not pretty, but it sets out to accomplish what we wanted to do. I'm pretty sure we'll end up merging this. |
@osterman Wow. That's a lot of effort, this might be a good example of that how to mitigate passing non-flat structures :) It would be nice if you also switch to null_data_source this reduces output rubbish during plan/apply, and specifically made for this, in other words resource is an overkill here. This would work now, though tags passing seemed reasonably easier and if I'm not mistaken achieved the same goal. My personal opinion that terraform is not yet ready for things we are trying to do... Don't get me wrong but it doesn't feel like an easy way. Though I really admire your approach and implementation 👍 |
So I still think, taking an approach of calling tags Name/Namespace/Stage as generated_tags. IMO it still looks neater for me to have an option like |
I think that this will get far easier in tf 0.12 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks complicated, but not too much.
Eventually we will simplify that for 0.12 tf
@osterman yeah, it looks a bit complicated, but what we can do :) The module is very usable now 👍 for it. |
@osterman Hey, Erik. I've noticed it seems that you missed https://github.com/stackfeed/terraform-null-label/commit/4d7fb86ab793edb4664ab7bef422fc80b84bfcd0 |
…ment as an optional variable. Swapped out null_resource for null_data_source, which reduces some of the output noise but keeps the funtionality the same.
what
why
demo
todo
var.tags
<--- need help