github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/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": &schema.Schema{ 22 Type: schema.TypeString, 23 ForceNew: true, 24 Required: true, 25 }, 26 27 "elb": &schema.Schema{ 28 Type: schema.TypeString, 29 ForceNew: true, 30 Required: true, 31 }, 32 }, 33 } 34 } 35 36 func resourceAwsAutoscalingAttachmentCreate(d *schema.ResourceData, meta interface{}) error { 37 asgconn := meta.(*AWSClient).autoscalingconn 38 asgName := d.Get("autoscaling_group_name").(string) 39 elbName := d.Get("elb").(string) 40 41 attachElbInput := &autoscaling.AttachLoadBalancersInput{ 42 AutoScalingGroupName: aws.String(asgName), 43 LoadBalancerNames: []*string{aws.String(elbName)}, 44 } 45 46 log.Printf("[INFO] registering asg %s with ELBs %s", asgName, elbName) 47 48 if _, err := asgconn.AttachLoadBalancers(attachElbInput); err != nil { 49 return errwrap.Wrapf(fmt.Sprintf("Failure attaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, elbName), err) 50 } 51 52 d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", asgName))) 53 54 return resourceAwsAutoscalingAttachmentRead(d, meta) 55 } 56 57 func resourceAwsAutoscalingAttachmentRead(d *schema.ResourceData, meta interface{}) error { 58 asgconn := meta.(*AWSClient).autoscalingconn 59 asgName := d.Get("autoscaling_group_name").(string) 60 elbName := d.Get("elb").(string) 61 62 // Retrieve the ASG properites to get list of associated ELBs 63 asg, err := getAwsAutoscalingGroup(asgName, asgconn) 64 65 if err != nil { 66 return err 67 } 68 if asg == nil { 69 log.Printf("[INFO] Autoscaling Group %q not found", asgName) 70 d.SetId("") 71 return nil 72 } 73 74 found := false 75 for _, i := range asg.LoadBalancerNames { 76 if elbName == *i { 77 d.Set("elb", elbName) 78 found = true 79 break 80 } 81 } 82 83 if !found { 84 log.Printf("[WARN] Association for %s was not found in ASG assocation", elbName) 85 d.SetId("") 86 } 87 88 return nil 89 } 90 91 func resourceAwsAutoscalingAttachmentDelete(d *schema.ResourceData, meta interface{}) error { 92 asgconn := meta.(*AWSClient).autoscalingconn 93 asgName := d.Get("autoscaling_group_name").(string) 94 elbName := d.Get("elb").(string) 95 96 log.Printf("[INFO] Deleting ELB %s association from: %s", elbName, asgName) 97 98 detachOpts := &autoscaling.DetachLoadBalancersInput{ 99 AutoScalingGroupName: aws.String(asgName), 100 LoadBalancerNames: []*string{aws.String(elbName)}, 101 } 102 103 if _, err := asgconn.DetachLoadBalancers(detachOpts); err != nil { 104 return errwrap.Wrapf(fmt.Sprintf("Failure detaching AutoScaling Group %s with Elastic Load Balancer: %s: {{err}}", asgName, elbName), err) 105 } 106 107 return nil 108 }