github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/vcd/resource_vcd_network_test.go (about)

     1  package vcd
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"regexp"
     7  	"testing"
     8  
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/terraform"
    11  	"github.com/hmrc/vmware-govcd"
    12  )
    13  
    14  func TestAccVcdNetwork_Basic(t *testing.T) {
    15  	var network govcd.OrgVDCNetwork
    16  	generatedHrefRegexp := regexp.MustCompile("^https://")
    17  
    18  	resource.Test(t, resource.TestCase{
    19  		PreCheck:     func() { testAccPreCheck(t) },
    20  		Providers:    testAccProviders,
    21  		CheckDestroy: testAccCheckVcdNetworkDestroy,
    22  		Steps: []resource.TestStep{
    23  			resource.TestStep{
    24  				Config: fmt.Sprintf(testAccCheckVcdNetwork_basic, os.Getenv("VCD_EDGE_GATWEWAY")),
    25  				Check: resource.ComposeTestCheckFunc(
    26  					testAccCheckVcdNetworkExists("vcd_network.foonet", &network),
    27  					testAccCheckVcdNetworkAttributes(&network),
    28  					resource.TestCheckResourceAttr(
    29  						"vcd_network.foonet", "name", "foonet"),
    30  					resource.TestCheckResourceAttr(
    31  						"vcd_network.foonet", "static_ip_pool.#", "1"),
    32  					resource.TestCheckResourceAttr(
    33  						"vcd_network.foonet", "gateway", "10.10.102.1"),
    34  					resource.TestMatchResourceAttr(
    35  						"vcd_network.foonet", "href", generatedHrefRegexp),
    36  				),
    37  			},
    38  		},
    39  	})
    40  }
    41  
    42  func testAccCheckVcdNetworkExists(n string, network *govcd.OrgVDCNetwork) resource.TestCheckFunc {
    43  	return func(s *terraform.State) error {
    44  		rs, ok := s.RootModule().Resources[n]
    45  		if !ok {
    46  			return fmt.Errorf("Not found: %s", n)
    47  		}
    48  
    49  		if rs.Primary.ID == "" {
    50  			return fmt.Errorf("No VAPP ID is set")
    51  		}
    52  
    53  		conn := testAccProvider.Meta().(*VCDClient)
    54  
    55  		resp, err := conn.OrgVdc.FindVDCNetwork(rs.Primary.ID)
    56  		if err != nil {
    57  			return fmt.Errorf("Network does not exist.")
    58  		}
    59  
    60  		*network = resp
    61  
    62  		return nil
    63  	}
    64  }
    65  
    66  func testAccCheckVcdNetworkDestroy(s *terraform.State) error {
    67  	conn := testAccProvider.Meta().(*VCDClient)
    68  
    69  	for _, rs := range s.RootModule().Resources {
    70  		if rs.Type != "vcd_network" {
    71  			continue
    72  		}
    73  
    74  		_, err := conn.OrgVdc.FindVDCNetwork(rs.Primary.ID)
    75  
    76  		if err == nil {
    77  			return fmt.Errorf("Network still exists.")
    78  		}
    79  
    80  		return nil
    81  	}
    82  
    83  	return nil
    84  }
    85  
    86  func testAccCheckVcdNetworkAttributes(network *govcd.OrgVDCNetwork) resource.TestCheckFunc {
    87  	return func(s *terraform.State) error {
    88  
    89  		if network.OrgVDCNetwork.Name != "foonet" {
    90  			return fmt.Errorf("Bad name: %s", network.OrgVDCNetwork.Name)
    91  		}
    92  
    93  		return nil
    94  	}
    95  }
    96  
    97  const testAccCheckVcdNetwork_basic = `
    98  resource "vcd_network" "foonet" {
    99  	name = "foonet"
   100  	edge_gateway = "%s"
   101  	gateway = "10.10.102.1"
   102  	static_ip_pool {
   103  		start_address = "10.10.102.2"
   104  		end_address = "10.10.102.254"
   105  	}
   106  }
   107  `