Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"fmt"
"regexp"
"testing"
"regexp"

"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-google/google/acctest"
"github.com/hashicorp/terraform-provider-google/google/services/compute"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

func TestAccComputeRouterPeer_basic(t *testing.T) {
Expand Down Expand Up @@ -502,8 +501,6 @@ func testAccCheckComputeRouterPeerDestroyProducer(t *testing.T) func(s *terrafor
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)

routersService := compute.NewClient(config, config.UserAgent).Routers

for _, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_router" {
continue
Expand All @@ -521,7 +518,14 @@ func testAccCheckComputeRouterPeerDestroyProducer(t *testing.T) func(s *terrafor

routerName := rs.Primary.Attributes["router"]

_, err = routersService.Get(project, region, routerName).Do()
url := fmt.Sprintf("%sprojects/%s/regions/%s/routers/%s", config.ComputeBasePath, project, region, routerName)
_, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
})

if err == nil {
return fmt.Errorf("Error, Router %s in region %s still exists",
Expand All @@ -537,8 +541,6 @@ func testAccCheckComputeRouterPeerDelete(t *testing.T, n string) resource.TestCh
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)

routersService := compute.NewClient(config, config.UserAgent).Routers

for _, rs := range s.RootModule().Resources {
if rs.Type != "google_compute_router_peer" {
continue
Expand All @@ -557,17 +559,30 @@ func testAccCheckComputeRouterPeerDelete(t *testing.T, n string) resource.TestCh
name := rs.Primary.Attributes["name"]
routerName := rs.Primary.Attributes["router"]

router, err := routersService.Get(project, region, routerName).Do()
url := fmt.Sprintf("%sprojects/%s/regions/%s/routers/%s", config.ComputeBasePath, project, region, routerName)
router, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
})

if err != nil {
return fmt.Errorf("Error Reading Router %s: %s", routerName, err)
}

peers := router.BgpPeers
for _, peer := range peers {

if peer.Name == name {
return fmt.Errorf("Peer %s still exists on router %s/%s", name, region, router.Name)
routerResName, _ := router["name"].(string)
if rawPeers, ok := router["bgpPeers"].([]interface{}); ok {
for _, rawPeer := range rawPeers {
peer, ok := rawPeer.(map[string]interface{})
if !ok {
continue
}
peerName, _ := peer["name"].(string)
if peerName == name {
return fmt.Errorf("Peer %s still exists on router %s/%s", name, region, routerResName)
}
}
}
}
Expand Down Expand Up @@ -602,21 +617,34 @@ func testAccCheckComputeRouterPeerExists(t *testing.T, n string) resource.TestCh
name := rs.Primary.Attributes["name"]
routerName := rs.Primary.Attributes["router"]

routersService := compute.NewClient(config, config.UserAgent).Routers
router, err := routersService.Get(project, region, routerName).Do()
url := fmt.Sprintf("%sprojects/%s/regions/%s/routers/%s", config.ComputeBasePath, project, region, routerName)
router, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: config,
Method: "GET",
Project: project,
RawURL: url,
UserAgent: config.UserAgent,
})

if err != nil {
return fmt.Errorf("Error Reading Router %s: %s", routerName, err)
}

for _, peer := range router.BgpPeers {

if peer.Name == name {
return nil
routerResName, _ := router["name"].(string)
if rawPeers, ok := router["bgpPeers"].([]interface{}); ok {
for _, rawPeer := range rawPeers {
peer, ok := rawPeer.(map[string]interface{})
if !ok {
continue
}
peerName, _ := peer["name"].(string)
if peerName == name {
return nil
}
}
}

return fmt.Errorf("Peer %s not found for router %s", name, router.Name)
return fmt.Errorf("Peer %s not found for router %s", name, routerResName)
}
}

Expand Down
Loading