github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_autoscaling_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/autoscaling"
     9  	"github.com/hashicorp/errwrap"
    10  	"github.com/hashicorp/terraform/helper/resource"
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  )
    13  
    14  func resourceAwsAutoscalingAttachment() *schema.Resource {
    15  	return &schema.Resource{
    16  		Create: resourceAwsAutoscalingAttachmentCreate,
    17  		Read:   resourceAwsAutoscalingAttachmentRead,
    18  		Delete: resourceAwsAutoscalingAttachmentDelete,
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"autoscaling_group_name": {
    22  				Type:     schema.TypeString,
    23  				ForceNew: true,
    24  				Required: true,
    25  			},
    26  
    27  			"elb": {
    28  				Type:     schema.TypeString,
    29  				ForceNew: true,
    30  				Optional: true,
    31  			},
    32  
    33  			"alb_target_group_arn": {
    34  				Type:     schema.TypeString,
    35  				ForceNew: true,
    36  				Optional: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceAwsAutoscalingAttachmentCreate(d *schema.ResourceData, meta interface{}) error {
    43  	asgconn := meta.(*AWSClient).autoscalingconn
    44  	asgName := d.Get("autoscaling_group_name").(string)
    45  
    46  	if v, ok := d.GetOk("elb"); ok {
    47  		attachOpts := &autoscaling.AttachLoadBalancersInput{
    48  			AutoScalingGroupName: aws.String(asgName),
    49  			LoadBalancerNames:    []*string{aws.String(v.(string))},
    50  		}
    51  
    52  		log.Printf("[INFO] registering asg %s with ELBs %s", asgName, v.(string))
    53  
    54  		if _, err := asgconn.AttachLoadBalancers(attachOpts); err != nil {
    55  			return errwrap.Wrapf(fmt.Sprintf("Failure attaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, v.(string)), err)
    56  		}
    57  	}
    58  
    59  	if v, ok := d.GetOk("alb_target_group_arn"); ok {
    60  		attachOpts := &autoscaling.AttachLoadBalancerTargetGroupsInput{
    61  			AutoScalingGroupName: aws.String(asgName),
    62  			TargetGroupARNs:      []*string{aws.String(v.(string))},
    63  		}
    64  
    65  		log.Printf("[INFO] registering asg %s with ALB Target Group %s", asgName, v.(string))
    66  
    67  		if _, err := asgconn.AttachLoadBalancerTargetGroups(attachOpts); err != nil {
    68  			return errwrap.Wrapf(fmt.Sprintf("Failure attaching AutoScaling Group %s with ALB Target Group: %s: {{err}}", asgName, v.(string)), err)
    69  		}
    70  	}
    71  
    72  	d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", asgName)))
    73  
    74  	return resourceAwsAutoscalingAttachmentRead(d, meta)
    75  }
    76  
    77  func resourceAwsAutoscalingAttachmentRead(d *schema.ResourceData, meta interface{}) error {
    78  	asgconn := meta.(*AWSClient).autoscalingconn
    79  	asgName := d.Get("autoscaling_group_name").(string)
    80  
    81  	// Retrieve the ASG properites to get list of associated ELBs
    82  	asg, err := getAwsAutoscalingGroup(asgName, asgconn)
    83  
    84  	if err != nil {
    85  		return err
    86  	}
    87  	if asg == nil {
    88  		log.Printf("[INFO] Autoscaling Group %q not found", asgName)
    89  		d.SetId("")
    90  		return nil
    91  	}
    92  
    93  	if v, ok := d.GetOk("elb"); ok {
    94  		found := false
    95  		for _, i := range asg.LoadBalancerNames {
    96  			if v.(string) == *i {
    97  				d.Set("elb", v.(string))
    98  				found = true
    99  				break
   100  			}
   101  		}
   102  
   103  		if !found {
   104  			log.Printf("[WARN] Association for %s was not found in ASG assocation", v.(string))
   105  			d.SetId("")
   106  		}
   107  	}
   108  
   109  	if v, ok := d.GetOk("alb_target_group_arn"); ok {
   110  		found := false
   111  		for _, i := range asg.TargetGroupARNs {
   112  			if v.(string) == *i {
   113  				d.Set("alb_target_group_arn", v.(string))
   114  				found = true
   115  				break
   116  			}
   117  		}
   118  
   119  		if !found {
   120  			log.Printf("[WARN] Association for %s was not found in ASG assocation", v.(string))
   121  			d.SetId("")
   122  		}
   123  	}
   124  
   125  	return nil
   126  }
   127  
   128  func resourceAwsAutoscalingAttachmentDelete(d *schema.ResourceData, meta interface{}) error {
   129  	asgconn := meta.(*AWSClient).autoscalingconn
   130  	asgName := d.Get("autoscaling_group_name").(string)
   131  
   132  	if v, ok := d.GetOk("elb"); ok {
   133  		detachOpts := &autoscaling.DetachLoadBalancersInput{
   134  			AutoScalingGroupName: aws.String(asgName),
   135  			LoadBalancerNames:    []*string{aws.String(v.(string))},
   136  		}
   137  
   138  		log.Printf("[INFO] Deleting ELB %s association from: %s", v.(string), asgName)
   139  		if _, err := asgconn.DetachLoadBalancers(detachOpts); err != nil {
   140  			return errwrap.Wrapf(fmt.Sprintf("Failure detaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, v.(string)), err)
   141  		}
   142  	}
   143  
   144  	if v, ok := d.GetOk("alb_target_group_arn"); ok {
   145  		detachOpts := &autoscaling.DetachLoadBalancerTargetGroupsInput{
   146  			AutoScalingGroupName: aws.String(asgName),
   147  			TargetGroupARNs:      []*string{aws.String(v.(string))},
   148  		}
   149  
   150  		log.Printf("[INFO] Deleting ALB Target Group %s association from: %s", v.(string), asgName)
   151  		if _, err := asgconn.DetachLoadBalancerTargetGroups(detachOpts); err != nil {
   152  			return errwrap.Wrapf(fmt.Sprintf("Failure detaching AutoScaling Group %s with ALB Target Group: %s: {{err}}", asgName, v.(string)), err)
   153  		}
   154  	}
   155  
   156  	return nil
   157  }