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 support for 'redirect' and 'fixed-response' into lb_listener and lb_listener_rule #5430

Merged
merged 13 commits into from
Aug 20, 2018
239 changes: 225 additions & 14 deletions aws/resource_aws_lb_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -70,26 +71,134 @@ func resourceAwsLbListener() *schema.Resource {
"default_action": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_group_arn": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
elbv2.ActionTypeEnumFixedResponse,
elbv2.ActionTypeEnumForward,
elbv2.ActionTypeEnumRedirect,
}, true),
},

"target_group_arn": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot("forward"),
},

"redirect": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot("redirect"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"host": {
Type: schema.TypeString,
Optional: true,
Default: "#{host}",
},

"path": {
Type: schema.TypeString,
Optional: true,
Default: "/#{path}",
},

"port": {
Type: schema.TypeString,
Optional: true,
Default: "#{port}",
},

"protocol": {
Type: schema.TypeString,
Optional: true,
Default: "#{protocol}",
ValidateFunc: validation.StringInSlice([]string{
"#{protocol}",
"HTTP",
"HTTPS",
}, false),
},

"query": {
Type: schema.TypeString,
Optional: true,
Default: "#{query}",
},

"status_code": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"HTTP_301",
"HTTP_302",
}, false),
},
},
},
},

"fixed_response": {
Type: schema.TypeList,
Optional: true,
DiffSuppressFunc: suppressIfDefaultActionTypeNot("fixed-response"),
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"content_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"text/plain",
"text/css",
"text/html",
"application/javascript",
"application/json",
}, false),
},

"message_body": {
Type: schema.TypeString,
Optional: true,
},

"status_code": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[245]\d\d$`), ""),
},
},
},
},
},
},
},
},
}
}

func suppressIfDefaultActionTypeNot(t string) schema.SchemaDiffSuppressFunc {
return func(k, old, new string, d *schema.ResourceData) bool {
take := 2
i := strings.IndexFunc(k, func(r rune) bool {
if r == '.' {
take -= 1
return take == 0
}
return false
})
at := k[:i+1] + "type"
return d.Get(at).(string) != t
}
}

func resourceAwsLbListenerCreate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbv2conn

Expand Down Expand Up @@ -118,10 +227,49 @@ func resourceAwsLbListenerCreate(d *schema.ResourceData, meta interface{}) error
for i, defaultAction := range defaultActions {
defaultActionMap := defaultAction.(map[string]interface{})

params.DefaultActions[i] = &elbv2.Action{
TargetGroupArn: aws.String(defaultActionMap["target_group_arn"].(string)),
Type: aws.String(defaultActionMap["type"].(string)),
action := &elbv2.Action{
Type: aws.String(defaultActionMap["type"].(string)),
}

switch defaultActionMap["type"].(string) {
case "forward":
action.TargetGroupArn = aws.String(defaultActionMap["target_group_arn"].(string))

case "redirect":
redirectList := defaultActionMap["redirect"].([]interface{})

if len(redirectList) == 1 {
redirectMap := redirectList[0].(map[string]interface{})

action.RedirectConfig = &elbv2.RedirectActionConfig{
Host: aws.String(redirectMap["host"].(string)),
Path: aws.String(redirectMap["path"].(string)),
Port: aws.String(redirectMap["port"].(string)),
Protocol: aws.String(redirectMap["protocol"].(string)),
Query: aws.String(redirectMap["query"].(string)),
StatusCode: aws.String(redirectMap["status_code"].(string)),
}
} else {
return errors.New("for actions of type 'redirect', you must specify a 'redirect' block")
}

case "fixed-response":
fixedResponseList := defaultActionMap["fixed_response"].([]interface{})

if len(fixedResponseList) == 1 {
fixedResponseMap := fixedResponseList[0].(map[string]interface{})

action.FixedResponseConfig = &elbv2.FixedResponseActionConfig{
ContentType: aws.String(fixedResponseMap["content_type"].(string)),
MessageBody: aws.String(fixedResponseMap["message_body"].(string)),
StatusCode: aws.String(fixedResponseMap["status_code"].(string)),
}
} else {
return errors.New("for actions of type 'fixed-response', you must specify a 'fixed_response' block")
}
}

params.DefaultActions[i] = action
}
}

Expand Down Expand Up @@ -187,11 +335,36 @@ func resourceAwsLbListenerRead(d *schema.ResourceData, meta interface{}) error {
defaultActions := make([]map[string]interface{}, 0)
if listener.DefaultActions != nil && len(listener.DefaultActions) > 0 {
for _, defaultAction := range listener.DefaultActions {
action := map[string]interface{}{
"target_group_arn": aws.StringValue(defaultAction.TargetGroupArn),
"type": aws.StringValue(defaultAction.Type),
defaultActionMap := make(map[string]interface{})
defaultActionMap["type"] = aws.StringValue(defaultAction.Type)

switch aws.StringValue(defaultAction.Type) {
case "forward":
defaultActionMap["target_group_arn"] = aws.StringValue(defaultAction.TargetGroupArn)

case "redirect":
defaultActionMap["redirect"] = []map[string]interface{}{
{
"host": aws.StringValue(defaultAction.RedirectConfig.Host),
"path": aws.StringValue(defaultAction.RedirectConfig.Path),
"port": aws.StringValue(defaultAction.RedirectConfig.Port),
"protocol": aws.StringValue(defaultAction.RedirectConfig.Protocol),
"query": aws.StringValue(defaultAction.RedirectConfig.Query),
"status_code": aws.StringValue(defaultAction.RedirectConfig.StatusCode),
},
}

case "fixed-response":
defaultActionMap["fixed_response"] = []map[string]interface{}{
{
"content_type": aws.StringValue(defaultAction.FixedResponseConfig.ContentType),
"message_body": aws.StringValue(defaultAction.FixedResponseConfig.MessageBody),
"status_code": aws.StringValue(defaultAction.FixedResponseConfig.StatusCode),
},
}
}
defaultActions = append(defaultActions, action)

defaultActions = append(defaultActions, defaultActionMap)
}
}
d.Set("default_action", defaultActions)
Expand Down Expand Up @@ -225,10 +398,48 @@ func resourceAwsLbListenerUpdate(d *schema.ResourceData, meta interface{}) error
for i, defaultAction := range defaultActions {
defaultActionMap := defaultAction.(map[string]interface{})

params.DefaultActions[i] = &elbv2.Action{
TargetGroupArn: aws.String(defaultActionMap["target_group_arn"].(string)),
Type: aws.String(defaultActionMap["type"].(string)),
action := &elbv2.Action{}
action.Type = aws.String(defaultActionMap["type"].(string))

switch defaultActionMap["type"].(string) {
case "forward":
action.TargetGroupArn = aws.String(defaultActionMap["target_group_arn"].(string))

case "redirect":
redirectList := defaultActionMap["redirect"].([]interface{})

if len(redirectList) == 1 {
redirectMap := redirectList[0].(map[string]interface{})

action.RedirectConfig = &elbv2.RedirectActionConfig{
Host: aws.String(redirectMap["host"].(string)),
Path: aws.String(redirectMap["path"].(string)),
Port: aws.String(redirectMap["port"].(string)),
Protocol: aws.String(redirectMap["protocol"].(string)),
Query: aws.String(redirectMap["query"].(string)),
StatusCode: aws.String(redirectMap["status_code"].(string)),
}
} else {
return errors.New("for actions of type 'redirect', you must specify a 'redirect' block")
}

case "fixed-response":
fixedResponseList := defaultActionMap["fixed_response"].([]interface{})

if len(fixedResponseList) == 1 {
fixedResponseMap := fixedResponseList[0].(map[string]interface{})

action.FixedResponseConfig = &elbv2.FixedResponseActionConfig{
ContentType: aws.String(fixedResponseMap["content_type"].(string)),
MessageBody: aws.String(fixedResponseMap["message_body"].(string)),
StatusCode: aws.String(fixedResponseMap["status_code"].(string)),
}
} else {
return errors.New("for actions of type 'fixed-response', you must specify a 'fixed_response' block")
}
}

params.DefaultActions[i] = action
}
}

Expand Down
Loading