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

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/aws/aws-sdk-go/aws"
     8  	"github.com/aws/aws-sdk-go/service/elb"
     9  	"github.com/hashicorp/terraform/helper/resource"
    10  	"github.com/hashicorp/terraform/helper/schema"
    11  )
    12  
    13  func resourceAwsElbAttachment() *schema.Resource {
    14  	return &schema.Resource{
    15  		Create: resourceAwsElbAttachmentCreate,
    16  		Read:   resourceAwsElbAttachmentRead,
    17  		Delete: resourceAwsElbAttachmentDelete,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"elb": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				ForceNew: true,
    23  				Required: true,
    24  			},
    25  
    26  			"instance": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				ForceNew: true,
    29  				Required: true,
    30  			},
    31  		},
    32  	}
    33  }
    34  
    35  func resourceAwsElbAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
    36  	elbconn := meta.(*AWSClient).elbconn
    37  	elbName := d.Get("elb").(string)
    38  
    39  	instance := d.Get("instance").(string)
    40  
    41  	registerInstancesOpts := elb.RegisterInstancesWithLoadBalancerInput{
    42  		LoadBalancerName: aws.String(elbName),
    43  		Instances:        []*elb.Instance{{InstanceId: aws.String(instance)}},
    44  	}
    45  
    46  	log.Printf("[INFO] registering instance %s with ELB %s", instance, elbName)
    47  
    48  	_, err := elbconn.RegisterInstancesWithLoadBalancer(&registerInstancesOpts)
    49  	if err != nil {
    50  		return fmt.Errorf("Failure registering instances with ELB: %s", err)
    51  	}
    52  
    53  	d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", elbName)))
    54  
    55  	return nil
    56  }
    57  
    58  func resourceAwsElbAttachmentRead(d *schema.ResourceData, meta interface{}) error {
    59  	elbconn := meta.(*AWSClient).elbconn
    60  	elbName := d.Get("elb").(string)
    61  
    62  	// only add the instance that was previously defined for this resource
    63  	expected := d.Get("instance").(string)
    64  
    65  	// Retrieve the ELB properties to get a list of attachments
    66  	describeElbOpts := &elb.DescribeLoadBalancersInput{
    67  		LoadBalancerNames: []*string{aws.String(elbName)},
    68  	}
    69  
    70  	resp, err := elbconn.DescribeLoadBalancers(describeElbOpts)
    71  	if err != nil {
    72  		if isLoadBalancerNotFound(err) {
    73  			log.Printf("[ERROR] ELB %s not found", elbName)
    74  			d.SetId("")
    75  			return nil
    76  		}
    77  		return fmt.Errorf("Error retrieving ELB: %s", err)
    78  	}
    79  	if len(resp.LoadBalancerDescriptions) != 1 {
    80  		log.Printf("[ERROR] Unable to find ELB: %s", resp.LoadBalancerDescriptions)
    81  		d.SetId("")
    82  		return nil
    83  	}
    84  
    85  	// only set the instance Id that this resource manages
    86  	found := false
    87  	for _, i := range resp.LoadBalancerDescriptions[0].Instances {
    88  		if expected == *i.InstanceId {
    89  			d.Set("instance", expected)
    90  			found = true
    91  		}
    92  	}
    93  
    94  	if !found {
    95  		log.Printf("[WARN] instance %s not found in elb attachments", expected)
    96  		d.SetId("")
    97  	}
    98  
    99  	return nil
   100  }
   101  
   102  func resourceAwsElbAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
   103  	elbconn := meta.(*AWSClient).elbconn
   104  	elbName := d.Get("elb").(string)
   105  
   106  	instance := d.Get("instance").(string)
   107  
   108  	log.Printf("[INFO] Deleting Attachment %s from: %s", instance, elbName)
   109  
   110  	deRegisterInstancesOpts := elb.DeregisterInstancesFromLoadBalancerInput{
   111  		LoadBalancerName: aws.String(elbName),
   112  		Instances:        []*elb.Instance{{InstanceId: aws.String(instance)}},
   113  	}
   114  
   115  	_, err := elbconn.DeregisterInstancesFromLoadBalancer(&deRegisterInstancesOpts)
   116  	if err != nil {
   117  		return fmt.Errorf("Failure deregistering instances from ELB: %s", err)
   118  	}
   119  
   120  	return nil
   121  }