From 5135b731297e5db4861c55567b25c39281f52554 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Fri, 6 Jun 2025 21:47:17 +0000 Subject: [PATCH] Add headers, expectedOutputUrl, and expectedRedirectResponseCode fields to URL map (#14118) [upstream:7ab204c263691cd7a96daedcde947e3b904b7e5f] Signed-off-by: Modular Magician --- .../backing_file.tf | 15 ++++ url_map_test_expected_output_url/main.tf | 52 ++++++++++++ url_map_test_expected_output_url/motd | 7 ++ url_map_test_expected_output_url/tutorial.md | 79 +++++++++++++++++++ url_map_test_headers/backing_file.tf | 15 ++++ url_map_test_headers/main.tf | 58 ++++++++++++++ url_map_test_headers/motd | 7 ++ url_map_test_headers/tutorial.md | 79 +++++++++++++++++++ .../backing_file.tf | 15 ++++ url_map_test_redirect_response_code/main.tf | 73 +++++++++++++++++ url_map_test_redirect_response_code/motd | 7 ++ .../tutorial.md | 79 +++++++++++++++++++ 12 files changed, 486 insertions(+) create mode 100644 url_map_test_expected_output_url/backing_file.tf create mode 100644 url_map_test_expected_output_url/main.tf create mode 100644 url_map_test_expected_output_url/motd create mode 100644 url_map_test_expected_output_url/tutorial.md create mode 100644 url_map_test_headers/backing_file.tf create mode 100644 url_map_test_headers/main.tf create mode 100644 url_map_test_headers/motd create mode 100644 url_map_test_headers/tutorial.md create mode 100644 url_map_test_redirect_response_code/backing_file.tf create mode 100644 url_map_test_redirect_response_code/main.tf create mode 100644 url_map_test_redirect_response_code/motd create mode 100644 url_map_test_redirect_response_code/tutorial.md diff --git a/url_map_test_expected_output_url/backing_file.tf b/url_map_test_expected_output_url/backing_file.tf new file mode 100644 index 00000000..c60b1199 --- /dev/null +++ b/url_map_test_expected_output_url/backing_file.tf @@ -0,0 +1,15 @@ +# This file has some scaffolding to make sure that names are unique and that +# a region and zone are selected when you try to create your Terraform resources. + +locals { + name_suffix = "${random_pet.suffix.id}" +} + +resource "random_pet" "suffix" { + length = 2 +} + +provider "google" { + region = "us-central1" + zone = "us-central1-c" +} diff --git a/url_map_test_expected_output_url/main.tf b/url_map_test_expected_output_url/main.tf new file mode 100644 index 00000000..f350a5a0 --- /dev/null +++ b/url_map_test_expected_output_url/main.tf @@ -0,0 +1,52 @@ +resource "google_compute_health_check" "health-check-${local.name_suffix}" { + name = "health-check-${local.name_suffix}" + timeout_sec = 1 + check_interval_sec = 1 + + tcp_health_check { + port = "80" + } +} + +resource "google_compute_backend_service" "backend-${local.name_suffix}" { + name = "backend-${local.name_suffix}" + port_name = "http" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = [google_compute_health_check.health-check-${local.name_suffix}.id] +} + +resource "google_compute_url_map" "urlmap" { + name = "urlmap-${local.name_suffix}" + description = "URL map with expected output URL tests" + default_service = google_compute_backend_service.backend-${local.name_suffix}.id + + test { + description = "Test with expected output URL" + host = "example.com" + path = "/" + service = google_compute_backend_service.backend-${local.name_suffix}.id + + headers { + name = "User-Agent" + value = "TestBot/1.0" + } + + expected_output_url = "http://example.com/" + } + + test { + description = "Test API routing with expected output URL" + host = "api.example.com" + path = "/v1/users" + service = google_compute_backend_service.backend-${local.name_suffix}.id + + headers { + name = "Authorization" + value = "Bearer token123" + } + + expected_output_url = "http://api.example.com/v1/users" + } +} diff --git a/url_map_test_expected_output_url/motd b/url_map_test_expected_output_url/motd new file mode 100644 index 00000000..45a906e8 --- /dev/null +++ b/url_map_test_expected_output_url/motd @@ -0,0 +1,7 @@ +=== + +These examples use real resources that will be billed to the +Google Cloud Platform project you use - so make sure that you +run "terraform destroy" before quitting! + +=== diff --git a/url_map_test_expected_output_url/tutorial.md b/url_map_test_expected_output_url/tutorial.md new file mode 100644 index 00000000..f9085714 --- /dev/null +++ b/url_map_test_expected_output_url/tutorial.md @@ -0,0 +1,79 @@ +# Url Map Test Expected Output Url - Terraform + +## Setup + + + +Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform. + + + +Terraform provisions real GCP resources, so anything you create in this session will be billed against this project. + +## Terraforming! + +Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command +to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up +the project name from the environment variable. + +```bash +export GOOGLE_CLOUD_PROJECT={{project-id}} +``` + +After that, let's get Terraform started. Run the following to pull in the providers. + +```bash +terraform init +``` + +With the providers downloaded and a project set, you're ready to use Terraform. Go ahead! + +```bash +terraform apply +``` + +Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan. + +```bash +yes +``` + + +## Post-Apply + +### Editing your config + +Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed. + +```bash +terraform plan +``` + +So let's make a change! Try editing a number, or appending a value to the name in the editor. Then, +run a 'plan' again. + +```bash +terraform plan +``` + +Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes +at the 'yes' prompt. + +```bash +terraform apply +``` + +```bash +yes +``` + +## Cleanup + +Run the following to remove the resources Terraform provisioned: + +```bash +terraform destroy +``` +```bash +yes +``` diff --git a/url_map_test_headers/backing_file.tf b/url_map_test_headers/backing_file.tf new file mode 100644 index 00000000..c60b1199 --- /dev/null +++ b/url_map_test_headers/backing_file.tf @@ -0,0 +1,15 @@ +# This file has some scaffolding to make sure that names are unique and that +# a region and zone are selected when you try to create your Terraform resources. + +locals { + name_suffix = "${random_pet.suffix.id}" +} + +resource "random_pet" "suffix" { + length = 2 +} + +provider "google" { + region = "us-central1" + zone = "us-central1-c" +} diff --git a/url_map_test_headers/main.tf b/url_map_test_headers/main.tf new file mode 100644 index 00000000..9a54c2b3 --- /dev/null +++ b/url_map_test_headers/main.tf @@ -0,0 +1,58 @@ +resource "google_compute_health_check" "health-check-${local.name_suffix}" { + name = "health-check-${local.name_suffix}" + timeout_sec = 1 + check_interval_sec = 1 + + tcp_health_check { + port = "80" + } +} + +resource "google_compute_backend_service" "backend-${local.name_suffix}" { + name = "backend-${local.name_suffix}" + port_name = "http" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = [google_compute_health_check.health-check-${local.name_suffix}.id] +} + +resource "google_compute_url_map" "urlmap" { + name = "urlmap-${local.name_suffix}" + description = "URL map with test headers" + default_service = google_compute_backend_service.backend-${local.name_suffix}.id + + test { + description = "Test with custom headers" + host = "example.com" + path = "/" + service = google_compute_backend_service.backend-${local.name_suffix}.id + + headers { + name = "User-Agent" + value = "TestBot/1.0" + } + + headers { + name = "X-Custom-Header" + value = "test-value" + } + } + + test { + description = "Test with authorization headers" + host = "api.example.com" + path = "/v1/test" + service = google_compute_backend_service.backend-${local.name_suffix}.id + + headers { + name = "Authorization" + value = "Bearer token123" + } + + headers { + name = "Content-Type" + value = "application/json" + } + } +} diff --git a/url_map_test_headers/motd b/url_map_test_headers/motd new file mode 100644 index 00000000..45a906e8 --- /dev/null +++ b/url_map_test_headers/motd @@ -0,0 +1,7 @@ +=== + +These examples use real resources that will be billed to the +Google Cloud Platform project you use - so make sure that you +run "terraform destroy" before quitting! + +=== diff --git a/url_map_test_headers/tutorial.md b/url_map_test_headers/tutorial.md new file mode 100644 index 00000000..42be428c --- /dev/null +++ b/url_map_test_headers/tutorial.md @@ -0,0 +1,79 @@ +# Url Map Test Headers - Terraform + +## Setup + + + +Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform. + + + +Terraform provisions real GCP resources, so anything you create in this session will be billed against this project. + +## Terraforming! + +Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command +to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up +the project name from the environment variable. + +```bash +export GOOGLE_CLOUD_PROJECT={{project-id}} +``` + +After that, let's get Terraform started. Run the following to pull in the providers. + +```bash +terraform init +``` + +With the providers downloaded and a project set, you're ready to use Terraform. Go ahead! + +```bash +terraform apply +``` + +Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan. + +```bash +yes +``` + + +## Post-Apply + +### Editing your config + +Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed. + +```bash +terraform plan +``` + +So let's make a change! Try editing a number, or appending a value to the name in the editor. Then, +run a 'plan' again. + +```bash +terraform plan +``` + +Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes +at the 'yes' prompt. + +```bash +terraform apply +``` + +```bash +yes +``` + +## Cleanup + +Run the following to remove the resources Terraform provisioned: + +```bash +terraform destroy +``` +```bash +yes +``` diff --git a/url_map_test_redirect_response_code/backing_file.tf b/url_map_test_redirect_response_code/backing_file.tf new file mode 100644 index 00000000..c60b1199 --- /dev/null +++ b/url_map_test_redirect_response_code/backing_file.tf @@ -0,0 +1,15 @@ +# This file has some scaffolding to make sure that names are unique and that +# a region and zone are selected when you try to create your Terraform resources. + +locals { + name_suffix = "${random_pet.suffix.id}" +} + +resource "random_pet" "suffix" { + length = 2 +} + +provider "google" { + region = "us-central1" + zone = "us-central1-c" +} diff --git a/url_map_test_redirect_response_code/main.tf b/url_map_test_redirect_response_code/main.tf new file mode 100644 index 00000000..02296481 --- /dev/null +++ b/url_map_test_redirect_response_code/main.tf @@ -0,0 +1,73 @@ +resource "google_compute_health_check" "health-check-${local.name_suffix}" { + name = "health-check-${local.name_suffix}" + timeout_sec = 1 + check_interval_sec = 1 + + tcp_health_check { + port = "80" + } +} + +resource "google_compute_backend_service" "backend-${local.name_suffix}" { + name = "backend-${local.name_suffix}" + port_name = "http" + protocol = "HTTP" + timeout_sec = 10 + + health_checks = [google_compute_health_check.health-check-${local.name_suffix}.id] +} + +resource "google_compute_url_map" "urlmap" { + name = "urlmap-${local.name_suffix}" + description = "URL map with redirect response code tests" + default_service = google_compute_backend_service.backend-${local.name_suffix}.id + + host_rule { + hosts = ["example.com"] + path_matcher = "allpaths" + } + + path_matcher { + name = "allpaths" + default_service = google_compute_backend_service.backend-${local.name_suffix}.id + + path_rule { + paths = ["/redirect/*"] + url_redirect { + host_redirect = "newsite.com" + path_redirect = "/new-path/" + https_redirect = true + redirect_response_code = "MOVED_PERMANENTLY_DEFAULT" + strip_query = false + } + } + } + + test { + description = "Test redirect with expected response code" + host = "example.com" + path = "/redirect/old-page" + + headers { + name = "Referer" + value = "https://oldsite.com" + } + + expected_output_url = "https://newsite.com/new-path/" + expected_redirect_response_code = 301 + } + + test { + description = "Test another redirect scenario" + host = "example.com" + path = "/redirect/another-page" + + headers { + name = "User-Agent" + value = "TestBot/1.0" + } + + expected_output_url = "https://newsite.com/new-path/" + expected_redirect_response_code = 301 + } +} diff --git a/url_map_test_redirect_response_code/motd b/url_map_test_redirect_response_code/motd new file mode 100644 index 00000000..45a906e8 --- /dev/null +++ b/url_map_test_redirect_response_code/motd @@ -0,0 +1,7 @@ +=== + +These examples use real resources that will be billed to the +Google Cloud Platform project you use - so make sure that you +run "terraform destroy" before quitting! + +=== diff --git a/url_map_test_redirect_response_code/tutorial.md b/url_map_test_redirect_response_code/tutorial.md new file mode 100644 index 00000000..598d477d --- /dev/null +++ b/url_map_test_redirect_response_code/tutorial.md @@ -0,0 +1,79 @@ +# Url Map Test Redirect Response Code - Terraform + +## Setup + + + +Welcome to Terraform in Google Cloud Shell! We need you to let us know what project you'd like to use with Terraform. + + + +Terraform provisions real GCP resources, so anything you create in this session will be billed against this project. + +## Terraforming! + +Let's use {{project-id}} with Terraform! Click the Cloud Shell icon below to copy the command +to your shell, and then run it from the shell by pressing Enter/Return. Terraform will pick up +the project name from the environment variable. + +```bash +export GOOGLE_CLOUD_PROJECT={{project-id}} +``` + +After that, let's get Terraform started. Run the following to pull in the providers. + +```bash +terraform init +``` + +With the providers downloaded and a project set, you're ready to use Terraform. Go ahead! + +```bash +terraform apply +``` + +Terraform will show you what it plans to do, and prompt you to accept. Type "yes" to accept the plan. + +```bash +yes +``` + + +## Post-Apply + +### Editing your config + +Now you've provisioned your resources in GCP! If you run a "plan", you should see no changes needed. + +```bash +terraform plan +``` + +So let's make a change! Try editing a number, or appending a value to the name in the editor. Then, +run a 'plan' again. + +```bash +terraform plan +``` + +Afterwards you can run an apply, which implicitly does a plan and shows you the intended changes +at the 'yes' prompt. + +```bash +terraform apply +``` + +```bash +yes +``` + +## Cleanup + +Run the following to remove the resources Terraform provisioned: + +```bash +terraform destroy +``` +```bash +yes +```