github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/openstack/resource_openstack_lb_member_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 TestAccLBV2Member_basic(t *testing.T) {
    13  	var member pools.Member
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckLBV2MemberDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: TestAccLBV2MemberConfig_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckLBV2MemberExists("openstack_lb_member_v2.member_1", &member),
    24  				),
    25  			},
    26  			resource.TestStep{
    27  				Config: TestAccLBV2MemberConfig_update,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					resource.TestCheckResourceAttr("openstack_lb_member_v2.member_1", "weight", "10"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func TestAccLBV2Member_timeout(t *testing.T) {
    37  	var member pools.Member
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckLBV2MemberDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: TestAccLBV2MemberConfig_timeout,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckLBV2MemberExists("openstack_lb_member_v2.member_1", &member),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func testAccCheckLBV2MemberDestroy(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_member_v2" {
    63  			continue
    64  		}
    65  
    66  		poolId := rs.Primary.Attributes["pool_id"]
    67  		_, err := pools.GetMember(networkingClient, poolId, rs.Primary.ID).Extract()
    68  		if err == nil {
    69  			return fmt.Errorf("Member still exists: %s", rs.Primary.ID)
    70  		}
    71  	}
    72  
    73  	return nil
    74  }
    75  
    76  func testAccCheckLBV2MemberExists(n string, member *pools.Member) resource.TestCheckFunc {
    77  	return func(s *terraform.State) error {
    78  		rs, ok := s.RootModule().Resources[n]
    79  		if !ok {
    80  			return fmt.Errorf("Not found: %s", n)
    81  		}
    82  
    83  		if rs.Primary.ID == "" {
    84  			return fmt.Errorf("No ID is set")
    85  		}
    86  
    87  		config := testAccProvider.Meta().(*Config)
    88  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    89  		if err != nil {
    90  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    91  		}
    92  
    93  		poolId := rs.Primary.Attributes["pool_id"]
    94  		found, err := pools.GetMember(networkingClient, poolId, rs.Primary.ID).Extract()
    95  		if err != nil {
    96  			return err
    97  		}
    98  
    99  		if found.ID != rs.Primary.ID {
   100  			return fmt.Errorf("Member not found")
   101  		}
   102  
   103  		*member = *found
   104  
   105  		return nil
   106  	}
   107  }
   108  
   109  const TestAccLBV2MemberConfig_basic = `
   110  resource "openstack_networking_network_v2" "network_1" {
   111    name = "network_1"
   112    admin_state_up = "true"
   113  }
   114  
   115  resource "openstack_networking_subnet_v2" "subnet_1" {
   116    name = "subnet_1"
   117    network_id = "${openstack_networking_network_v2.network_1.id}"
   118    cidr = "192.168.199.0/24"
   119    ip_version = 4
   120  }
   121  
   122  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   123    name = "loadbalancer_1"
   124    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   125  }
   126  
   127  resource "openstack_lb_listener_v2" "listener_1" {
   128    name = "listener_1"
   129    protocol = "HTTP"
   130    protocol_port = 8080
   131    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   132  }
   133  
   134  resource "openstack_lb_pool_v2" "pool_1" {
   135    name = "pool_1"
   136    protocol = "HTTP"
   137    lb_method = "ROUND_ROBIN"
   138    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   139  }
   140  
   141  resource "openstack_lb_member_v2" "member_1" {
   142    address = "192.168.199.10"
   143    protocol_port = 8080
   144    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   145    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   146  }
   147  `
   148  
   149  const TestAccLBV2MemberConfig_update = `
   150  resource "openstack_networking_network_v2" "network_1" {
   151    name = "network_1"
   152    admin_state_up = "true"
   153  }
   154  
   155  resource "openstack_networking_subnet_v2" "subnet_1" {
   156    name = "subnet_1"
   157    cidr = "192.168.199.0/24"
   158    ip_version = 4
   159    network_id = "${openstack_networking_network_v2.network_1.id}"
   160  }
   161  
   162  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   163    name = "loadbalancer_1"
   164    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   165  }
   166  
   167  resource "openstack_lb_listener_v2" "listener_1" {
   168    name = "listener_1"
   169    protocol = "HTTP"
   170    protocol_port = 8080
   171    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   172  }
   173  
   174  resource "openstack_lb_pool_v2" "pool_1" {
   175    name = "pool_1"
   176    protocol = "HTTP"
   177    lb_method = "ROUND_ROBIN"
   178    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   179  }
   180  
   181  resource "openstack_lb_member_v2" "member_1" {
   182    address = "192.168.199.10"
   183    protocol_port = 8080
   184    weight = 10
   185    admin_state_up = "true"
   186    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   187    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   188  }
   189  `
   190  
   191  const TestAccLBV2MemberConfig_timeout = `
   192  resource "openstack_networking_network_v2" "network_1" {
   193    name = "network_1"
   194    admin_state_up = "true"
   195  }
   196  
   197  resource "openstack_networking_subnet_v2" "subnet_1" {
   198    name = "subnet_1"
   199    network_id = "${openstack_networking_network_v2.network_1.id}"
   200    cidr = "192.168.199.0/24"
   201    ip_version = 4
   202  }
   203  
   204  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   205    name = "loadbalancer_1"
   206    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   207  }
   208  
   209  resource "openstack_lb_listener_v2" "listener_1" {
   210    name = "listener_1"
   211    protocol = "HTTP"
   212    protocol_port = 8080
   213    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   214  }
   215  
   216  resource "openstack_lb_pool_v2" "pool_1" {
   217    name = "pool_1"
   218    protocol = "HTTP"
   219    lb_method = "ROUND_ROBIN"
   220    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   221  }
   222  
   223  resource "openstack_lb_member_v2" "member_1" {
   224    address = "192.168.199.10"
   225    protocol_port = 8080
   226    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   227    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   228  
   229    timeouts {
   230      create = "5m"
   231      delete = "5m"
   232    }
   233  }
   234  `