github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/openstack/resource_openstack_lb_loadbalancer_v2_test.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  
    11  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/loadbalancers"
    12  	"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/groups"
    13  	"github.com/gophercloud/gophercloud/openstack/networking/v2/ports"
    14  )
    15  
    16  func TestAccLBV2LoadBalancer_basic(t *testing.T) {
    17  	var lb loadbalancers.LoadBalancer
    18  
    19  	resource.Test(t, resource.TestCase{
    20  		PreCheck:     func() { testAccPreCheck(t) },
    21  		Providers:    testAccProviders,
    22  		CheckDestroy: testAccCheckLBV2LoadBalancerDestroy,
    23  		Steps: []resource.TestStep{
    24  			resource.TestStep{
    25  				Config: testAccLBV2LoadBalancerConfig_basic,
    26  				Check: resource.ComposeTestCheckFunc(
    27  					testAccCheckLBV2LoadBalancerExists("openstack_lb_loadbalancer_v2.loadbalancer_1", &lb),
    28  				),
    29  			},
    30  			resource.TestStep{
    31  				Config: testAccLBV2LoadBalancerConfig_update,
    32  				Check: resource.ComposeTestCheckFunc(
    33  					resource.TestCheckResourceAttr(
    34  						"openstack_lb_loadbalancer_v2.loadbalancer_1", "name", "loadbalancer_1_updated"),
    35  					resource.TestMatchResourceAttr(
    36  						"openstack_lb_loadbalancer_v2.loadbalancer_1", "vip_port_id",
    37  						regexp.MustCompile("^[a-f0-9-]+")),
    38  				),
    39  			},
    40  		},
    41  	})
    42  }
    43  
    44  func TestAccLBV2LoadBalancer_secGroup(t *testing.T) {
    45  	var lb loadbalancers.LoadBalancer
    46  	var sg_1, sg_2 groups.SecGroup
    47  
    48  	resource.Test(t, resource.TestCase{
    49  		PreCheck:     func() { testAccPreCheck(t) },
    50  		Providers:    testAccProviders,
    51  		CheckDestroy: testAccCheckLBV2LoadBalancerDestroy,
    52  		Steps: []resource.TestStep{
    53  			resource.TestStep{
    54  				Config: testAccLBV2LoadBalancer_secGroup,
    55  				Check: resource.ComposeTestCheckFunc(
    56  					testAccCheckLBV2LoadBalancerExists(
    57  						"openstack_lb_loadbalancer_v2.loadbalancer_1", &lb),
    58  					testAccCheckNetworkingV2SecGroupExists(
    59  						"openstack_networking_secgroup_v2.secgroup_1", &sg_1),
    60  					testAccCheckNetworkingV2SecGroupExists(
    61  						"openstack_networking_secgroup_v2.secgroup_1", &sg_2),
    62  					resource.TestCheckResourceAttr(
    63  						"openstack_lb_loadbalancer_v2.loadbalancer_1", "security_group_ids.#", "1"),
    64  					testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_1),
    65  				),
    66  			},
    67  			resource.TestStep{
    68  				Config: testAccLBV2LoadBalancer_secGroup_update1,
    69  				Check: resource.ComposeTestCheckFunc(
    70  					testAccCheckLBV2LoadBalancerExists(
    71  						"openstack_lb_loadbalancer_v2.loadbalancer_1", &lb),
    72  					testAccCheckNetworkingV2SecGroupExists(
    73  						"openstack_networking_secgroup_v2.secgroup_2", &sg_1),
    74  					testAccCheckNetworkingV2SecGroupExists(
    75  						"openstack_networking_secgroup_v2.secgroup_2", &sg_2),
    76  					resource.TestCheckResourceAttr(
    77  						"openstack_lb_loadbalancer_v2.loadbalancer_1", "security_group_ids.#", "2"),
    78  					testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_1),
    79  					testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_2),
    80  				),
    81  			},
    82  			resource.TestStep{
    83  				Config: testAccLBV2LoadBalancer_secGroup_update2,
    84  				Check: resource.ComposeTestCheckFunc(
    85  					testAccCheckLBV2LoadBalancerExists(
    86  						"openstack_lb_loadbalancer_v2.loadbalancer_1", &lb),
    87  					testAccCheckNetworkingV2SecGroupExists(
    88  						"openstack_networking_secgroup_v2.secgroup_2", &sg_1),
    89  					testAccCheckNetworkingV2SecGroupExists(
    90  						"openstack_networking_secgroup_v2.secgroup_2", &sg_2),
    91  					resource.TestCheckResourceAttr(
    92  						"openstack_lb_loadbalancer_v2.loadbalancer_1", "security_group_ids.#", "1"),
    93  					testAccCheckLBV2LoadBalancerHasSecGroup(&lb, &sg_2),
    94  				),
    95  			},
    96  		},
    97  	})
    98  }
    99  
   100  func testAccCheckLBV2LoadBalancerDestroy(s *terraform.State) error {
   101  	config := testAccProvider.Meta().(*Config)
   102  	networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   103  	if err != nil {
   104  		return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   105  	}
   106  
   107  	for _, rs := range s.RootModule().Resources {
   108  		if rs.Type != "openstack_lb_loadbalancer_v2" {
   109  			continue
   110  		}
   111  
   112  		_, err := loadbalancers.Get(networkingClient, rs.Primary.ID).Extract()
   113  		if err == nil {
   114  			return fmt.Errorf("LoadBalancer still exists: %s", rs.Primary.ID)
   115  		}
   116  	}
   117  
   118  	return nil
   119  }
   120  
   121  func testAccCheckLBV2LoadBalancerExists(
   122  	n string, lb *loadbalancers.LoadBalancer) resource.TestCheckFunc {
   123  	return func(s *terraform.State) error {
   124  		rs, ok := s.RootModule().Resources[n]
   125  		if !ok {
   126  			return fmt.Errorf("Not found: %s", n)
   127  		}
   128  
   129  		if rs.Primary.ID == "" {
   130  			return fmt.Errorf("No ID is set")
   131  		}
   132  
   133  		config := testAccProvider.Meta().(*Config)
   134  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   135  		if err != nil {
   136  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   137  		}
   138  
   139  		found, err := loadbalancers.Get(networkingClient, rs.Primary.ID).Extract()
   140  		if err != nil {
   141  			return err
   142  		}
   143  
   144  		if found.ID != rs.Primary.ID {
   145  			return fmt.Errorf("Member not found")
   146  		}
   147  
   148  		*lb = *found
   149  
   150  		return nil
   151  	}
   152  }
   153  
   154  func testAccCheckLBV2LoadBalancerHasSecGroup(
   155  	lb *loadbalancers.LoadBalancer, sg *groups.SecGroup) resource.TestCheckFunc {
   156  	return func(s *terraform.State) error {
   157  		config := testAccProvider.Meta().(*Config)
   158  		networkingClient, err := config.networkingV2Client(OS_REGION_NAME)
   159  		if err != nil {
   160  			return fmt.Errorf("Error creating OpenStack networking client: %s", err)
   161  		}
   162  
   163  		port, err := ports.Get(networkingClient, lb.VipPortID).Extract()
   164  		if err != nil {
   165  			return err
   166  		}
   167  
   168  		for _, p := range port.SecurityGroups {
   169  			if p == sg.ID {
   170  				return nil
   171  			}
   172  		}
   173  
   174  		return fmt.Errorf("LoadBalancer does not have the security group")
   175  	}
   176  }
   177  
   178  const testAccLBV2LoadBalancerConfig_basic = `
   179  resource "openstack_networking_network_v2" "network_1" {
   180    name = "network_1"
   181    admin_state_up = "true"
   182  }
   183  
   184  resource "openstack_networking_subnet_v2" "subnet_1" {
   185    name = "subnet_1"
   186    cidr = "192.168.199.0/24"
   187    ip_version = 4
   188    network_id = "${openstack_networking_network_v2.network_1.id}"
   189  }
   190  
   191  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   192    name = "loadbalancer_1"
   193    loadbalancer_provider = "haproxy"
   194    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   195  }
   196  `
   197  
   198  const testAccLBV2LoadBalancerConfig_update = `
   199  resource "openstack_networking_network_v2" "network_1" {
   200    name = "network_1"
   201    admin_state_up = "true"
   202  }
   203  
   204  resource "openstack_networking_subnet_v2" "subnet_1" {
   205    name = "subnet_1"
   206    cidr = "192.168.199.0/24"
   207    ip_version = 4
   208    network_id = "${openstack_networking_network_v2.network_1.id}"
   209  }
   210  
   211  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   212    name = "loadbalancer_1_updated"
   213    loadbalancer_provider = "haproxy"
   214    admin_state_up = "true"
   215    vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   216  }
   217  `
   218  
   219  const testAccLBV2LoadBalancer_secGroup = `
   220  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   221    name = "secgroup_1"
   222    description = "secgroup_1"
   223  }
   224  
   225  resource "openstack_networking_secgroup_v2" "secgroup_2" {
   226    name = "secgroup_2"
   227    description = "secgroup_2"
   228  }
   229  
   230  resource "openstack_networking_network_v2" "network_1" {
   231    name = "network_1"
   232    admin_state_up = "true"
   233  }
   234  
   235  resource "openstack_networking_subnet_v2" "subnet_1" {
   236    name = "subnet_1"
   237    network_id = "${openstack_networking_network_v2.network_1.id}"
   238    cidr = "192.168.199.0/24"
   239  }
   240  
   241  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   242      name = "loadbalancer_1"
   243      vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   244      security_group_ids = [
   245        "${openstack_networking_secgroup_v2.secgroup_1.id}"
   246      ]
   247  }
   248  `
   249  
   250  const testAccLBV2LoadBalancer_secGroup_update1 = `
   251  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   252    name = "secgroup_1"
   253    description = "secgroup_1"
   254  }
   255  
   256  resource "openstack_networking_secgroup_v2" "secgroup_2" {
   257    name = "secgroup_2"
   258    description = "secgroup_2"
   259  }
   260  
   261  resource "openstack_networking_network_v2" "network_1" {
   262    name = "network_1"
   263    admin_state_up = "true"
   264  }
   265  
   266  resource "openstack_networking_subnet_v2" "subnet_1" {
   267    name = "subnet_1"
   268    network_id = "${openstack_networking_network_v2.network_1.id}"
   269    cidr = "192.168.199.0/24"
   270  }
   271  
   272  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   273      name = "loadbalancer_1"
   274      vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   275      security_group_ids = [
   276        "${openstack_networking_secgroup_v2.secgroup_1.id}",
   277        "${openstack_networking_secgroup_v2.secgroup_2.id}"
   278      ]
   279  }
   280  `
   281  
   282  const testAccLBV2LoadBalancer_secGroup_update2 = `
   283  resource "openstack_networking_secgroup_v2" "secgroup_1" {
   284    name = "secgroup_1"
   285    description = "secgroup_1"
   286  }
   287  
   288  resource "openstack_networking_secgroup_v2" "secgroup_2" {
   289    name = "secgroup_2"
   290    description = "secgroup_2"
   291  }
   292  
   293  resource "openstack_networking_network_v2" "network_1" {
   294    name = "network_1"
   295    admin_state_up = "true"
   296  }
   297  
   298  resource "openstack_networking_subnet_v2" "subnet_1" {
   299    name = "subnet_1"
   300    network_id = "${openstack_networking_network_v2.network_1.id}"
   301    cidr = "192.168.199.0/24"
   302  }
   303  
   304  resource "openstack_lb_loadbalancer_v2" "loadbalancer_1" {
   305      name = "loadbalancer_1"
   306      vip_subnet_id = "${openstack_networking_subnet_v2.subnet_1.id}"
   307      security_group_ids = [
   308        "${openstack_networking_secgroup_v2.secgroup_2.id}"
   309      ]
   310      depends_on = ["openstack_networking_secgroup_v2.secgroup_1"]
   311  }
   312  `