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 examples for L7-XLB Header & Parameter Based Routing #6699

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changelog/3703.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
```release-note:none
```
206 changes: 206 additions & 0 deletions google/resource_compute_url_map_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,212 @@ resource "google_compute_health_check" "default" {
`, context)
}

func TestAccComputeUrlMap_urlMapHeaderBasedRoutingExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeUrlMapDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeUrlMap_urlMapHeaderBasedRoutingExample(context),
},
{
ResourceName: "google_compute_url_map.urlmap",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeUrlMap_urlMapHeaderBasedRoutingExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_url_map" "urlmap" {
name = "urlmap%{random_suffix}"
description = "header-based routing example"
default_service = google_compute_backend_service.default.id

host_rule {
hosts = ["*"]
path_matcher = "allpaths"
}

path_matcher {
name = "allpaths"
default_service = google_compute_backend_service.default.id

route_rules {
priority = 1
service = google_compute_backend_service.service-a.id
match_rules {
prefix_match = "/"
ignore_case = true
header_matches {
header_name = "abtest"
exact_match = "a"
}
}
}
route_rules {
priority = 2
service = google_compute_backend_service.service-b.id
match_rules {
ignore_case = true
prefix_match = "/"
header_matches {
header_name = "abtest"
exact_match = "b"
}
}
}
}
}

resource "google_compute_backend_service" "default" {
name = "default%{random_suffix}"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-a" {
name = "tf-test-service-a%{random_suffix}"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-b" {
name = "tf-test-service-b%{random_suffix}"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_http_health_check" "default" {
name = "tf-test-health-check%{random_suffix}"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
`, context)
}

func TestAccComputeUrlMap_urlMapParameterBasedRoutingExample(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeUrlMapDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeUrlMap_urlMapParameterBasedRoutingExample(context),
},
{
ResourceName: "google_compute_url_map.urlmap",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccComputeUrlMap_urlMapParameterBasedRoutingExample(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_url_map" "urlmap" {
name = "urlmap%{random_suffix}"
description = "parameter-based routing example"
default_service = google_compute_backend_service.default.id

host_rule {
hosts = ["*"]
path_matcher = "allpaths"
}

path_matcher {
name = "allpaths"
default_service = google_compute_backend_service.default.id

route_rules {
priority = 1
service = google_compute_backend_service.service-a.id
match_rules {
prefix_match = "/"
ignore_case = true
query_parameter_matches {
name = "abtest"
exact_match = "a"
}
}
}
route_rules {
priority = 2
service = google_compute_backend_service.service-b.id
match_rules {
ignore_case = true
prefix_match = "/"
query_parameter_matches {
name = "abtest"
exact_match = "b"
}
}
}
}
}

resource "google_compute_backend_service" "default" {
name = "default%{random_suffix}"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-a" {
name = "tf-test-service-a%{random_suffix}"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-b" {
name = "tf-test-service-b%{random_suffix}"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_http_health_check" "default" {
name = "tf-test-health-check%{random_suffix}"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
`, context)
}

func testAccCheckComputeUrlMapDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
for name, rs := range s.RootModule().Resources {
Expand Down
168 changes: 168 additions & 0 deletions website/docs/r/compute_url_map.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,174 @@ resource "google_compute_health_check" "default" {
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=url_map_header_based_routing&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Url Map Header Based Routing


```hcl
resource "google_compute_url_map" "urlmap" {
name = "urlmap"
description = "header-based routing example"
default_service = google_compute_backend_service.default.id

host_rule {
hosts = ["*"]
path_matcher = "allpaths"
}

path_matcher {
name = "allpaths"
default_service = google_compute_backend_service.default.id

route_rules {
priority = 1
service = google_compute_backend_service.service-a.id
match_rules {
prefix_match = "/"
ignore_case = true
header_matches {
header_name = "abtest"
exact_match = "a"
}
}
}
route_rules {
priority = 2
service = google_compute_backend_service.service-b.id
match_rules {
ignore_case = true
prefix_match = "/"
header_matches {
header_name = "abtest"
exact_match = "b"
}
}
}
}
}

resource "google_compute_backend_service" "default" {
name = "default"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-a" {
name = "service-a"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-b" {
name = "service-b"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_http_health_check" "default" {
name = "health-check"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=url_map_parameter_based_routing&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## Example Usage - Url Map Parameter Based Routing


```hcl
resource "google_compute_url_map" "urlmap" {
name = "urlmap"
description = "parameter-based routing example"
default_service = google_compute_backend_service.default.id

host_rule {
hosts = ["*"]
path_matcher = "allpaths"
}

path_matcher {
name = "allpaths"
default_service = google_compute_backend_service.default.id

route_rules {
priority = 1
service = google_compute_backend_service.service-a.id
match_rules {
prefix_match = "/"
ignore_case = true
query_parameter_matches {
name = "abtest"
exact_match = "a"
}
}
}
route_rules {
priority = 2
service = google_compute_backend_service.service-b.id
match_rules {
ignore_case = true
prefix_match = "/"
query_parameter_matches {
name = "abtest"
exact_match = "b"
}
}
}
}
}

resource "google_compute_backend_service" "default" {
name = "default"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-a" {
name = "service-a"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_backend_service" "service-b" {
name = "service-b"
port_name = "http"
protocol = "HTTP"
timeout_sec = 10

health_checks = [google_compute_http_health_check.default.id]
}

resource "google_compute_http_health_check" "default" {
name = "health-check"
request_path = "/"
check_interval_sec = 1
timeout_sec = 1
}
```

## Argument Reference

Expand Down