github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_loadbalancer_rule_test.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/terraform/helper/resource"
     9  	"github.com/hashicorp/terraform/terraform"
    10  	"github.com/xanzy/go-cloudstack/cloudstack"
    11  )
    12  
    13  func TestAccCloudStackLoadBalancerRule_basic(t *testing.T) {
    14  	resource.Test(t, resource.TestCase{
    15  		PreCheck:     func() { testAccPreCheck(t) },
    16  		Providers:    testAccProviders,
    17  		CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy,
    18  		Steps: []resource.TestStep{
    19  			resource.TestStep{
    20  				Config: testAccCloudStackLoadBalancerRule_basic,
    21  				Check: resource.ComposeTestCheckFunc(
    22  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
    23  					resource.TestCheckResourceAttr(
    24  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
    25  					resource.TestCheckResourceAttr(
    26  						"cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"),
    27  					resource.TestCheckResourceAttr(
    28  						"cloudstack_loadbalancer_rule.foo", "public_port", "80"),
    29  					resource.TestCheckResourceAttr(
    30  						"cloudstack_loadbalancer_rule.foo", "private_port", "80"),
    31  				),
    32  			},
    33  		},
    34  	})
    35  }
    36  
    37  func TestAccCloudStackLoadBalancerRule_update(t *testing.T) {
    38  	var id string
    39  
    40  	resource.Test(t, resource.TestCase{
    41  		PreCheck:     func() { testAccPreCheck(t) },
    42  		Providers:    testAccProviders,
    43  		CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy,
    44  		Steps: []resource.TestStep{
    45  			resource.TestStep{
    46  				Config: testAccCloudStackLoadBalancerRule_basic,
    47  				Check: resource.ComposeTestCheckFunc(
    48  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id),
    49  					resource.TestCheckResourceAttr(
    50  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
    51  					resource.TestCheckResourceAttr(
    52  						"cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"),
    53  					resource.TestCheckResourceAttr(
    54  						"cloudstack_loadbalancer_rule.foo", "public_port", "80"),
    55  					resource.TestCheckResourceAttr(
    56  						"cloudstack_loadbalancer_rule.foo", "private_port", "80"),
    57  				),
    58  			},
    59  
    60  			resource.TestStep{
    61  				Config: testAccCloudStackLoadBalancerRule_update,
    62  				Check: resource.ComposeTestCheckFunc(
    63  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", &id),
    64  					resource.TestCheckResourceAttr(
    65  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
    66  					resource.TestCheckResourceAttr(
    67  						"cloudstack_loadbalancer_rule.foo", "algorithm", "leastconn"),
    68  					resource.TestCheckResourceAttr(
    69  						"cloudstack_loadbalancer_rule.foo", "public_port", "80"),
    70  					resource.TestCheckResourceAttr(
    71  						"cloudstack_loadbalancer_rule.foo", "private_port", "80"),
    72  				),
    73  			},
    74  		},
    75  	})
    76  }
    77  
    78  func TestAccCloudStackLoadBalancerRule_forceNew(t *testing.T) {
    79  	resource.Test(t, resource.TestCase{
    80  		PreCheck:     func() { testAccPreCheck(t) },
    81  		Providers:    testAccProviders,
    82  		CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy,
    83  		Steps: []resource.TestStep{
    84  			resource.TestStep{
    85  				Config: testAccCloudStackLoadBalancerRule_basic,
    86  				Check: resource.ComposeTestCheckFunc(
    87  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
    88  					resource.TestCheckResourceAttr(
    89  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
    90  					resource.TestCheckResourceAttr(
    91  						"cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"),
    92  					resource.TestCheckResourceAttr(
    93  						"cloudstack_loadbalancer_rule.foo", "public_port", "80"),
    94  					resource.TestCheckResourceAttr(
    95  						"cloudstack_loadbalancer_rule.foo", "private_port", "80"),
    96  				),
    97  			},
    98  
    99  			resource.TestStep{
   100  				Config: testAccCloudStackLoadBalancerRule_forcenew,
   101  				Check: resource.ComposeTestCheckFunc(
   102  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
   103  					resource.TestCheckResourceAttr(
   104  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
   105  					resource.TestCheckResourceAttr(
   106  						"cloudstack_loadbalancer_rule.foo", "algorithm", "leastconn"),
   107  					resource.TestCheckResourceAttr(
   108  						"cloudstack_loadbalancer_rule.foo", "public_port", "443"),
   109  					resource.TestCheckResourceAttr(
   110  						"cloudstack_loadbalancer_rule.foo", "private_port", "443"),
   111  				),
   112  			},
   113  		},
   114  	})
   115  }
   116  
   117  func TestAccCloudStackLoadBalancerRule_vpc(t *testing.T) {
   118  	resource.Test(t, resource.TestCase{
   119  		PreCheck:     func() { testAccPreCheck(t) },
   120  		Providers:    testAccProviders,
   121  		CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy,
   122  		Steps: []resource.TestStep{
   123  			resource.TestStep{
   124  				Config: testAccCloudStackLoadBalancerRule_vpc,
   125  				Check: resource.ComposeTestCheckFunc(
   126  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
   127  					resource.TestCheckResourceAttr(
   128  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
   129  					resource.TestCheckResourceAttr(
   130  						"cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"),
   131  					resource.TestCheckResourceAttr(
   132  						"cloudstack_loadbalancer_rule.foo", "public_port", "80"),
   133  					resource.TestCheckResourceAttr(
   134  						"cloudstack_loadbalancer_rule.foo", "private_port", "80"),
   135  				),
   136  			},
   137  		},
   138  	})
   139  }
   140  
   141  func TestAccCloudStackLoadBalancerRule_vpcUpdate(t *testing.T) {
   142  	resource.Test(t, resource.TestCase{
   143  		PreCheck:     func() { testAccPreCheck(t) },
   144  		Providers:    testAccProviders,
   145  		CheckDestroy: testAccCheckCloudStackLoadBalancerRuleDestroy,
   146  		Steps: []resource.TestStep{
   147  			resource.TestStep{
   148  				Config: testAccCloudStackLoadBalancerRule_vpc,
   149  				Check: resource.ComposeTestCheckFunc(
   150  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
   151  					resource.TestCheckResourceAttr(
   152  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb"),
   153  					resource.TestCheckResourceAttr(
   154  						"cloudstack_loadbalancer_rule.foo", "algorithm", "roundrobin"),
   155  					resource.TestCheckResourceAttr(
   156  						"cloudstack_loadbalancer_rule.foo", "public_port", "80"),
   157  					resource.TestCheckResourceAttr(
   158  						"cloudstack_loadbalancer_rule.foo", "private_port", "80"),
   159  				),
   160  			},
   161  
   162  			resource.TestStep{
   163  				Config: testAccCloudStackLoadBalancerRule_vpc_update,
   164  				Check: resource.ComposeTestCheckFunc(
   165  					testAccCheckCloudStackLoadBalancerRuleExist("cloudstack_loadbalancer_rule.foo", nil),
   166  					resource.TestCheckResourceAttr(
   167  						"cloudstack_loadbalancer_rule.foo", "name", "terraform-lb-update"),
   168  					resource.TestCheckResourceAttr(
   169  						"cloudstack_loadbalancer_rule.foo", "algorithm", "leastconn"),
   170  					resource.TestCheckResourceAttr(
   171  						"cloudstack_loadbalancer_rule.foo", "public_port", "443"),
   172  					resource.TestCheckResourceAttr(
   173  						"cloudstack_loadbalancer_rule.foo", "private_port", "443"),
   174  				),
   175  			},
   176  		},
   177  	})
   178  }
   179  
   180  func testAccCheckCloudStackLoadBalancerRuleExist(n string, id *string) resource.TestCheckFunc {
   181  	return func(s *terraform.State) error {
   182  		rs, ok := s.RootModule().Resources[n]
   183  		if !ok {
   184  			return fmt.Errorf("Not found: %s", n)
   185  		}
   186  
   187  		if rs.Primary.ID == "" {
   188  			return fmt.Errorf("No loadbalancer rule ID is set")
   189  		}
   190  
   191  		if id != nil {
   192  			if *id != "" && *id != rs.Primary.ID {
   193  				return fmt.Errorf("Resource ID has changed!")
   194  			}
   195  
   196  			*id = rs.Primary.ID
   197  		}
   198  
   199  		cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   200  		_, count, err := cs.LoadBalancer.GetLoadBalancerRuleByID(rs.Primary.ID)
   201  
   202  		if err != nil {
   203  			return err
   204  		}
   205  
   206  		if count == 0 {
   207  			return fmt.Errorf("Loadbalancer rule %s not found", n)
   208  		}
   209  
   210  		return nil
   211  	}
   212  }
   213  
   214  func testAccCheckCloudStackLoadBalancerRuleDestroy(s *terraform.State) error {
   215  	cs := testAccProvider.Meta().(*cloudstack.CloudStackClient)
   216  
   217  	for _, rs := range s.RootModule().Resources {
   218  		if rs.Type != "cloudstack_loadbalancer_rule" {
   219  			continue
   220  		}
   221  
   222  		if rs.Primary.ID == "" {
   223  			return fmt.Errorf("No Loadbalancer rule ID is set")
   224  		}
   225  
   226  		for k, id := range rs.Primary.Attributes {
   227  			if !strings.Contains(k, "uuid") {
   228  				continue
   229  			}
   230  
   231  			_, _, err := cs.LoadBalancer.GetLoadBalancerRuleByID(id)
   232  			if err == nil {
   233  				return fmt.Errorf("Loadbalancer rule %s still exists", rs.Primary.ID)
   234  			}
   235  		}
   236  	}
   237  
   238  	return nil
   239  }
   240  
   241  var testAccCloudStackLoadBalancerRule_basic = fmt.Sprintf(`
   242  resource "cloudstack_instance" "foobar1" {
   243    name = "terraform-server1"
   244    display_name = "terraform"
   245    service_offering= "%s"
   246    network_id = "%s"
   247    template = "%s"
   248    zone = "%s"
   249    expunge = true
   250  }
   251  
   252  resource "cloudstack_loadbalancer_rule" "foo" {
   253    name = "terraform-lb"
   254    ip_address_id = "%s"
   255    algorithm = "roundrobin"
   256    public_port = 80
   257    private_port = 80
   258    member_ids = ["${cloudstack_instance.foobar1.id}"]
   259  }
   260  `,
   261  	CLOUDSTACK_SERVICE_OFFERING_1,
   262  	CLOUDSTACK_NETWORK_1,
   263  	CLOUDSTACK_TEMPLATE,
   264  	CLOUDSTACK_ZONE,
   265  	CLOUDSTACK_PUBLIC_IPADDRESS)
   266  
   267  var testAccCloudStackLoadBalancerRule_update = fmt.Sprintf(`
   268  resource "cloudstack_instance" "foobar1" {
   269    name = "terraform-server1"
   270    display_name = "terraform"
   271    service_offering= "%s"
   272    network_id = "%s"
   273    template = "%s"
   274    zone = "%s"
   275    expunge = true
   276  }
   277  
   278  resource "cloudstack_loadbalancer_rule" "foo" {
   279    name = "terraform-lb-update"
   280    ip_address_id = "%s"
   281    algorithm = "leastconn"
   282    public_port = 80
   283    private_port = 80
   284    member_ids = ["${cloudstack_instance.foobar1.id}"]
   285  }
   286  `,
   287  	CLOUDSTACK_SERVICE_OFFERING_1,
   288  	CLOUDSTACK_NETWORK_1,
   289  	CLOUDSTACK_TEMPLATE,
   290  	CLOUDSTACK_ZONE,
   291  	CLOUDSTACK_PUBLIC_IPADDRESS)
   292  
   293  var testAccCloudStackLoadBalancerRule_forcenew = fmt.Sprintf(`
   294  resource "cloudstack_instance" "foobar1" {
   295    name = "terraform-server1"
   296    display_name = "terraform"
   297    service_offering= "%s"
   298    network_id = "%s"
   299    template = "%s"
   300    zone = "%s"
   301    expunge = true
   302  }
   303  
   304  resource "cloudstack_loadbalancer_rule" "foo" {
   305    name = "terraform-lb-update"
   306    ip_address_id = "%s"
   307    algorithm = "leastconn"
   308    public_port = 443
   309    private_port = 443
   310    member_ids = ["${cloudstack_instance.foobar1.id}"]
   311  }
   312  `,
   313  	CLOUDSTACK_SERVICE_OFFERING_1,
   314  	CLOUDSTACK_NETWORK_1,
   315  	CLOUDSTACK_TEMPLATE,
   316  	CLOUDSTACK_ZONE,
   317  	CLOUDSTACK_PUBLIC_IPADDRESS)
   318  
   319  var testAccCloudStackLoadBalancerRule_vpc = fmt.Sprintf(`
   320  resource "cloudstack_vpc" "foobar" {
   321  	name = "terraform-vpc"
   322  	cidr = "%s"
   323  	vpc_offering = "%s"
   324  	zone = "%s"
   325  }
   326  
   327  resource "cloudstack_network" "foo" {
   328    name = "terraform-network"
   329    cidr = "%s"
   330    network_offering = "%s"
   331    vpc_id = "${cloudstack_vpc.foobar.id}"
   332    zone = "${cloudstack_vpc.foobar.zone}"
   333  }
   334  
   335  resource "cloudstack_ipaddress" "foo" {
   336    vpc_id = "${cloudstack_vpc.foobar.id}"
   337  }
   338  
   339  resource "cloudstack_instance" "foobar1" {
   340    name = "terraform-server1"
   341    display_name = "terraform"
   342    service_offering= "%s"
   343    network_id = "${cloudstack_network.foo.id}"
   344    template = "%s"
   345    zone = "${cloudstack_network.foo.zone}"
   346    expunge = true
   347  }
   348  
   349  resource "cloudstack_loadbalancer_rule" "foo" {
   350    name = "terraform-lb"
   351    ip_address_id = "${cloudstack_ipaddress.foo.id}"
   352    algorithm = "roundrobin"
   353    network_id = "${cloudstack_network.foo.id}"
   354    public_port = 80
   355    private_port = 80
   356    member_ids = ["${cloudstack_instance.foobar1.id}"]
   357  }`,
   358  	CLOUDSTACK_VPC_CIDR_1,
   359  	CLOUDSTACK_VPC_OFFERING,
   360  	CLOUDSTACK_ZONE,
   361  	CLOUDSTACK_VPC_NETWORK_CIDR,
   362  	CLOUDSTACK_VPC_NETWORK_OFFERING,
   363  	CLOUDSTACK_SERVICE_OFFERING_1,
   364  	CLOUDSTACK_TEMPLATE)
   365  
   366  var testAccCloudStackLoadBalancerRule_vpc_update = fmt.Sprintf(`
   367  resource "cloudstack_vpc" "foobar" {
   368    name = "terraform-vpc"
   369    cidr = "%s"
   370    vpc_offering = "%s"
   371    zone = "%s"
   372  }
   373  
   374  resource "cloudstack_network" "foo" {
   375    name = "terraform-network"
   376    cidr = "%s"
   377    network_offering = "%s"
   378    vpc_id = "${cloudstack_vpc.foobar.id}"
   379    zone = "${cloudstack_vpc.foobar.zone}"
   380  }
   381  
   382  resource "cloudstack_ipaddress" "foo" {
   383    vpc_id = "${cloudstack_vpc.foobar.id}"
   384  }
   385  
   386  resource "cloudstack_instance" "foobar1" {
   387    name = "terraform-server1"
   388    display_name = "terraform"
   389    service_offering= "%s"
   390    network_id = "${cloudstack_network.foo.id}"
   391    template = "%s"
   392    zone = "${cloudstack_network.foo.zone}"
   393    expunge = true
   394  }
   395  
   396  resource "cloudstack_instance" "foobar2" {
   397    name = "terraform-server2"
   398    display_name = "terraform"
   399    service_offering= "%s"
   400    network_id = "${cloudstack_network.foo.id}"
   401    template = "%s"
   402    zone = "${cloudstack_network.foo.zone}"
   403    expunge = true
   404  }
   405  
   406  resource "cloudstack_loadbalancer_rule" "foo" {
   407    name = "terraform-lb-update"
   408    ip_address_id = "${cloudstack_ipaddress.foo.id}"
   409    algorithm = "leastconn"
   410    network_id = "${cloudstack_network.foo.id}"
   411    public_port = 443
   412    private_port = 443
   413    member_ids = ["${cloudstack_instance.foobar1.id}", "${cloudstack_instance.foobar2.id}"]
   414  }`,
   415  	CLOUDSTACK_VPC_CIDR_1,
   416  	CLOUDSTACK_VPC_OFFERING,
   417  	CLOUDSTACK_ZONE,
   418  	CLOUDSTACK_VPC_NETWORK_CIDR,
   419  	CLOUDSTACK_VPC_NETWORK_OFFERING,
   420  	CLOUDSTACK_SERVICE_OFFERING_1,
   421  	CLOUDSTACK_TEMPLATE,
   422  	CLOUDSTACK_SERVICE_OFFERING_1,
   423  	CLOUDSTACK_TEMPLATE)