2014-08-26 06:41:15 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
2015-03-18 18:10:39 +01:00
|
|
|
"google.golang.org/api/compute/v1"
|
2014-08-26 06:41:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAccComputeNetwork_basic(t *testing.T) {
|
|
|
|
var network compute.Network
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckComputeNetworkDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccComputeNetwork_basic,
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckComputeNetworkExists(
|
|
|
|
"google_compute_network.foobar", &network),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeNetworkDestroy(s *terraform.State) error {
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
for _, rs := range s.RootModule().Resources {
|
2014-08-26 06:41:15 +02:00
|
|
|
if rs.Type != "google_compute_network" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := config.clientCompute.Networks.Get(
|
2014-09-17 02:16:53 +02:00
|
|
|
config.Project, rs.Primary.ID).Do()
|
2014-08-26 06:41:15 +02:00
|
|
|
if err == nil {
|
|
|
|
return fmt.Errorf("Network still exists")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckComputeNetworkExists(n string, network *compute.Network) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
2014-09-17 02:16:53 +02:00
|
|
|
rs, ok := s.RootModule().Resources[n]
|
2014-08-26 06:41:15 +02:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", n)
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
if rs.Primary.ID == "" {
|
2014-08-26 06:41:15 +02:00
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
config := testAccProvider.Meta().(*Config)
|
|
|
|
|
|
|
|
found, err := config.clientCompute.Networks.Get(
|
2014-09-17 02:16:53 +02:00
|
|
|
config.Project, rs.Primary.ID).Do()
|
2014-08-26 06:41:15 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-09-17 02:16:53 +02:00
|
|
|
if found.Name != rs.Primary.ID {
|
2014-08-26 06:41:15 +02:00
|
|
|
return fmt.Errorf("Network not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
*network = *found
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const testAccComputeNetwork_basic = `
|
|
|
|
resource "google_compute_network" "foobar" {
|
|
|
|
name = "terraform-test"
|
|
|
|
ipv4_range = "10.0.0.0/16"
|
|
|
|
}`
|