github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_compute_network_test.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"google.golang.org/api/compute/v1"
    10  )
    11  
    12  func TestAccComputeNetwork_basic(t *testing.T) {
    13  	var network compute.Network
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckComputeNetworkDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccComputeNetwork_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckComputeNetworkExists(
    24  						"google_compute_network.foobar", &network),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckComputeNetworkDestroy(s *terraform.State) error {
    32  	config := testAccProvider.Meta().(*Config)
    33  
    34  	for _, rs := range s.RootModule().Resources {
    35  		if rs.Type != "google_compute_network" {
    36  			continue
    37  		}
    38  
    39  		_, err := config.clientCompute.Networks.Get(
    40  			config.Project, rs.Primary.ID).Do()
    41  		if err == nil {
    42  			return fmt.Errorf("Network still exists")
    43  		}
    44  	}
    45  
    46  	return nil
    47  }
    48  
    49  func testAccCheckComputeNetworkExists(n string, network *compute.Network) resource.TestCheckFunc {
    50  	return func(s *terraform.State) error {
    51  		rs, ok := s.RootModule().Resources[n]
    52  		if !ok {
    53  			return fmt.Errorf("Not found: %s", n)
    54  		}
    55  
    56  		if rs.Primary.ID == "" {
    57  			return fmt.Errorf("No ID is set")
    58  		}
    59  
    60  		config := testAccProvider.Meta().(*Config)
    61  
    62  		found, err := config.clientCompute.Networks.Get(
    63  			config.Project, rs.Primary.ID).Do()
    64  		if err != nil {
    65  			return err
    66  		}
    67  
    68  		if found.Name != rs.Primary.ID {
    69  			return fmt.Errorf("Network not found")
    70  		}
    71  
    72  		*network = *found
    73  
    74  		return nil
    75  	}
    76  }
    77  
    78  const testAccComputeNetwork_basic = `
    79  resource "google_compute_network" "foobar" {
    80  	name = "terraform-test"
    81  	ipv4_range = "10.0.0.0/16"
    82  }`