github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/cloudstack/resource_cloudstack_private_gateway_test.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func TestAccCloudStackPrivateGateway_basic(t *testing.T) {
    13  	var gateway cloudstack.PrivateGateway
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackPrivateGatewayDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackPrivateGateway_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackPrivateGatewayExists(
    24  						"cloudstack_private_gateway.foo", &gateway),
    25  					testAccCheckCloudStackPrivateGatewayAttributes(&gateway),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckCloudStackPrivateGatewayExists(
    33  	n string, gateway *cloudstack.PrivateGateway) resource.TestCheckFunc {
    34  	return func(s *terraform.State) error {
    35  		rs, ok := s.RootModule().Resources[n]
    36  		if !ok {
    37  			return fmt.Errorf("Not found: %s", n)
    38  		}
    39  
    40  		if rs.Primary.ID == "" {
    41  			return fmt.Errorf("No Private Gateway ID is set")
    42  		}
    43  
    44  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    45  		pgw, _, err := cs.VPC.GetPrivateGatewayByID(rs.Primary.ID)
    46  
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		if pgw.Id != rs.Primary.ID {
    52  			return fmt.Errorf("Private Gateway not found")
    53  		}
    54  
    55  		*gateway = *pgw
    56  
    57  		return nil
    58  	}
    59  }
    60  
    61  func testAccCheckCloudStackPrivateGatewayAttributes(
    62  	gateway *cloudstack.PrivateGateway) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  
    65  		if gateway.Gateway != CLOUDSTACK_PRIVGW_GATEWAY {
    66  			return fmt.Errorf("Bad Gateway: %s", gateway.Gateway)
    67  		}
    68  
    69  		if gateway.Ipaddress != CLOUDSTACK_PRIVGW_IPADDRESS {
    70  			return fmt.Errorf("Bad Gateway: %s", gateway.Ipaddress)
    71  		}
    72  
    73  		if gateway.Netmask != CLOUDSTACK_PRIVGW_NETMASK {
    74  			return fmt.Errorf("Bad Gateway: %s", gateway.Netmask)
    75  		}
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccCheckCloudStackPrivateGatewayDestroy(s *terraform.State) error {
    82  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    83  
    84  	for _, rs := range s.RootModule().Resources {
    85  		if rs.Type != "cloudstack_private_gateway" {
    86  			continue
    87  		}
    88  
    89  		if rs.Primary.ID == "" {
    90  			return fmt.Errorf("No private gateway ID is set")
    91  		}
    92  
    93  		gateway, _, err := cs.VPC.GetPrivateGatewayByID(rs.Primary.ID)
    94  		if err == nil && gateway.Id != "" {
    95  			return fmt.Errorf("Private gateway %s still exists", rs.Primary.ID)
    96  		}
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  var testAccCloudStackPrivateGateway_basic = fmt.Sprintf(`
   103  resource "cloudstack_vpc" "foobar" {
   104    name = "terraform-vpc"
   105    cidr = "%s"
   106    vpc_offering = "%s"
   107    zone = "%s"
   108  }
   109  
   110  resource "cloudstack_private_gateway" "foo" {
   111    gateway = "%s"
   112    ip_address = "%s"
   113    netmask = "%s"
   114    vlan = "%s"
   115    vpc_id = "${cloudstack_vpc.foobar.id}"
   116  }`,
   117  	CLOUDSTACK_VPC_CIDR_1,
   118  	CLOUDSTACK_VPC_OFFERING,
   119  	CLOUDSTACK_ZONE,
   120  	CLOUDSTACK_PRIVGW_GATEWAY,
   121  	CLOUDSTACK_PRIVGW_IPADDRESS,
   122  	CLOUDSTACK_PRIVGW_NETMASK,
   123  	CLOUDSTACK_PRIVGW_VLAN)