github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/cloudstack/resource_cloudstack_vpn_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 TestAccCloudStackVPNGateway_basic(t *testing.T) {
    13  	var vpnGateway cloudstack.VpnGateway
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackVPNGatewayDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackVPNGateway_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackVPNGatewayExists(
    24  						"cloudstack_vpn_gateway.foo", &vpnGateway),
    25  				),
    26  			},
    27  		},
    28  	})
    29  }
    30  
    31  func testAccCheckCloudStackVPNGatewayExists(
    32  	n string, vpnGateway *cloudstack.VpnGateway) resource.TestCheckFunc {
    33  	return func(s *terraform.State) error {
    34  		rs, ok := s.RootModule().Resources[n]
    35  		if !ok {
    36  			return fmt.Errorf("Not found: %s", n)
    37  		}
    38  
    39  		if rs.Primary.ID == "" {
    40  			return fmt.Errorf("No VPN Gateway ID is set")
    41  		}
    42  
    43  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    44  		v, _, err := cs.VPN.GetVpnGatewayByID(rs.Primary.ID)
    45  
    46  		if err != nil {
    47  			return err
    48  		}
    49  
    50  		if v.Id != rs.Primary.ID {
    51  			return fmt.Errorf("VPN Gateway not found")
    52  		}
    53  
    54  		*vpnGateway = *v
    55  
    56  		return nil
    57  	}
    58  }
    59  
    60  func testAccCheckCloudStackVPNGatewayDestroy(s *terraform.State) error {
    61  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    62  
    63  	for _, rs := range s.RootModule().Resources {
    64  		if rs.Type != "cloudstack_vpn_gateway" {
    65  			continue
    66  		}
    67  
    68  		if rs.Primary.ID == "" {
    69  			return fmt.Errorf("No VPN Gateway ID is set")
    70  		}
    71  
    72  		_, _, err := cs.VPN.GetVpnGatewayByID(rs.Primary.ID)
    73  		if err == nil {
    74  			return fmt.Errorf("VPN Gateway %s still exists", rs.Primary.ID)
    75  		}
    76  	}
    77  
    78  	return nil
    79  }
    80  
    81  var testAccCloudStackVPNGateway_basic = fmt.Sprintf(`
    82  resource "cloudstack_vpc" "foo" {
    83    name = "terraform-vpc"
    84    display_text = "terraform-vpc-text"
    85    cidr = "%s"
    86    vpc_offering = "%s"
    87    zone = "%s"
    88  }
    89  
    90  resource "cloudstack_vpn_gateway" "foo" {
    91    vpc_id = "${cloudstack_vpc.foo.id}"
    92  }`,
    93  	CLOUDSTACK_VPC_CIDR_1,
    94  	CLOUDSTACK_VPC_OFFERING,
    95  	CLOUDSTACK_ZONE)