Skip to content

Commit

Permalink
add test and fix delete default route functionality (#3948) (#7199)
Browse files Browse the repository at this point in the history
* add test and fix delete default route functionality

* fix lint error

* update to correct project

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 4, 2020
1 parent 4999219 commit c20a63b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/3948.txt
Original file line number Diff line number Diff line change
@@ -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.
```
7 changes: 5 additions & 2 deletions google/resource_compute_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions google/resource_compute_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,21 @@ 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,
CheckDestroy: testAccCheckComputeNetworkDestroyProducer(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),
),
},
},
})
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit c20a63b

Please sign in to comment.