github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_alb_target_group_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/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/elbv2" 10 "github.com/hashicorp/errwrap" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 ) 14 15 func resourceAwsAlbTargetGroupAttachment() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsAlbAttachmentCreate, 18 Read: resourceAwsAlbAttachmentRead, 19 Delete: resourceAwsAlbAttachmentDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "target_group_arn": { 23 Type: schema.TypeString, 24 ForceNew: true, 25 Required: true, 26 }, 27 28 "target_id": { 29 Type: schema.TypeString, 30 ForceNew: true, 31 Required: true, 32 }, 33 34 "port": { 35 Type: schema.TypeInt, 36 ForceNew: true, 37 Optional: true, 38 }, 39 }, 40 } 41 } 42 43 func resourceAwsAlbAttachmentCreate(d *schema.ResourceData, meta interface{}) error { 44 elbconn := meta.(*AWSClient).elbv2conn 45 46 target := &elbv2.TargetDescription{ 47 Id: aws.String(d.Get("target_id").(string)), 48 } 49 50 if v, ok := d.GetOk("port"); ok { 51 target.Port = aws.Int64(int64(v.(int))) 52 } 53 54 params := &elbv2.RegisterTargetsInput{ 55 TargetGroupArn: aws.String(d.Get("target_group_arn").(string)), 56 Targets: []*elbv2.TargetDescription{target}, 57 } 58 59 log.Printf("[INFO] Registering Target %s with Target Group %s", d.Get("target_id").(string), 60 d.Get("target_group_arn").(string)) 61 62 _, err := elbconn.RegisterTargets(params) 63 if err != nil { 64 return errwrap.Wrapf("Error registering targets with target group: {{err}}", err) 65 } 66 67 d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", d.Get("target_group_arn")))) 68 69 return nil 70 } 71 72 func resourceAwsAlbAttachmentDelete(d *schema.ResourceData, meta interface{}) error { 73 elbconn := meta.(*AWSClient).elbv2conn 74 75 target := &elbv2.TargetDescription{ 76 Id: aws.String(d.Get("target_id").(string)), 77 } 78 79 if v, ok := d.GetOk("port"); ok { 80 target.Port = aws.Int64(int64(v.(int))) 81 } 82 83 params := &elbv2.DeregisterTargetsInput{ 84 TargetGroupArn: aws.String(d.Get("target_group_arn").(string)), 85 Targets: []*elbv2.TargetDescription{target}, 86 } 87 88 _, err := elbconn.DeregisterTargets(params) 89 if err != nil && !isTargetGroupNotFound(err) { 90 return errwrap.Wrapf("Error deregistering Targets: {{err}}", err) 91 } 92 93 d.SetId("") 94 95 return nil 96 } 97 98 // resourceAwsAlbAttachmentRead requires all of the fields in order to describe the correct 99 // target, so there is no work to do beyond ensuring that the target and group still exist. 100 func resourceAwsAlbAttachmentRead(d *schema.ResourceData, meta interface{}) error { 101 elbconn := meta.(*AWSClient).elbv2conn 102 103 target := &elbv2.TargetDescription{ 104 Id: aws.String(d.Get("target_id").(string)), 105 } 106 107 if v, ok := d.GetOk("port"); ok { 108 target.Port = aws.Int64(int64(v.(int))) 109 } 110 111 resp, err := elbconn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{ 112 TargetGroupArn: aws.String(d.Get("target_group_arn").(string)), 113 Targets: []*elbv2.TargetDescription{target}, 114 }) 115 if err != nil { 116 if isTargetGroupNotFound(err) { 117 log.Printf("[WARN] Target group does not exist, removing target attachment %s", d.Id()) 118 d.SetId("") 119 return nil 120 } 121 if isInvalidTarget(err) { 122 log.Printf("[WARN] Target does not exist, removing target attachment %s", d.Id()) 123 d.SetId("") 124 return nil 125 } 126 return errwrap.Wrapf("Error reading Target Health: {{err}}", err) 127 } 128 129 if len(resp.TargetHealthDescriptions) != 1 { 130 log.Printf("[WARN] Target does not exist, removing target attachment %s", d.Id()) 131 d.SetId("") 132 return nil 133 } 134 135 return nil 136 } 137 138 func isInvalidTarget(err error) bool { 139 elberr, ok := err.(awserr.Error) 140 return ok && elberr.Code() == "InvalidTarget" 141 }