github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_lb_cookie_stickiness_policy.go (about)

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