Skip to content

Commit

Permalink
Modify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zli82016 committed Sep 17, 2023
1 parent f4264e9 commit 025e43e
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 213 deletions.
19 changes: 18 additions & 1 deletion mmv1/third_party/terraform/acctest/bootstrap_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,17 +410,34 @@ func BootstrapSharedTestGlobalAddress(t *testing.T, testId, networkId string) st
return address.Name
}

// BootstrapSharedServiceNetworkingConnection will create a shared network
// if it hasn't been created in the test project, a global address
// if it hasn't been created in the test project, and a service networking connection
// if it hasn't been created in the test project.
//
// BootstrapSharedServiceNetworkingConnection returns a persistent compute network name
// for a test or set of tests.
//
// To delete a service networking conneciton, all of the service instances that use that connection
// must be deleted first. After the service instances are deleted, some service producers delay the deletion
// utnil a waiting period has passed. For example, after four days that you delete a SQL instance,
// the service networking connection can be deleted.
// That is the reason to use the shared service networking connection for thest resources.
// https://cloud.google.com/vpc/docs/configure-private-services-access#removing-connection
//
// testId specifies the test for which a shared network and a gobal address are used/initialized.
func BootstrapSharedServiceNetworkingConnection(t *testing.T, testId string) string {
parentService := "services/servicenetworking.googleapis.com"
project := envvar.GetTestProjectFromEnv()
projectNumber := envvar.GetTestProjectNumberFromEnv()

config := BootstrapConfig(t)
if config == nil {
return ""
}

networkName := BootstrapSharedTestNetwork(t, testId)
networkId := fmt.Sprintf("projects/%v/global/networks/%v", project, networkName)
networkId := fmt.Sprintf("projects/%v/global/networks/%v", projectNumber, networkName)
globalAddressName := BootstrapSharedTestGlobalAddress(t, testId, networkId)

readCall := config.NewServiceNetworkingClient(config.UserAgent).Services.Connections.List(parentService).Network(networkId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestAccAlloydbBackup_update(t *testing.T) {

random_suffix := acctest.RandString(t, 10)
context := map[string]interface{}{
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-network"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-backup-update-1"),
"random_suffix": random_suffix,
}

Expand Down Expand Up @@ -83,7 +83,7 @@ func TestAccAlloydbBackup_createBackupWithMandatoryFields(t *testing.T) {

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbbackup-mandatory"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-backup-mandatory-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestAccAlloydbBackup_usingCMEK(t *testing.T) {
t.Parallel()

context := map[string]interface{}{
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-backup-cmek"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-backup-cmek-1"),
"random_suffix": acctest.RandString(t, 10),
"key_name": "tf-test-key-" + acctest.RandString(t, 10),
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestAccAlloydbCluster_restore(t *testing.T) {

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-restore"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-restore-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccAlloydbInstance_update(t *testing.T) {
random_suffix := acctest.RandString(t, 10)
context := map[string]interface{}{
"random_suffix": random_suffix,
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-update"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-update-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestAccAlloydbInstance_createInstanceWithMandatoryFields(t *testing.T) {

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-mandatory"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-mandatory-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestAccAlloydbInstance_createInstanceWithMaximumFields(t *testing.T) {

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-maximum"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-maximum-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestAccAlloydbInstance_createPrimaryAndReadPoolInstance(t *testing.T) {

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-readpool"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-readpool-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestAccAlloydbInstance_updateDatabaseFlagInPrimaryInstance(t *testing.T) {

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydbinstance-updatedb"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "alloydb-instance-updatedb-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestAccCloudbuildWorkerPool_withNetwork(t *testing.T) {
context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"project": envvar.GetTestProjectFromEnv(),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "cloudbuild-workerpool"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "cloudbuild-workerpool-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAccCloudIdsEndpoint_basic(t *testing.T) {

context := map[string]interface{}{
"random_suffix": acctest.RandString(t, 10),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "cloud-ids-endpoint"),
"network_name": acctest.BootstrapSharedServiceNetworkingConnection(t, "cloud-ids-endpoint-1"),
}

acctest.VcrTest(t, resource.TestCase{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAccMemcacheInstance_update(t *testing.T) {

prefix := fmt.Sprintf("%d", acctest.RandInt(t))
name := fmt.Sprintf("tf-test-%s", prefix)
network := acctest.BootstrapSharedServiceNetworkingConnection(t, "memcache-instance-update")
network := acctest.BootstrapSharedServiceNetworkingConnection(t, "memcache-instance-update-1")

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (
"strings"
"time"

tpgcompute "github.com/hashicorp/terraform-provider-google/google/services/compute"
"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"google.golang.org/api/compute/v1"
"google.golang.org/api/servicenetworking/v1"
)

Expand Down Expand Up @@ -285,42 +287,41 @@ func resourceServiceNetworkingConnectionDelete(d *schema.ResourceData, meta inte
}

if err := ServiceNetworkingOperationWaitTime(config, op, "Delete Service Networking Connection", userAgent, project, d.Timeout(schema.TimeoutCreate)); err != nil {
return err
// obj := make(map[string]interface{})
// peering := d.Get("peering").(string)
// obj["name"] = peering
// url := fmt.Sprintf("%s%s/removePeering", config.ComputeBasePath, serviceNetworkingNetworkName)

// networkFieldValue, err := tpgresource.ParseNetworkFieldValue(network, d, config)
// if err != nil {
// return errwrap.Wrapf("Failed to retrieve network field value, err: {{err}}", err)
// }

// project := networkFieldValue.Project
// res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
// Config: config,
// Method: "POST",
// Project: project,
// RawURL: url,
// UserAgent: userAgent,
// Body: obj,
// Timeout: d.Timeout(schema.TimeoutDelete),
// })
// if err != nil {
// return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ServiceNetworkingConnection %q", d.Id()))
// }

// op := &compute.Operation{}
// err = tpgresource.Convert(res, op)
// if err != nil {
// return err
// }

// err = tpgcompute.ComputeOperationWaitTime(
// config, op, project, "Updating Network", userAgent, d.Timeout(schema.TimeoutDelete))
// if err != nil {
// return err
// }
obj := make(map[string]interface{})
peering := d.Get("peering").(string)
obj["name"] = peering
url := fmt.Sprintf("%s%s/removePeering", config.ComputeBasePath, serviceNetworkingNetworkName)

networkFieldValue, err := tpgresource.ParseNetworkFieldValue(network, d, config)
if err != nil {
return errwrap.Wrapf("Failed to retrieve network field value, err: {{err}}", err)
}

project := networkFieldValue.Project
res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "POST",
Project: project,
RawURL: url,
UserAgent: userAgent,
Body: obj,
Timeout: d.Timeout(schema.TimeoutDelete),
})
if err != nil {
return transport_tpg.HandleNotFoundError(err, d, fmt.Sprintf("ServiceNetworkingConnection %q", d.Id()))
}

op := &compute.Operation{}
err = tpgresource.Convert(res, op)
if err != nil {
return err
}

err = tpgcompute.ComputeOperationWaitTime(
config, op, project, "Updating Network", userAgent, d.Timeout(schema.TimeoutDelete))
if err != nil {
return err
}
}

d.SetId("")
Expand Down
Loading

0 comments on commit 025e43e

Please sign in to comment.