github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/cloudstack/resource_cloudstack_static_route_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 TestAccCloudStackStaticRoute_basic(t *testing.T) {
    13  	var staticroute cloudstack.StaticRoute
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackStaticRouteDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackStaticRoute_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackStaticRouteExists(
    24  						"cloudstack_static_route.bar", &staticroute),
    25  					testAccCheckCloudStackStaticRouteAttributes(&staticroute),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckCloudStackStaticRouteExists(
    33  	n string, staticroute *cloudstack.StaticRoute) 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 Static Route ID is set")
    42  		}
    43  
    44  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    45  		route, _, err := cs.VPC.GetStaticRouteByID(rs.Primary.ID)
    46  
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		if route.Id != rs.Primary.ID {
    52  			return fmt.Errorf("Static Route not found")
    53  		}
    54  
    55  		*staticroute = *route
    56  
    57  		return nil
    58  	}
    59  }
    60  
    61  func testAccCheckCloudStackStaticRouteAttributes(
    62  	staticroute *cloudstack.StaticRoute) resource.TestCheckFunc {
    63  	return func(s *terraform.State) error {
    64  
    65  		if staticroute.Cidr != CLOUDSTACK_STATIC_ROUTE_CIDR {
    66  			return fmt.Errorf("Bad Cidr: %s", staticroute.Cidr)
    67  		}
    68  
    69  		return nil
    70  	}
    71  }
    72  
    73  func testAccCheckCloudStackStaticRouteDestroy(s *terraform.State) error {
    74  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    75  
    76  	for _, rs := range s.RootModule().Resources {
    77  		if rs.Type != "cloudstack_static_route" {
    78  			continue
    79  		}
    80  
    81  		if rs.Primary.ID == "" {
    82  			return fmt.Errorf("No static route ID is set")
    83  		}
    84  
    85  		staticroute, _, err := cs.VPC.GetStaticRouteByID(rs.Primary.ID)
    86  		if err == nil && staticroute.Id != "" {
    87  			return fmt.Errorf("Static route %s still exists", rs.Primary.ID)
    88  		}
    89  	}
    90  
    91  	return nil
    92  }
    93  
    94  var testAccCloudStackStaticRoute_basic = fmt.Sprintf(`
    95  resource "cloudstack_vpc" "foobar" {
    96    name = "terraform-vpc"
    97    cidr = "%s"
    98    vpc_offering = "%s"
    99    zone = "%s"
   100  }
   101  
   102  resource "cloudstack_private_gateway" "foo" {
   103    gateway = "%s"
   104    ip_address = "%s"
   105    netmask = "%s"
   106    vlan = "%s"
   107    vpc_id = "${cloudstack_vpc.foobar.id}"
   108  }
   109  
   110  resource "cloudstack_static_route" "bar" {
   111    cidr = "%s"
   112    gateway_id = "${cloudstack_private_gateway.foo.id}"
   113  }`,
   114  	CLOUDSTACK_VPC_CIDR_1,
   115  	CLOUDSTACK_VPC_OFFERING,
   116  	CLOUDSTACK_ZONE,
   117  	CLOUDSTACK_PRIVGW_GATEWAY,
   118  	CLOUDSTACK_PRIVGW_IPADDRESS,
   119  	CLOUDSTACK_PRIVGW_NETMASK,
   120  	CLOUDSTACK_PRIVGW_VLAN,
   121  	CLOUDSTACK_STATIC_ROUTE_CIDR)