github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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 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("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  		poolId := rs.Primary.Attributes["pool_id"]
    49  		_, err := pools.GetMember(networkingClient, poolId, rs.Primary.ID).Extract()
    50  		if err == nil {
    51  			return fmt.Errorf("Member still exists: %s", rs.Primary.ID)
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckLBV2MemberExists(n string, member *pools.Member) resource.TestCheckFunc {
    59  	return func(s *terraform.State) error {
    60  		rs, ok := s.RootModule().Resources[n]
    61  		if !ok {
    62  			return fmt.Errorf("Not found: %s", n)
    63  		}
    64  
    65  		if rs.Primary.ID == "" {
    66  			return fmt.Errorf("No ID is set")
    67  		}
    68  
    69  		config := testAccProvider.Meta().(*Config)
    70  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    71  		if err != nil {
    72  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    73  		}
    74  
    75  		poolId := rs.Primary.Attributes["pool_id"]
    76  		found, err := pools.GetMember(networkingClient, poolId, rs.Primary.ID).Extract()
    77  		if err != nil {
    78  			return err
    79  		}
    80  
    81  		if found.ID != rs.Primary.ID {
    82  			return fmt.Errorf("Member not found")
    83  		}
    84  
    85  		*member = *found
    86  
    87  		return nil
    88  	}
    89  }
    90  
    91  const TestAccLBV2MemberConfig_basic = `
    92  resource "openstack_networking_network_v2" "network_1" {
    93    name = "network_1"
    94    admin_state_up = "true"
    95  }
    96  
    97  resource "openstack_networking_subnet_v2" "subnet_1" {
    98    name = "subnet_1"
    99    network_id = "${openstack_networking_network_v2.network_1.id}"
   100    cidr = "192.168.199.0/24"
   101    ip_version = 4
   102  }
   103  
   104  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   105    name = "loadbalancer_1"
   106    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   107  }
   108  
   109  resource "openstack_lb_listener_v2" "listener_1" {
   110    name = "listener_1"
   111    protocol = "HTTP"
   112    protocol_port = 8080
   113    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   114  }
   115  
   116  resource "openstack_lb_pool_v2" "pool_1" {
   117    name = "pool_1"
   118    protocol = "HTTP"
   119    lb_method = "ROUND_ROBIN"
   120    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   121  }
   122  
   123  resource "openstack_lb_member_v2" "member_1" {
   124    address = "192.168.199.10"
   125    protocol_port = 8080
   126    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   127    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   128  }
   129  `
   130  
   131  const TestAccLBV2MemberConfig_update = `
   132  resource "openstack_networking_network_v2" "network_1" {
   133    name = "network_1"
   134    admin_state_up = "true"
   135  }
   136  
   137  resource "openstack_networking_subnet_v2" "subnet_1" {
   138    name = "subnet_1"
   139    cidr = "192.168.199.0/24"
   140    ip_version = 4
   141    network_id = "${openstack_networking_network_v2.network_1.id}"
   142  }
   143  
   144  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   145    name = "loadbalancer_1"
   146    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   147  }
   148  
   149  resource "openstack_lb_listener_v2" "listener_1" {
   150    name = "listener_1"
   151    protocol = "HTTP"
   152    protocol_port = 8080
   153    loadbalancer_id = "${openstack_lb_loadbalancer_v2.loadbalancer_1.id}"
   154  }
   155  
   156  resource "openstack_lb_pool_v2" "pool_1" {
   157    name = "pool_1"
   158    protocol = "HTTP"
   159    lb_method = "ROUND_ROBIN"
   160    listener_id = "${openstack_lb_listener_v2.listener_1.id}"
   161  }
   162  
   163  resource "openstack_lb_member_v2" "member_1" {
   164    address = "192.168.199.10"
   165    protocol_port = 8080
   166    weight = 10
   167    admin_state_up = "true"
   168    pool_id = "${openstack_lb_pool_v2.pool_1.id}"
   169    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   170  }
   171  `