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:31:58 +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-06-08 01:18:14 +02:00
|
|
|
func TestAccAWSAppCookieStickinessPolicy_basic(t *testing.T) {
|
2016-05-26 10:37:54 +02:00
|
|
|
lbName := fmt.Sprintf("tf-test-lb-%s", acctest.RandString(5))
|
2016-06-14 23:52:55 +02:00
|
|
|
|
2015-04-27 00:01:48 +02:00
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
2016-05-26 10:37:54 +02:00
|
|
|
Config: testAccAppCookieStickinessPolicyConfig(lbName),
|
2015-04-27 00:01:48 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAppCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_app_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
2016-05-26 10:37:54 +02:00
|
|
|
Config: testAccAppCookieStickinessPolicyConfigUpdate(lbName),
|
2015-04-27 00:01:48 +02:00
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAppCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
2015-05-04 00:31:58 +02:00
|
|
|
"aws_app_cookie_stickiness_policy.foo",
|
2015-04-27 00:01:48 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-14 23:52:55 +02:00
|
|
|
func TestAccAWSAppCookieStickinessPolicy_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: testAccCheckAppCookieStickinessPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAppCookieStickinessPolicyConfig(lbName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAppCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_app_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
PreConfig: removeLB,
|
|
|
|
Config: testAccAppCookieStickinessPolicyConfigDestroy(lbName),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-27 00:01:48 +02:00
|
|
|
func testAccCheckAppCookieStickinessPolicyDestroy(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_app_cookie_stickiness_policy" {
|
|
|
|
continue
|
|
|
|
}
|
2015-04-27 00:01:48 +02:00
|
|
|
|
2015-12-22 04:23:57 +01:00
|
|
|
lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(
|
|
|
|
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 testAccCheckAppCookieStickinessPolicy(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 := resourceAwsAppCookieStickinessPolicyParseId(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
|
|
|
// ensure the policy is re-added is it goes missing
|
|
|
|
func TestAccAWSAppCookieStickinessPolicy_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 AppCookieStickinessPolicy: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.Test(t, resource.TestCase{
|
|
|
|
PreCheck: func() { testAccPreCheck(t) },
|
|
|
|
Providers: testAccProviders,
|
|
|
|
CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy,
|
|
|
|
Steps: []resource.TestStep{
|
|
|
|
resource.TestStep{
|
|
|
|
Config: testAccAppCookieStickinessPolicyConfig(lbName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAppCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_app_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
resource.TestStep{
|
|
|
|
PreConfig: removePolicy,
|
|
|
|
Config: testAccAppCookieStickinessPolicyConfig(lbName),
|
|
|
|
Check: resource.ComposeTestCheckFunc(
|
|
|
|
testAccCheckAppCookieStickinessPolicy(
|
|
|
|
"aws_elb.lb",
|
|
|
|
"aws_app_cookie_stickiness_policy.foo",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-05-26 10:37:54 +02:00
|
|
|
func testAccAppCookieStickinessPolicyConfig(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:31:58 +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_app_cookie_stickiness_policy" "foo" {
|
2015-05-04 00:31:58 +02:00
|
|
|
name = "foo-policy"
|
|
|
|
load_balancer = "${aws_elb.lb.id}"
|
2015-04-27 00:01:48 +02:00
|
|
|
lb_port = 80
|
|
|
|
cookie_name = "MyAppCookie"
|
2016-05-26 10:37:54 +02:00
|
|
|
}`, rName)
|
2015-04-27 00:01:48 +02:00
|
|
|
}
|
|
|
|
|
2015-05-04 00:31:58 +02:00
|
|
|
// Change the cookie_name to "MyOtherAppCookie".
|
2016-05-26 10:37:54 +02:00
|
|
|
func testAccAppCookieStickinessPolicyConfigUpdate(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:31:58 +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_app_cookie_stickiness_policy" "foo" {
|
2015-05-04 00:31:58 +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:31:58 +02:00
|
|
|
cookie_name = "MyOtherAppCookie"
|
2016-05-26 10:37:54 +02:00
|
|
|
}`, rName)
|
2015-04-27 00:01:48 +02:00
|
|
|
}
|
2016-06-14 23:52:55 +02:00
|
|
|
|
|
|
|
// attempt to destroy the policy, but we'll delete the LB in the PreConfig
|
|
|
|
func testAccAppCookieStickinessPolicyConfigDestroy(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)
|
|
|
|
}
|