github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/awslabs/aws-sdk-go/aws" 8 "github.com/awslabs/aws-sdk-go/aws/awserr" 9 "github.com/awslabs/aws-sdk-go/service/elb" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceAwsLBCookieStickinessPolicy() *schema.Resource { 14 return &schema.Resource{ 15 // There is no concept of "updating" an LB Stickiness policy in 16 // the AWS API. 17 Create: resourceAwsLBCookieStickinessPolicyCreate, 18 Update: resourceAwsLBCookieStickinessPolicyCreate, 19 20 Read: resourceAwsLBCookieStickinessPolicyRead, 21 Delete: resourceAwsLBCookieStickinessPolicyDelete, 22 23 Schema: map[string]*schema.Schema{ 24 "name": &schema.Schema{ 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 30 "load_balancer": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 ForceNew: true, 34 }, 35 36 "lb_port": &schema.Schema{ 37 Type: schema.TypeInt, 38 Required: true, 39 ForceNew: true, 40 }, 41 42 "cookie_expiration_period": &schema.Schema{ 43 Type: schema.TypeInt, 44 Optional: true, 45 ForceNew: true, 46 }, 47 }, 48 } 49 } 50 51 func resourceAwsLBCookieStickinessPolicyCreate(d *schema.ResourceData, meta interface{}) error { 52 elbconn := meta.(*AWSClient).elbconn 53 54 // Provision the LBStickinessPolicy 55 lbspOpts := &elb.CreateLBCookieStickinessPolicyInput{ 56 CookieExpirationPeriod: aws.Long(int64(d.Get("cookie_expiration_period").(int))), 57 LoadBalancerName: aws.String(d.Get("load_balancer").(string)), 58 PolicyName: aws.String(d.Get("name").(string)), 59 } 60 61 if _, err := elbconn.CreateLBCookieStickinessPolicy(lbspOpts); err != nil { 62 return fmt.Errorf("Error creating LBCookieStickinessPolicy: %s", err) 63 } 64 65 setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{ 66 LoadBalancerName: aws.String(d.Get("load_balancer").(string)), 67 LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))), 68 PolicyNames: []*string{aws.String(d.Get("name").(string))}, 69 } 70 71 if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil { 72 return fmt.Errorf("Error setting LBCookieStickinessPolicy: %s", err) 73 } 74 75 d.SetId(fmt.Sprintf("%s:%d:%s", 76 *lbspOpts.LoadBalancerName, 77 *setLoadBalancerOpts.LoadBalancerPort, 78 *lbspOpts.PolicyName)) 79 return nil 80 } 81 82 func resourceAwsLBCookieStickinessPolicyRead(d *schema.ResourceData, meta interface{}) error { 83 elbconn := meta.(*AWSClient).elbconn 84 85 lbName, lbPort, policyName := resourceAwsLBCookieStickinessPolicyParseId(d.Id()) 86 87 request := &elb.DescribeLoadBalancerPoliciesInput{ 88 LoadBalancerName: aws.String(lbName), 89 PolicyNames: []*string{aws.String(policyName)}, 90 } 91 92 getResp, err := elbconn.DescribeLoadBalancerPolicies(request) 93 if err != nil { 94 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "PolicyNotFound" { 95 // The policy is gone. 96 d.SetId("") 97 return nil 98 } 99 return fmt.Errorf("Error retrieving policy: %s", err) 100 } 101 102 if len(getResp.PolicyDescriptions) != 1 { 103 return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions) 104 } 105 106 // We can get away with this because there's only one attribute, the 107 // cookie expiration, in these descriptions. 108 policyDesc := getResp.PolicyDescriptions[0] 109 cookieAttr := policyDesc.PolicyAttributeDescriptions[0] 110 if *cookieAttr.AttributeName != "CookieExpirationPeriod" { 111 return fmt.Errorf("Unable to find cookie expiration period.") 112 } 113 d.Set("cookie_expiration_period", cookieAttr.AttributeValue) 114 115 d.Set("name", policyName) 116 d.Set("load_balancer", lbName) 117 d.Set("lb_port", lbPort) 118 119 return nil 120 } 121 122 func resourceAwsLBCookieStickinessPolicyDelete(d *schema.ResourceData, meta interface{}) error { 123 elbconn := meta.(*AWSClient).elbconn 124 125 lbName, _, policyName := resourceAwsLBCookieStickinessPolicyParseId(d.Id()) 126 127 // Perversely, if we Set an empty list of PolicyNames, we detach the 128 // policies attached to a listener, which is required to delete the 129 // policy itself. 130 setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{ 131 LoadBalancerName: aws.String(d.Get("load_balancer").(string)), 132 LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))), 133 PolicyNames: []*string{}, 134 } 135 136 if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil { 137 return fmt.Errorf("Error removing LBCookieStickinessPolicy: %s", err) 138 } 139 140 request := &elb.DeleteLoadBalancerPolicyInput{ 141 LoadBalancerName: aws.String(lbName), 142 PolicyName: aws.String(policyName), 143 } 144 145 if _, err := elbconn.DeleteLoadBalancerPolicy(request); err != nil { 146 return fmt.Errorf("Error deleting LB stickiness policy %s: %s", d.Id(), err) 147 } 148 return nil 149 } 150 151 // resourceAwsLBCookieStickinessPolicyParseId takes an ID and parses it into 152 // it's constituent parts. You need three axes (LB name, policy name, and LB 153 // port) to create or identify a stickiness policy in AWS's API. 154 func resourceAwsLBCookieStickinessPolicyParseId(id string) (string, string, string) { 155 parts := strings.SplitN(id, ":", 3) 156 return parts[0], parts[1], parts[2] 157 }