github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/openstack/resource_openstack_lb_loadbalancer_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/loadbalancers"
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  func TestAccLBV2LoadBalancer_basic(t *testing.T) {
    13  	var lb loadbalancers.LoadBalancer
    14  
    15  	resource.Test(t, resource.TestCase{
    16  		PreCheck:     func() { testAccPreCheck(t) },
    17  		Providers:    testAccProviders,
    18  		CheckDestroy: testAccCheckLBV2LoadBalancerDestroy,
    19  		Steps: []resource.TestStep{
    20  			resource.TestStep{
    21  				Config: TestAccLBV2LoadBalancerConfig_basic,
    22  				Check: resource.ComposeTestCheckFunc(
    23  					testAccCheckLBV2LoadBalancerExists(t, "openstack_lb_loadbalancer_v2.loadbalancer_1", &lb),
    24  				),
    25  			},
    26  			resource.TestStep{
    27  				Config: TestAccLBV2LoadBalancerConfig_update,
    28  				Check: resource.ComposeTestCheckFunc(
    29  					resource.TestCheckResourceAttr("openstack_lb_loadbalancer_v2.loadbalancer_1", "name", "tf_test_loadbalancer_v2_updated"),
    30  				),
    31  			},
    32  		},
    33  	})
    34  }
    35  
    36  func testAccCheckLBV2LoadBalancerDestroy(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("(testAccCheckLBV2LoadBalancerDestroy) Error creating OpenStack networking client: %s", err)
    41  	}
    42  
    43  	for _, rs := range s.RootModule().Resources {
    44  		if rs.Type != "openstack_lb_loadbalancer_v2" {
    45  			continue
    46  		}
    47  
    48  		_, err := loadbalancers.Get(networkingClient, rs.Primary.ID).Extract()
    49  		if err == nil {
    50  			return fmt.Errorf("LoadBalancer still exists: %s", rs.Primary.ID)
    51  		}
    52  	}
    53  
    54  	return nil
    55  }
    56  
    57  func testAccCheckLBV2LoadBalancerExists(t *testing.T, n string, lb *loadbalancers.LoadBalancer) 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("(testAccCheckLBV2LoadBalancerExists) Error creating OpenStack networking client: %s", err)
    72  		}
    73  
    74  		found, err := loadbalancers.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  		*lb = *found
    84  
    85  		return nil
    86  	}
    87  }
    88  
    89  var TestAccLBV2LoadBalancerConfig_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  var TestAccLBV2LoadBalancerConfig_update = fmt.Sprintf(`
   108    resource "openstack_networking_network_v2" "network_1" {
   109  		name = "tf_test_network"
   110  		admin_state_up = "true"
   111    }
   112  
   113    resource "openstack_networking_subnet_v2" "subnet_1" {
   114  		network_id = "${openstack_networking_network_v2.network_1.id}"
   115  		cidr = "192.168.199.0/24"
   116  		ip_version = 4
   117  		name = "tf_test_subnet"
   118    }
   119  
   120    resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   121  		vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   122  		name = "tf_test_loadbalancer_v2_updated"
   123  		admin_state_up = "true"
   124    }
   125  `)