github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/google/resource_compute_subnetwork_test.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/hashicorp/terraform/helper/acctest" 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/terraform" 10 "google.golang.org/api/compute/v1" 11 ) 12 13 func TestAccComputeSubnetwork_basic(t *testing.T) { 14 var subnetwork1 compute.Subnetwork 15 var subnetwork2 compute.Subnetwork 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckComputeSubnetworkDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccComputeSubnetwork_basic, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckComputeSubnetworkExists( 26 "google_compute_subnetwork.network-ref-by-url", &subnetwork1), 27 testAccCheckComputeSubnetworkExists( 28 "google_compute_subnetwork.network-ref-by-name", &subnetwork2), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckComputeSubnetworkDestroy(s *terraform.State) error { 36 config := testAccProvider.Meta().(*Config) 37 38 for _, rs := range s.RootModule().Resources { 39 if rs.Type != "google_compute_subnetwork" { 40 continue 41 } 42 43 region, subnet_name := splitSubnetID(rs.Primary.ID) 44 _, err := config.clientCompute.Subnetworks.Get( 45 config.Project, region, subnet_name).Do() 46 if err == nil { 47 return fmt.Errorf("Network still exists") 48 } 49 } 50 51 return nil 52 } 53 54 func testAccCheckComputeSubnetworkExists(n string, subnetwork *compute.Subnetwork) resource.TestCheckFunc { 55 return func(s *terraform.State) error { 56 rs, ok := s.RootModule().Resources[n] 57 if !ok { 58 return fmt.Errorf("Not found: %s", n) 59 } 60 61 if rs.Primary.ID == "" { 62 return fmt.Errorf("No ID is set") 63 } 64 65 config := testAccProvider.Meta().(*Config) 66 67 region, subnet_name := splitSubnetID(rs.Primary.ID) 68 found, err := config.clientCompute.Subnetworks.Get( 69 config.Project, region, subnet_name).Do() 70 if err != nil { 71 return err 72 } 73 74 if found.Name != subnet_name { 75 return fmt.Errorf("Subnetwork not found") 76 } 77 78 *subnetwork = *found 79 80 return nil 81 } 82 } 83 84 var testAccComputeSubnetwork_basic = fmt.Sprintf(` 85 resource "google_compute_network" "custom-test" { 86 name = "network-test-%s" 87 auto_create_subnetworks = false 88 } 89 90 resource "google_compute_subnetwork" "network-ref-by-url" { 91 name = "subnetwork-test-%s" 92 ip_cidr_range = "10.0.0.0/16" 93 region = "us-central1" 94 network = "${google_compute_network.custom-test.self_link}" 95 } 96 97 98 resource "google_compute_subnetwork" "network-ref-by-name" { 99 name = "subnetwork-test-%s" 100 ip_cidr_range = "10.1.0.0/16" 101 region = "us-central1" 102 network = "${google_compute_network.custom-test.name}" 103 } 104 105 `, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))