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

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/hashicorp/terraform/helper/resource"
     8  	"github.com/hashicorp/terraform/terraform"
     9  
    10  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
    11  )
    12  
    13  func TestAccLBV1VIP_basic(t *testing.T) {
    14  	var vip vips.VirtualIP
    15  
    16  	resource.Test(t, resource.TestCase{
    17  		PreCheck:     func() { testAccPreCheck(t) },
    18  		Providers:    testAccProviders,
    19  		CheckDestroy: testAccCheckLBV1VIPDestroy,
    20  		Steps: []resource.TestStep{
    21  			resource.TestStep{
    22  				Config: testAccLBV1VIP_basic,
    23  				Check: resource.ComposeTestCheckFunc(
    24  					testAccCheckLBV1VIPExists(t, "openstack_lb_vip_v1.vip_1", &vip),
    25  				),
    26  			},
    27  			resource.TestStep{
    28  				Config: testAccLBV1VIP_update,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					resource.TestCheckResourceAttr("openstack_lb_vip_v1.vip_1", "name", "tf_test_lb_vip_updated"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func testAccCheckLBV1VIPDestroy(s *terraform.State) error {
    38  	config := testAccProvider.Meta().(*Config)
    39  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    40  	if err != nil {
    41  		return fmt.Errorf("(testAccCheckLBV1VIPDestroy) Error creating OpenStack networking client: %s", err)
    42  	}
    43  
    44  	for _, rs := range s.RootModule().Resources {
    45  		if rs.Type != "openstack_lb_vip_v1" {
    46  			continue
    47  		}
    48  
    49  		_, err := vips.Get(networkingClient, rs.Primary.ID).Extract()
    50  		if err == nil {
    51  			return fmt.Errorf("LB VIP still exists")
    52  		}
    53  	}
    54  
    55  	return nil
    56  }
    57  
    58  func testAccCheckLBV1VIPExists(t *testing.T, n string, vip *vips.VirtualIP) 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("(testAccCheckLBV1VIPExists) Error creating OpenStack networking client: %s", err)
    73  		}
    74  
    75  		found, err := vips.Get(networkingClient, rs.Primary.ID).Extract()
    76  		if err != nil {
    77  			return err
    78  		}
    79  
    80  		if found.ID != rs.Primary.ID {
    81  			return fmt.Errorf("VIP not found")
    82  		}
    83  
    84  		*vip = *found
    85  
    86  		return nil
    87  	}
    88  }
    89  
    90  var testAccLBV1VIP_basic = fmt.Sprintf(`
    91    resource "openstack_networking_network_v2" "network_1" {
    92      region = "%s"
    93      name = "network_1"
    94      admin_state_up = "true"
    95    }
    96  
    97    resource "openstack_networking_subnet_v2" "subnet_1" {
    98      region = "%s"
    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_pool_v1" "pool_1" {
   105      region = "%s"
   106      name = "tf_test_lb_pool"
   107      protocol = "HTTP"
   108      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   109      lb_method = "ROUND_ROBIN"
   110    }
   111  
   112    resource "openstack_lb_vip_v1" "vip_1" {
   113      region = "RegionOne"
   114      name = "tf_test_lb_vip"
   115      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   116      protocol = "HTTP"
   117      port = 80
   118      pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   119      admin_state_up = true
   120      persistence {
   121        type = "SOURCE_IP"
   122      }
   123    }`,
   124  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)
   125  
   126  var testAccLBV1VIP_update = fmt.Sprintf(`
   127    resource "openstack_networking_network_v2" "network_1" {
   128      region = "%s"
   129      name = "network_1"
   130      admin_state_up = "true"
   131    }
   132  
   133    resource "openstack_networking_subnet_v2" "subnet_1" {
   134      region = "%s"
   135      network_id = "${openstack_networking_network_v2.network_1.id}"
   136      cidr = "192.168.199.0/24"
   137      ip_version = 4
   138    }
   139  
   140    resource "openstack_lb_pool_v1" "pool_1" {
   141      region = "%s"
   142      name = "tf_test_lb_pool"
   143      protocol = "HTTP"
   144      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   145      lb_method = "ROUND_ROBIN"
   146    }
   147  
   148    resource "openstack_lb_vip_v1" "vip_1" {
   149      region = "RegionOne"
   150      name = "tf_test_lb_vip_updated"
   151      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   152      protocol = "HTTP"
   153      port = 80
   154      pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   155      admin_state_up = true
   156      persistence {
   157        type = "SOURCE_IP"
   158      }
   159    }`,
   160  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)