github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 TestAccLBV1Member_timeout(t *testing.T) {
    37  	var member members.Member
    38  
    39  	resource.Test(t, resource.TestCase{
    40  		PreCheck:     func() { testAccPreCheck(t) },
    41  		Providers:    testAccProviders,
    42  		CheckDestroy: testAccCheckLBV1MemberDestroy,
    43  		Steps: []resource.TestStep{
    44  			resource.TestStep{
    45  				Config: testAccLBV1Member_timeout,
    46  				Check: resource.ComposeTestCheckFunc(
    47  					testAccCheckLBV1MemberExists("openstack_lb_member_v1.member_1", &member),
    48  				),
    49  			},
    50  		},
    51  	})
    52  }
    53  
    54  func testAccCheckLBV1MemberDestroy(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_v1" {
    63  			continue
    64  		}
    65  
    66  		_, err := members.Get(networkingClient, rs.Primary.ID).Extract()
    67  		if err == nil {
    68  			return fmt.Errorf("LB Member still exists")
    69  		}
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  func testAccCheckLBV1MemberExists(n string, member *members.Member) resource.TestCheckFunc {
    76  	return func(s *terraform.State) error {
    77  		rs, ok := s.RootModule().Resources[n]
    78  		if !ok {
    79  			return fmt.Errorf("Not found: %s", n)
    80  		}
    81  
    82  		if rs.Primary.ID == "" {
    83  			return fmt.Errorf("No ID is set")
    84  		}
    85  
    86  		config := testAccProvider.Meta().(*Config)
    87  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    88  		if err != nil {
    89  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
    90  		}
    91  
    92  		found, err := members.Get(networkingClient, rs.Primary.ID).Extract()
    93  		if err != nil {
    94  			return err
    95  		}
    96  
    97  		if found.ID != rs.Primary.ID {
    98  			return fmt.Errorf("Member not found")
    99  		}
   100  
   101  		*member = *found
   102  
   103  		return nil
   104  	}
   105  }
   106  
   107  const testAccLBV1Member_basic = `
   108  resource "openstack_networking_network_v2" "network_1" {
   109    name = "network_1"
   110    admin_state_up = "true"
   111  }
   112  
   113  resource "openstack_networking_subnet_v2" "subnet_1" {
   114    cidr = "192.168.199.0/24"
   115    ip_version = 4
   116    network_id = "${openstack_networking_network_v2.network_1.id}"
   117  }
   118  
   119  resource "openstack_lb_pool_v1" "pool_1" {
   120    name = "pool_1"
   121    protocol = "HTTP"
   122    lb_method = "ROUND_ROBIN"
   123    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   124  }
   125  
   126  resource "openstack_lb_member_v1" "member_1" {
   127    address = "192.168.199.10"
   128    port = 80
   129    admin_state_up = true
   130    pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   131  }
   132  `
   133  
   134  const testAccLBV1Member_update = `
   135  resource "openstack_networking_network_v2" "network_1" {
   136    name = "network_1"
   137    admin_state_up = "true"
   138  }
   139  
   140  resource "openstack_networking_subnet_v2" "subnet_1" {
   141    cidr = "192.168.199.0/24"
   142    ip_version = 4
   143    network_id = "${openstack_networking_network_v2.network_1.id}"
   144  }
   145  
   146  resource "openstack_lb_pool_v1" "pool_1" {
   147    name = "pool_1"
   148    protocol = "HTTP"
   149    lb_method = "ROUND_ROBIN"
   150    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   151  }
   152  
   153  resource "openstack_lb_member_v1" "member_1" {
   154    address = "192.168.199.10"
   155    port = 80
   156    admin_state_up = false
   157    pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   158  }
   159  `
   160  
   161  const testAccLBV1Member_timeout = `
   162  resource "openstack_networking_network_v2" "network_1" {
   163    name = "network_1"
   164    admin_state_up = "true"
   165  }
   166  
   167  resource "openstack_networking_subnet_v2" "subnet_1" {
   168    cidr = "192.168.199.0/24"
   169    ip_version = 4
   170    network_id = "${openstack_networking_network_v2.network_1.id}"
   171  }
   172  
   173  resource "openstack_lb_pool_v1" "pool_1" {
   174    name = "pool_1"
   175    protocol = "HTTP"
   176    lb_method = "ROUND_ROBIN"
   177    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   178  }
   179  
   180  resource "openstack_lb_member_v1" "member_1" {
   181    address = "192.168.199.10"
   182    port = 80
   183    admin_state_up = true
   184    pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   185  
   186    timeouts {
   187      create = "5m"
   188      delete = "5m"
   189    }
   190  }
   191  `