From 34f7d16a8442f09aa4a52e0ceafb8dae73f89b08 Mon Sep 17 00:00:00 2001 From: Stephen Lewis Date: Wed, 13 Mar 2024 09:25:19 -0700 Subject: [PATCH] force missing tests to run - take 2 --- mmv1/products/alloydb/Cluster.yaml | 4 + .../resource_alloydb_cluster_restore_test.go | 506 ----- .../resource_alloydb_cluster_sweeper.go.erb | 135 -- ...resource_alloydb_secondary_cluster_test.go | 1744 ----------------- 4 files changed, 4 insertions(+), 2385 deletions(-) delete mode 100644 mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_restore_test.go delete mode 100644 mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_sweeper.go.erb delete mode 100644 mmv1/third_party/terraform/services/alloydb/resource_alloydb_secondary_cluster_test.go diff --git a/mmv1/products/alloydb/Cluster.yaml b/mmv1/products/alloydb/Cluster.yaml index 8e2327a9b9b4..cac4579cc2f9 100644 --- a/mmv1/products/alloydb/Cluster.yaml +++ b/mmv1/products/alloydb/Cluster.yaml @@ -135,6 +135,10 @@ properties: output: true description: | The system-generated UID of the resource. + - !ruby/object:Api::Type::String + name: 'forceMissingTestsCheck' + description: | + Test field - !ruby/object:Api::Type::KeyValueLabels name: 'labels' description: 'User-defined labels for the alloydb cluster.' diff --git a/mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_restore_test.go b/mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_restore_test.go deleted file mode 100644 index 5288d8ea62f8..000000000000 --- a/mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_restore_test.go +++ /dev/null @@ -1,506 +0,0 @@ -package alloydb_test - -import ( - "regexp" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-provider-google/google/acctest" -) - -/* - * Restore tests are kept separate from other cluster tests because they require an instance and a backup to exist - */ - -// Restore tests depend on instances and backups being taken, which can take up to 10 minutes. Since the instance doesn't change in between tests, -// we condense everything into individual test cases. -func TestAccAlloydbCluster_restore(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-restore-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbClusterAndInstanceAndBackup(context), - }, - { - ResourceName: "google_alloydb_cluster.source", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "cluster_id", "location"}, - }, - { - // Invalid input check - cannot pass in both sources - Config: testAccAlloydbClusterAndInstanceAndBackup_OnlyOneSourceAllowed(context), - ExpectError: regexp.MustCompile("\"restore_continuous_backup_source\": conflicts with restore_backup_source"), - }, - { - // Invalid input check - both source cluster and point in time are required - Config: testAccAlloydbClusterAndInstanceAndBackup_SourceClusterAndPointInTimeRequired(context), - ExpectError: regexp.MustCompile("The argument \"point_in_time\" is required, but no definition was found."), - }, - { - // Validate backup restore succeeds - Config: testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackup(context), - }, - { - ResourceName: "google_alloydb_cluster.restored_from_backup", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "cluster_id", "location", "restore_backup_source"}, - }, - { - // Validate PITR succeeds - Config: testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime(context), - }, - { - ResourceName: "google_alloydb_cluster.restored_from_point_in_time", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "cluster_id", "location", "restore_continuous_backup_source"}, - }, - { - // Make sure updates work without recreating the clusters - Config: testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime_AllowedUpdate(context), - }, - { - Config: testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime_NotAllowedUpdate(context), - ExpectError: regexp.MustCompile("the plan calls for this resource to be\ndestroyed"), - }, - { - Config: testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime_AllowDestroy(context), - }, - }, - }) -} - -func testAccAlloydbClusterAndInstanceAndBackup(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Cannot restore if multiple sources are present -func testAccAlloydbClusterAndInstanceAndBackup_OnlyOneSourceAllowed(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_cluster" "restored" { - cluster_id = "tf-test-alloydb-backup-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_backup_source { - backup_name = google_alloydb_backup.default.name - } - restore_continuous_backup_source { - cluster = google_alloydb_cluster.source.name - point_in_time = google_alloydb_backup.default.update_time - } - - lifecycle { - prevent_destroy = true - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Cannot restore if multiple sources are present -func testAccAlloydbClusterAndInstanceAndBackup_SourceClusterAndPointInTimeRequired(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_cluster" "restored" { - cluster_id = "tf-test-alloydb-backup-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - - restore_continuous_backup_source { - cluster = google_alloydb_cluster.source.name - } - - lifecycle { - prevent_destroy = true - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -func testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackup(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_cluster" "restored_from_backup" { - cluster_id = "tf-test-alloydb-backup-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_backup_source { - backup_name = google_alloydb_backup.default.name - } - - lifecycle { - prevent_destroy = true - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// The source cluster, instance, and backup should all exist prior to this being invoked. Otherwise the PITR restore will not succeed -// due to the time being too early. -func testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_cluster" "restored_from_backup" { - cluster_id = "tf-test-alloydb-backup-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_backup_source { - backup_name = google_alloydb_backup.default.name - } - - lifecycle { - prevent_destroy = true - } -} - -resource "google_alloydb_cluster" "restored_from_point_in_time" { - cluster_id = "tf-test-alloydb-pitr-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_continuous_backup_source { - cluster = google_alloydb_cluster.source.name - point_in_time = google_alloydb_backup.default.update_time - } - - lifecycle { - prevent_destroy = true - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This updates the PITR and backup restored clusters by adding a continuous backup configuration. This can be done in place -// and does not require re-creating the cluster from scratch. -func testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime_AllowedUpdate(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_cluster" "restored_from_backup" { - cluster_id = "tf-test-alloydb-backup-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_backup_source { - backup_name = google_alloydb_backup.default.name - } - - continuous_backup_config { - enabled = true - recovery_window_days = 20 - } - - lifecycle { - prevent_destroy = true - } -} - -resource "google_alloydb_cluster" "restored_from_point_in_time" { - cluster_id = "tf-test-alloydb-pitr-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_continuous_backup_source { - cluster = google_alloydb_cluster.source.name - point_in_time = google_alloydb_backup.default.update_time - } - - continuous_backup_config { - enabled = true - recovery_window_days = 20 - } - - lifecycle { - prevent_destroy = true - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Updating the source cluster, point in time, or source backup are not allowed. This type of operation would -// require deleting and recreating the cluster -func testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime_NotAllowedUpdate(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_backup" "default2" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup2-%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_cluster" "restored_from_backup" { - cluster_id = "tf-test-alloydb-backup-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_backup_source { - backup_name = google_alloydb_backup.default2.name - } - - continuous_backup_config { - enabled = true - recovery_window_days = 20 - } - - lifecycle { - prevent_destroy = true - } - - depends_on = [google_alloydb_backup.default2] -} - -resource "google_alloydb_cluster" "restored_from_point_in_time" { - cluster_id = "tf-test-alloydb-pitr-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_continuous_backup_source { - cluster = google_alloydb_cluster.restored_from_backup.name - point_in_time = google_alloydb_backup.default.update_time - } - - continuous_backup_config { - enabled = true - recovery_window_days = 20 - } - - lifecycle { - prevent_destroy = true - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// The source cluster, instance, and backup should all exist prior to this being invoked. Otherwise the PITR restore will not succeed -// due to the time being too early. -func testAccAlloydbClusterAndInstanceAndBackup_RestoredFromBackupAndRestoredFromPointInTime_AllowDestroy(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "source" { - cluster_id = "tf-test-alloydb-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "source" { - cluster = google_alloydb_cluster.source.name - instance_id = "tf-test-alloydb-instance%{random_suffix}" - instance_type = "PRIMARY" -} - -resource "google_alloydb_backup" "default" { - location = "us-central1" - backup_id = "tf-test-alloydb-backup%{random_suffix}" - cluster_name = google_alloydb_cluster.source.name - - depends_on = [google_alloydb_instance.source] -} - -resource "google_alloydb_cluster" "restored_from_backup" { - cluster_id = "tf-test-alloydb-backup-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_backup_source { - backup_name = google_alloydb_backup.default.name - } -} - -resource "google_alloydb_cluster" "restored_from_point_in_time" { - cluster_id = "tf-test-alloydb-pitr-restored-cluster-%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id - restore_continuous_backup_source { - cluster = google_alloydb_cluster.source.name - point_in_time = google_alloydb_backup.default.update_time - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} diff --git a/mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_sweeper.go.erb b/mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_sweeper.go.erb deleted file mode 100644 index f801542b1d17..000000000000 --- a/mmv1/third_party/terraform/services/alloydb/resource_alloydb_cluster_sweeper.go.erb +++ /dev/null @@ -1,135 +0,0 @@ -<% autogen_exception -%> -package alloydb - -import ( - "context" - "log" - "strings" - "testing" - - "github.com/hashicorp/terraform-provider-google/google/envvar" - "github.com/hashicorp/terraform-provider-google/google/sweeper" - "github.com/hashicorp/terraform-provider-google/google/tpgresource" - transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport" -) - -func init() { - sweeper.AddTestSweepers("AlloydbCluster", testSweepAlloydbCluster) -} - -// At the time of writing, the CI only passes us-central1 as the region -func testSweepAlloydbCluster(region string) error { - resourceName := "AlloydbCluster" - log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) - - config, err := sweeper.SharedConfigForRegion(region) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) - return err - } - - err = config.LoadAndValidate(context.Background()) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) - return err - } - - t := &testing.T{} - billingId := envvar.GetTestBillingAccountFromEnv(t) - - // Setup variables to replace in list template - d := &tpgresource.ResourceDataMock{ - FieldsInSchema: map[string]interface{}{ - "project": config.Project, - "region": region, - "location": region, - "zone": "-", - "billing_account": billingId, - }, - } - -<% unless version == 'ga' -%> - listTemplate := strings.Split("https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/{{location}}/clusters", "?")[0] -<% else -%> - listTemplate := strings.Split("https://alloydb.googleapis.com/v1/projects/{{project}}/locations/{{location}}/clusters", "?")[0] -<% end -%> - listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) - return nil - } - - res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "GET", - Project: config.Project, - RawURL: listUrl, - UserAgent: config.UserAgent, - }) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) - return nil - } - - resourceList, ok := res["clusters"] - if !ok { - log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") - return nil - } - - rl := resourceList.([]interface{}) - - log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) - // Keep count of items that aren't sweepable for logging. - nonPrefixCount := 0 - for _, ri := range rl { - obj := ri.(map[string]interface{}) - var name string - // Id detected in the delete URL, attempt to use id. - if obj["id"] != nil { - name = tpgresource.GetResourceNameFromSelfLink(obj["id"].(string)) - } else if obj["name"] != nil { - name = tpgresource.GetResourceNameFromSelfLink(obj["name"].(string)) - } else { - log.Printf("[INFO][SWEEPER_LOG] %s resource name and id were nil", resourceName) - return nil - } - // Skip resources that shouldn't be sweeped - if !sweeper.IsSweepableTestResource(name) { - nonPrefixCount++ - continue - } - - <% unless version == 'ga' -%> - deleteTemplate := "https://alloydb.googleapis.com/v1beta/projects/{{project}}/locations/{{location}}/clusters/{{cluster_id}}" - <% else -%> - deleteTemplate := "https://alloydb.googleapis.com/v1/projects/{{project}}/locations/{{location}}/clusters/{{cluster_id}}" - <% end -%> - deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) - return nil - } - deleteUrl = deleteUrl + name + "?force=true" - - // Don't wait on operations as we may have a lot to delete - _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "DELETE", - Project: config.Project, - RawURL: deleteUrl, - UserAgent: config.UserAgent, - }) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) - } else { - log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) - } - } - - if nonPrefixCount > 0 { - log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) - } - - return nil -} diff --git a/mmv1/third_party/terraform/services/alloydb/resource_alloydb_secondary_cluster_test.go b/mmv1/third_party/terraform/services/alloydb/resource_alloydb_secondary_cluster_test.go deleted file mode 100644 index 4ea15246ddbd..000000000000 --- a/mmv1/third_party/terraform/services/alloydb/resource_alloydb_secondary_cluster_test.go +++ /dev/null @@ -1,1744 +0,0 @@ -package alloydb_test - -import ( - "regexp" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-provider-google/google/acctest" -) - -// The cluster creation should succeed with minimal number of arguments -func TestAccAlloydbCluster_secondaryClusterMandatoryFields(t *testing.T) { - t.Parallel() - // https://github.com/hashicorp/terraform-provider-google/issues/16231 - acctest.SkipIfVcr(t) - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterMandatoryFields(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterMandatoryFields(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network = data.google_compute_network.default.id - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Validation test to ensure proper error is raised if create secondary cluster is called without any secondary_config field -func TestAccAlloydbCluster_secondaryClusterMissingSecondaryConfig(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterMissingSecondaryConfig(context), - ExpectError: regexp.MustCompile("Error creating cluster. Can not create secondary cluster without secondary_config field."), - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterMissingSecondaryConfig(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network = data.google_compute_network.default.id - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Validation test to ensure proper error is raised if secondary_config field is provided but no cluster_type is specified. -func TestAccAlloydbCluster_secondaryClusterDefinedSecondaryConfigButMissingClusterTypeSecondary(t *testing.T) { - t.Parallel() - - // Unskip in https://github.com/hashicorp/terraform-provider-google/issues/16231 - acctest.SkipIfVcr(t) - - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterDefinedSecondaryConfigButMissingClusterTypeSecondary(context), - ExpectError: regexp.MustCompile("Error creating cluster. Add {cluster_type: \"SECONDARY\"} if attempting to create a secondary cluster, otherwise remove the secondary_config."), - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterDefinedSecondaryConfigButMissingClusterTypeSecondary(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network = data.google_compute_network.default.id - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Validation test to ensure proper error is raised if secondary_config field is provided but cluster_type is primary -func TestAccAlloydbCluster_secondaryClusterDefinedSecondaryConfigButClusterTypeIsPrimary(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterDefinedSecondaryConfigButClusterTypeIsPrimary(context), - ExpectError: regexp.MustCompile("Error creating cluster. Add {cluster_type: \"SECONDARY\"} if attempting to create a secondary cluster, otherwise remove the secondary_config."), - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterDefinedSecondaryConfigButClusterTypeIsPrimary(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This test passes if secondary cluster can be updated -func TestAccAlloydbCluster_secondaryClusterUpdate(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterMandatoryFields(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterUpdate(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterUpdate(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network = data.google_compute_network.default.id - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - labels = { - foo = "bar" - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Test if adding automatedBackupPolicy throws an error as it can not be enabled on secondary cluster -func TestAccAlloydbCluster_secondaryClusterAddAutomatedBackupPolicy(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - "hour": 23, - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterMandatoryFields(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - // Invalid input check - can not add automated backup policy to a secondary cluster - Config: testAccAlloydbCluster_secondaryClusterAddAutomatedBackupPolicy(context), - ExpectError: regexp.MustCompile("cannot enable automated backups on secondary cluster until it is promoted"), - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterAddAutomatedBackupPolicy(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network = data.google_compute_network.default.id - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - automated_backup_policy { - location = "us-central1" - backup_window = "1800s" - enabled = true - - weekly_schedule { - days_of_week = ["MONDAY"] - - start_times { - hours = %{hour} - minutes = 0 - seconds = 0 - nanos = 0 - } - } - - quantity_based_retention { - count = 1 - } - - labels = { - test = "tf-test-alloydb-secondary-cluster%{random_suffix}" - } - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -func TestAccAlloydbCluster_secondaryClusterUsingCMEK(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - "key_name": "tf-test-key-" + acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterUsingCMEK(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterUsingCMEK(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network = data.google_compute_network.default.id - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - encryption_config { - kms_key_name = google_kms_crypto_key.key.id - } - - depends_on = [ - google_alloydb_instance.primary, - google_kms_crypto_key_iam_member.crypto_key - ] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} - -resource "google_kms_key_ring" "keyring" { - name = "%{key_name}" - location = "us-east1" -} - -resource "google_kms_crypto_key" "key" { - name = "%{key_name}" - key_ring = google_kms_key_ring.keyring.id -} - -resource "google_kms_crypto_key_iam_member" "crypto_key" { - crypto_key_id = google_kms_crypto_key.key.id - role = "roles/cloudkms.cryptoKeyEncrypterDecrypter" - member = "serviceAccount:service-${data.google_project.project.number}@gcp-sa-alloydb.iam.gserviceaccount.com" -} -`, context) -} - -// Ensures secondary cluster creation works with networkConfig. -func TestAccAlloydbCluster_secondaryClusterWithNetworkConfig(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterWithNetworkConfig(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterWithNetworkConfig(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - } -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - } - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// Ensures secondary cluster creation works with networkConfig and a specified allocated IP range. -func TestAccAlloydbCluster_secondaryClusterWithNetworkConfigAndAllocatedIPRange(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "address_name": acctest.BootstrapSharedTestGlobalAddress(t, "alloydbinstance-network-config-1"), - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "random_suffix": acctest.RandString(t, 10), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbCluster_secondaryClusterWithNetworkConfigAndAllocatedIPRange(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterWithNetworkConfigAndAllocatedIPRange(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - allocated_ip_range = data.google_compute_global_address.private_ip_alloc.name - } -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-east1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - allocated_ip_range = data.google_compute_global_address.private_ip_alloc.name - } - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - depends_on = [google_alloydb_instance.primary] -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} - -data "google_compute_global_address" "private_ip_alloc" { - name = "%{address_name}" -} -`, context) -} - -// This test passes if secondary cluster can be promoted -func TestAccAlloydbCluster_secondaryClusterPromote(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "secondary_cluster_location": "us-east1", - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstance(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromote(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbInstance_secondaryClusterWithInstance(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - deletion_policy = "FORCE" - - depends_on = [google_alloydb_instance.primary] -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -func testAccAlloydbCluster_secondaryClusterPromote(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This test passes if secondary cluster can be promoted and updated simultaneously -func TestAccAlloydbCluster_secondaryClusterPromoteAndSimultaneousUpdate(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "secondary_cluster_location": "us-east1", - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstance(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteAndSimultaneousUpdate(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterPromoteAndSimultaneousUpdate(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = true - } - - labels = { - foo = "bar" - } -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This test passes if secondary cluster can be promoted and the original primary can be deleted after promotion -func TestAccAlloydbCluster_secondaryClusterPromoteAndDeleteOriginalPrimary(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "secondary_cluster_location": "us-east1", - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstance(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromote(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteAndDeleteOriginalPrimary(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterPromoteAndDeleteOriginalPrimary(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This test passes if the promoted secondary cluster can be updated -func TestAccAlloydbCluster_secondaryClusterPromoteAndUpdate(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "secondary_cluster_location": "us-east1", - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstance(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromote(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteAndUpdate(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterPromoteAndUpdate(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } - - labels = { - foo = "bar" - } - -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This test passes if secondary cluster can be promoted with networkConfig and a specified allocated IP range -func TestAccAlloydbCluster_secondaryClusterPromoteWithNetworkConfigAndAllocatedIPRange(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "address_name": acctest.BootstrapSharedTestGlobalAddress(t, "alloydbinstance-network-config-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstanceAndNetworkConfigAndAllocatedIPRange(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteWithNetworkConfigAndAllocatedIPRange(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbInstance_secondaryClusterWithInstanceAndNetworkConfigAndAllocatedIPRange(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - allocated_ip_range = data.google_compute_global_address.private_ip_alloc.name - } -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-south1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - allocated_ip_range = data.google_compute_global_address.private_ip_alloc.name - } - cluster_type = "SECONDARY" - - continuous_backup_config { - enabled = false - } - - secondary_config { - primary_cluster_name = google_alloydb_cluster.primary.name - } - - deletion_policy = "FORCE" - - depends_on = [google_alloydb_instance.primary] -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} - -data "google_compute_global_address" "private_ip_alloc" { - name = "%{address_name}" -} -`, context) -} - -func testAccAlloydbCluster_secondaryClusterPromoteWithNetworkConfigAndAllocatedIPRange(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - allocated_ip_range = data.google_compute_global_address.private_ip_alloc.name - } -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "us-south1" - network_config { - network = "projects/${data.google_project.project.number}/global/networks/${data.google_compute_network.default.name}" - allocated_ip_range = data.google_compute_global_address.private_ip_alloc.name - } - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} - -data "google_compute_global_address" "private_ip_alloc" { - name = "%{address_name}" -} -`, context) -} - -// This test passes if automated backup policy and inital user can be added and deleted from the promoted secondary cluster -func TestAccAlloydbCluster_secondaryClusterPromoteAndAddAndDeleteAutomatedBackupPolicyAndInitialUser(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "secondary_cluster_location": "us-south1", - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - "hour": 23, - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstance(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromote(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteAndAddAutomatedBackupPolicyAndInitialUser(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromote(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterPromoteAndAddAutomatedBackupPolicyAndInitialUser(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } - - initial_user { - user = "tf-test-alloydb-secondary-cluster%{random_suffix}" - password = "tf-test-alloydb-secondary-cluster%{random_suffix}" - } - - automated_backup_policy { - location = "%{secondary_cluster_location}" - backup_window = "1800s" - enabled = true - - weekly_schedule { - days_of_week = ["MONDAY"] - - start_times { - hours = %{hour} - minutes = 0 - seconds = 0 - nanos = 0 - } - } - - quantity_based_retention { - count = 1 - } - - labels = { - test = "tf-test-alloydb-secondary-cluster%{random_suffix}" - } - } -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This test passes if time based retention policy can be added and deleted from the promoted secondary cluster -func TestAccAlloydbCluster_secondaryClusterPromoteAndDeleteTimeBasedRetentionPolicy(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "secondary_cluster_location": "us-south1", - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstance(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromote(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteWithTimeBasedRetentionPolicy(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteWithoutTimeBasedRetentionPolicy(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterPromoteWithTimeBasedRetentionPolicy(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } - - initial_user { - user = "tf-test-alloydb-secondary-cluster%{random_suffix}" - password = "tf-test-alloydb-secondary-cluster%{random_suffix}" - } - - automated_backup_policy { - location = "%{secondary_cluster_location}" - backup_window = "1800s" - enabled = true - - weekly_schedule { - days_of_week = ["MONDAY"] - - start_times { - hours = 23 - minutes = 0 - seconds = 0 - nanos = 0 - } - } - time_based_retention { - retention_period = "4.5s" - } - } - lifecycle { - ignore_changes = [ - automated_backup_policy[0].time_based_retention - ] - } -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -func testAccAlloydbCluster_secondaryClusterPromoteWithoutTimeBasedRetentionPolicy(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = false - } - - initial_user { - user = "tf-test-alloydb-secondary-cluster%{random_suffix}" - password = "tf-test-alloydb-secondary-cluster%{random_suffix}" - } - - automated_backup_policy { - location = "%{secondary_cluster_location}" - backup_window = "1800s" - enabled = true - - weekly_schedule { - days_of_week = ["MONDAY"] - - start_times { - hours = 23 - minutes = 0 - seconds = 0 - nanos = 0 - } - } - } - lifecycle { - ignore_changes = [ - automated_backup_policy[0].time_based_retention - ] - } -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -} - -// This test passes if continuous backup config can be enabled in the promoted secondary cluster -func TestAccAlloydbCluster_secondaryClusterPromoteAndAddContinuousBackupConfig(t *testing.T) { - t.Parallel() - - context := map[string]interface{}{ - "random_suffix": acctest.RandString(t, 10), - "secondary_cluster_location": "us-south1", - "network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-network-config-1"), - } - - acctest.VcrTest(t, resource.TestCase{ - PreCheck: func() { acctest.AccTestPreCheck(t) }, - ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), - CheckDestroy: testAccCheckAlloydbClusterDestroyProducer(t), - Steps: []resource.TestStep{ - { - Config: testAccAlloydbInstance_secondaryClusterWithInstance(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromote(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - { - Config: testAccAlloydbCluster_secondaryClusterPromoteAndAddContinuousBackupConfig(context), - }, - { - ResourceName: "google_alloydb_cluster.secondary", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"initial_user", "restore_backup_source", "restore_continuous_backup_source", "cluster_id", "location", "deletion_policy", "labels", "annotations", "terraform_labels", "reconciling"}, - }, - }, - }) -} - -func testAccAlloydbCluster_secondaryClusterPromoteAndAddContinuousBackupConfig(context map[string]interface{}) string { - return acctest.Nprintf(` -resource "google_alloydb_cluster" "primary" { - cluster_id = "tf-test-alloydb-primary-cluster%{random_suffix}" - location = "us-central1" - network = data.google_compute_network.default.id -} - -resource "google_alloydb_instance" "primary" { - cluster = google_alloydb_cluster.primary.name - instance_id = "tf-test-alloydb-primary-instance%{random_suffix}" - instance_type = "PRIMARY" - - machine_config { - cpu_count = 2 - } -} - -resource "google_alloydb_cluster" "secondary" { - cluster_id = "tf-test-alloydb-secondary-cluster%{random_suffix}" - location = "%{secondary_cluster_location}" - network = data.google_compute_network.default.id - cluster_type = "PRIMARY" - - continuous_backup_config { - enabled = true - recovery_window_days = 14 - } - -} - -resource "google_alloydb_instance" "secondary" { - cluster = google_alloydb_cluster.secondary.name - instance_id = "tf-test-alloydb-secondary-instance%{random_suffix}" - instance_type = google_alloydb_cluster.secondary.cluster_type - - machine_config { - cpu_count = 2 - } - - lifecycle { - ignore_changes = [instance_type] - } -} - -data "google_project" "project" {} - -data "google_compute_network" "default" { - name = "%{network_name}" -} -`, context) -}