github.com/i0n/terraform@v0.4.3-0.20150506151324-010a39a58ec1/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/awslabs/aws-sdk-go/aws"
     8  	"github.com/awslabs/aws-sdk-go/service/elb"
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceAwsAppCookieStickinessPolicy() *schema.Resource {
    13  	return &schema.Resource{
    14  		// There is no concept of "updating" an App Stickiness policy in
    15  		// the AWS API.
    16  		Create: resourceAwsAppCookieStickinessPolicyCreate,
    17  		Update: resourceAwsAppCookieStickinessPolicyCreate,
    18  
    19  		Read:   resourceAwsAppCookieStickinessPolicyRead,
    20  		Delete: resourceAwsAppCookieStickinessPolicyDelete,
    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_name": &schema.Schema{
    42  				Type:     schema.TypeString,
    43  				Required: true,
    44  				ForceNew: true,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func resourceAwsAppCookieStickinessPolicyCreate(d *schema.ResourceData, meta interface{}) error {
    51  	elbconn := meta.(*AWSClient).elbconn
    52  
    53  	// Provision the AppStickinessPolicy
    54  	acspOpts := &elb.CreateAppCookieStickinessPolicyInput{
    55  		CookieName:       aws.String(d.Get("cookie_name").(string)),
    56  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
    57  		PolicyName:       aws.String(d.Get("name").(string)),
    58  	}
    59  
    60  	if _, err := elbconn.CreateAppCookieStickinessPolicy(acspOpts); err != nil {
    61  		return fmt.Errorf("Error creating AppCookieStickinessPolicy: %s", err)
    62  	}
    63  
    64  	setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
    65  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
    66  		LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))),
    67  		PolicyNames:      []*string{aws.String(d.Get("name").(string))},
    68  	}
    69  
    70  	if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
    71  		return fmt.Errorf("Error setting AppCookieStickinessPolicy: %s", err)
    72  	}
    73  
    74  	d.SetId(fmt.Sprintf("%s:%d:%s",
    75  		*acspOpts.LoadBalancerName,
    76  		*setLoadBalancerOpts.LoadBalancerPort,
    77  		*acspOpts.PolicyName))
    78  	return nil
    79  }
    80  
    81  func resourceAwsAppCookieStickinessPolicyRead(d *schema.ResourceData, meta interface{}) error {
    82  	elbconn := meta.(*AWSClient).elbconn
    83  
    84  	lbName, lbPort, policyName := resourceAwsAppCookieStickinessPolicyParseId(d.Id())
    85  
    86  	request := &elb.DescribeLoadBalancerPoliciesInput{
    87  		LoadBalancerName: aws.String(lbName),
    88  		PolicyNames:      []*string{aws.String(policyName)},
    89  	}
    90  
    91  	getResp, err := elbconn.DescribeLoadBalancerPolicies(request)
    92  	if err != nil {
    93  		if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "PolicyNotFound" {
    94  			// The policy is gone.
    95  			d.SetId("")
    96  			return nil
    97  		}
    98  		return fmt.Errorf("Error retrieving policy: %s", err)
    99  	}
   100  
   101  	if len(getResp.PolicyDescriptions) != 1 {
   102  		return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions)
   103  	}
   104  
   105  	// We can get away with this because there's only one attribute, the
   106  	// cookie expiration, in these descriptions.
   107  	policyDesc := getResp.PolicyDescriptions[0]
   108  	cookieAttr := policyDesc.PolicyAttributeDescriptions[0]
   109  	if *cookieAttr.AttributeName != "CookieName" {
   110  		return fmt.Errorf("Unable to find cookie Name.")
   111  	}
   112  	d.Set("cookie_name", cookieAttr.AttributeValue)
   113  
   114  	d.Set("name", policyName)
   115  	d.Set("load_balancer", lbName)
   116  	d.Set("lb_port", lbPort)
   117  
   118  	return nil
   119  }
   120  
   121  func resourceAwsAppCookieStickinessPolicyDelete(d *schema.ResourceData, meta interface{}) error {
   122  	elbconn := meta.(*AWSClient).elbconn
   123  
   124  	lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(d.Id())
   125  
   126  	// Perversely, if we Set an empty list of PolicyNames, we detach the
   127  	// policies attached to a listener, which is required to delete the
   128  	// policy itself.
   129  	setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
   130  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
   131  		LoadBalancerPort: aws.Long(int64(d.Get("lb_port").(int))),
   132  		PolicyNames:      []*string{},
   133  	}
   134  
   135  	if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
   136  		return fmt.Errorf("Error removing AppCookieStickinessPolicy: %s", err)
   137  	}
   138  
   139  	request := &elb.DeleteLoadBalancerPolicyInput{
   140  		LoadBalancerName: aws.String(lbName),
   141  		PolicyName:       aws.String(policyName),
   142  	}
   143  
   144  	if _, err := elbconn.DeleteLoadBalancerPolicy(request); err != nil {
   145  		return fmt.Errorf("Error deleting App stickiness policy %s: %s", d.Id(), err)
   146  	}
   147  	return nil
   148  }
   149  
   150  // resourceAwsAppCookieStickinessPolicyParseId takes an ID and parses it into
   151  // it's constituent parts. You need three axes (LB name, policy name, and LB
   152  // port) to create or identify a stickiness policy in AWS's API.
   153  func resourceAwsAppCookieStickinessPolicyParseId(id string) (string, string, string) {
   154  	parts := strings.SplitN(id, ":", 3)
   155  	return parts[0], parts[1], parts[2]
   156  }