github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/aws/resource_aws_load_balancer_backend_server_policy.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     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 resourceAwsLoadBalancerBackendServerPolicies() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAwsLoadBalancerBackendServerPoliciesCreate,
    17  		Read:   resourceAwsLoadBalancerBackendServerPoliciesRead,
    18  		Update: resourceAwsLoadBalancerBackendServerPoliciesCreate,
    19  		Delete: resourceAwsLoadBalancerBackendServerPoliciesDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"load_balancer_name": &schema.Schema{
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  			},
    26  
    27  			"policy_names": &schema.Schema{
    28  				Type:     schema.TypeSet,
    29  				Elem:     &schema.Schema{Type: schema.TypeString},
    30  				Optional: true,
    31  				Set:      schema.HashString,
    32  			},
    33  
    34  			"instance_port": &schema.Schema{
    35  				Type:     schema.TypeInt,
    36  				Required: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceAwsLoadBalancerBackendServerPoliciesCreate(d *schema.ResourceData, meta interface{}) error {
    43  	elbconn := meta.(*AWSClient).elbconn
    44  
    45  	loadBalancerName := d.Get("load_balancer_name")
    46  
    47  	policyNames := []*string{}
    48  	if v, ok := d.GetOk("policy_names"); ok {
    49  		policyNames = expandStringList(v.(*schema.Set).List())
    50  	}
    51  
    52  	setOpts := &elb.SetLoadBalancerPoliciesForBackendServerInput{
    53  		LoadBalancerName: aws.String(loadBalancerName.(string)),
    54  		InstancePort:     aws.Int64(int64(d.Get("instance_port").(int))),
    55  		PolicyNames:      policyNames,
    56  	}
    57  
    58  	if _, err := elbconn.SetLoadBalancerPoliciesForBackendServer(setOpts); err != nil {
    59  		return fmt.Errorf("Error setting LoadBalancerPoliciesForBackendServer: %s", err)
    60  	}
    61  
    62  	d.SetId(fmt.Sprintf("%s:%s", *setOpts.LoadBalancerName, strconv.FormatInt(*setOpts.InstancePort, 10)))
    63  	return resourceAwsLoadBalancerBackendServerPoliciesRead(d, meta)
    64  }
    65  
    66  func resourceAwsLoadBalancerBackendServerPoliciesRead(d *schema.ResourceData, meta interface{}) error {
    67  	elbconn := meta.(*AWSClient).elbconn
    68  
    69  	loadBalancerName, instancePort := resourceAwsLoadBalancerBackendServerPoliciesParseId(d.Id())
    70  
    71  	describeElbOpts := &elb.DescribeLoadBalancersInput{
    72  		LoadBalancerNames: []*string{aws.String(loadBalancerName)},
    73  	}
    74  
    75  	describeResp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
    76  
    77  	if err != nil {
    78  		if ec2err, ok := err.(awserr.Error); ok {
    79  			if ec2err.Code() == "LoadBalancerNotFound" {
    80  				d.SetId("")
    81  				return fmt.Errorf("LoadBalancerNotFound: %s", err)
    82  			}
    83  		}
    84  		return fmt.Errorf("Error retrieving ELB description: %s", err)
    85  	}
    86  
    87  	if len(describeResp.LoadBalancerDescriptions) != 1 {
    88  		return fmt.Errorf("Unable to find ELB: %#v", describeResp.LoadBalancerDescriptions)
    89  	}
    90  
    91  	lb := describeResp.LoadBalancerDescriptions[0]
    92  
    93  	policyNames := []*string{}
    94  	for _, backendServer := range lb.BackendServerDescriptions {
    95  		if instancePort != strconv.Itoa(int(*backendServer.InstancePort)) {
    96  			continue
    97  		}
    98  
    99  		for _, name := range backendServer.PolicyNames {
   100  			policyNames = append(policyNames, name)
   101  		}
   102  	}
   103  
   104  	d.Set("load_balancer_name", loadBalancerName)
   105  	d.Set("instance_port", instancePort)
   106  	d.Set("policy_names", flattenStringList(policyNames))
   107  
   108  	return nil
   109  }
   110  
   111  func resourceAwsLoadBalancerBackendServerPoliciesDelete(d *schema.ResourceData, meta interface{}) error {
   112  	elbconn := meta.(*AWSClient).elbconn
   113  
   114  	loadBalancerName, instancePort := resourceAwsLoadBalancerBackendServerPoliciesParseId(d.Id())
   115  
   116  	instancePortInt, err := strconv.ParseInt(instancePort, 10, 64)
   117  	if err != nil {
   118  		return fmt.Errorf("Error parsing instancePort as integer: %s", err)
   119  	}
   120  
   121  	setOpts := &elb.SetLoadBalancerPoliciesForBackendServerInput{
   122  		LoadBalancerName: aws.String(loadBalancerName),
   123  		InstancePort:     aws.Int64(instancePortInt),
   124  		PolicyNames:      []*string{},
   125  	}
   126  
   127  	if _, err := elbconn.SetLoadBalancerPoliciesForBackendServer(setOpts); err != nil {
   128  		return fmt.Errorf("Error setting LoadBalancerPoliciesForBackendServer: %s", err)
   129  	}
   130  
   131  	d.SetId("")
   132  	return nil
   133  }
   134  
   135  func resourceAwsLoadBalancerBackendServerPoliciesParseId(id string) (string, string) {
   136  	parts := strings.SplitN(id, ":", 2)
   137  	return parts[0], parts[1]
   138  }