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

fix: Replace try() with can(coalesce()) to better handle null values #392

Closed
Closed
Changes from all 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
45 changes: 22 additions & 23 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ resource "aws_lb_listener" "this" {
certificate_arn = try(each.value.certificate_arn, null)

dynamic "default_action" {
for_each = try([each.value.authenticate_cognito], [])
for_each = can(coalesce(each.value.authenticate_cognito)) ? [each.value.authenticate_cognito] : []
Copy link
Member

Choose a reason for hiding this comment

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

this is not a change that we are going to accept. you should change your variables in your wrapper to make it work with the existing implementation. I would start with not using null and instead default to an empty list.

Copy link
Author

Choose a reason for hiding this comment

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

I'm confused though... The change is a safer null-safe implementation with the same end result...

From memory I tried a few different implementations of the object and couldn't find a way of not having the null value come through...
Will setup a reproducer to confirm....

Copy link
Member

Choose a reason for hiding this comment

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

The change is a safer null-safe implementation

I don't know what is meant by this. but in general, you are applying defaults that conflict with the module - therefore, you should adapt your implementation, not the other way around


content {
authenticate_cognito {
Expand All @@ -112,7 +112,7 @@ resource "aws_lb_listener" "this" {
}

dynamic "default_action" {
for_each = try([each.value.authenticate_oidc], [])
for_each = can(coalesce(each.value.authentication_oidc)) ? [each.value.authenticate_oidc] : []

content {
authenticate_oidc {
Expand All @@ -135,7 +135,7 @@ resource "aws_lb_listener" "this" {
}

dynamic "default_action" {
for_each = try([each.value.fixed_response], [])
for_each = can(coalesce(each.value.fixed_response)) ? [each.value.fixed_response] : []

content {
fixed_response {
Expand All @@ -150,7 +150,7 @@ resource "aws_lb_listener" "this" {
}

dynamic "default_action" {
for_each = try([each.value.forward], [])
for_each = can(coalesce(each.value.forward)) ? [each.value.forward] : []

content {
order = try(default_action.value.order, null)
Expand All @@ -160,12 +160,12 @@ resource "aws_lb_listener" "this" {
}

dynamic "default_action" {
for_each = try([each.value.weighted_forward], [])
for_each = can(coalesce(each.value.weighted_forward)) ? [each.value.weighted_forward] : []

content {
forward {
dynamic "target_group" {
for_each = try(default_action.value.target_groups, [])
for_each = can(coalesce(default_action.value.target_groups)) ? default_action.value.target_groups : []

content {
arn = try(target_group.value.arn, aws_lb_target_group.this[target_group.value.target_group_key].arn, null)
Expand All @@ -174,7 +174,7 @@ resource "aws_lb_listener" "this" {
}

dynamic "stickiness" {
for_each = try([default_action.value.stickiness], [])
for_each = can(coalesce(default_action.value.stickiness)) ? [default_action.value.stickiness] : []

content {
duration = try(stickiness.value.duration, 60)
Expand All @@ -189,7 +189,7 @@ resource "aws_lb_listener" "this" {
}

dynamic "default_action" {
for_each = try([each.value.redirect], [])
for_each = can(coalesce(each.value.redirect)) ? [each.value.redirect] : []

content {
order = try(default_action.value.order, null)
Expand All @@ -208,7 +208,7 @@ resource "aws_lb_listener" "this" {
}

dynamic "mutual_authentication" {
for_each = try([each.value.mutual_authentication], [])
for_each = can(coalesce(each.value.mutual_authentication)) ? [each.value.mutual_authentication] : []
content {
mode = mutual_authentication.value.mode
trust_store_arn = try(mutual_authentication.value.trust_store_arn, null)
Expand Down Expand Up @@ -367,7 +367,7 @@ resource "aws_lb_listener_rule" "this" {

content {
dynamic "host_header" {
for_each = try([condition.value.host_header], [])
for_each = can(coalesce(condition.value.host_header)) ? [condition.value.host_header] : []

content {
values = host_header.value.values
Expand All @@ -381,7 +381,7 @@ resource "aws_lb_listener_rule" "this" {

content {
dynamic "http_header" {
for_each = try([condition.value.http_header], [])
for_each = can(coalesce(condition.value.http_header)) ? [condition.value.http_header] : []

content {
http_header_name = http_header.value.http_header_name
Expand All @@ -396,7 +396,7 @@ resource "aws_lb_listener_rule" "this" {

content {
dynamic "http_request_method" {
for_each = try([condition.value.http_request_method], [])
for_each = can(coalesce(condition.value.http_request_method)) ? [condition.value.http_request_method] : []

content {
values = http_request_method.value.values
Expand All @@ -410,7 +410,7 @@ resource "aws_lb_listener_rule" "this" {

content {
dynamic "path_pattern" {
for_each = try([condition.value.path_pattern], [])
for_each = can(coalesce(condition.value.path_pattern)) ? [condition.value.path_pattern] : []

content {
values = path_pattern.value.values
Expand All @@ -424,7 +424,7 @@ resource "aws_lb_listener_rule" "this" {

content {
dynamic "query_string" {
for_each = try(flatten([condition.value.query_string]), [])
for_each = can(coalesce(condition.value.query_string)) ? [condition.value.query_string] : []

content {
key = try(query_string.value.key, null)
Expand All @@ -439,7 +439,7 @@ resource "aws_lb_listener_rule" "this" {

content {
dynamic "source_ip" {
for_each = try([condition.value.source_ip], [])
for_each = can(coalesce(condition.value.source_ip)) ? [condition.value.source_ip] : []

content {
values = source_ip.value.values
Expand Down Expand Up @@ -493,7 +493,7 @@ resource "aws_lb_target_group" "this" {
deregistration_delay = try(each.value.deregistration_delay, null)

dynamic "health_check" {
for_each = try([each.value.health_check], [])
for_each = can(coalesce(each.value.health_check)) ? [each.value.health_check] : []

content {
enabled = try(health_check.value.enabled, null)
Expand Down Expand Up @@ -523,7 +523,7 @@ resource "aws_lb_target_group" "this" {
slow_start = try(each.value.slow_start, null)

dynamic "stickiness" {
for_each = try([each.value.stickiness], [])
for_each = can(coalesce(each.value.stickiness)) ? [each.value.stickiness] : []

content {
cookie_duration = try(stickiness.value.cookie_duration, null)
Expand All @@ -534,7 +534,7 @@ resource "aws_lb_target_group" "this" {
}

dynamic "target_failover" {
for_each = try(each.value.target_failover, [])
for_each = can(coalesce(each.value.target_failover)) ? each.value.target_failover : []

content {
on_deregistration = target_failover.value.on_deregistration
Expand All @@ -543,12 +543,11 @@ resource "aws_lb_target_group" "this" {
}

dynamic "target_group_health" {
for_each = try([each.value.target_group_health], [])
for_each = can(coalesce(each.value.target_group_health)) ? [each.value.target_group_health] : []

content {

dynamic "dns_failover" {
for_each = try([target_group_health.value.dns_failover], [])
for_each = can(coalesce(target_group_health.value.dns_failover)) ? [target_group_health.value.dns_failover] : []

content {
minimum_healthy_targets_count = try(dns_failover.value.minimum_healthy_targets_count, null)
Expand All @@ -557,7 +556,7 @@ resource "aws_lb_target_group" "this" {
}

dynamic "unhealthy_state_routing" {
for_each = try([target_group_health.value.unhealthy_state_routing], [])
for_each = can(coalesce(target_group_health.value.unhealthy_state_routing)) ? [target_group_health.value.unhealthy_state_routing] : []

content {
minimum_healthy_targets_count = try(unhealthy_state_routing.value.minimum_healthy_targets_count, null)
Expand All @@ -568,7 +567,7 @@ resource "aws_lb_target_group" "this" {
}

dynamic "target_health_state" {
for_each = try([each.value.target_health_state], [])
for_each = can(coalesce(each.value.target_health_state)) ? [each.value.target_health_state] : []
content {
enable_unhealthy_connection_termination = try(target_health_state.value.enable_unhealthy_connection_termination, true)
unhealthy_draining_interval = try(target_health_state.value.unhealthy_draining_interval, null)
Expand Down
Loading