github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_app_cookie_stickiness_policy.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     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 resourceAwsAppCookieStickinessPolicy() *schema.Resource {
    15  	return &schema.Resource{
    16  		// There is no concept of "updating" an App Stickiness policy in
    17  		// the AWS API.
    18  		Create: resourceAwsAppCookieStickinessPolicyCreate,
    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  				ValidateFunc: func(v interface{}, k string) (ws []string, es []error) {
    28  					value := v.(string)
    29  					if !regexp.MustCompile(`^[0-9A-Za-z-]+$`).MatchString(value) {
    30  						es = append(es, fmt.Errorf(
    31  							"only alphanumeric characters and hyphens allowed in %q", k))
    32  					}
    33  					return
    34  				},
    35  			},
    36  
    37  			"load_balancer": &schema.Schema{
    38  				Type:     schema.TypeString,
    39  				Required: true,
    40  				ForceNew: true,
    41  			},
    42  
    43  			"lb_port": &schema.Schema{
    44  				Type:     schema.TypeInt,
    45  				Required: true,
    46  				ForceNew: true,
    47  			},
    48  
    49  			"cookie_name": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Required: true,
    52  				ForceNew: true,
    53  			},
    54  		},
    55  	}
    56  }
    57  
    58  func resourceAwsAppCookieStickinessPolicyCreate(d *schema.ResourceData, meta interface{}) error {
    59  	elbconn := meta.(*AWSClient).elbconn
    60  
    61  	// Provision the AppStickinessPolicy
    62  	acspOpts := &elb.CreateAppCookieStickinessPolicyInput{
    63  		CookieName:       aws.String(d.Get("cookie_name").(string)),
    64  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
    65  		PolicyName:       aws.String(d.Get("name").(string)),
    66  	}
    67  
    68  	if _, err := elbconn.CreateAppCookieStickinessPolicy(acspOpts); err != nil {
    69  		return fmt.Errorf("Error creating AppCookieStickinessPolicy: %s", err)
    70  	}
    71  
    72  	setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
    73  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
    74  		LoadBalancerPort: aws.Int64(int64(d.Get("lb_port").(int))),
    75  		PolicyNames:      []*string{aws.String(d.Get("name").(string))},
    76  	}
    77  
    78  	if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
    79  		return fmt.Errorf("Error setting AppCookieStickinessPolicy: %s", err)
    80  	}
    81  
    82  	d.SetId(fmt.Sprintf("%s:%d:%s",
    83  		*acspOpts.LoadBalancerName,
    84  		*setLoadBalancerOpts.LoadBalancerPort,
    85  		*acspOpts.PolicyName))
    86  	return nil
    87  }
    88  
    89  func resourceAwsAppCookieStickinessPolicyRead(d *schema.ResourceData, meta interface{}) error {
    90  	elbconn := meta.(*AWSClient).elbconn
    91  
    92  	lbName, lbPort, policyName := resourceAwsAppCookieStickinessPolicyParseId(d.Id())
    93  
    94  	request := &elb.DescribeLoadBalancerPoliciesInput{
    95  		LoadBalancerName: aws.String(lbName),
    96  		PolicyNames:      []*string{aws.String(policyName)},
    97  	}
    98  
    99  	getResp, err := elbconn.DescribeLoadBalancerPolicies(request)
   100  	if err != nil {
   101  		if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "PolicyNotFound" {
   102  			// The policy is gone.
   103  			d.SetId("")
   104  			return nil
   105  		}
   106  		return fmt.Errorf("Error retrieving policy: %s", err)
   107  	}
   108  
   109  	if len(getResp.PolicyDescriptions) != 1 {
   110  		return fmt.Errorf("Unable to find policy %#v", getResp.PolicyDescriptions)
   111  	}
   112  
   113  	// We can get away with this because there's only one attribute, the
   114  	// cookie expiration, in these descriptions.
   115  	policyDesc := getResp.PolicyDescriptions[0]
   116  	cookieAttr := policyDesc.PolicyAttributeDescriptions[0]
   117  	if *cookieAttr.AttributeName != "CookieName" {
   118  		return fmt.Errorf("Unable to find cookie Name.")
   119  	}
   120  	d.Set("cookie_name", cookieAttr.AttributeValue)
   121  
   122  	d.Set("name", policyName)
   123  	d.Set("load_balancer", lbName)
   124  	d.Set("lb_port", lbPort)
   125  
   126  	return nil
   127  }
   128  
   129  func resourceAwsAppCookieStickinessPolicyDelete(d *schema.ResourceData, meta interface{}) error {
   130  	elbconn := meta.(*AWSClient).elbconn
   131  
   132  	lbName, _, policyName := resourceAwsAppCookieStickinessPolicyParseId(d.Id())
   133  
   134  	// Perversely, if we Set an empty list of PolicyNames, we detach the
   135  	// policies attached to a listener, which is required to delete the
   136  	// policy itself.
   137  	setLoadBalancerOpts := &elb.SetLoadBalancerPoliciesOfListenerInput{
   138  		LoadBalancerName: aws.String(d.Get("load_balancer").(string)),
   139  		LoadBalancerPort: aws.Int64(int64(d.Get("lb_port").(int))),
   140  		PolicyNames:      []*string{},
   141  	}
   142  
   143  	if _, err := elbconn.SetLoadBalancerPoliciesOfListener(setLoadBalancerOpts); err != nil {
   144  		return fmt.Errorf("Error removing AppCookieStickinessPolicy: %s", err)
   145  	}
   146  
   147  	request := &elb.DeleteLoadBalancerPolicyInput{
   148  		LoadBalancerName: aws.String(lbName),
   149  		PolicyName:       aws.String(policyName),
   150  	}
   151  
   152  	if _, err := elbconn.DeleteLoadBalancerPolicy(request); err != nil {
   153  		return fmt.Errorf("Error deleting App stickiness policy %s: %s", d.Id(), err)
   154  	}
   155  	return nil
   156  }
   157  
   158  // resourceAwsAppCookieStickinessPolicyParseId takes an ID and parses it into
   159  // it's constituent parts. You need three axes (LB name, policy name, and LB
   160  // port) to create or identify a stickiness policy in AWS's API.
   161  func resourceAwsAppCookieStickinessPolicyParseId(id string) (string, string, string) {
   162  	parts := strings.SplitN(id, ":", 3)
   163  	return parts[0], parts[1], parts[2]
   164  }