github.com/andresvia/terraform@v0.6.15-0.20160412045437-d51c75946785/builtin/providers/cloudstack/resource_cloudstack_static_nat_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 TestAccCloudStackStaticNAT_basic(t *testing.T) {
    13  	var ipaddr cloudstack.PublicIpAddress
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckCloudStackStaticNATDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackStaticNAT_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackStaticNATExists(
    24  						"cloudstack_static_nat.foo", &ipaddr),
    25  					testAccCheckCloudStackStaticNATAttributes(&ipaddr),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func testAccCheckCloudStackStaticNATExists(
    33  	n string, ipaddr *cloudstack.PublicIpAddress) 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 NAT ID is set")
    42  		}
    43  
    44  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    45  		ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
    46  
    47  		if err != nil {
    48  			return err
    49  		}
    50  
    51  		if ip.Id != rs.Primary.ID {
    52  			return fmt.Errorf("Static NAT not found")
    53  		}
    54  
    55  		if !ip.Isstaticnat {
    56  			return fmt.Errorf("Static NAT not enabled")
    57  		}
    58  
    59  		*ipaddr = *ip
    60  
    61  		return nil
    62  	}
    63  }
    64  
    65  func testAccCheckCloudStackStaticNATAttributes(
    66  	ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
    67  	return func(s *terraform.State) error {
    68  
    69  		if ipaddr.Associatednetworkname != CLOUDSTACK_NETWORK_1 {
    70  			return fmt.Errorf("Bad network: %s", ipaddr.Associatednetworkname)
    71  		}
    72  
    73  		if ipaddr.Virtualmachinename != "terraform-test" {
    74  			return fmt.Errorf("Bad virtual_machine: %s", ipaddr.Virtualmachinename)
    75  		}
    76  
    77  		return nil
    78  	}
    79  }
    80  
    81  func testAccCheckCloudStackStaticNATDestroy(s *terraform.State) error {
    82  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    83  
    84  	for _, rs := range s.RootModule().Resources {
    85  		if rs.Type != "cloudstack_static_nat" {
    86  			continue
    87  		}
    88  
    89  		if rs.Primary.ID == "" {
    90  			return fmt.Errorf("No static NAT ID is set")
    91  		}
    92  
    93  		ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
    94  		if err == nil && ip.Isstaticnat {
    95  			return fmt.Errorf("Static NAT %s still enabled", rs.Primary.ID)
    96  		}
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  var testAccCloudStackStaticNAT_basic = fmt.Sprintf(`
   103  resource "cloudstack_instance" "foobar" {
   104    name = "terraform-test"
   105    display_name = "terraform-test"
   106    service_offering= "%s"
   107    network = "%s"
   108    template = "%s"
   109    zone = "%s"
   110    user_data = "foobar\nfoo\nbar"
   111    expunge = true
   112  }
   113  
   114  resource "cloudstack_ipaddress" "foo" {
   115    network = "%s"
   116  }
   117  
   118  resource "cloudstack_static_nat" "foo" {
   119  	ipaddress = "${cloudstack_ipaddress.foo.id}"
   120  	network = "${cloudstack_ipaddress.foo.network}"
   121    virtual_machine = "${cloudstack_instance.foobar.id}"
   122  }`,
   123  	CLOUDSTACK_SERVICE_OFFERING_1,
   124  	CLOUDSTACK_NETWORK_1,
   125  	CLOUDSTACK_TEMPLATE,
   126  	CLOUDSTACK_ZONE,
   127  	CLOUDSTACK_NETWORK_1,
   128  )