github.com/anuaimi/terraform@v0.6.4-0.20150904235404-2bf9aec61da8/builtin/providers/aws/resource_aws_app_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 resourceAwsAppCookieStickinessPolicy() *schema.Resource {
    14  	return &schema.Resource{
    15  		// There is no concept of "updating" an App Stickiness policy in
    16  		// the AWS API.
    17  		Create: resourceAwsAppCookieStickinessPolicyCreate,
    18  		Update: resourceAwsAppCookieStickinessPolicyCreate,
    19  
    20  		Read:   resourceAwsAppCookieStickinessPolicyRead,
    21  		Delete: resourceAwsAppCookieStickinessPolicyDelete,
    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_name": &schema.Schema{
    43  				Type:     schema.TypeString,
    44  				Required: true,
    45  				ForceNew: true,
    46  			},
    47  		},
    48  	}
    49  }
    50  
    51  func resourceAwsAppCookieStickinessPolicyCreate(d *schema.ResourceData, meta interface{}) error {
    52  	elbconn := meta.(*AWSClient).elbconn
    53  
    54  	// Provision the AppStickinessPolicy
    55  	acspOpts := &elb.CreateAppCookieStickinessPolicyInput{
    56  		CookieName:       aws.String(d.Get("cookie_name").(string)),
    57  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
    58  		PolicyName:       aws.String(d.Get("name").(string)),
    59  	}
    60  
    61  	if _, err := elbconn.CreateAppCookieStickinessPolicy(acspOpts); err != nil {
    62  		return fmt.Errorf("Error creating AppCookieStickinessPolicy: %s", err)
    63  	}
    64  
    65  	setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
    66  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
    67  		LoadBalancerPort: aws.Int64(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 AppCookieStickinessPolicy: %s", err)
    73  	}
    74  
    75  	d.SetId(fmt.Sprintf("%s:%d:%s",
    76  		*acspOpts.LoadBalancerName,
    77  		*setLoadBalancerOpts.LoadBalancerPort,
    78  		*acspOpts.PolicyName))
    79  	return nil
    80  }
    81  
    82  func resourceAwsAppCookieStickinessPolicyRead(d *schema.ResourceData, meta interface{}) error {
    83  	elbconn := meta.(*AWSClient).elbconn
    84  
    85  	lbName, lbPort, policyName := resourceAwsAppCookieStickinessPolicyParseId(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 != "CookieName" {
   111  		return fmt.Errorf("Unable to find cookie Name.")
   112  	}
   113  	d.Set("cookie_name", 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 resourceAwsAppCookieStickinessPolicyDelete(d *schema.ResourceData, meta interface{}) error {
   123  	elbconn := meta.(*AWSClient).elbconn
   124  
   125  	lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(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.Int64(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 AppCookieStickinessPolicy: %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 App stickiness policy %s: %s", d.Id(), err)
   147  	}
   148  	return nil
   149  }
   150  
   151  // resourceAwsAppCookieStickinessPolicyParseId 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 resourceAwsAppCookieStickinessPolicyParseId(id string) (string, string, string) {
   155  	parts := strings.SplitN(id, ":", 3)
   156  	return parts[0], parts[1], parts[2]
   157  }