From c20a63bf44120127143a4536626bdfdc35160ff9 Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 4 Sep 2020 06:59:27 -0700 Subject: [PATCH] add test and fix delete default route functionality (#3948) (#7199) * add test and fix delete default route functionality * fix lint error * update to correct project Signed-off-by: Modular Magician --- .changelog/3948.txt | 3 +++ google/resource_compute_network.go | 7 +++-- google/resource_compute_network_test.go | 34 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 .changelog/3948.txt diff --git a/.changelog/3948.txt b/.changelog/3948.txt new file mode 100644 index 00000000000..1769baaa8ae --- /dev/null +++ b/.changelog/3948.txt @@ -0,0 +1,3 @@ +```release-note:bug +compute: fixed bug where `delete_default_routes_on_create=true` was not actually deleting the default routes on create. +``` diff --git a/google/resource_compute_network.go b/google/resource_compute_network.go index e04680d1f5a..e4b939095de 100644 --- a/google/resource_compute_network.go +++ b/google/resource_compute_network.go @@ -191,8 +191,11 @@ func resourceComputeNetworkCreate(d *schema.ResourceData, meta interface{}) erro if d.Get("delete_default_routes_on_create").(bool) { token := "" for paginate := true; paginate; { - networkLink := fmt.Sprintf("%s/%s", url, d.Get("name").(string)) - filter := fmt.Sprintf("(network=\"%s\") AND (destRange=\"0.0.0.0/0\")", networkLink) + network, err := config.clientCompute.Networks.Get(project, d.Get("name").(string)).Do() + if err != nil { + return fmt.Errorf("Error finding network in proj: %s", err) + } + filter := fmt.Sprintf("(network=\"%s\") AND (destRange=\"0.0.0.0/0\")", network.SelfLink) log.Printf("[DEBUG] Getting routes for network %q with filter '%q'", d.Get("name").(string), filter) resp, err := config.clientCompute.Routes.List(project).Filter(filter).Do() if err != nil { diff --git a/google/resource_compute_network_test.go b/google/resource_compute_network_test.go index a630249445a..c287916205f 100644 --- a/google/resource_compute_network_test.go +++ b/google/resource_compute_network_test.go @@ -127,6 +127,8 @@ func TestAccComputeNetwork_default_routing_mode(t *testing.T) { func TestAccComputeNetwork_networkDeleteDefaultRoute(t *testing.T) { t.Parallel() + var network compute.Network + vcrTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, @@ -134,6 +136,12 @@ func TestAccComputeNetwork_networkDeleteDefaultRoute(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccComputeNetwork_deleteDefaultRoute(randString(t, 10)), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeNetworkExists( + t, "google_compute_network.bar", &network), + testAccCheckComputeNetworkDefaultRoutesDeleted( + t, "google_compute_network.bar", &network), + ), }, }, }) @@ -168,6 +176,32 @@ func testAccCheckComputeNetworkExists(t *testing.T, n string, network *compute.N } } +func testAccCheckComputeNetworkDefaultRoutesDeleted(t *testing.T, n string, network *compute.Network) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.Attributes["name"] == "" { + return fmt.Errorf("No ID is set") + } + + config := googleProviderConfig(t) + + routes, err := config.clientCompute.Routes.List(config.Project).Filter(fmt.Sprintf("(network=\"%s\") AND (destRange=\"0.0.0.0/0\")", network.SelfLink)).Do() + if err != nil { + return err + } + + if len(routes.Items) > 0 { + return fmt.Errorf("Default routes were not deleted") + } + + return nil + } +} + func testAccCheckComputeNetworkIsAutoSubnet(t *testing.T, n string, network *compute.Network) resource.TestCheckFunc { return func(s *terraform.State) error { config := googleProviderConfig(t)