@@ -2,11 +2,14 @@ package services
22
33import (
44 "context"
5+ "fmt"
6+
57 "github.com/aws/aws-application-networking-k8s/pkg/utils"
68 "github.com/aws/aws-sdk-go/aws"
79 "github.com/aws/aws-sdk-go/aws/session"
810 taggingapi "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
911 taggingapiiface "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
12+ "github.com/aws/aws-sdk-go/service/vpclattice"
1013)
1114
1215//go:generate mockgen -destination tagging_mocks.go -package services github.com/aws/aws-application-networking-k8s/pkg/aws/services Tagging
@@ -26,8 +29,6 @@ const (
2629type Tags = map [string ]* string
2730
2831type Tagging interface {
29- taggingapiiface.ResourceGroupsTaggingAPIAPI
30-
3132 // Receives a list of arns and returns arn-to-tags map.
3233 GetTagsForArns (ctx context.Context , arns []string ) (map [string ]Tags , error )
3334
@@ -39,6 +40,11 @@ type defaultTagging struct {
3940 taggingapiiface.ResourceGroupsTaggingAPIAPI
4041}
4142
43+ type latticeTagging struct {
44+ Lattice
45+ vpcId string
46+ }
47+
4248func (t * defaultTagging ) GetTagsForArns (ctx context.Context , arns []string ) (map [string ]Tags , error ) {
4349 chunks := utils .Chunks (utils .SliceMap (arns , aws .String ), maxArnsPerGetResourcesApi )
4450 result := make (map [string ]Tags )
@@ -80,6 +86,67 @@ func NewDefaultTagging(sess *session.Session, region string) *defaultTagging {
8086 return & defaultTagging {ResourceGroupsTaggingAPIAPI : api }
8187}
8288
89+ // Use VPC Lattice API instead of the Resource Groups Tagging API
90+ func NewLatticeTagging (sess * session.Session , acc string , region string , vpcId string ) * latticeTagging {
91+ api := NewDefaultLattice (sess , acc , region )
92+ return & latticeTagging {Lattice : api , vpcId : vpcId }
93+ }
94+
95+ func (t * latticeTagging ) GetTagsForArns (ctx context.Context , arns []string ) (map [string ]Tags , error ) {
96+ result := map [string ]Tags {}
97+
98+ for _ , arn := range arns {
99+ tags , err := t .ListTagsForResourceWithContext (ctx ,
100+ & vpclattice.ListTagsForResourceInput {ResourceArn : aws .String (arn )},
101+ )
102+ if err != nil {
103+ return nil , err
104+ }
105+ result [arn ] = tags .Tags
106+ }
107+ return result , nil
108+ }
109+
110+ func (t * latticeTagging ) FindResourcesByTags (ctx context.Context , resourceType ResourceType , tags Tags ) ([]string , error ) {
111+ if resourceType != ResourceTypeTargetGroup {
112+ return nil , fmt .Errorf ("unsupported resource type %q for FindResourcesByTags" , resourceType )
113+ }
114+
115+ tgs , err := t .ListTargetGroupsAsList (ctx , & vpclattice.ListTargetGroupsInput {
116+ VpcIdentifier : aws .String (t .vpcId ),
117+ })
118+ if err != nil {
119+ return nil , err
120+ }
121+
122+ arns := make ([]string , 0 , len (tgs ))
123+
124+ for _ , tg := range tgs {
125+ resp , err := t .ListTagsForResourceWithContext (ctx ,
126+ & vpclattice.ListTagsForResourceInput {ResourceArn : tg .Arn },
127+ )
128+ if err != nil {
129+ return nil , err
130+ }
131+
132+ if containsTags (resp .Tags , tags ) {
133+ arns = append (arns , aws .StringValue (tg .Arn ))
134+ }
135+ }
136+
137+ return arns , nil
138+ }
139+
140+ func containsTags (source , check Tags ) bool {
141+ for k , v := range check {
142+ sourceV , ok := source [k ]
143+ if ! ok || aws .StringValue (sourceV ) != aws .StringValue (v ) {
144+ return false
145+ }
146+ }
147+ return len (check ) != 0
148+ }
149+
83150func convertTags (tags []* taggingapi.Tag ) Tags {
84151 out := make (Tags )
85152 for _ , tag := range tags {
0 commit comments