github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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("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", "vip_1_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("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(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("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  const testAccLBV1VIP_basic = `
    91  resource "openstack_networking_network_v2" "network_1" {
    92    name = "network_1"
    93    admin_state_up = "true"
    94  }
    95  
    96  resource "openstack_networking_subnet_v2" "subnet_1" {
    97    cidr = "192.168.199.0/24"
    98    ip_version = 4
    99    network_id = "${openstack_networking_network_v2.network_1.id}"
   100  }
   101  
   102  resource "openstack_lb_pool_v1" "pool_1" {
   103    name = "pool_1"
   104    protocol = "HTTP"
   105    lb_method = "ROUND_ROBIN"
   106    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   107  }
   108  
   109  resource "openstack_lb_vip_v1" "vip_1" {
   110    name = "vip_1"
   111    protocol = "HTTP"
   112    port = 80
   113    admin_state_up = true
   114    pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   115    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   116  
   117    persistence {
   118      type = "SOURCE_IP"
   119    }
   120  }
   121  `
   122  
   123  const testAccLBV1VIP_update = `
   124  resource "openstack_networking_network_v2" "network_1" {
   125    name = "network_1"
   126    admin_state_up = "true"
   127  }
   128  
   129  resource "openstack_networking_subnet_v2" "subnet_1" {
   130    cidr = "192.168.199.0/24"
   131    ip_version = 4
   132    network_id = "${openstack_networking_network_v2.network_1.id}"
   133  }
   134  
   135  resource "openstack_lb_pool_v1" "pool_1" {
   136    name = "pool_1"
   137    protocol = "HTTP"
   138    lb_method = "ROUND_ROBIN"
   139    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   140  }
   141  
   142  resource "openstack_lb_vip_v1" "vip_1" {
   143    name = "vip_1_updated"
   144    protocol = "HTTP"
   145    port = 80
   146    admin_state_up = true
   147    pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   148    subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   149  
   150    persistence {
   151      type = "SOURCE_IP"
   152    }
   153  }
   154  `