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