github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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("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", "pool_1_updated"),
    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("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(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("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  const TestAccLBV2PoolConfig_basic = `
    90  resource "openstack_networking_network_v2" "network_1" {
    91    name = "network_1"
    92    admin_state_up = "true"
    93  }
    94  
    95  resource "openstack_networking_subnet_v2" "subnet_1" {
    96    name = "subnet_1"
    97    cidr = "192.168.199.0/24"
    98    ip_version = 4
    99    network_id = "${openstack_networking_network_v2.network_1.id}"
   100  }
   101  
   102  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   103    name = "loadbalancer_1"
   104    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   105  }
   106  
   107  resource "openstack_lb_listener_v2" "listener_1" {
   108    name = "listener_1"
   109    protocol = "HTTP"
   110    protocol_port = 8080
   111    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   112  }
   113  
   114  resource "openstack_lb_pool_v2" "pool_1" {
   115    name = "pool_1"
   116    protocol = "HTTP"
   117    lb_method = "ROUND_ROBIN"
   118    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   119  }
   120  `
   121  
   122  const TestAccLBV2PoolConfig_update = `
   123  resource "openstack_networking_network_v2" "network_1" {
   124    name = "network_1"
   125    admin_state_up = "true"
   126  }
   127  
   128  resource "openstack_networking_subnet_v2" "subnet_1" {
   129    name = "subnet_1"
   130    cidr = "192.168.199.0/24"
   131    ip_version = 4
   132    network_id = "${openstack_networking_network_v2.network_1.id}"
   133  }
   134  
   135  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   136    name = "loadbalancer_1"
   137    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   138  }
   139  
   140  resource "openstack_lb_listener_v2" "listener_1" {
   141    name = "listener_1"
   142    protocol = "HTTP"
   143    protocol_port = 8080
   144    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   145  }
   146  
   147  resource "openstack_lb_pool_v2" "pool_1" {
   148    name = "pool_1_updated"
   149    protocol = "HTTP"
   150    lb_method = "LEAST_CONNECTIONS"
   151    admin_state_up = "true"
   152    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   153  }
   154  `