github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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("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("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(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("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  const testAccLBV1Member_basic = `
    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    cidr = "192.168.199.0/24"
    97    ip_version = 4
    98    network_id = "${openstack_networking_network_v2.network_1.id}"
    99  }
   100  
   101  resource "openstack_lb_pool_v1" "pool_1" {
   102    name = "pool_1"
   103    protocol = "HTTP"
   104    lb_method = "ROUND_ROBIN"
   105    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   106  }
   107  
   108  resource "openstack_lb_member_v1" "member_1" {
   109    address = "192.168.199.10"
   110    port = 80
   111    admin_state_up = true
   112    pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   113  }
   114  `
   115  
   116  const testAccLBV1Member_update = `
   117  resource "openstack_networking_network_v2" "network_1" {
   118    name = "network_1"
   119    admin_state_up = "true"
   120  }
   121  
   122  resource "openstack_networking_subnet_v2" "subnet_1" {
   123    cidr = "192.168.199.0/24"
   124    ip_version = 4
   125    network_id = "${openstack_networking_network_v2.network_1.id}"
   126  }
   127  
   128  resource "openstack_lb_pool_v1" "pool_1" {
   129    name = "pool_1"
   130    protocol = "HTTP"
   131    lb_method = "ROUND_ROBIN"
   132    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   133  }
   134  
   135  resource "openstack_lb_member_v1" "member_1" {
   136    address = "192.168.199.10"
   137    port = 80
   138    admin_state_up = false
   139    pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   140  }
   141  `