github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/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  				),
    46  			},
    47  		},
    48  	})
    49  }
    50  
    51  func testAccCheckCloudStackIPAddressExists(
    52  	n string, ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
    53  	return func(s *terraform.State) error {
    54  		rs, ok := s.RootModule().Resources[n]
    55  		if !ok {
    56  			return fmt.Errorf("Not found: %s", n)
    57  		}
    58  
    59  		if rs.Primary.ID == "" {
    60  			return fmt.Errorf("No IP address ID is set")
    61  		}
    62  
    63  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    64  		pip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
    65  
    66  		if err != nil {
    67  			return err
    68  		}
    69  
    70  		if pip.Id != rs.Primary.ID {
    71  			return fmt.Errorf("IP address not found")
    72  		}
    73  
    74  		*ipaddr = *pip
    75  
    76  		return nil
    77  	}
    78  }
    79  
    80  func testAccCheckCloudStackIPAddressAttributes(
    81  	ipaddr *cloudstack.PublicIpAddress) resource.TestCheckFunc {
    82  	return func(s *terraform.State) error {
    83  
    84  		if ipaddr.Associatednetworkid != CLOUDSTACK_NETWORK_1 {
    85  			return fmt.Errorf("Bad network ID: %s", ipaddr.Associatednetworkid)
    86  		}
    87  
    88  		return nil
    89  	}
    90  }
    91  
    92  func testAccCheckCloudStackIPAddressDestroy(s *terraform.State) error {
    93  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
    94  
    95  	for _, rs := range s.RootModule().Resources {
    96  		if rs.Type != "cloudstack_ipaddress" {
    97  			continue
    98  		}
    99  
   100  		if rs.Primary.ID == "" {
   101  			return fmt.Errorf("No IP address ID is set")
   102  		}
   103  
   104  		ip, _, err := cs.Address.GetPublicIpAddressByID(rs.Primary.ID)
   105  		if err == nil && ip.Associatednetworkid != "" {
   106  			return fmt.Errorf("Public IP %s still associated", rs.Primary.ID)
   107  		}
   108  	}
   109  
   110  	return nil
   111  }
   112  
   113  var testAccCloudStackIPAddress_basic = fmt.Sprintf(`
   114  resource "cloudstack_ipaddress" "foo" {
   115    network_id = "%s"
   116  }`, CLOUDSTACK_NETWORK_1)
   117  
   118  var testAccCloudStackIPAddress_vpc = fmt.Sprintf(`
   119  resource "cloudstack_vpc" "foobar" {
   120    name = "terraform-vpc"
   121    cidr = "%s"
   122    vpc_offering = "%s"
   123    zone = "%s"
   124  }
   125  
   126  resource "cloudstack_ipaddress" "foo" {
   127    vpc_id = "${cloudstack_vpc.foobar.id}"
   128  }`,
   129  	CLOUDSTACK_VPC_CIDR_1,
   130  	CLOUDSTACK_VPC_OFFERING,
   131  	CLOUDSTACK_ZONE)