github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_lb_member_v1_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/members"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccLBV1Member_basic(t *testing.T) {
    13  	var member members.Member
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckLBV1MemberDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: testAccLBV1Member_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckLBV1MemberExists(t, "openstack_lb_member_v1.member_1", &member),
    24  				),
    25  			},
    26  			resource.TestStep{
    27  				Config: testAccLBV1Member_update,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					resource.TestCheckResourceAttr("openstack_lb_member_v1.member_1", "admin_state_up", "false"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckLBV1MemberDestroy(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("(testAccCheckLBV1MemberDestroy) Error creating OpenStack networking client: %s", err)
    41  	}
    42  
    43  	for _, rs := range s.RootModule().Resources {
    44  		if rs.Type != "openstack_lb_member_v1" {
    45  			continue
    46  		}
    47  
    48  		_, err := members.Get(networkingClient, rs.Primary.ID).Extract()
    49  		if err == nil {
    50  			return fmt.Errorf("LB Member still exists")
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func testAccCheckLBV1MemberExists(t *testing.T, n string, member *members.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("(testAccCheckLBV1MemberExists) Error creating OpenStack networking client: %s", err)
    72  		}
    73  
    74  		found, err := members.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  		*member = *found
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  var testAccLBV1Member_basic = fmt.Sprintf(`
    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      network_id = "${openstack_networking_network_v2.network_1.id}"
    97      cidr = "192.168.199.0/24"
    98      ip_version = 4
    99    }
   100  
   101    resource "openstack_lb_pool_v1" "pool_1" {
   102      name = "tf_test_lb_pool"
   103      protocol = "HTTP"
   104      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   105      lb_method = "ROUND_ROBIN"
   106    }
   107  
   108    resource "openstack_lb_member_v1" "member_1" {
   109      pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   110      address = "192.168.199.10"
   111      port = 80
   112      admin_state_up = true
   113    }`)
   114  
   115  var testAccLBV1Member_update = fmt.Sprintf(`
   116    resource "openstack_networking_network_v2" "network_1" {
   117      name = "network_1"
   118      admin_state_up = "true"
   119    }
   120  
   121    resource "openstack_networking_subnet_v2" "subnet_1" {
   122      network_id = "${openstack_networking_network_v2.network_1.id}"
   123      cidr = "192.168.199.0/24"
   124      ip_version = 4
   125    }
   126  
   127    resource "openstack_lb_pool_v1" "pool_1" {
   128      name = "tf_test_lb_pool"
   129      protocol = "HTTP"
   130      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   131      lb_method = "ROUND_ROBIN"
   132    }
   133  
   134    resource "openstack_lb_member_v1" "member_1" {
   135      pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   136      address = "192.168.199.10"
   137      port = 80
   138      admin_state_up = false
   139    }`)