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