From ccd41ff3fb89d7cc93533a1e0b8207f7fba1069a Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Wed, 24 Feb 2021 17:00:50 -0800 Subject: [PATCH] Use bootstrapped service networking networks for redis, memcache tests (#4513) * Use bootstrapped service networking networks for redis, memcache tests * Fix sprintf args * Correct address name * Ignore other memcache tests --- mmv1/products/memcache/terraform.yaml | 6 +++ mmv1/products/redis/terraform.yaml | 8 ++++ .../examples/memcache_instance_basic.tf.erb | 19 ++++++--- .../redis_instance_private_service.tf.erb | 22 +++++++---- .../resource_memcache_instance_test.go.erb | 39 ++++++++++--------- 5 files changed, 63 insertions(+), 31 deletions(-) diff --git a/mmv1/products/memcache/terraform.yaml b/mmv1/products/memcache/terraform.yaml index 11bdc23bc9a6..c385bb8863b1 100644 --- a/mmv1/products/memcache/terraform.yaml +++ b/mmv1/products/memcache/terraform.yaml @@ -22,10 +22,16 @@ overrides: !ruby/object:Overrides::ResourceOverrides examples: - !ruby/object:Provider::Terraform::Examples min_version: beta + # Temporary as CI has used up servicenetworking quota + skip_vcr: true name: "memcache_instance_basic" primary_resource_id: "instance" vars: instance_name: "test-instance" + network_name: "test-network" + address_name: "address" + test_vars_overrides: + network_name: 'BootstrapSharedTestNetwork(t, "memcache-private")' properties: name: !ruby/object:Overrides::Terraform::PropertyOverride custom_flatten: 'templates/terraform/custom_flatten/name_from_self_link.erb' diff --git a/mmv1/products/redis/terraform.yaml b/mmv1/products/redis/terraform.yaml index 8c4aeda754a3..1ac586d91e8e 100644 --- a/mmv1/products/redis/terraform.yaml +++ b/mmv1/products/redis/terraform.yaml @@ -42,7 +42,15 @@ overrides: !ruby/object:Overrides::ResourceOverrides network_name: 'BootstrapSharedTestNetwork(t, "redis-full")' - !ruby/object:Provider::Terraform::Examples name: "redis_instance_private_service" + # Temporary for servicenetworking problems + skip_vcr: true primary_resource_id: "cache" + vars: + instance_name: "private-cache" + address_name: "address" + network_name: "redis-test-network" + test_vars_overrides: + network_name: 'BootstrapSharedTestNetwork(t, "redis-private")' properties: alternativeLocationId: !ruby/object:Overrides::Terraform::PropertyOverride default_from_api: true diff --git a/mmv1/templates/terraform/examples/memcache_instance_basic.tf.erb b/mmv1/templates/terraform/examples/memcache_instance_basic.tf.erb index 7125625de563..ce23929e1668 100644 --- a/mmv1/templates/terraform/examples/memcache_instance_basic.tf.erb +++ b/mmv1/templates/terraform/examples/memcache_instance_basic.tf.erb @@ -1,20 +1,27 @@ -resource "google_compute_network" "network" { - provider = google-beta - name = "tf-test%{random_suffix}" +// This example assumes this network already exists. +// The API creates a tenant network per network authorized for a +// Redis instance and that network is not deleted when the user-created +// network (authorized_network) is deleted, so this prevents issues +// with tenant network quota. +// If this network hasn't been created and you are using this example in your +// config, add an additional network resource or change +// this from "data"to "resource" +data "google_compute_network" "memcache_network" { + name = "<%= ctx[:vars]['network_name'] %>" } resource "google_compute_global_address" "service_range" { provider = google-beta - name = "tf-test%{random_suffix}" + name = "<%= ctx[:vars]['address_name'] %>" purpose = "VPC_PEERING" address_type = "INTERNAL" prefix_length = 16 - network = google_compute_network.network.id + network = data.google_compute_network.memcache_network.id } resource "google_service_networking_connection" "private_service_connection" { provider = google-beta - network = google_compute_network.network.id + network = data.google_compute_network.memcache_network.id service = "servicenetworking.googleapis.com" reserved_peering_ranges = [google_compute_global_address.service_range.name] } diff --git a/mmv1/templates/terraform/examples/redis_instance_private_service.tf.erb b/mmv1/templates/terraform/examples/redis_instance_private_service.tf.erb index e491379eeb4a..800df78e22aa 100644 --- a/mmv1/templates/terraform/examples/redis_instance_private_service.tf.erb +++ b/mmv1/templates/terraform/examples/redis_instance_private_service.tf.erb @@ -1,30 +1,38 @@ -resource "google_compute_network" "network" { - name = "tf-test%{random_suffix}" +// This example assumes this network already exists. +// The API creates a tenant network per network authorized for a +// Redis instance and that network is not deleted when the user-created +// network (authorized_network) is deleted, so this prevents issues +// with tenant network quota. +// If this network hasn't been created and you are using this example in your +// config, add an additional network resource or change +// this from "data"to "resource" +data "google_compute_network" "redis-network" { + name = "<%= ctx[:vars]['network_name'] %>" } resource "google_compute_global_address" "service_range" { - name = "tf-test%{random_suffix}" + name = "<%= ctx[:vars]['address_name'] %>" purpose = "VPC_PEERING" address_type = "INTERNAL" prefix_length = 16 - network = google_compute_network.network.id + network = data.google_compute_network.redis-network.id } resource "google_service_networking_connection" "private_service_connection" { - network = google_compute_network.network.id + network = data.google_compute_network.redis-network.id service = "servicenetworking.googleapis.com" reserved_peering_ranges = [google_compute_global_address.service_range.name] } resource "google_redis_instance" "<%= ctx[:primary_resource_id] %>" { - name = "tf-test%{random_suffix}" + name = "<%= ctx[:vars]['instance_name'] %>" tier = "STANDARD_HA" memory_size_gb = 1 location_id = "us-central1-a" alternative_location_id = "us-central1-f" - authorized_network = google_compute_network.network.id + authorized_network = data.google_compute_network.redis-network.id connect_mode = "PRIVATE_SERVICE_ACCESS" redis_version = "REDIS_4_0" diff --git a/mmv1/third_party/terraform/tests/resource_memcache_instance_test.go.erb b/mmv1/third_party/terraform/tests/resource_memcache_instance_test.go.erb index 16254f179582..3e5d1a054132 100644 --- a/mmv1/third_party/terraform/tests/resource_memcache_instance_test.go.erb +++ b/mmv1/third_party/terraform/tests/resource_memcache_instance_test.go.erb @@ -11,9 +11,12 @@ import ( func TestAccMemcacheInstance_update(t *testing.T) { t.Parallel() + // Temporary as CI has used up servicenetworking quota + skipIfVcr(t) prefix := fmt.Sprintf("%d", randInt(t)) name := fmt.Sprintf("tf-test-%s", prefix) + network := BootstrapSharedTestNetwork(t, "memcache-update") vcrTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -21,7 +24,7 @@ func TestAccMemcacheInstance_update(t *testing.T) { CheckDestroy: testAccCheckMemcacheInstanceDestroyProducer(t), Steps: []resource.TestStep{ { - Config: testAccMemcacheInstance_update(prefix, name), + Config: testAccMemcacheInstance_update(prefix, name, network), }, { ResourceName: "google_memcache_instance.test", @@ -29,7 +32,7 @@ func TestAccMemcacheInstance_update(t *testing.T) { ImportStateVerify: true, }, { - Config: testAccMemcacheInstance_update2(prefix, name), + Config: testAccMemcacheInstance_update2(prefix, name, network), }, { ResourceName: "google_memcache_instance.test", @@ -40,22 +43,18 @@ func TestAccMemcacheInstance_update(t *testing.T) { }) } -func testAccMemcacheInstance_update(prefix, name string) string { +func testAccMemcacheInstance_update(prefix, name, network string) string { return fmt.Sprintf(` -resource "google_compute_network" "network" { - name = "tf-test%s" -} - resource "google_compute_global_address" "service_range" { name = "tf-test%s" purpose = "VPC_PEERING" address_type = "INTERNAL" prefix_length = 16 - network = google_compute_network.network.id + network = data.google_compute_network.memcache_network.id } resource "google_service_networking_connection" "private_service_connection" { - network = google_compute_network.network.id + network = data.google_compute_network.memcache_network.id service = "servicenetworking.googleapis.com" reserved_peering_ranges = [google_compute_global_address.service_range.name] } @@ -78,25 +77,25 @@ resource "google_memcache_instance" "test" { } } } -`, prefix, prefix, name) -} -func testAccMemcacheInstance_update2(prefix, name string) string { - return fmt.Sprintf(` -resource "google_compute_network" "network" { - name = "tf-test%s" +data "google_compute_network" "memcache_network" { + name = "%s" +} +`, prefix, name, network) } +func testAccMemcacheInstance_update2(prefix, name, network string) string { + return fmt.Sprintf(` resource "google_compute_global_address" "service_range" { name = "tf-test%s" purpose = "VPC_PEERING" address_type = "INTERNAL" prefix_length = 16 - network = google_compute_network.network.id + network = data.google_compute_network.memcache_network.id } resource "google_service_networking_connection" "private_service_connection" { - network = google_compute_network.network.id + network = data.google_compute_network.memcache_network.id service = "servicenetworking.googleapis.com" reserved_peering_ranges = [google_compute_global_address.service_range.name] } @@ -119,6 +118,10 @@ resource "google_memcache_instance" "test" { } } } -`, prefix, prefix, name) + +data "google_compute_network" "memcache_network" { + name = "%s" +} +`, prefix, name, network) } <% end -%>