github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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/gophercloud/gophercloud/openstack/compute/v2/extensions/secgroups"
    11  	"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
    12  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/monitors"
    13  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/pools"
    14  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas/vips"
    15  	"github.com/gophercloud/gophercloud/openstack/networking/v2/networks"
    16  	"github.com/gophercloud/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  					resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "lb_provider", "haproxy"),
    32  				),
    33  			},
    34  			resource.TestStep{
    35  				Config: testAccLBV1Pool_update,
    36  				Check: resource.ComposeTestCheckFunc(
    37  					resource.TestCheckResourceAttr("openstack_lb_pool_v1.pool_1", "name", "tf_test_lb_pool_updated"),
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func TestAccLBV1Pool_fullstack(t *testing.T) {
    45  	var instance1, instance2 servers.Server
    46  	var monitor monitors.Monitor
    47  	var network networks.Network
    48  	var pool pools.Pool
    49  	var secgroup secgroups.SecurityGroup
    50  	var subnet subnets.Subnet
    51  	var vip vips.VirtualIP
    52  
    53  	resource.Test(t, resource.TestCase{
    54  		PreCheck:     func() { testAccPreCheck(t) },
    55  		Providers:    testAccProviders,
    56  		CheckDestroy: testAccCheckLBV1PoolDestroy,
    57  		Steps: []resource.TestStep{
    58  			resource.TestStep{
    59  				Config: testAccLBV1Pool_fullstack_1,
    60  				Check: resource.ComposeTestCheckFunc(
    61  					testAccCheckNetworkingV2NetworkExists(t, "openstack_networking_network_v2.network_1", &network),
    62  					testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
    63  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup),
    64  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1),
    65  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_2", &instance2),
    66  					testAccCheckLBV1PoolExists(t, "openstack_lb_pool_v1.pool_1", &pool),
    67  					testAccCheckLBV1MonitorExists(t, "openstack_lb_monitor_v1.monitor_1", &monitor),
    68  					testAccCheckLBV1VIPExists(t, "openstack_lb_vip_v1.vip_1", &vip),
    69  				),
    70  			},
    71  			resource.TestStep{
    72  				Config: testAccLBV1Pool_fullstack_2,
    73  				Check: resource.ComposeTestCheckFunc(
    74  					testAccCheckNetworkingV2NetworkExists(t, "openstack_networking_network_v2.network_1", &network),
    75  					testAccCheckNetworkingV2SubnetExists(t, "openstack_networking_subnet_v2.subnet_1", &subnet),
    76  					testAccCheckComputeV2SecGroupExists(t, "openstack_compute_secgroup_v2.secgroup_1", &secgroup),
    77  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_1", &instance1),
    78  					testAccCheckComputeV2InstanceExists(t, "openstack_compute_instance_v2.instance_2", &instance2),
    79  					testAccCheckLBV1PoolExists(t, "openstack_lb_pool_v1.pool_1", &pool),
    80  					testAccCheckLBV1MonitorExists(t, "openstack_lb_monitor_v1.monitor_1", &monitor),
    81  					testAccCheckLBV1VIPExists(t, "openstack_lb_vip_v1.vip_1", &vip),
    82  				),
    83  			},
    84  		},
    85  	})
    86  }
    87  
    88  func testAccCheckLBV1PoolDestroy(s *terraform.State) error {
    89  	config := testAccProvider.Meta().(*Config)
    90  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
    91  	if err != nil {
    92  		return fmt.Errorf("(testAccCheckLBV1PoolDestroy) Error creating OpenStack networking client: %s", err)
    93  	}
    94  
    95  	for _, rs := range s.RootModule().Resources {
    96  		if rs.Type != "openstack_lb_pool_v1" {
    97  			continue
    98  		}
    99  
   100  		_, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
   101  		if err == nil {
   102  			return fmt.Errorf("LB Pool still exists")
   103  		}
   104  	}
   105  
   106  	return nil
   107  }
   108  
   109  func testAccCheckLBV1PoolExists(t *testing.T, n string, pool *pools.Pool) resource.TestCheckFunc {
   110  	return func(s *terraform.State) error {
   111  		rs, ok := s.RootModule().Resources[n]
   112  		if !ok {
   113  			return fmt.Errorf("Not found: %s", n)
   114  		}
   115  
   116  		if rs.Primary.ID == "" {
   117  			return fmt.Errorf("No ID is set")
   118  		}
   119  
   120  		config := testAccProvider.Meta().(*Config)
   121  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   122  		if err != nil {
   123  			return fmt.Errorf("(testAccCheckLBV1PoolExists) Error creating OpenStack networking client: %s", err)
   124  		}
   125  
   126  		found, err := pools.Get(networkingClient, rs.Primary.ID).Extract()
   127  		if err != nil {
   128  			return err
   129  		}
   130  
   131  		if found.ID != rs.Primary.ID {
   132  			return fmt.Errorf("Pool not found")
   133  		}
   134  
   135  		*pool = *found
   136  
   137  		return nil
   138  	}
   139  }
   140  
   141  var testAccLBV1Pool_basic = fmt.Sprintf(`
   142    resource "openstack_networking_network_v2" "network_1" {
   143      region = "%s"
   144      name = "network_1"
   145      admin_state_up = "true"
   146    }
   147  
   148    resource "openstack_networking_subnet_v2" "subnet_1" {
   149      region = "%s"
   150      network_id = "${openstack_networking_network_v2.network_1.id}"
   151      cidr = "192.168.199.0/24"
   152      ip_version = 4
   153    }
   154  
   155    resource "openstack_lb_pool_v1" "pool_1" {
   156      region = "%s"
   157      name = "tf_test_lb_pool"
   158      protocol = "HTTP"
   159      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   160      lb_method = "ROUND_ROBIN"
   161      lb_provider = "haproxy"
   162    }`,
   163  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)
   164  
   165  var testAccLBV1Pool_update = fmt.Sprintf(`
   166    resource "openstack_networking_network_v2" "network_1" {
   167      region = "%s"
   168      name = "network_1"
   169      admin_state_up = "true"
   170    }
   171  
   172    resource "openstack_networking_subnet_v2" "subnet_1" {
   173      region = "%s"
   174      network_id = "${openstack_networking_network_v2.network_1.id}"
   175      cidr = "192.168.199.0/24"
   176      ip_version = 4
   177    }
   178  
   179    resource "openstack_lb_pool_v1" "pool_1" {
   180      region = "%s"
   181      name = "tf_test_lb_pool_updated"
   182      protocol = "HTTP"
   183      subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   184      lb_method = "ROUND_ROBIN"
   185    }`,
   186  	OS_REGION_NAME, OS_REGION_NAME, OS_REGION_NAME)
   187  
   188  var testAccLBV1Pool_fullstack_1 = fmt.Sprintf(`
   189  	resource "openstack_networking_network_v2" "network_1" {
   190  		name = "network_1"
   191  		admin_state_up = "true"
   192  	}
   193  
   194  	resource "openstack_networking_subnet_v2" "subnet_1" {
   195  		network_id = "${openstack_networking_network_v2.network_1.id}"
   196  		cidr = "192.168.199.0/24"
   197  		ip_version = 4
   198  	}
   199  
   200  	resource "openstack_compute_secgroup_v2" "secgroup_1" {
   201  		name = "secgroup_1"
   202  		description = "Rules for secgroup_1"
   203  
   204  		rule {
   205  			from_port = -1
   206  			to_port = -1
   207  			ip_protocol = "icmp"
   208  			cidr = "0.0.0.0/0"
   209  		}
   210  
   211  		rule {
   212  			from_port = 80
   213  			to_port = 80
   214  			ip_protocol = "tcp"
   215  			cidr = "0.0.0.0/0"
   216  		}
   217  	}
   218  
   219  	resource "openstack_compute_instance_v2" "instance_1" {
   220  		name = "instance_1"
   221  		security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   222  		network {
   223  			uuid = "${openstack_networking_network_v2.network_1.id}"
   224  		}
   225  	}
   226  
   227  	resource "openstack_compute_instance_v2" "instance_2" {
   228  		name = "instance_2"
   229  		security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   230  		network {
   231  			uuid = "${openstack_networking_network_v2.network_1.id}"
   232  		}
   233  	}
   234  
   235  	resource "openstack_lb_monitor_v1" "monitor_1" {
   236  		type = "TCP"
   237  		delay = 30
   238  		timeout = 5
   239  		max_retries = 3
   240  		admin_state_up = "true"
   241  	}
   242  
   243  	resource "openstack_lb_pool_v1" "pool_1" {
   244  		name = "pool_1"
   245  		protocol = "TCP"
   246  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   247  		lb_method = "ROUND_ROBIN"
   248  		monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"]
   249  	}
   250  
   251  	resource "openstack_lb_member_v1" "member_1" {
   252  		pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   253  		address = "${openstack_compute_instance_v2.instance_1.access_ip_v4}"
   254  		port = 80
   255  		admin_state_up = true
   256  	}
   257  
   258  	resource "openstack_lb_member_v1" "member_2" {
   259  		pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   260  		address = "${openstack_compute_instance_v2.instance_2.access_ip_v4}"
   261  		port = 80
   262  		admin_state_up = true
   263  	}
   264  
   265  	resource "openstack_lb_vip_v1" "vip_1" {
   266  		name = "vip_1"
   267  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   268  		protocol = "TCP"
   269  		port = 80
   270  		pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   271  		admin_state_up = true
   272  	}`)
   273  
   274  var testAccLBV1Pool_fullstack_2 = fmt.Sprintf(`
   275  	resource "openstack_networking_network_v2" "network_1" {
   276  		name = "network_1"
   277  		admin_state_up = "true"
   278  	}
   279  
   280  	resource "openstack_networking_subnet_v2" "subnet_1" {
   281  		network_id = "${openstack_networking_network_v2.network_1.id}"
   282  		cidr = "192.168.199.0/24"
   283  		ip_version = 4
   284  	}
   285  
   286  	resource "openstack_compute_secgroup_v2" "secgroup_1" {
   287  		name = "secgroup_1"
   288  		description = "Rules for secgroup_1"
   289  
   290  		rule {
   291  			from_port = -1
   292  			to_port = -1
   293  			ip_protocol = "icmp"
   294  			cidr = "0.0.0.0/0"
   295  		}
   296  
   297  		rule {
   298  			from_port = 80
   299  			to_port = 80
   300  			ip_protocol = "tcp"
   301  			cidr = "0.0.0.0/0"
   302  		}
   303  	}
   304  
   305  	resource "openstack_compute_instance_v2" "instance_1" {
   306  		name = "instance_1"
   307  		security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   308  		network {
   309  			uuid = "${openstack_networking_network_v2.network_1.id}"
   310  		}
   311  	}
   312  
   313  	resource "openstack_compute_instance_v2" "instance_2" {
   314  		name = "instance_2"
   315  		security_groups = ["default", "${openstack_compute_secgroup_v2.secgroup_1.name}"]
   316  		user_data = "#cloud-config\ndisable_root: false"
   317  		network {
   318  			uuid = "${openstack_networking_network_v2.network_1.id}"
   319  		}
   320  	}
   321  
   322  	resource "openstack_lb_monitor_v1" "monitor_1" {
   323  		type = "TCP"
   324  		delay = 30
   325  		timeout = 5
   326  		max_retries = 3
   327  		admin_state_up = "true"
   328  	}
   329  
   330  	resource "openstack_lb_pool_v1" "pool_1" {
   331  		name = "pool_1"
   332  		protocol = "TCP"
   333  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   334  		lb_method = "ROUND_ROBIN"
   335  		monitor_ids = ["${openstack_lb_monitor_v1.monitor_1.id}"]
   336  	}
   337  
   338  	resource "openstack_lb_member_v1" "member_1" {
   339  		pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   340  		address = "${openstack_compute_instance_v2.instance_1.access_ip_v4}"
   341  		port = 80
   342  		admin_state_up = true
   343  	}
   344  
   345  	resource "openstack_lb_member_v1" "member_2" {
   346  		pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   347  		address = "${openstack_compute_instance_v2.instance_2.access_ip_v4}"
   348  		port = 80
   349  		admin_state_up = true
   350  	}
   351  
   352  	resource "openstack_lb_vip_v1" "vip_1" {
   353  		name = "vip_1"
   354  		subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   355  		protocol = "TCP"
   356  		port = 80
   357  		pool_id = "${openstack_lb_pool_v1.pool_1.id}"
   358  		admin_state_up = true
   359  	}`)