github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccLBV2Pool_timeout(t *testing.T) {
    37  	var pool pools.Pool
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckLBV2PoolDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: TestAccLBV2PoolConfig_timeout,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckLBV2PoolExists("openstack_lb_pool_v2.pool_1", &pool),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func testAccCheckLBV2PoolDestroy(s *terraform.State) error {
    55  	config := testAccProvider.Meta().(*Config)
    56  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    57  	if err != nil {
    58  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    59  	}
    60  
    61  	for _, rs := range s.RootModule().Resources {
    62  		if rs.Type != "openstack_lb_pool_v2" {
    63  			continue
    64  		}
    65  
    66  		_, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
    67  		if err == nil {
    68  			return fmt.Errorf("Pool still exists: %s", rs.Primary.ID)
    69  		}
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func testAccCheckLBV2PoolExists(n string, pool *pools.Pool) resource.TestCheckFunc {
    76  	return func(s *terraform.State) error {
    77  		rs, ok := s.RootModule().Resources[n]
    78  		if !ok {
    79  			return fmt.Errorf("Not found: %s", n)
    80  		}
    81  
    82  		if rs.Primary.ID == "" {
    83  			return fmt.Errorf("No ID is set")
    84  		}
    85  
    86  		config := testAccProvider.Meta().(*Config)
    87  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    88  		if err != nil {
    89  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    90  		}
    91  
    92  		found, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		if found.ID != rs.Primary.ID {
    98  			return fmt.Errorf("Member not found")
    99  		}
   100  
   101  		*pool = *found
   102  
   103  		return nil
   104  	}
   105  }
   106  
   107  const TestAccLBV2PoolConfig_basic = `
   108  resource "openstack_networking_network_v2" "network_1" {
   109    name = "network_1"
   110    admin_state_up = "true"
   111  }
   112  
   113  resource "openstack_networking_subnet_v2" "subnet_1" {
   114    name = "subnet_1"
   115    cidr = "192.168.199.0/24"
   116    ip_version = 4
   117    network_id = "${openstack_networking_network_v2.network_1.id}"
   118  }
   119  
   120  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   121    name = "loadbalancer_1"
   122    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   123  }
   124  
   125  resource "openstack_lb_listener_v2" "listener_1" {
   126    name = "listener_1"
   127    protocol = "HTTP"
   128    protocol_port = 8080
   129    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   130  }
   131  
   132  resource "openstack_lb_pool_v2" "pool_1" {
   133    name = "pool_1"
   134    protocol = "HTTP"
   135    lb_method = "ROUND_ROBIN"
   136    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   137  }
   138  `
   139  
   140  const TestAccLBV2PoolConfig_update = `
   141  resource "openstack_networking_network_v2" "network_1" {
   142    name = "network_1"
   143    admin_state_up = "true"
   144  }
   145  
   146  resource "openstack_networking_subnet_v2" "subnet_1" {
   147    name = "subnet_1"
   148    cidr = "192.168.199.0/24"
   149    ip_version = 4
   150    network_id = "${openstack_networking_network_v2.network_1.id}"
   151  }
   152  
   153  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   154    name = "loadbalancer_1"
   155    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   156  }
   157  
   158  resource "openstack_lb_listener_v2" "listener_1" {
   159    name = "listener_1"
   160    protocol = "HTTP"
   161    protocol_port = 8080
   162    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   163  }
   164  
   165  resource "openstack_lb_pool_v2" "pool_1" {
   166    name = "pool_1_updated"
   167    protocol = "HTTP"
   168    lb_method = "LEAST_CONNECTIONS"
   169    admin_state_up = "true"
   170    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   171  }
   172  `
   173  
   174  const TestAccLBV2PoolConfig_timeout = `
   175  resource "openstack_networking_network_v2" "network_1" {
   176    name = "network_1"
   177    admin_state_up = "true"
   178  }
   179  
   180  resource "openstack_networking_subnet_v2" "subnet_1" {
   181    name = "subnet_1"
   182    cidr = "192.168.199.0/24"
   183    ip_version = 4
   184    network_id = "${openstack_networking_network_v2.network_1.id}"
   185  }
   186  
   187  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   188    name = "loadbalancer_1"
   189    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   190  }
   191  
   192  resource "openstack_lb_listener_v2" "listener_1" {
   193    name = "listener_1"
   194    protocol = "HTTP"
   195    protocol_port = 8080
   196    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   197  }
   198  
   199  resource "openstack_lb_pool_v2" "pool_1" {
   200    name = "pool_1"
   201    protocol = "HTTP"
   202    lb_method = "ROUND_ROBIN"
   203    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   204  
   205    timeouts {
   206      create = "5m"
   207      delete = "5m"
   208    }
   209  }
   210  `