Skip to content

Commit

Permalink
Merge pull request #19496 from hashicorp/b-aws_lb_listener_rule-blank…
Browse files Browse the repository at this point in the history
…-redirect-query

r/aws_lb_listener_rule: Allow empty string for `action.redirect.query`
  • Loading branch information
ewbankkit authored May 26, 2021
2 parents 8a4d82b + 2313f6a commit a8e6c9e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/19496.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_lb_listener_rule: Allow blank string for `action.redirect.query` nested argument
```
2 changes: 1 addition & 1 deletion aws/resource_aws_lb_listener_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func resourceAwsLbbListenerRule() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Default: "#{query}",
ValidateFunc: validation.StringLenBetween(1, 128),
ValidateFunc: validation.StringLenBetween(0, 128),
},

"status_code": {
Expand Down
62 changes: 58 additions & 4 deletions aws/resource_aws_lb_listener_rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"regexp"
"strconv"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -285,7 +286,7 @@ func TestAccAWSLBListenerRule_redirect(t *testing.T) {
CheckDestroy: testAccCheckAWSLBListenerRuleDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLBListenerRuleConfig_redirect(lbName),
Config: testAccAWSLBListenerRuleConfig_redirect(lbName, "null"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBListenerRuleExists(resourceName, &conf),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "elasticloadbalancing", regexp.MustCompile(fmt.Sprintf(`listener-rule/app/%s/.+$`, lbName))),
Expand All @@ -308,6 +309,54 @@ func TestAccAWSLBListenerRule_redirect(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "condition.#", "1"),
),
},
{
Config: testAccAWSLBListenerRuleConfig_redirect(lbName, "param1=value1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBListenerRuleExists(resourceName, &conf),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "elasticloadbalancing", regexp.MustCompile(fmt.Sprintf(`listener-rule/app/%s/.+$`, lbName))),
resource.TestCheckResourceAttrPair(resourceName, "listener_arn", frontEndListenerResourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "priority", "100"),
resource.TestCheckResourceAttr(resourceName, "action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "action.0.order", "1"),
resource.TestCheckResourceAttr(resourceName, "action.0.type", "redirect"),
resource.TestCheckResourceAttr(resourceName, "action.0.target_group_arn", ""),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.#", "1"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.host", "#{host}"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.path", "/#{path}"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.port", "443"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.protocol", "HTTPS"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.query", "param1=value1"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.status_code", "HTTP_301"),
resource.TestCheckResourceAttr(resourceName, "action.0.fixed_response.#", "0"),
resource.TestCheckResourceAttr(resourceName, "action.0.authenticate_cognito.#", "0"),
resource.TestCheckResourceAttr(resourceName, "action.0.authenticate_oidc.#", "0"),
resource.TestCheckResourceAttr(resourceName, "condition.#", "1"),
),
},
{
Config: testAccAWSLBListenerRuleConfig_redirect(lbName, ""),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSLBListenerRuleExists(resourceName, &conf),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "elasticloadbalancing", regexp.MustCompile(fmt.Sprintf(`listener-rule/app/%s/.+$`, lbName))),
resource.TestCheckResourceAttrPair(resourceName, "listener_arn", frontEndListenerResourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "priority", "100"),
resource.TestCheckResourceAttr(resourceName, "action.#", "1"),
resource.TestCheckResourceAttr(resourceName, "action.0.order", "1"),
resource.TestCheckResourceAttr(resourceName, "action.0.type", "redirect"),
resource.TestCheckResourceAttr(resourceName, "action.0.target_group_arn", ""),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.#", "1"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.host", "#{host}"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.path", "/#{path}"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.port", "443"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.protocol", "HTTPS"),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.query", ""),
resource.TestCheckResourceAttr(resourceName, "action.0.redirect.0.status_code", "HTTP_301"),
resource.TestCheckResourceAttr(resourceName, "action.0.fixed_response.#", "0"),
resource.TestCheckResourceAttr(resourceName, "action.0.authenticate_cognito.#", "0"),
resource.TestCheckResourceAttr(resourceName, "action.0.authenticate_oidc.#", "0"),
resource.TestCheckResourceAttr(resourceName, "condition.#", "1"),
),
},
},
})
}
Expand Down Expand Up @@ -2016,7 +2065,11 @@ resource "aws_security_group" "alb_test" {
`, lbName, targetGroupName)
}

func testAccAWSLBListenerRuleConfig_redirect(lbName string) string {
func testAccAWSLBListenerRuleConfig_redirect(lbName, query string) string {
if query != "null" {
query = strconv.Quote(query)
}

return fmt.Sprintf(`
resource "aws_lb_listener_rule" "static" {
listener_arn = aws_lb_listener.front_end.arn
Expand All @@ -2028,6 +2081,7 @@ resource "aws_lb_listener_rule" "static" {
redirect {
port = "443"
protocol = "HTTPS"
query = %[2]s
status_code = "HTTP_301"
}
}
Expand Down Expand Up @@ -2056,7 +2110,7 @@ resource "aws_lb_listener" "front_end" {
}
resource "aws_lb" "alb_test" {
name = "%s"
name = %[1]q
internal = true
security_groups = [aws_security_group.alb_test.id]
subnets = aws_subnet.alb_test[*].id
Expand Down Expand Up @@ -2126,7 +2180,7 @@ resource "aws_security_group" "alb_test" {
Name = "TestAccAWSALB_redirect"
}
}
`, lbName)
`, lbName, query)
}

func testAccAWSLBListenerRuleConfig_fixedResponse(lbName, response string) string {
Expand Down

0 comments on commit a8e6c9e

Please sign in to comment.