From 0120899f2aa073e531fe5f9c67eb01ac2b4023c3 Mon Sep 17 00:00:00 2001 From: Christopher Tiwald Date: Sun, 26 Apr 2015 17:57:46 -0400 Subject: [PATCH 1/6] aws: Add support for aws_lb_cookie_stickiness_policy. This resource represents a session stickiness policy in AWS, and can be applied to an ELB's client-facing listeners. --- builtin/providers/aws/provider.go | 1 + ...esource_aws_lb_cookie_stickiness_policy.go | 156 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 3e534c4b6ede..f717a2e55e46 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -86,6 +86,7 @@ func Provider() terraform.ResourceProvider { "aws_internet_gateway": resourceAwsInternetGateway(), "aws_key_pair": resourceAwsKeyPair(), "aws_launch_configuration": resourceAwsLaunchConfiguration(), + "aws_lb_cookie_stickiness_policy": resourceAwsLBCookieStickinessPolicy(), "aws_main_route_table_association": resourceAwsMainRouteTableAssociation(), "aws_network_acl": resourceAwsNetworkAcl(), "aws_network_interface": resourceAwsNetworkInterface(), diff --git a/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go new file mode 100644 index 000000000000..cc3d89c76b41 --- /dev/null +++ b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go @@ -0,0 +1,156 @@ +package aws + +import ( + "fmt" + "strings" + + "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/service/elb" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsLBCookieStickinessPolicy() *schema.Resource { + return &schema.Resource{ + // There is no concept of "updating" an LB Stickiness policy in + // the AWS API. + Create: resourceAwsLBCookieStickinessPolicyCreate, + Update: resourceAwsLBCookieStickinessPolicyCreate, + + Read: resourceAwsLBCookieStickinessPolicyRead, + Delete: resourceAwsLBCookieStickinessPolicyDelete, + + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "load_balancer": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "lb_port": &schema.Schema{ + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + + "cookie_expiration_period": &schema.Schema{ + Type: schema.TypeInt, + Optional: true, + ForceNew: true, + }, + }, + } +} + +func resourceAwsLBCookieStickinessPolicyCreate(d *schema.ResourceData, meta interface{}) error { + elbconn := meta.(*AWSClient).elbconn + + // Provision the LBStickinessPolicy + lbspOpts := &elb.CreateLBCookieStickinessPolicyInput{ + CookieExpirationPeriod: aws.Long(int64(d.Get("cookie_expiration_period").(int))), + LoadBalancerName: aws.String(d.Get("load_balancer").(string)), + PolicyName: aws.String(d.Get("name").(string)), + } + + if _, err := elbconn.CreateLBCookieStickinessPolicy(lbspOpts); err != nil { + return fmt.Errorf("Error creating LBCookieStickinessPolicy: %s", err) + } + + setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{ + LoadBalancerName: aws.String(d.Get("load_balancer").(string)), + LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))), + PolicyNames: []*string{aws.String(d.Get("name").(string))}, + } + + if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil { + return fmt.Errorf("Error setting LBCookieStickinessPolicy: %s", err) + } + + d.SetId(fmt.Sprintf("%s:%d:%s", + *lbspOpts.LoadBalancerName, + *setLoadBalancerOpts.LoadBalancerPort, + *lbspOpts.PolicyName)) + return nil +} + +func resourceAwsLBCookieStickinessPolicyRead(d *schema.ResourceData, meta interface{}) error { + elbconn := meta.(*AWSClient).elbconn + + lbName, lbPort, policyName := resourceAwsLBCookieStickinessPolicyParseId(d.Id()) + + request := &elb.DescribeLoadBalancerPoliciesInput{ + LoadBalancerName: aws.String(lbName), + PolicyNames: []*string{aws.String(policyName)}, + } + + getResp, err := elbconn.DescribeLoadBalancerPolicies(request) + if err != nil { + if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "PolicyNotFound" { + // The policy is gone. + d.SetId("") + return nil + } + return fmt.Errorf("Error retrieving policy: %s", err) + } + + if len(getResp.PolicyDescriptions) != 1 { + return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions) + } + + // We can get away with this because there's only one attribute, the + // cookie expiration, in these descriptions. + policyDesc := getResp.PolicyDescriptions[0] + cookieAttr := policyDesc.PolicyAttributeDescriptions[0] + if *cookieAttr.AttributeName != "CookieExpirationPeriod" { + return fmt.Errorf("Unable to find cookie expiration period.") + } + d.Set("cookie_expiration_period", cookieAttr.AttributeValue) + + d.Set("name", policyName) + d.Set("load_balancer", lbName) + d.Set("lb_port", lbPort) + + return nil +} + +func resourceAwsLBCookieStickinessPolicyDelete(d *schema.ResourceData, meta interface{}) error { + elbconn := meta.(*AWSClient).elbconn + + lbName, _, policyName := resourceAwsLBCookieStickinessPolicyParseId(d.Id()) + + // Perversely, if we Set an empty list of PolicyNames, we detach the + // policies attached to a listener, which is required to delete the + // policy itself. + setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{ + LoadBalancerName: aws.String(d.Get("load_balancer").(string)), + LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))), + PolicyNames: []*string{}, + } + + if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil { + return fmt.Errorf("Error removing LBCookieStickinessPolicy: %s", err) + } + + request := &elb.DeleteLoadBalancerPolicyInput{ + LoadBalancerName: aws.String(lbName), + PolicyName: aws.String(policyName), + } + + if _, err := elbconn.DeleteLoadBalancerPolicy(request); err != nil { + return fmt.Errorf("Error deleting LB stickiness policy %s: %s", d.Id(), err) + } + return nil +} + +// resourceAwsLBCookieStickinessPolicyParseId takes an ID and parses it into +// it's constituent parts. You need three axes (LB name, policy name, and LB +// port) to create or identify a stickiness policy in AWS's API. +func resourceAwsLBCookieStickinessPolicyParseId(id string) (string, string, string) { + parts := strings.SplitN(id, ":", 3) + return parts[0], parts[1], parts[2] +} From 448fb4895fd142272ba028ac015a3906117971a7 Mon Sep 17 00:00:00 2001 From: Christopher Tiwald Date: Sun, 26 Apr 2015 18:01:48 -0400 Subject: [PATCH 2/6] aws: Add acceptance test of aws_lb_cookie_stickiness_policy. --- ...ce_aws_lb_cookie_stickiness_policy_test.go | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go diff --git a/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go new file mode 100644 index 000000000000..028b0317d1fd --- /dev/null +++ b/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy_test.go @@ -0,0 +1,125 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/service/elb" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSLBCookieStickinessPolicy(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLBCookieStickinessPolicyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccLBCookieStickinessPolicyConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckLBCookieStickinessPolicy( + "aws_elb.lb", + "aws_lb_cookie_stickiness_policy.foo", + ), + ), + }, + resource.TestStep{ + Config: testAccLBCookieStickinessPolicyConfigUpdate, + Check: resource.ComposeTestCheckFunc( + testAccCheckLBCookieStickinessPolicy( + "aws_elb.lb", + "aws_lb_cookie_stickiness_policy.bar", + ), + ), + }, + }, + }) +} + +func testAccCheckLBCookieStickinessPolicyDestroy(s *terraform.State) error { + if len(s.RootModule().Resources) > 0 { + return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources) + } + + 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 + } +} + +const testAccLBCookieStickinessPolicyConfig = ` +resource "aws_elb" "lb" { + name = "test-lb" + availability_zones = ["us-east-1a"] + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +resource "aws_lb_cookie_stickiness_policy" "foo" { + name = "foo_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_expiration_period = 600 +} +` + +const testAccLBCookieStickinessPolicyConfigUpdate = ` +resource "aws_elb" "lb" { + name = "test-lb" + availability_zones = ["us-east-1a"] + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +resource "aws_lb_cookie_stickiness_policy" "foo" { + name = "foo_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_expiration_period = 600 +} + +resource "aws_lb_cookie_stickiness_policy" "bar" { + name = "bar_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_expiration_period = 600 +} +` From 30f8fd738a06a5280d78ce409a89c841584f75cb Mon Sep 17 00:00:00 2001 From: Christopher Tiwald Date: Sun, 26 Apr 2015 18:05:11 -0400 Subject: [PATCH 3/6] aws: Add docs for lb_cookie_stickiness_policy. --- .../lb_cookie_stickiness_policy.html.markdown | 56 +++++++++++++++++++ website/source/layouts/aws.erb | 4 ++ 2 files changed, 60 insertions(+) create mode 100644 website/source/docs/providers/aws/r/lb_cookie_stickiness_policy.html.markdown diff --git a/website/source/docs/providers/aws/r/lb_cookie_stickiness_policy.html.markdown b/website/source/docs/providers/aws/r/lb_cookie_stickiness_policy.html.markdown new file mode 100644 index 000000000000..d728bc2edd32 --- /dev/null +++ b/website/source/docs/providers/aws/r/lb_cookie_stickiness_policy.html.markdown @@ -0,0 +1,56 @@ +--- +layout: "aws" +page_title: "AWS: aws_lb_cookie_stickiness_policy" +sidebar_current: "docs-aws-lb-cookie-stickiness-policy" +description: |- + Provides a load balancer cookie stickiness policy, which allows an ELB to control the sticky session lifetime of the browser. +--- + +# aws\_lb\_cookie\_stickiness\_policy + +Provides a load balancer cookie stickiness policy, which allows an ELB to control the sticky session lifetime of the browser. + +## Example Usage + +``` +resource "aws_elb" "lb" { + name = "test-lb" + availability_zones = ["us-east-1a"] + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +resource "aws_lb_cookie_stickiness_policy" "foo" { + name = "foo_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_expiration_period = 600 +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the stickiness policy. +* `load_balancer` - (Required) The load balancer to which the policy + should be attached. +* `lb_port` - (Required) The load balancer port to which the policy + should be applied. This must be an active listener on the load +balancer. +* `cookie_expiration_period` - (Optional) The time period after which + the session cookie should be considered stale, expressed in seconds. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the policy. +* `name` - The name of the stickiness policy. +* `load_balancer` - The load balancer to which the policy is attached. +* `lb_port` - The load balancer port to which the policy is applied. +* `cookie_expiration_period` - The time period after which the session cookie is considered stale, expressed in seconds. diff --git a/website/source/layouts/aws.erb b/website/source/layouts/aws.erb index 812f13506db8..13af8f27fd35 100644 --- a/website/source/layouts/aws.erb +++ b/website/source/layouts/aws.erb @@ -53,6 +53,10 @@ aws_launch_configuration + > + aws_lb_cookie_stickiness_policy + + > aws_main_route_table_association From 0533d60195730730e3f8fbcdefde7d84741c4845 Mon Sep 17 00:00:00 2001 From: Christopher Tiwald Date: Sun, 26 Apr 2015 17:57:46 -0400 Subject: [PATCH 4/6] aws: Add support for aws_app_cookie_stickiness_policy. This resource represents a session stickiness policy in AWS, and can be applied to an ELB's client-facing listeners. --- builtin/providers/aws/provider.go | 1 + ...source_aws_app_cookie_stickiness_policy.go | 156 ++++++++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index f717a2e55e46..142c487ffef3 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -76,6 +76,7 @@ func Provider() terraform.ResourceProvider { ResourcesMap: map[string]*schema.Resource{ "aws_autoscaling_group": resourceAwsAutoscalingGroup(), + "aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(), "aws_db_instance": resourceAwsDbInstance(), "aws_db_parameter_group": resourceAwsDbParameterGroup(), "aws_db_security_group": resourceAwsDbSecurityGroup(), diff --git a/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go new file mode 100644 index 000000000000..62a3713faf10 --- /dev/null +++ b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go @@ -0,0 +1,156 @@ +package aws + +import ( + "fmt" + "strings" + + "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/service/elb" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsAppCookieStickinessPolicy() *schema.Resource { + return &schema.Resource{ + // There is no concept of "updating" an App Stickiness policy in + // the AWS API. + Create: resourceAwsAppCookieStickinessPolicyCreate, + Update: resourceAwsAppCookieStickinessPolicyCreate, + + Read: resourceAwsAppCookieStickinessPolicyRead, + Delete: resourceAwsAppCookieStickinessPolicyDelete, + + Schema: map[string]*schema.Schema{ + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "load_balancer": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "lb_port": &schema.Schema{ + Type: schema.TypeInt, + Required: true, + ForceNew: true, + }, + + "cookie_name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + } +} + +func resourceAwsAppCookieStickinessPolicyCreate(d *schema.ResourceData, meta interface{}) error { + elbconn := meta.(*AWSClient).elbconn + + // Provision the AppStickinessPolicy + acspOpts := &elb.CreateAppCookieStickinessPolicyInput{ + CookieName: aws.String(d.Get("cookie_name").(string)), + LoadBalancerName: aws.String(d.Get("load_balancer").(string)), + PolicyName: aws.String(d.Get("name").(string)), + } + + if _, err := elbconn.CreateAppCookieStickinessPolicy(acspOpts); err != nil { + return fmt.Errorf("Error creating AppCookieStickinessPolicy: %s", err) + } + + setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{ + LoadBalancerName: aws.String(d.Get("load_balancer").(string)), + LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))), + PolicyNames: []*string{aws.String(d.Get("name").(string))}, + } + + if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil { + return fmt.Errorf("Error setting AppCookieStickinessPolicy: %s", err) + } + + d.SetId(fmt.Sprintf("%s:%d:%s", + *acspOpts.LoadBalancerName, + *setLoadBalancerOpts.LoadBalancerPort, + *acspOpts.PolicyName)) + return nil +} + +func resourceAwsAppCookieStickinessPolicyRead(d *schema.ResourceData, meta interface{}) error { + elbconn := meta.(*AWSClient).elbconn + + lbName, lbPort, policyName := resourceAwsAppCookieStickinessPolicyParseId(d.Id()) + + request := &elb.DescribeLoadBalancerPoliciesInput{ + LoadBalancerName: aws.String(lbName), + PolicyNames: []*string{aws.String(policyName)}, + } + + getResp, err := elbconn.DescribeLoadBalancerPolicies(request) + if err != nil { + if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "PolicyNotFound" { + // The policy is gone. + d.SetId("") + return nil + } + return fmt.Errorf("Error retrieving policy: %s", err) + } + + if len(getResp.PolicyDescriptions) != 1 { + return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions) + } + + // We can get away with this because there's only one attribute, the + // cookie expiration, in these descriptions. + policyDesc := getResp.PolicyDescriptions[0] + cookieAttr := policyDesc.PolicyAttributeDescriptions[0] + if *cookieAttr.AttributeName != "CookieName" { + return fmt.Errorf("Unable to find cookie Name.") + } + d.Set("cookie_name", cookieAttr.AttributeValue) + + d.Set("name", policyName) + d.Set("load_balancer", lbName) + d.Set("lb_port", lbPort) + + return nil +} + +func resourceAwsAppCookieStickinessPolicyDelete(d *schema.ResourceData, meta interface{}) error { + elbconn := meta.(*AWSClient).elbconn + + lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(d.Id()) + + // Perversely, if we Set an empty list of PolicyNames, we detach the + // policies attached to a listener, which is required to delete the + // policy itself. + setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{ + LoadBalancerName: aws.String(d.Get("load_balancer").(string)), + LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))), + PolicyNames: []*string{}, + } + + if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil { + return fmt.Errorf("Error removing AppCookieStickinessPolicy: %s", err) + } + + request := &elb.DeleteLoadBalancerPolicyInput{ + LoadBalancerName: aws.String(lbName), + PolicyName: aws.String(policyName), + } + + if _, err := elbconn.DeleteLoadBalancerPolicy(request); err != nil { + return fmt.Errorf("Error deleting App stickiness policy %s: %s", d.Id(), err) + } + return nil +} + +// resourceAwsAppCookieStickinessPolicyParseId takes an ID and parses it into +// it's constituent parts. You need three axes (LB name, policy name, and LB +// port) to create or identify a stickiness policy in AWS's API. +func resourceAwsAppCookieStickinessPolicyParseId(id string) (string, string, string) { + parts := strings.SplitN(id, ":", 3) + return parts[0], parts[1], parts[2] +} From 99ce8cf25e8cd0664b6de5fdcf674011f2fe67e6 Mon Sep 17 00:00:00 2001 From: Christopher Tiwald Date: Sun, 26 Apr 2015 18:01:48 -0400 Subject: [PATCH 5/6] aws: Add acceptance test of aws_app_cookie_stickiness_policy. --- ...e_aws_app_cookie_stickiness_policy_test.go | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go diff --git a/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go new file mode 100644 index 000000000000..3846fb68ea3d --- /dev/null +++ b/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy_test.go @@ -0,0 +1,125 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/awslabs/aws-sdk-go/aws" + "github.com/awslabs/aws-sdk-go/service/elb" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSAppCookieStickinessPolicy(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAppCookieStickinessPolicyDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAppCookieStickinessPolicyConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAppCookieStickinessPolicy( + "aws_elb.lb", + "aws_app_cookie_stickiness_policy.foo", + ), + ), + }, + resource.TestStep{ + Config: testAccAppCookieStickinessPolicyConfigUpdate, + Check: resource.ComposeTestCheckFunc( + testAccCheckAppCookieStickinessPolicy( + "aws_elb.lb", + "aws_app_cookie_stickiness_policy.bar", + ), + ), + }, + }, + }) +} + +func testAccCheckAppCookieStickinessPolicyDestroy(s *terraform.State) error { + if len(s.RootModule().Resources) > 0 { + return fmt.Errorf("Expected all resources to be gone, but found: %#v", s.RootModule().Resources) + } + + 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 + } +} + +const testAccAppCookieStickinessPolicyConfig = ` +resource "aws_elb" "lb" { + name = "test-lb" + availability_zones = ["us-east-1a"] + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +resource "aws_app_cookie_stickiness_policy" "foo" { + name = "foo_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_name = "MyAppCookie" +} +` + +const testAccAppCookieStickinessPolicyConfigUpdate = ` +resource "aws_elb" "lb" { + name = "test-lb" + availability_zones = ["us-east-1a"] + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +resource "aws_app_cookie_stickiness_policy" "foo" { + name = "foo_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_name = "MyAppCookie" +} + +resource "aws_app_cookie_stickiness_policy" "bar" { + name = "bar_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_name = "MyAppCookie" +} +` From d42441f9cd52aa7e090214a60f0287c7a81b89c9 Mon Sep 17 00:00:00 2001 From: Christopher Tiwald Date: Sun, 26 Apr 2015 18:05:11 -0400 Subject: [PATCH 6/6] aws: Add docs for app_cookie_stickiness_policy. --- ...app_cookie_stickiness_policy.html.markdown | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 website/source/docs/providers/aws/r/app_cookie_stickiness_policy.html.markdown diff --git a/website/source/docs/providers/aws/r/app_cookie_stickiness_policy.html.markdown b/website/source/docs/providers/aws/r/app_cookie_stickiness_policy.html.markdown new file mode 100644 index 000000000000..10292d7e3005 --- /dev/null +++ b/website/source/docs/providers/aws/r/app_cookie_stickiness_policy.html.markdown @@ -0,0 +1,55 @@ +--- +layout: "aws" +page_title: "AWS: aws_app_cookie_stickiness_policy" +sidebar_current: "docs-aws-app-cookie-stickiness-policy" +description: |- + Provides an application cookie stickiness policy, which allows an ELB to wed its stickiness cookie to a cookie generated by your application. +--- + +# aws\_app\_cookie\_stickiness\_policy + +Provides an application cookie stickiness policy, which allows an ELB to wed its sticky cookie's expiration to a cookie generated by your application. + +## Example Usage + +``` +resource "aws_elb" "lb" { + name = "test-lb" + availability_zones = ["us-east-1a"] + listener { + instance_port = 8000 + instance_protocol = "http" + lb_port = 80 + lb_protocol = "http" + } +} + +resource "aws_app_cookie_stickiness_policy" "foo" { + name = "foo_policy" + load_balancer = "${aws_elb.lb}" + lb_port = 80 + cookie_name = "MyAppCookie" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The name of the stickiness policy. +* `load_balancer` - (Required) The load balancer to which the policy + should be attached. +* `lb_port` - (Required) The load balancer port to which the policy + should be applied. This must be an active listener on the load +balancer. +* `cookie_name` - (Required) The application cookie whose lifetime the ELB's cookie should follow. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the policy. +* `name` - The name of the stickiness policy. +* `load_balancer` - The load balancer to which the policy is attached. +* `lb_port` - The load balancer port to which the policy is applied. +* `cookie_name` - The application cookie whose lifetime the ELB's cookie should follow.