github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_lb_pool_v2_test.go (about)

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