github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/cloudstack/resource_cloudstack_ipaddress_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 TestAccCloudStackIPAddress_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: testAccCheckCloudStackIPAddressDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccCloudStackIPAddress_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckCloudStackIPAddressExists(
    24  						"cloudstack_ipaddress.foo", &ipaddr),
    25  					testAccCheckCloudStackIPAddressAttributes(&ipaddr),
    26  				),
    27  			},
    28  		},
    29  	})
    30  }
    31  
    32  func TestAccCloudStackIPAddress_vpc(t *testing.T) {
    33  	var ipaddr cloudstack.PublicIpAddress
    34  
    35  	resource.Test(t, resource.TestCase{
    36  		PreCheck:     func() { testAccPreCheck(t) },
    37  		Providers:    testAccProviders,
    38  		CheckDestroy: testAccCheckCloudStackIPAddressDestroy,
    39  		Steps: []resource.TestStep{
    40  			resource.TestStep{
    41  				Config: testAccCloudStackIPAddress_vpc,
    42  				Check: resource.ComposeTestCheckFunc(
    43  					testAccCheckCloudStackIPAddressExists(
    44  						"cloudstack_ipaddress.foo", &ipaddr),
    45  					resource.TestCheckResourceAttr(
    46  						"cloudstack_ipaddress.foo", "vpc", "terraform-vpc"),
    47  				),
    48  			},
    49  		},
    50  	})
    51  }
    52  
    53  func testAccCheckCloudStackIPAddressExists(
    54  	n string, ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
    55  	return func(s *terraform.State) error {
    56  		rs, ok := s.RootModule().Resources[n]
    57  		if !ok {
    58  			return fmt.Errorf("Not found: %s", n)
    59  		}
    60  
    61  		if rs.Primary.ID == "" {
    62  			return fmt.Errorf("No IP address ID is set")
    63  		}
    64  
    65  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    66  		pip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
    67  
    68  		if err != nil {
    69  			return err
    70  		}
    71  
    72  		if pip.Id != rs.Primary.ID {
    73  			return fmt.Errorf("IP address not found")
    74  		}
    75  
    76  		*ipaddr = *pip
    77  
    78  		return nil
    79  	}
    80  }
    81  
    82  func testAccCheckCloudStackIPAddressAttributes(
    83  	ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
    84  	return func(s *terraform.State) error {
    85  
    86  		if ipaddr.Associatednetworkname != CLOUDSTACK_NETWORK_1 {
    87  			return fmt.Errorf("Bad network: %s", ipaddr.Associatednetworkname)
    88  		}
    89  
    90  		return nil
    91  	}
    92  }
    93  
    94  func testAccCheckCloudStackIPAddressDestroy(s *terraform.State) error {
    95  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    96  
    97  	for _, rs := range s.RootModule().Resources {
    98  		if rs.Type != "cloudstack_ipaddress" {
    99  			continue
   100  		}
   101  
   102  		if rs.Primary.ID == "" {
   103  			return fmt.Errorf("No IP address ID is set")
   104  		}
   105  
   106  		ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
   107  		if err == nil && ip.Associatednetworkid != "" {
   108  			return fmt.Errorf("Public IP %s still associated", rs.Primary.ID)
   109  		}
   110  	}
   111  
   112  	return nil
   113  }
   114  
   115  var testAccCloudStackIPAddress_basic = fmt.Sprintf(`
   116  resource "cloudstack_ipaddress" "foo" {
   117    network = "%s"
   118  }`, CLOUDSTACK_NETWORK_1)
   119  
   120  var testAccCloudStackIPAddress_vpc = fmt.Sprintf(`
   121  resource "cloudstack_vpc" "foobar" {
   122    name = "terraform-vpc"
   123    cidr = "%s"
   124    vpc_offering = "%s"
   125    zone = "%s"
   126  }
   127  
   128  resource "cloudstack_ipaddress" "foo" {
   129    vpc = "${cloudstack_vpc.foobar.name}"
   130  }`,
   131  	CLOUDSTACK_VPC_CIDR_1,
   132  	CLOUDSTACK_VPC_OFFERING,
   133  	CLOUDSTACK_ZONE)