2015-04-27 00:01:48 +02:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
2015-12-22 04:23:57 +01:00
|
|
|
"github.com/aws/aws-sdk-go/aws/awserr"
|
2015-06-03 20:36:57 +02:00
|
|
|
"github.com/aws/aws-sdk-go/service/elb"
|
2015-05-04 00:29:42 +02:00
|
|
|
|
2016-05-26 10:37:54 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/acctest"
|
2015-04-27 00:01:48 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
2015-12-09 00:48:57 +01:00
|
|
|
func TestAccAWSLBCookieStickinessPolicy_basic(t *testing.T) {
|
2016-05-26 10:37:54 +02:00
|
|
|
lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
|
2015-04-27 00:01:48 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-05-26 10:37:54 +02:00
|
|
|
Config: testAccLBCookieStickinessPolicyConfig(lbName),
|
2015-04-27 00:01:48 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLBCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_lb_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-05-26 10:37:54 +02:00
|
|
|
Config: testAccLBCookieStickinessPolicyConfigUpdate(lbName),
|
2015-04-27 00:01:48 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLBCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
2015-05-04 00:29:42 +02:00
|
|
|
"aws_lb_cookie_stickiness_policy.foo",
|
2015-04-27 00:01:48 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckLBCookieStickinessPolicyDestroy(s *terraform.State) error {
|
2015-12-22 04:23:57 +01:00
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elbconn
|
|
|
|
|
|
|
|
for _, rs := range s.RootModule().Resources {
|
|
|
|
if rs.Type != "aws_lb_cookie_stickiness_policy" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
lbName, _, policyName := resourceAwsLBCookieStickinessPolicyParseId(rs.Primary.ID)
|
|
|
|
out, err := conn.DescribeLoadBalancerPolicies(
|
|
|
|
&elb.DescribeLoadBalancerPoliciesInput{
|
|
|
|
LoadBalancerName: aws.String(lbName),
|
|
|
|
PolicyNames: []*string{aws.String(policyName)},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
if ec2err, ok := err.(awserr.Error); ok && (ec2err.Code() == "PolicyNotFound" || ec2err.Code() == "LoadBalancerNotFound") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(out.PolicyDescriptions) > 0 {
|
|
|
|
return fmt.Errorf("Policy still exists")
|
|
|
|
}
|
2015-04-27 00:01:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAccCheckLBCookieStickinessPolicy(elbResource string, policyResource string) resource.TestCheckFunc {
|
|
|
|
return func(s *terraform.State) error {
|
|
|
|
rs, ok := s.RootModule().Resources[elbResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", elbResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rs.Primary.ID == "" {
|
|
|
|
return fmt.Errorf("No ID is set")
|
|
|
|
}
|
|
|
|
|
|
|
|
policy, ok := s.RootModule().Resources[policyResource]
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("Not found: %s", policyResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
elbconn := testAccProvider.Meta().(*AWSClient).elbconn
|
|
|
|
elbName, _, policyName := resourceAwsLBCookieStickinessPolicyParseId(policy.Primary.ID)
|
|
|
|
_, err := elbconn.DescribeLoadBalancerPolicies(&elb.DescribeLoadBalancerPoliciesInput{
|
|
|
|
LoadBalancerName: aws.String(elbName),
|
|
|
|
PolicyNames: []*string{aws.String(policyName)},
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:59:40 +02:00
|
|
|
func TestAccCheckLBCookieStickinessPolicy_drift(t *testing.T) {
|
|
|
|
lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
|
|
|
|
|
|
|
|
// We only want to remove the reference to the policy from the listner,
|
|
|
|
// beacause that's all that can be done via the console.
|
|
|
|
removePolicy := func() {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elbconn
|
|
|
|
|
|
|
|
setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
|
|
|
|
LoadBalancerName: aws.String(lbName),
|
|
|
|
LoadBalancerPort: aws.Int64(80),
|
|
|
|
PolicyNames: []*string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := conn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
|
|
|
|
t.Fatalf("Error removing LBCookieStickinessPolicy: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccLBCookieStickinessPolicyConfig(lbName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLBCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_lb_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
PreConfig: removePolicy,
|
|
|
|
Config: testAccLBCookieStickinessPolicyConfig(lbName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLBCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_lb_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-01-27 20:14:23 +01:00
|
|
|
func TestAccAWSLBCookieStickinessPolicy_missingLB(t *testing.T) {
|
|
|
|
lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
|
|
|
|
|
|
|
|
// check that we can destroy the policy if the LB is missing
|
|
|
|
removeLB := func() {
|
|
|
|
conn := testAccProvider.Meta().(*AWSClient).elbconn
|
|
|
|
deleteElbOpts := elb.DeleteLoadBalancerInput{
|
|
|
|
LoadBalancerName: aws.String(lbName),
|
|
|
|
}
|
|
|
|
if _, err := conn.DeleteLoadBalancer(&deleteElbOpts); err != nil {
|
|
|
|
t.Fatalf("Error deleting ELB: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccLBCookieStickinessPolicyConfig(lbName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckLBCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_lb_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
PreConfig: removeLB,
|
|
|
|
Config: testAccLBCookieStickinessPolicyConfigDestroy(lbName),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-26 10:37:54 +02:00
|
|
|
func testAccLBCookieStickinessPolicyConfig(rName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-04-27 00:01:48 +02:00
|
|
|
resource "aws_elb" "lb" {
|
2016-05-26 10:37:54 +02:00
|
|
|
name = "%s"
|
2015-05-04 00:29:42 +02:00
|
|
|
availability_zones = ["us-west-2a"]
|
2015-04-27 00:01:48 +02:00
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_lb_cookie_stickiness_policy" "foo" {
|
2015-05-04 00:29:42 +02:00
|
|
|
name = "foo-policy"
|
|
|
|
load_balancer = "${aws_elb.lb.id}"
|
2015-04-27 00:01:48 +02:00
|
|
|
lb_port = 80
|
2016-05-26 10:37:54 +02:00
|
|
|
}`, rName)
|
2015-04-27 00:01:48 +02:00
|
|
|
}
|
|
|
|
|
2015-05-04 00:29:42 +02:00
|
|
|
// Sets the cookie_expiration_period to 300s.
|
2016-05-26 10:37:54 +02:00
|
|
|
func testAccLBCookieStickinessPolicyConfigUpdate(rName string) string {
|
|
|
|
return fmt.Sprintf(`
|
2015-04-27 00:01:48 +02:00
|
|
|
resource "aws_elb" "lb" {
|
2016-05-26 10:37:54 +02:00
|
|
|
name = "%s"
|
2015-05-04 00:29:42 +02:00
|
|
|
availability_zones = ["us-west-2a"]
|
2015-04-27 00:01:48 +02:00
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource "aws_lb_cookie_stickiness_policy" "foo" {
|
2015-05-04 00:29:42 +02:00
|
|
|
name = "foo-policy"
|
|
|
|
load_balancer = "${aws_elb.lb.id}"
|
2015-04-27 00:01:48 +02:00
|
|
|
lb_port = 80
|
2015-05-04 00:29:42 +02:00
|
|
|
cookie_expiration_period = 300
|
2016-05-26 10:37:54 +02:00
|
|
|
}`, rName)
|
2015-04-27 00:01:48 +02:00
|
|
|
}
|
2017-01-27 20:14:23 +01:00
|
|
|
|
|
|
|
// attempt to destroy the policy, but we'll delete the LB in the PreConfig
|
|
|
|
func testAccLBCookieStickinessPolicyConfigDestroy(rName string) string {
|
|
|
|
return fmt.Sprintf(`
|
|
|
|
resource "aws_elb" "lb" {
|
|
|
|
name = "%s"
|
|
|
|
availability_zones = ["us-west-2a"]
|
|
|
|
listener {
|
|
|
|
instance_port = 8000
|
|
|
|
instance_protocol = "http"
|
|
|
|
lb_port = 80
|
|
|
|
lb_protocol = "http"
|
|
|
|
}
|
|
|
|
}`, rName)
|
|
|
|
}
|