github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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(t, "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 testAccCheckLBV2MemberDestroy(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("(testAccCheckLBV2MemberDestroy) Error creating OpenStack networking client: %s", err)
    41  	}
    42  
    43  	for _, rs := range s.RootModule().Resources {
    44  		if rs.Type != "openstack_lb_member_v2" {
    45  			continue
    46  		}
    47  
    48  		_, err := pools.GetMember(networkingClient, rs.Primary.Attributes["pool_id"], rs.Primary.ID).Extract()
    49  		if err == nil {
    50  			return fmt.Errorf("Member still exists: %s", rs.Primary.ID)
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func testAccCheckLBV2MemberExists(t *testing.T, n string, member *pools.Member) 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("(testAccCheckLBV2MemberExists) Error creating OpenStack networking client: %s", err)
    72  		}
    73  
    74  		found, err := pools.GetMember(networkingClient, rs.Primary.Attributes["pool_id"], 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  		*member = *found
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  var TestAccLBV2MemberConfig_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  	resource "openstack_lb_member_v2" "member_1" {
   122  		address = "192.168.199.10"
   123  		pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   124  		protocol_port = 8080
   125  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   126  	}`)
   127  
   128  var TestAccLBV2MemberConfig_update = fmt.Sprintf(`
   129  	resource "openstack_networking_network_v2" "network_1" {
   130  		name = "tf_test_network"
   131  		admin_state_up = "true"
   132  	}
   133  
   134  	resource "openstack_networking_subnet_v2" "subnet_1" {
   135  		network_id = "${openstack_networking_network_v2.network_1.id}"
   136  		cidr = "192.168.199.0/24"
   137  		ip_version = 4
   138  		name = "tf_test_subnet"
   139  	}
   140  
   141  	resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   142  		vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   143  		name = "tf_test_loadbalancer_v2"
   144  	}
   145  
   146  	resource "openstack_lb_listener_v2" "listener_1" {
   147  		protocol = "HTTP"
   148  		protocol_port = 8080
   149  		loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   150  		name = "tf_test_listener"
   151  	}
   152  
   153  	resource "openstack_lb_pool_v2" "pool_1" {
   154  		protocol = "HTTP"
   155  		lb_method = "ROUND_ROBIN"
   156  		listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   157  		name = "tf_test_pool"
   158  	}
   159  
   160  	resource "openstack_lb_member_v2" "member_1" {
   161  		address = "192.168.199.10"
   162  		pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   163  		protocol_port = 8080
   164  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   165  		weight = 10
   166  		admin_state_up = "true"
   167  	}`)