This repository has been archived by the owner on May 6, 2024. It is now read-only.
/
target_group.go
157 lines (123 loc) · 3.81 KB
/
target_group.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package elbv2
import (
"github.com/aws/aws-sdk-go/aws"
awselbv2 "github.com/aws/aws-sdk-go/service/elbv2"
"github.com/jpignata/fargate/console"
)
type CreateTargetGroupInput struct {
Name string
Protocol string
Port int64
VpcId string
}
type TargetGroup struct {
Name string
Arn string
LoadBalancerArn string
}
func (elbv2 *ELBV2) CreateTargetGroup(input *CreateTargetGroupInput) string {
console.Debug("Creating ELB target group")
resp, err := elbv2.svc.CreateTargetGroup(
&awselbv2.CreateTargetGroupInput{
Name: aws.String(input.Name),
Port: aws.Int64(input.Port),
Protocol: aws.String(input.Protocol),
TargetType: aws.String(awselbv2.TargetTypeEnumIp),
VpcId: aws.String(input.VpcId),
},
)
if err != nil || len(resp.TargetGroups) != 1 {
console.ErrorExit(err, "Could not create ELB target group")
}
targetGroupArn := aws.StringValue(resp.TargetGroups[0].TargetGroupArn)
console.Debug("Created ELB target group [%s]", input.Name)
return targetGroupArn
}
func (elbv2 *ELBV2) DeleteTargetGroup(targetGroupName string) {
console.Debug("Deleting ELB target group")
targetGroup := elbv2.describeTargetGroupByName(targetGroupName)
elbv2.svc.DeleteTargetGroup(
&awselbv2.DeleteTargetGroupInput{
TargetGroupArn: targetGroup.TargetGroupArn,
},
)
}
func (elbv2 *ELBV2) DeleteTargetGroupByArn(targetGroupArn string) {
_, err := elbv2.svc.DeleteTargetGroup(
&awselbv2.DeleteTargetGroupInput{
TargetGroupArn: aws.String(targetGroupArn),
},
)
if err != nil {
console.ErrorExit(err, "Could not delete ELB target group")
}
}
func (elbv2 *ELBV2) GetTargetGroupArn(targetGroupName string) string {
resp, _ := elbv2.svc.DescribeTargetGroups(
&awselbv2.DescribeTargetGroupsInput{
Names: aws.StringSlice([]string{targetGroupName}),
},
)
if len(resp.TargetGroups) == 1 {
return aws.StringValue(resp.TargetGroups[0].TargetGroupArn)
}
return ""
}
func (elbv2 *ELBV2) GetTargetGroupLoadBalancerArn(targetGroupArn string) string {
targetGroup := elbv2.describeTargetGroupByArn(targetGroupArn)
if len(targetGroup.LoadBalancerArns) > 0 {
return aws.StringValue(targetGroup.LoadBalancerArns[0])
} else {
return ""
}
}
func (elbv2 *ELBV2) DescribeTargetGroups(targetGroupArns []string) []TargetGroup {
var targetGroups []TargetGroup
resp, err := elbv2.svc.DescribeTargetGroups(
&awselbv2.DescribeTargetGroupsInput{
TargetGroupArns: aws.StringSlice(targetGroupArns),
},
)
if err != nil {
console.ErrorExit(err, "Could not describe ELB target groups")
}
for _, targetGroup := range resp.TargetGroups {
tg := TargetGroup{
Name: aws.StringValue(targetGroup.TargetGroupName),
Arn: aws.StringValue(targetGroup.TargetGroupArn),
}
if len(targetGroup.LoadBalancerArns) > 0 {
tg.LoadBalancerArn = aws.StringValue(targetGroup.LoadBalancerArns[0])
}
targetGroups = append(targetGroups, tg)
}
return targetGroups
}
func (elbv2 *ELBV2) describeTargetGroupByName(targetGroupName string) *awselbv2.TargetGroup {
resp, err := elbv2.svc.DescribeTargetGroups(
&awselbv2.DescribeTargetGroupsInput{
Names: aws.StringSlice([]string{targetGroupName}),
},
)
if err != nil {
console.ErrorExit(err, "Could not describe ELB target groups")
}
if len(resp.TargetGroups) != 1 {
console.IssueExit("Could not describe ELB target groups")
}
return resp.TargetGroups[0]
}
func (elbv2 *ELBV2) describeTargetGroupByArn(targetGroupArn string) *awselbv2.TargetGroup {
resp, err := elbv2.svc.DescribeTargetGroups(
&awselbv2.DescribeTargetGroupsInput{
TargetGroupArns: aws.StringSlice([]string{targetGroupArn}),
},
)
if err != nil {
console.ErrorExit(err, "Could not describe ELB target groups")
}
if len(resp.TargetGroups) != 1 {
console.IssueExit("Could not describe ELB target groups")
}
return resp.TargetGroups[0]
}