github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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/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 TestAccComputeNetwork_basic(t *testing.T) {
    14  	var network compute.Network
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckComputeNetworkDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccComputeNetwork_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckComputeNetworkExists(
    25  						"google_compute_network.foobar", &network),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccComputeNetwork_auto_subnet(t *testing.T) {
    33  	var network compute.Network
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckComputeNetworkDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccComputeNetwork_auto_subnet,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckComputeNetworkExists(
    44  						"google_compute_network.bar", &network),
    45  					testAccCheckComputeNetworkIsAutoSubnet(
    46  						"google_compute_network.bar", &network),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func TestAccComputeNetwork_custom_subnet(t *testing.T) {
    54  	var network compute.Network
    55  
    56  	resource.Test(t, resource.TestCase{
    57  		PreCheck:     func() { testAccPreCheck(t) },
    58  		Providers:    testAccProviders,
    59  		CheckDestroy: testAccCheckComputeNetworkDestroy,
    60  		Steps: []resource.TestStep{
    61  			resource.TestStep{
    62  				Config: testAccComputeNetwork_custom_subnet,
    63  				Check: resource.ComposeTestCheckFunc(
    64  					testAccCheckComputeNetworkExists(
    65  						"google_compute_network.baz", &network),
    66  					testAccCheckComputeNetworkIsCustomSubnet(
    67  						"google_compute_network.baz", &network),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func testAccCheckComputeNetworkDestroy(s *terraform.State) error {
    75  	config := testAccProvider.Meta().(*Config)
    76  
    77  	for _, rs := range s.RootModule().Resources {
    78  		if rs.Type != "google_compute_network" {
    79  			continue
    80  		}
    81  
    82  		_, err := config.clientCompute.Networks.Get(
    83  			config.Project, rs.Primary.ID).Do()
    84  		if err == nil {
    85  			return fmt.Errorf("Network still exists")
    86  		}
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  func testAccCheckComputeNetworkExists(n string, network *compute.Network) resource.TestCheckFunc {
    93  	return func(s *terraform.State) error {
    94  		rs, ok := s.RootModule().Resources[n]
    95  		if !ok {
    96  			return fmt.Errorf("Not found: %s", n)
    97  		}
    98  
    99  		if rs.Primary.ID == "" {
   100  			return fmt.Errorf("No ID is set")
   101  		}
   102  
   103  		config := testAccProvider.Meta().(*Config)
   104  
   105  		found, err := config.clientCompute.Networks.Get(
   106  			config.Project, rs.Primary.ID).Do()
   107  		if err != nil {
   108  			return err
   109  		}
   110  
   111  		if found.Name != rs.Primary.ID {
   112  			return fmt.Errorf("Network not found")
   113  		}
   114  
   115  		*network = *found
   116  
   117  		return nil
   118  	}
   119  }
   120  
   121  func testAccCheckComputeNetworkIsAutoSubnet(n string, network *compute.Network) resource.TestCheckFunc {
   122  	return func(s *terraform.State) error {
   123  		config := testAccProvider.Meta().(*Config)
   124  
   125  		found, err := config.clientCompute.Networks.Get(
   126  			config.Project, network.Name).Do()
   127  		if err != nil {
   128  			return err
   129  		}
   130  
   131  		if !found.AutoCreateSubnetworks {
   132  			return fmt.Errorf("should have AutoCreateSubnetworks = true")
   133  		}
   134  
   135  		if found.IPv4Range != "" {
   136  			return fmt.Errorf("should not have IPv4Range")
   137  		}
   138  
   139  		return nil
   140  	}
   141  }
   142  
   143  func testAccCheckComputeNetworkIsCustomSubnet(n string, network *compute.Network) resource.TestCheckFunc {
   144  	return func(s *terraform.State) error {
   145  		config := testAccProvider.Meta().(*Config)
   146  
   147  		found, err := config.clientCompute.Networks.Get(
   148  			config.Project, network.Name).Do()
   149  		if err != nil {
   150  			return err
   151  		}
   152  
   153  		if found.AutoCreateSubnetworks {
   154  			return fmt.Errorf("should have AutoCreateSubnetworks = false")
   155  		}
   156  
   157  		if found.IPv4Range != "" {
   158  			return fmt.Errorf("should not have IPv4Range")
   159  		}
   160  
   161  		return nil
   162  	}
   163  }
   164  
   165  var testAccComputeNetwork_basic = fmt.Sprintf(`
   166  resource "google_compute_network" "foobar" {
   167  	name = "network-test-%s"
   168  	ipv4_range = "10.0.0.0/16"
   169  }`, acctest.RandString(10))
   170  
   171  var testAccComputeNetwork_auto_subnet = fmt.Sprintf(`
   172  resource "google_compute_network" "bar" {
   173  	name = "network-test-%s"
   174  	auto_create_subnetworks = true
   175  }`, acctest.RandString(10))
   176  
   177  var testAccComputeNetwork_custom_subnet = fmt.Sprintf(`
   178  resource "google_compute_network" "baz" {
   179  	name = "network-test-%s"
   180  	auto_create_subnetworks = false
   181  }`, acctest.RandString(10))