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