github.com/jsoriano/terraform@v0.6.7-0.20151026070445-8b70867fdd95/builtin/providers/openstack/resource_openstack_lb_pool_v1_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/pools"
    11  )
    12  
    13  func TestAccLBV1Pool_basic(t *testing.T) {
    14  	var pool pools.Pool
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckLBV1PoolDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccLBV1Pool_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckLBV1PoolExists(t, "openstack_lb_pool_v1.pool_1", &pool),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccLBV1Pool_update,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "name", "tf_test_lb_pool_updated"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckLBV1PoolDestroy(s *terraform.State) error {
    38  	config := testAccProvider.Meta().(*Config)
    39  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    40  	if err != nil {
    41  		return fmt.Errorf("(testAccCheckLBV1PoolDestroy) Error creating OpenStack networking client: %s", err)
    42  	}
    43  
    44  	for _, rs := range s.RootModule().Resources {
    45  		if rs.Type != "openstack_lb_pool_v1" {
    46  			continue
    47  		}
    48  
    49  		_, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
    50  		if err == nil {
    51  			return fmt.Errorf("LB Pool still exists")
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckLBV1PoolExists(t *testing.T, n string, pool *pools.Pool) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[n]
    61  		if !ok {
    62  			return fmt.Errorf("Not found: %s", n)
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("No ID is set")
    67  		}
    68  
    69  		config := testAccProvider.Meta().(*Config)
    70  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    71  		if err != nil {
    72  			return fmt.Errorf("(testAccCheckLBV1PoolExists) Error creating OpenStack networking client: %s", err)
    73  		}
    74  
    75  		found, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		if found.ID != rs.Primary.ID {
    81  			return fmt.Errorf("Pool not found")
    82  		}
    83  
    84  		*pool = *found
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  var testAccLBV1Pool_basic = fmt.Sprintf(`
    91    resource "openstack_networking_network_v2" "network_1" {
    92      region = "%s"
    93      name = "network_1"
    94      admin_state_up = "true"
    95    }
    96  
    97    resource "openstack_networking_subnet_v2" "subnet_1" {
    98      region = "%s"
    99      network_id = "${openstack_networking_network_v2.network_1.id}"
   100      cidr = "192.168.199.0/24"
   101      ip_version = 4
   102    }
   103  
   104    resource "openstack_lb_pool_v1" "pool_1" {
   105      region = "%s"
   106      name = "tf_test_lb_pool"
   107      protocol = "HTTP"
   108      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   109      lb_method = "ROUND_ROBIN"
   110    }`,
   111  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)
   112  
   113  var testAccLBV1Pool_update = fmt.Sprintf(`
   114    resource "openstack_networking_network_v2" "network_1" {
   115      region = "%s"
   116      name = "network_1"
   117      admin_state_up = "true"
   118    }
   119  
   120    resource "openstack_networking_subnet_v2" "subnet_1" {
   121      region = "%s"
   122      network_id = "${openstack_networking_network_v2.network_1.id}"
   123      cidr = "192.168.199.0/24"
   124      ip_version = 4
   125    }
   126  
   127    resource "openstack_lb_pool_v1" "pool_1" {
   128      region = "%s"
   129      name = "tf_test_lb_pool_updated"
   130      protocol = "HTTP"
   131      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   132      lb_method = "ROUND_ROBIN"
   133    }`,
   134  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)