github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/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 Required: true, 38 }, 39 }, 40 } 41 } 42 43 func resourceAwsAlbAttachmentCreate(d *schema.ResourceData, meta interface{}) error { 44 elbconn := meta.(*AWSClient).elbv2conn 45 46 params := &elbv2.RegisterTargetsInput{ 47 TargetGroupArn: aws.String(d.Get("target_group_arn").(string)), 48 Targets: []*elbv2.TargetDescription{ 49 { 50 Id: aws.String(d.Get("target_id").(string)), 51 Port: aws.Int64(int64(d.Get("port").(int))), 52 }, 53 }, 54 } 55 56 log.Printf("[INFO] Registering Target %s (%d) with Target Group %s", d.Get("target_id").(string), 57 d.Get("port").(int), d.Get("target_group_arn").(string)) 58 59 _, err := elbconn.RegisterTargets(params) 60 if err != nil { 61 return errwrap.Wrapf("Error registering targets with target group: {{err}}", err) 62 } 63 64 d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", d.Get("target_group_arn")))) 65 66 return nil 67 } 68 69 func resourceAwsAlbAttachmentDelete(d *schema.ResourceData, meta interface{}) error { 70 elbconn := meta.(*AWSClient).elbv2conn 71 72 params := &elbv2.DeregisterTargetsInput{ 73 TargetGroupArn: aws.String(d.Get("target_group_arn").(string)), 74 Targets: []*elbv2.TargetDescription{ 75 { 76 Id: aws.String(d.Get("target_id").(string)), 77 Port: aws.Int64(int64(d.Get("port").(int))), 78 }, 79 }, 80 } 81 82 _, err := elbconn.DeregisterTargets(params) 83 if err != nil && !isTargetGroupNotFound(err) { 84 return errwrap.Wrapf("Error deregistering Targets: {{err}}", err) 85 } 86 87 d.SetId("") 88 89 return nil 90 } 91 92 // resourceAwsAlbAttachmentRead requires all of the fields in order to describe the correct 93 // target, so there is no work to do beyond ensuring that the target and group still exist. 94 func resourceAwsAlbAttachmentRead(d *schema.ResourceData, meta interface{}) error { 95 elbconn := meta.(*AWSClient).elbv2conn 96 resp, err := elbconn.DescribeTargetHealth(&elbv2.DescribeTargetHealthInput{ 97 TargetGroupArn: aws.String(d.Get("target_group_arn").(string)), 98 Targets: []*elbv2.TargetDescription{ 99 { 100 Id: aws.String(d.Get("target_id").(string)), 101 Port: aws.Int64(int64(d.Get("port").(int))), 102 }, 103 }, 104 }) 105 if err != nil { 106 if isTargetGroupNotFound(err) { 107 log.Printf("[WARN] Target group does not exist, removing target attachment %s", d.Id()) 108 d.SetId("") 109 return nil 110 } 111 if isInvalidTarget(err) { 112 log.Printf("[WARN] Target does not exist, removing target attachment %s", d.Id()) 113 d.SetId("") 114 return nil 115 } 116 return errwrap.Wrapf("Error reading Target Health: {{err}}", err) 117 } 118 119 if len(resp.TargetHealthDescriptions) != 1 { 120 log.Printf("[WARN] Target does not exist, removing target attachment %s", d.Id()) 121 d.SetId("") 122 return nil 123 } 124 125 return nil 126 } 127 128 func isInvalidTarget(err error) bool { 129 elberr, ok := err.(awserr.Error) 130 return ok && elberr.Code() == "InvalidTarget" 131 }