Skip to content

Commit

Permalink
Merge pull request #22996 from hashicorp/td-lb-target-group-invalid
Browse files Browse the repository at this point in the history
elbv2: Don't allow invalid configurations
  • Loading branch information
YakDriver authored Feb 7, 2022
2 parents b3b729b + 9878300 commit a5e368a
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 134 deletions.
3 changes: 3 additions & 0 deletions .changelog/22996.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
resource/aws_lb_target_group: For `protocol = "TCP"`, `stickiness` can no longer be type set to `lb_cookie` even when `enabled = false`; instead use type `source_ip`
```
138 changes: 60 additions & 78 deletions internal/service/elbv2/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,16 +252,6 @@ func ResourceTargetGroup() *schema.Resource {
"app_cookie", // Only for ALBs
"source_ip", // Only for NLBs
}, false),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
switch d.Get("protocol").(string) {
case elbv2.ProtocolEnumTcp, elbv2.ProtocolEnumUdp, elbv2.ProtocolEnumTcpUdp, elbv2.ProtocolEnumTls:
if new == "lb_cookie" && !d.Get("stickiness.0.enabled").(bool) {
log.Printf("[WARN] invalid configuration, this will fail in a future version: stickiness enabled %v, protocol %s, type %s", d.Get("stickiness.0.enabled").(bool), d.Get("protocol").(string), new)
return true
}
}
return false
},
},
},
},
Expand Down Expand Up @@ -469,41 +459,37 @@ func resourceTargetGroupCreate(d *schema.ResourceData, meta interface{}) error {
stickinessBlocks := v.([]interface{})
stickiness := stickinessBlocks[0].(map[string]interface{})

if !stickiness["enabled"].(bool) && (stickiness["type"].(string) == "lb_cookie" || stickiness["type"].(string) == "app_cookie") && d.Get("protocol").(string) != elbv2.ProtocolEnumHttp && d.Get("protocol").(string) != elbv2.ProtocolEnumHttps {
log.Printf("[WARN] invalid configuration, this will fail in a future version: stickiness enabled %v, protocol %s, type %s", stickiness["enabled"].(bool), d.Get("protocol").(string), stickiness["type"].(string))
} else {
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.enabled"),
Value: aws.String(strconv.FormatBool(stickiness["enabled"].(bool))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.type"),
Value: aws.String(stickiness["type"].(string)),
})
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.enabled"),
Value: aws.String(strconv.FormatBool(stickiness["enabled"].(bool))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.type"),
Value: aws.String(stickiness["type"].(string)),
})

switch d.Get("protocol").(string) {
case elbv2.ProtocolEnumHttp, elbv2.ProtocolEnumHttps:
switch stickiness["type"].(string) {
case "lb_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.lb_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
})
case "app_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.cookie_name"),
Value: aws.String(stickiness["cookie_name"].(string)),
})
default:
log.Printf("[WARN] Unexpected stickiness type. Expected lb_cookie or app_cookie, got %s", stickiness["type"].(string))
}
switch d.Get("protocol").(string) {
case elbv2.ProtocolEnumHttp, elbv2.ProtocolEnumHttps:
switch stickiness["type"].(string) {
case "lb_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.lb_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
})
case "app_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.cookie_name"),
Value: aws.String(stickiness["cookie_name"].(string)),
})
default:
log.Printf("[WARN] Unexpected stickiness type. Expected lb_cookie or app_cookie, got %s", stickiness["type"].(string))
}
}
}
Expand Down Expand Up @@ -700,41 +686,37 @@ func resourceTargetGroupUpdate(d *schema.ResourceData, meta interface{}) error {
if len(stickinessBlocks) == 1 {
stickiness := stickinessBlocks[0].(map[string]interface{})

if !stickiness["enabled"].(bool) && (stickiness["type"].(string) == "lb_cookie" || stickiness["type"].(string) == "app_cookie") && d.Get("protocol").(string) != elbv2.ProtocolEnumHttp && d.Get("protocol").(string) != elbv2.ProtocolEnumHttps {
log.Printf("[WARN] invalid configuration, this will fail in a future version: stickiness enabled %v, protocol %s, type %s", stickiness["enabled"].(bool), d.Get("protocol").(string), stickiness["type"].(string))
} else {
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.enabled"),
Value: aws.String(strconv.FormatBool(stickiness["enabled"].(bool))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.type"),
Value: aws.String(stickiness["type"].(string)),
})
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.enabled"),
Value: aws.String(strconv.FormatBool(stickiness["enabled"].(bool))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.type"),
Value: aws.String(stickiness["type"].(string)),
})

switch d.Get("protocol").(string) {
case elbv2.ProtocolEnumHttp, elbv2.ProtocolEnumHttps:
switch stickiness["type"].(string) {
case "lb_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.lb_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
})
case "app_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.cookie_name"),
Value: aws.String(stickiness["cookie_name"].(string)),
})
default:
log.Printf("[WARN] Unexpected stickiness type. Expected lb_cookie or app_cookie, got %s", stickiness["type"].(string))
}
switch d.Get("protocol").(string) {
case elbv2.ProtocolEnumHttp, elbv2.ProtocolEnumHttps:
switch stickiness["type"].(string) {
case "lb_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.lb_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
})
case "app_cookie":
attrs = append(attrs,
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.duration_seconds"),
Value: aws.String(fmt.Sprintf("%d", stickiness["cookie_duration"].(int))),
},
&elbv2.TargetGroupAttribute{
Key: aws.String("stickiness.app_cookie.cookie_name"),
Value: aws.String(stickiness["cookie_name"].(string)),
})
default:
log.Printf("[WARN] Unexpected stickiness type. Expected lb_cookie or app_cookie, got %s", stickiness["type"].(string))
}
}
} else if len(stickinessBlocks) == 0 {
Expand Down
Loading

0 comments on commit a5e368a

Please sign in to comment.