Skip to content

Commit

Permalink
feat(lb-parameterized-redirect): Update doc to specify parameterized …
Browse files Browse the repository at this point in the history
…url redirect (#5521)

* feat(lb-parameterized-redirect): Updated doc to specify parameterized url support

* update test case

* update test case
  • Loading branch information
deepaksibm authored Jul 22, 2024
1 parent c9acb2e commit b38c98d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
81 changes: 81 additions & 0 deletions ibm/service/vpc/resource_ibm_is_lb_listener_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,51 @@ func TestAccIBMISLBListenerPolicyHttpRedirectNew_basic(t *testing.T) {
},
})
}

func TestAccIBMISLBListenerPolicyParameterizedRedirectNew_basic(t *testing.T) {
var lb string
vpcname := fmt.Sprintf("tflblis-vpc-%d", acctest.RandIntRange(10, 100))
subnetname := fmt.Sprintf("tflblis-subnet-%d", acctest.RandIntRange(10, 100))
lbname := fmt.Sprintf("tflblis%d", acctest.RandIntRange(10, 100))
lbpolicyname := fmt.Sprintf("tflblispol%d", acctest.RandIntRange(10, 100))
protocol1 := "https"
port1 := "9086"
url := "https://{host}:8080/{port}/{host}/{path}"
urlUpdate := "{protocol}://test.{host}:80/{path}"

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMISLBListenerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMISLBListenerPolicyParameterizedRedirectNewConfig(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, lbname, port1, protocol1, lbpolicyname, url),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISLBListenerExists("ibm_is_lb_listener_policy.lb_listener_policy", lb),
resource.TestCheckResourceAttr(
"ibm_is_lb.testacc_LB", "name", lbname),
resource.TestCheckResourceAttr(
"ibm_is_lb_listener_policy.lb_listener_policy", "target.0.http_status_code", "302"),
resource.TestCheckResourceAttr(
"ibm_is_lb_listener_policy.lb_listener_policy", "target.0.url", url),
),
},
{
Config: testAccCheckIBMISLBListenerPolicyParameterizedRedirectNewConfig(vpcname, subnetname, acc.ISZoneName, acc.ISCIDR, lbname, port1, protocol1, lbpolicyname, urlUpdate),
Check: resource.ComposeTestCheckFunc(
testAccCheckIBMISLBListenerExists("ibm_is_lb_listener_policy.lb_listener_policy", lb),
resource.TestCheckResourceAttr(
"ibm_is_lb.testacc_LB", "name", lbname),
resource.TestCheckResourceAttr(
"ibm_is_lb_listener_policy.lb_listener_policy", "target.0.http_status_code", "302"),
resource.TestCheckResourceAttr(
"ibm_is_lb_listener_policy.lb_listener_policy", "target.0.url", urlUpdate),
),
},
},
})
}

func testAccCheckIBMISLBListenerPolicyDestroy(s *terraform.State) error {

sess, _ := acc.TestAccProvider.Meta().(conns.ClientSession).VpcV1API()
Expand Down Expand Up @@ -790,6 +835,42 @@ func testAccCheckIBMISLBListenerPolicyHttpsRedirectNewConfigRemoveUri(vpcname, s
priority = 2
}`, vpcname, subnetname, zone, cidr, lbname, acc.LbListerenerCertificateInstance, acc.LbListerenerCertificateInstance, lbpolicyname)

}

func testAccCheckIBMISLBListenerPolicyParameterizedRedirectNewConfig(vpcname, subnetname, zone, cidr, lbname, port, protocol, lbpolicyname, url string) string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "%s"
}
resource "ibm_is_subnet" "testacc_subnet" {
name = "%s"
vpc = "${ibm_is_vpc.testacc_vpc.id}"
zone = "%s"
ipv4_cidr_block = "%s"
}
resource "ibm_is_lb" "testacc_LB" {
name = "%s"
subnets = ["${ibm_is_subnet.testacc_subnet.id}"]
type = "private"
}
resource "ibm_is_lb_listener" "lb_listener1"{
lb = ibm_is_lb.testacc_LB.id
port = "9086"
protocol = "http"
}
resource "ibm_is_lb_listener_policy" "lb_listener_policy" {
name = "%s"
lb = ibm_is_lb.testacc_LB.id
listener = ibm_is_lb_listener.lb_listener1.listener_id
action = "redirect"
target {
http_status_code = 302
url = "%s"
}
priority = 2
}`, vpcname, subnetname, zone, cidr, lbname, lbpolicyname, url)

}
func testAccCheckIBMISLBListenerPolicyHttpsRedirectConfigUpdate(vpcname, subnetname, zone, cidr, lbname, port, protocol, lbpolicyname string) string {
return fmt.Sprintf(`
Expand Down
36 changes: 35 additions & 1 deletion website/docs/r/is_lb_listener_policy.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ resource "ibm_is_lb_listener_policy" "example" {
}
```

### Sample to create a load balancer listener policy for a `redirect` action with parameterized url.

```terraform
resource "ibm_is_lb" "example" {
name = "example-lb"
subnets = [ibm_is_subnet.example.id]
}
resource "ibm_is_lb_listener" "example" {
lb = ibm_is_lb.example.id
port = "9086"
protocol = "http"
}
resource "ibm_is_lb_listener_policy" "example" {
lb = ibm_is_lb.example.id
listener = ibm_is_lb_listener.example.listener_id
action = "redirect"
priority = 4
name = "example-listener-policy"
target {
http_status_code = 302
url = "https://{host}:8080/{port}/{host}/{path}"
}
}
```

### Sample to create a load balancer listener policy for a `https_redirect` action.

```terraform
Expand Down Expand Up @@ -177,7 +204,14 @@ Review the argument references that you can specify for your resource.
- `href` - (Optional, String) The listener's canonical URL.
- `id` - (Optional, String) The unique identifier for this load balancer listener.
- `name` - (Computed, String) The name for this load balancer pool. The name is unique across all pools for the load balancer.
- `url` - (Optional, String) The redirect target URL.
- `url` - (Optional, String) The redirect target URL. The URL supports [RFC 6570 level 1 expressions](https://datatracker.ietf.org/doc/html/rfc6570#section-1.2) for the following variables which expand to values from the originally requested URL (or the indicated defaults if the request did not include them):

**&#x2022;** protocol </br>
**&#x2022;** host </br>
**&#x2022;** port (default: 80 for HTTP requests, 443 for HTTPS requests) </br>
**&#x2022;** path (default: '/') </br>
**&#x2022;** query (default: '') </br>

~> **Note:** When action is `forward``target.id` should specify which pool the load balancer forwards the traffic to.
When action is `redirect`, `target.url` should specify the `url` and `target.http_status_code` to specify the code used in the redirect response.
Expand Down

0 comments on commit b38c98d

Please sign in to comment.