-
Notifications
You must be signed in to change notification settings - Fork 82
/
rgt.go
78 lines (66 loc) · 2.37 KB
/
rgt.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
package cloud
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
)
const (
ResourceTypeELBLoadBalancer = "elasticloadbalancing:loadbalancer"
ResourceTypeELBTargetGroup = "elasticloadbalancing:targetgroup"
ResourceTypeEC2SecurityGroup = "ec2:security-group"
ResourceTypeEC2Subnet = "ec2:subnet"
)
type RGT interface {
resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI
GetResourcesAsList(ctx context.Context, input *resourcegroupstaggingapi.GetResourcesInput) ([]*resourcegroupstaggingapi.ResourceTagMapping, error)
}
func NewRGT(session *session.Session) RGT {
return &defaultRGT{
resourcegroupstaggingapi.New(session),
}
}
var _ RGT = (*defaultRGT)(nil)
type defaultRGT struct {
resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI
}
func (c *defaultRGT) GetResourcesAsList(ctx context.Context, input *resourcegroupstaggingapi.GetResourcesInput) ([]*resourcegroupstaggingapi.ResourceTagMapping, error) {
var result []*resourcegroupstaggingapi.ResourceTagMapping
if err := c.GetResourcesPagesWithContext(ctx, input, func(output *resourcegroupstaggingapi.GetResourcesOutput, _ bool) bool {
for _, i := range output.ResourceTagMappingList {
result = append(result, i)
}
return true
}); err != nil {
return nil, err
}
return result, nil
}
func NewRGTTagFilters(tags map[string]string) []*resourcegroupstaggingapi.TagFilter {
tagFilters := make([]*resourcegroupstaggingapi.TagFilter, 0, len(tags))
for k, v := range tags {
tagFilters = append(tagFilters, &resourcegroupstaggingapi.TagFilter{
Key: aws.String(k),
Values: aws.StringSlice([]string{v}),
})
}
return tagFilters
}
func NewRGTTagFiltersV2(tags map[string][]string) []*resourcegroupstaggingapi.TagFilter {
tagFilters := make([]*resourcegroupstaggingapi.TagFilter, 0, len(tags))
for k, v := range tags {
tagFilters = append(tagFilters, &resourcegroupstaggingapi.TagFilter{
Key: aws.String(k),
Values: aws.StringSlice(v),
})
}
return tagFilters
}
func ParseRGTTags(tags []*resourcegroupstaggingapi.Tag) map[string]string {
result := make(map[string]string, len(tags))
for _, tag := range tags {
result[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}
return result
}