github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_lb_pool_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/rackspace/gophercloud/openstack/compute/v2/extensions/secgroups"
    11  	"github.com/rackspace/gophercloud/openstack/compute/v2/servers"
    12  	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/monitors"
    13  	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/pools"
    14  	"github.com/rackspace/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
    15  	"github.com/rackspace/gophercloud/openstack/networking/v2/networks"
    16  	"github.com/rackspace/gophercloud/openstack/networking/v2/subnets"
    17  )
    18  
    19  func TestAccLBV1Pool_basic(t *testing.T) {
    20  	var pool pools.Pool
    21  
    22  	resource.Test(t, resource.TestCase{
    23  		PreCheck:     func() { testAccPreCheck(t) },
    24  		Providers:    testAccProviders,
    25  		CheckDestroy: testAccCheckLBV1PoolDestroy,
    26  		Steps: []resource.TestStep{
    27  			resource.TestStep{
    28  				Config: testAccLBV1Pool_basic,
    29  				Check: resource.ComposeTestCheckFunc(
    30  					testAccCheckLBV1PoolExists(t, "openstack_lb_pool_v1.pool_1", &pool),
    31  				),
    32  			},
    33  			resource.TestStep{
    34  				Config: testAccLBV1Pool_update,
    35  				Check: resource.ComposeTestCheckFunc(
    36  					resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "name", "tf_test_lb_pool_updated"),
    37  				),
    38  			},
    39  		},
    40  	})
    41  }
    42  
    43  func TestAccLBV1Pool_fullstack(t *testing.T) {
    44  	var instance1, instance2 servers.Server
    45  	var monitor monitors.Monitor
    46  	var network networks.Network
    47  	var pool pools.Pool
    48  	var secgroup secgroups.SecurityGroup
    49  	var subnet subnets.Subnet
    50  	var vip vips.VirtualIP
    51  
    52  	resource.Test(t, resource.TestCase{
    53  		PreCheck:     func() { testAccPreCheck(t) },
    54  		Providers:    testAccProviders,
    55  		CheckDestroy: testAccCheckLBV1PoolDestroy,
    56  		Steps: []resource.TestStep{
    57  			resource.TestStep{
    58  				Config: testAccLBV1Pool_fullstack,
    59  				Check: resource.ComposeTestCheckFunc(
    60  					testAccCheckNetworkingV2NetworkExists(t, "openstack_networking_network_v2.network_1", &network),
    61  					testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
    62  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup),
    63  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1),
    64  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_2", &instance2),
    65  					testAccCheckLBV1PoolExists(t, "openstack_lb_pool_v1.pool_1", &pool),
    66  					testAccCheckLBV1MonitorExists(t, "openstack_lb_monitor_v1.monitor_1", &monitor),
    67  					testAccCheckLBV1VIPExists(t, "openstack_lb_vip_v1.vip_1", &vip),
    68  				),
    69  			},
    70  		},
    71  	})
    72  }
    73  
    74  func testAccCheckLBV1PoolDestroy(s *terraform.State) error {
    75  	config := testAccProvider.Meta().(*Config)
    76  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    77  	if err != nil {
    78  		return fmt.Errorf("(testAccCheckLBV1PoolDestroy) Error creating OpenStack networking client: %s", err)
    79  	}
    80  
    81  	for _, rs := range s.RootModule().Resources {
    82  		if rs.Type != "openstack_lb_pool_v1" {
    83  			continue
    84  		}
    85  
    86  		_, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
    87  		if err == nil {
    88  			return fmt.Errorf("LB Pool still exists")
    89  		}
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func testAccCheckLBV1PoolExists(t *testing.T, n string, pool *pools.Pool) resource.TestCheckFunc {
    96  	return func(s *terraform.State) error {
    97  		rs, ok := s.RootModule().Resources[n]
    98  		if !ok {
    99  			return fmt.Errorf("Not found: %s", n)
   100  		}
   101  
   102  		if rs.Primary.ID == "" {
   103  			return fmt.Errorf("No ID is set")
   104  		}
   105  
   106  		config := testAccProvider.Meta().(*Config)
   107  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   108  		if err != nil {
   109  			return fmt.Errorf("(testAccCheckLBV1PoolExists) Error creating OpenStack networking client: %s", err)
   110  		}
   111  
   112  		found, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
   113  		if err != nil {
   114  			return err
   115  		}
   116  
   117  		if found.ID != rs.Primary.ID {
   118  			return fmt.Errorf("Pool not found")
   119  		}
   120  
   121  		*pool = *found
   122  
   123  		return nil
   124  	}
   125  }
   126  
   127  var testAccLBV1Pool_basic = fmt.Sprintf(`
   128    resource "openstack_networking_network_v2" "network_1" {
   129      region = "%s"
   130      name = "network_1"
   131      admin_state_up = "true"
   132    }
   133  
   134    resource "openstack_networking_subnet_v2" "subnet_1" {
   135      region = "%s"
   136      network_id = "${openstack_networking_network_v2.network_1.id}"
   137      cidr = "192.168.199.0/24"
   138      ip_version = 4
   139    }
   140  
   141    resource "openstack_lb_pool_v1" "pool_1" {
   142      region = "%s"
   143      name = "tf_test_lb_pool"
   144      protocol = "HTTP"
   145      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   146      lb_method = "ROUND_ROBIN"
   147    }`,
   148  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)
   149  
   150  var testAccLBV1Pool_update = fmt.Sprintf(`
   151    resource "openstack_networking_network_v2" "network_1" {
   152      region = "%s"
   153      name = "network_1"
   154      admin_state_up = "true"
   155    }
   156  
   157    resource "openstack_networking_subnet_v2" "subnet_1" {
   158      region = "%s"
   159      network_id = "${openstack_networking_network_v2.network_1.id}"
   160      cidr = "192.168.199.0/24"
   161      ip_version = 4
   162    }
   163  
   164    resource "openstack_lb_pool_v1" "pool_1" {
   165      region = "%s"
   166      name = "tf_test_lb_pool_updated"
   167      protocol = "HTTP"
   168      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   169      lb_method = "ROUND_ROBIN"
   170    }`,
   171  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)
   172  
   173  var testAccLBV1Pool_fullstack = fmt.Sprintf(`
   174  	resource "openstack_networking_network_v2" "network_1" {
   175  		name = "network_1"
   176  		admin_state_up = "true"
   177  	}
   178  
   179  	resource "openstack_networking_subnet_v2" "subnet_1" {
   180  		network_id = "${openstack_networking_network_v2.network_1.id}"
   181  		cidr = "192.168.199.0/24"
   182  		ip_version = 4
   183  	}
   184  
   185  	resource "openstack_compute_secgroup_v2" "secgroup_1" {
   186  		name = "secgroup_1"
   187  		description = "Rules for secgroup_1"
   188  
   189  		rule {
   190  			from_port = -1
   191  			to_port = -1
   192  			ip_protocol = "icmp"
   193  			cidr = "0.0.0.0/0"
   194  		}
   195  
   196  		rule {
   197  			from_port = 80
   198  			to_port = 80
   199  			ip_protocol = "tcp"
   200  			cidr = "0.0.0.0/0"
   201  		}
   202  	}
   203  
   204  	resource "openstack_compute_instance_v2" "instance_1" {
   205  		name = "instance_1"
   206  		security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   207  		network {
   208  			uuid = "${openstack_networking_network_v2.network_1.id}"
   209  		}
   210  	}
   211  
   212  	resource "openstack_compute_instance_v2" "instance_2" {
   213  		name = "instance_2"
   214  		security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   215  		network {
   216  			uuid = "${openstack_networking_network_v2.network_1.id}"
   217  		}
   218  	}
   219  
   220  	resource "openstack_lb_monitor_v1" "monitor_1" {
   221  		type = "TCP"
   222  		delay = 30
   223  		timeout = 5
   224  		max_retries = 3
   225  		admin_state_up = "true"
   226  	}
   227  
   228  	resource "openstack_lb_pool_v1" "pool_1" {
   229  		name = "pool_1"
   230  		protocol = "TCP"
   231  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   232  		lb_method = "ROUND_ROBIN"
   233  		monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"]
   234  
   235  		member {
   236  			address = "${openstack_compute_instance_v2.instance_1.access_ip_v4}"
   237  			port = 80
   238  			admin_state_up = "true"
   239  		}
   240  
   241  		member {
   242  			address = "${openstack_compute_instance_v2.instance_2.access_ip_v4}"
   243  			port = 80
   244  			admin_state_up = "true"
   245  		}
   246  	}
   247  
   248  	resource "openstack_lb_vip_v1" "vip_1" {
   249  		name = "vip_1"
   250  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   251  		protocol = "TCP"
   252  		port = 80
   253  		pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   254  	}`)