/
aws_autoscaling_group.go
94 lines (77 loc) · 2.82 KB
/
aws_autoscaling_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
package mapper
import (
"context"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/retry"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
goCache "github.com/patrickmn/go-cache"
"github.com/terakoya76/modd/datadog"
)
const awsAutoScalingGroupCacheKey string = string(datadog.AwsAutoScalingGroup)
// AwsAutoScalingGroupClient is abstract interface of *autoscaling.Client.
type AwsAutoScalingGroupClient interface {
DescribeAutoScalingGroups(
ctx context.Context,
params *autoscaling.DescribeAutoScalingGroupsInput,
optFns ...func(*autoscaling.Options),
) (*autoscaling.DescribeAutoScalingGroupsOutput, error)
}
// AwsAutoScalingGroupTagsMapper implements TagsMapper for AWS AutoScalingGroup.
type AwsAutoScalingGroupTagsMapper struct {
cache *goCache.Cache
client AwsAutoScalingGroupClient
}
// BuildAwsAutoScalingGroupTagsMapper builds AwsAutoScalingGroupTagsMapper from args.
func BuildAwsAutoScalingGroupTagsMapper(cache *goCache.Cache, client AwsAutoScalingGroupClient) AwsAutoScalingGroupTagsMapper {
return AwsAutoScalingGroupTagsMapper{
cache: cache,
client: client,
}
}
// GetAwsAutoScalingGroupClient returns AWS AutoScalingGroup client.
func GetAwsAutoScalingGroupClient(ctx context.Context) (*autoscaling.Client, error) {
cfg, err := config.LoadDefaultConfig(ctx, config.WithRetryer(func() aws.Retryer {
return retry.AddWithMaxAttempts(retry.NewStandard(), 10)
}))
if err != nil {
return nil, fmt.Errorf("%w", err)
}
return autoscaling.NewFromConfig(cfg), nil
}
// GetTagsMapping returns the latest tags mapping.
func (tm AwsAutoScalingGroupTagsMapper) GetTagsMapping(ctx context.Context) (map[string]Tags, error) {
if cv, found := tm.cache.Get(awsAutoScalingGroupCacheKey); found {
mapping := cv.(map[string]Tags)
return mapping, nil
}
mapping := make(map[string]Tags)
// https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/autoscaling#DescribeAutoScalingGroupsInput
token := aws.String("")
for token != nil {
// NextToken could not be empty string
var input autoscaling.DescribeAutoScalingGroupsInput
if *token == "" {
input = autoscaling.DescribeAutoScalingGroupsInput{}
} else {
input = autoscaling.DescribeAutoScalingGroupsInput{NextToken: token}
}
output, err := tm.client.DescribeAutoScalingGroups(ctx, &input)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
for i := 0; i < len(output.AutoScalingGroups); i++ {
asg := output.AutoScalingGroups[i]
tags := make(Tags, len(asg.Tags))
for j, tag := range asg.Tags {
tags[j] = fmt.Sprintf("%s:%s", strings.ToLower(*tag.Key), strings.ToLower(*tag.Value))
}
mapping[*asg.AutoScalingGroupName] = tags
}
token = output.NextToken
}
tm.cache.Set(awsAutoScalingGroupCacheKey, mapping, goCache.DefaultExpiration)
return mapping, nil
}