From b38c98d3276d8310a9f0f1748d7c6740b6843d41 Mon Sep 17 00:00:00 2001 From: Deepak Selvakumar <77007253+deepaksibm@users.noreply.github.com> Date: Mon, 22 Jul 2024 18:28:23 +0530 Subject: [PATCH] feat(lb-parameterized-redirect): Update doc to specify parameterized url redirect (#5521) * feat(lb-parameterized-redirect): Updated doc to specify parameterized url support * update test case * update test case --- ...resource_ibm_is_lb_listener_policy_test.go | 81 +++++++++++++++++++ .../r/is_lb_listener_policy.html.markdown | 36 ++++++++- 2 files changed, 116 insertions(+), 1 deletion(-) diff --git a/ibm/service/vpc/resource_ibm_is_lb_listener_policy_test.go b/ibm/service/vpc/resource_ibm_is_lb_listener_policy_test.go index 75af6db710..89823aa520 100644 --- a/ibm/service/vpc/resource_ibm_is_lb_listener_policy_test.go +++ b/ibm/service/vpc/resource_ibm_is_lb_listener_policy_test.go @@ -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() @@ -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(` diff --git a/website/docs/r/is_lb_listener_policy.html.markdown b/website/docs/r/is_lb_listener_policy.html.markdown index 2d10caed99..bee5d92306 100644 --- a/website/docs/r/is_lb_listener_policy.html.markdown +++ b/website/docs/r/is_lb_listener_policy.html.markdown @@ -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 @@ -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): + + **•** protocol
+ **•** host
+ **•** port (default: 80 for HTTP requests, 443 for HTTPS requests)
+ **•** path (default: '/')
+ **•** query (default: '')
+ ~> **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.