From 17e4cb67ee8a5b1be1a99284cd377d54cb574efa Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 21 Jul 2022 11:20:18 -0700 Subject: [PATCH] Add new terraform sample - CDN XLB with Backend Bucket (#6252) (#4514) Signed-off-by: Modular Magician --- .changelog/6252.txt | 2 + ...e_compute_backend_bucket_generated_test.go | 145 ++++++++++++++++++ 2 files changed, 147 insertions(+) create mode 100644 .changelog/6252.txt diff --git a/.changelog/6252.txt b/.changelog/6252.txt new file mode 100644 index 0000000000..e4e22fc1e4 --- /dev/null +++ b/.changelog/6252.txt @@ -0,0 +1,2 @@ +```release-note:none +``` diff --git a/google-beta/resource_compute_backend_bucket_generated_test.go b/google-beta/resource_compute_backend_bucket_generated_test.go index c1b1a827a8..d0c1fb9afd 100644 --- a/google-beta/resource_compute_backend_bucket_generated_test.go +++ b/google-beta/resource_compute_backend_bucket_generated_test.go @@ -251,6 +251,151 @@ resource "google_storage_bucket" "image_bucket" { `, context) } +func TestAccComputeBackendBucket_externalCdnLbWithBackendBucketExample(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: testAccCheckComputeBackendBucketDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeBackendBucket_externalCdnLbWithBackendBucketExample(context), + }, + { + ResourceName: "google_compute_backend_bucket.default", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccComputeBackendBucket_externalCdnLbWithBackendBucketExample(context map[string]interface{}) string { + return Nprintf(` +# CDN load balancer with Cloud bucket as backend + + + +# VPC +resource "google_compute_network" "default" { + name = "tf-test-cdn-network%{random_suffix}" + auto_create_subnetworks = false +} + +# backend subnet +resource "google_compute_subnetwork" "default" { + name = "tf-test-cdn-subnet%{random_suffix}" + ip_cidr_range = "10.0.1.0/24" + region = "us-central1" + network = google_compute_network.default.id +} + +# reserve IP address +resource "google_compute_global_address" "default" { + name = "tf-test-cdn-static-ip%{random_suffix}" +} + +# forwarding rule +resource "google_compute_global_forwarding_rule" "default" { + name = "tf-test-cdn-forwarding-rule%{random_suffix}" + ip_protocol = "TCP" + load_balancing_scheme = "EXTERNAL" + port_range = "80" + target = google_compute_target_http_proxy.default.id + ip_address = google_compute_global_address.default.id +} + +# http proxy +resource "google_compute_target_http_proxy" "default" { + name = "tf-test-cdn-target-http-proxy%{random_suffix}" + url_map = google_compute_url_map.default.id +} + +# url map +resource "google_compute_url_map" "default" { + name = "tf-test-cdn-url-map%{random_suffix}" + default_service = google_compute_backend_bucket.default.id +} + +# backend bucket with CDN policy with default ttl settings +resource "google_compute_backend_bucket" "default" { + name = "tf-test-image-backend-bucket%{random_suffix}" + description = "Contains beautiful images" + bucket_name = google_storage_bucket.default.name + enable_cdn = true + cdn_policy { + cache_mode = "CACHE_ALL_STATIC" + client_ttl = 3600 + default_ttl = 3600 + max_ttl = 86400 + negative_caching = true + serve_while_stale = 86400 + } +} + +# Cloud Storage bucket +resource "google_storage_bucket" "default" { + name = "tf-test-cdn-backend-storage-bucket%{random_suffix}" + location = "US" + uniform_bucket_level_access = true + // delete bucket and contents on destroy. + force_destroy = true + // Assign specialty files + website { + main_page_suffix = "index.html" + not_found_page = "404.html" + } +} + +# make bucket public +resource "google_storage_bucket_iam_member" "default" { + bucket = google_storage_bucket.default.name + role = "roles/storage.objectViewer" + member = "allUsers" +} + +resource "google_storage_bucket_object" "index_page" { + name = "tf-test-index-page%{random_suffix}" + bucket = google_storage_bucket.default.name + content = <<-EOT + +

Congratulations on setting up Google Cloud CDN with Storage backend!

+ + EOT +} + +resource "google_storage_bucket_object" "error_page" { + name = "tf-test-404-page%{random_suffix}" + bucket = google_storage_bucket.default.name + content = <<-EOT + +

404 Error: Object you are looking for is no longer available!

+ + EOT +} + +# image object for testing, try to access http:///test.jpg +resource "google_storage_bucket_object" "test_image" { + name = "tf-test-test-object%{random_suffix}" +# Uncomment and add valid path to an object. +# source = "/path/to/an/object" +# content_type = "image/jpeg" + +# Delete after uncommenting above source and content_type attributes + content = "Data as string to be uploaded" + content_type = "text/plain" + + bucket = google_storage_bucket.default.name +} + +`, context) +} + func TestAccComputeBackendBucket_backendBucketBypassCacheExample(t *testing.T) { t.Parallel()