Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle tag errors in a uniform way #337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions sources/cloudfront/distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cloudfront

import (
"context"
"fmt"
"regexp"

"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -29,13 +28,17 @@ func distributionGetFunc(ctx context.Context, client CloudFrontClient, scope str
}
}

var tags map[string]string

// get tags
tagsOut, err := client.ListTagsForResource(ctx, &cloudfront.ListTagsForResourceInput{
Resource: d.ARN,
})

if err != nil {
return nil, fmt.Errorf("failed to get tags for distribution %v: %w", *d.ARN, err)
if err == nil {
tags = tagsToMap(tagsOut.Tags)
} else {
tags = sources.HandleTagsError(ctx, err)
}

attributes, err := sources.ToAttributesCase(d)
Expand All @@ -49,7 +52,7 @@ func distributionGetFunc(ctx context.Context, client CloudFrontClient, scope str
UniqueAttribute: "id",
Attributes: attributes,
Scope: scope,
Tags: tagsToMap(tagsOut.Tags),
Tags: tags,
}

if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion sources/cloudfront/streaming_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,19 @@ func streamingDistributionGetFunc(ctx context.Context, client CloudFrontClient,
}
}

var tags map[string]string

// Get the tags
tagsOut, err := client.ListTagsForResource(ctx, &cloudfront.ListTagsForResourceInput{
Resource: d.ARN,
})

if err == nil {
tags = tagsToMap(tagsOut.Tags)
} else {
tags = sources.HandleTagsError(ctx, err)
}

if err != nil {
return nil, fmt.Errorf("failed to get tags for streaming distribution %v: %w", *d.Id, err)
}
Expand All @@ -46,7 +54,7 @@ func streamingDistributionGetFunc(ctx context.Context, client CloudFrontClient,
UniqueAttribute: "id",
Attributes: attributes,
Scope: scope,
Tags: tagsToMap(tagsOut.Tags),
Tags: tags,
}

if d.Status != nil {
Expand Down
10 changes: 9 additions & 1 deletion sources/cloudwatch/alarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,19 @@ func alarmOutputMapper(ctx context.Context, client CloudwatchClient, scope strin
return nil, err
}

var tags map[string]string

// Get the tags
tagsOut, err := client.ListTagsForResource(ctx, &cloudwatch.ListTagsForResourceInput{
ResourceARN: arn,
})

if err == nil {
tags = tagsToMap(tagsOut.Tags)
} else {
tags = sources.HandleTagsError(ctx, err)
}

if err != nil {
return nil, err
}
Expand All @@ -100,7 +108,7 @@ func alarmOutputMapper(ctx context.Context, client CloudwatchClient, scope strin
UniqueAttribute: "alarmName",
Scope: scope,
Attributes: attrs,
Tags: tagsToMap(tagsOut.Tags),
Tags: tags,
}

// Combine all actions so that we can link the targeted item
Expand Down
4 changes: 2 additions & 2 deletions sources/dynamodb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dynamodb
import (
"context"
"errors"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
Expand Down Expand Up @@ -35,7 +34,8 @@ func tableGetFunc(ctx context.Context, client Client, scope string, input *dynam
})

if err != nil {
return nil, fmt.Errorf("failed to get tags for table: %w", err)
tagsMap = sources.HandleTagsError(ctx, err)
break
}

// Add tags to map
Expand Down
17 changes: 10 additions & 7 deletions sources/elb/elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ func loadBalancerOutputMapper(ctx context.Context, client elbClient, scope strin
tagsOut, err := client.DescribeTags(ctx, &elb.DescribeTagsInput{
LoadBalancerNames: loadBalancerNames,
})
if err != nil {
return nil, err
}

for _, tagDesc := range tagsOut.TagDescriptions {
if tagDesc.LoadBalancerName != nil {
tagsMap[*tagDesc.LoadBalancerName] = tagDesc.Tags
if err == nil {
for _, tagDesc := range tagsOut.TagDescriptions {
if tagDesc.LoadBalancerName != nil {
tagsMap[*tagDesc.LoadBalancerName] = tagDesc.Tags
}
}
}
}
Expand All @@ -65,7 +64,11 @@ func loadBalancerOutputMapper(ctx context.Context, client elbClient, scope strin
var tags map[string]string

if desc.LoadBalancerName != nil {
tags = tagsToMap(tagsMap[*desc.LoadBalancerName])
m, ok := tagsMap[*desc.LoadBalancerName]

if ok {
tags = tagsToMap(m)
}
}

item := sdp.Item{
Expand Down
6 changes: 1 addition & 5 deletions sources/elbv2/elb.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ func loadBalancerOutputMapper(ctx context.Context, client elbClient, scope strin
}
}

tagsMap, err := getTagsMap(ctx, client, arns)

if err != nil {
return nil, err
}
tagsMap := getTagsMap(ctx, client, arns)

for _, lb := range output.LoadBalancers {
attrs, err := sources.ToAttributesCase(lb)
Expand Down
6 changes: 1 addition & 5 deletions sources/elbv2/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ func listenerOutputMapper(ctx context.Context, client elbClient, scope string, _
}
}

tagsMap, err := getTagsMap(ctx, client, arns)

if err != nil {
return nil, err
}
tagsMap := getTagsMap(ctx, client, arns)

for _, listener := range output.Listeners {
// Redact the client secret and replace with the first 12 characters of
Expand Down
6 changes: 1 addition & 5 deletions sources/elbv2/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ func ruleOutputMapper(ctx context.Context, client elbClient, scope string, _ *el
}
}

tagsMap, err := getTagsMap(ctx, client, ruleArns)

if err != nil {
return nil, err
}
tagsMap := getTagsMap(ctx, client, ruleArns)

for _, rule := range output.Rules {
attrs, err := sources.ToAttributesCase(rule)
Expand Down
14 changes: 11 additions & 3 deletions sources/elbv2/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

elbv2 "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2"
"github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
"github.com/overmindtech/aws-source/sources"
)

type elbClient interface {
Expand All @@ -28,15 +29,22 @@ func tagsToMap(tags []types.Tag) map[string]string {
}

// Gets a map of ARN to tags (in map[string]string format) for the given ARNs
func getTagsMap(ctx context.Context, client elbClient, arns []string) (map[string]map[string]string, error) {
func getTagsMap(ctx context.Context, client elbClient, arns []string) map[string]map[string]string {
tagsMap := make(map[string]map[string]string)

if len(arns) > 0 {
tagsOut, err := client.DescribeTags(ctx, &elbv2.DescribeTagsInput{
ResourceArns: arns,
})
if err != nil {
return nil, err
tags := sources.HandleTagsError(ctx, err)

// Set these tags for all ARNs
for _, arn := range arns {
tagsMap[arn] = tags
}

return tagsMap
}

for _, tagDescription := range tagsOut.TagDescriptions {
Expand All @@ -46,5 +54,5 @@ func getTagsMap(ctx context.Context, client elbClient, arns []string) (map[strin
}
}

return tagsMap, nil
return tagsMap
}
6 changes: 1 addition & 5 deletions sources/elbv2/target_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ func targetGroupOutputMapper(ctx context.Context, client elbClient, scope string
}
}

tagsMap, err := getTagsMap(ctx, client, tgArns)

if err != nil {
return nil, err
}
tagsMap := getTagsMap(ctx, client, tgArns)

for _, tg := range output.TargetGroups {
attrs, err := sources.ToAttributesCase(tg)
Expand Down
3 changes: 1 addition & 2 deletions sources/get_list_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ func (s *GetListSource[AWSItem, ClientStruct, Options]) Get(ctx context.Context,
if s.ListTagsFunc != nil {
item.Tags, err = s.ListTagsFunc(ctx, awsItem, s.Client)
if err != nil {
s.cache.StoreError(err, s.CacheDuration, ck)
return nil, WrapAWSError(err)
item.Tags = HandleTagsError(ctx, err)
}
}

Expand Down
8 changes: 4 additions & 4 deletions sources/iam/instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func instanceProfileItemMapper(scope string, awsItem *types.InstanceProfile) (*s
return &item, nil
}

func instanceProfileListTagsFunc(ctx context.Context, ip *types.InstanceProfile, client *iam.Client) (map[string]string, error) {
func instanceProfileListTagsFunc(ctx context.Context, ip *types.InstanceProfile, client *iam.Client) map[string]string {
tags := make(map[string]string)

paginator := iam.NewListInstanceProfileTagsPaginator(client, &iam.ListInstanceProfileTagsInput{
Expand All @@ -108,7 +108,7 @@ func instanceProfileListTagsFunc(ctx context.Context, ip *types.InstanceProfile,
out, err := paginator.NextPage(ctx)

if err != nil {
return nil, err
return sources.HandleTagsError(ctx, err)
}

for _, tag := range out.Tags {
Expand All @@ -118,7 +118,7 @@ func instanceProfileListTagsFunc(ctx context.Context, ip *types.InstanceProfile,
}
}

return tags, nil
return tags
}

//go:generate docgen ../../docs-data
Expand Down Expand Up @@ -147,7 +147,7 @@ func NewInstanceProfileSource(config aws.Config, accountID string, region string
},
ListTagsFunc: func(ctx context.Context, ip *types.InstanceProfile, c *iam.Client) (map[string]string, error) {
limit.Wait(ctx) // Wait for rate limiting
return instanceProfileListTagsFunc(ctx, ip, c)
return instanceProfileListTagsFunc(ctx, ip, c), nil
},
ItemMapper: instanceProfileItemMapper,
}
Expand Down
2 changes: 1 addition & 1 deletion sources/iam/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func policyListTagsFunc(ctx context.Context, p *PolicyDetails, client IAMClient)
out, err := paginator.NextPage(ctx)

if err != nil {
return nil, err
return sources.HandleTagsError(ctx, err), nil
}

for _, tag := range out.Tags {
Expand Down
2 changes: 1 addition & 1 deletion sources/iam/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ func roleListTagsFunc(ctx context.Context, r *RoleDetails, client IAMClient) (ma
out, err := paginator.NextPage(ctx)

if err != nil {
return nil, err
return sources.HandleTagsError(ctx, err), nil
}

for _, tag := range out.Tags {
Expand Down
2 changes: 1 addition & 1 deletion sources/iam/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func userListTagsFunc(ctx context.Context, u *UserDetails, client IAMClient) (ma
out, err := paginator.NextPage(ctx)

if err != nil {
return nil, err
return sources.HandleTagsError(ctx, err), nil
}

for _, tag := range out.Tags {
Expand Down
10 changes: 7 additions & 3 deletions sources/rds/db_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ func dBClusterOutputMapper(ctx context.Context, client rdsClient, scope string,
items := make([]*sdp.Item, 0)

for _, cluster := range output.DBClusters {
var tags map[string]string

// Get tags for the cluster
tagsOut, err := client.ListTagsForResource(ctx, &rds.ListTagsForResourceInput{
ResourceName: cluster.DBClusterArn,
})

if err != nil {
return nil, err
if err == nil {
tags = tagsToMap(tagsOut.TagList)
} else {
tags = sources.HandleTagsError(ctx, err)
}

attributes, err := sources.ToAttributesCase(cluster)
Expand All @@ -33,7 +37,7 @@ func dBClusterOutputMapper(ctx context.Context, client rdsClient, scope string,
UniqueAttribute: "dBClusterIdentifier",
Attributes: attributes,
Scope: scope,
Tags: tagsToMap(tagsOut.TagList),
Tags: tags,
}

var a *sources.ARN
Expand Down
10 changes: 7 additions & 3 deletions sources/rds/db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ func dBInstanceOutputMapper(ctx context.Context, client rdsClient, scope string,
items := make([]*sdp.Item, 0)

for _, instance := range output.DBInstances {
var tags map[string]string

// Get the tags for the instance
tagsOut, err := client.ListTagsForResource(ctx, &rds.ListTagsForResourceInput{
ResourceName: instance.DBInstanceArn,
})

if err != nil {
return nil, err
if err == nil {
tags = tagsToMap(tagsOut.TagList)
} else {
tags = sources.HandleTagsError(ctx, err)
}

var dbSubnetGroup *string
Expand All @@ -106,7 +110,7 @@ func dBInstanceOutputMapper(ctx context.Context, client rdsClient, scope string,
UniqueAttribute: "dBInstanceIdentifier",
Attributes: attributes,
Scope: scope,
Tags: tagsToMap(tagsOut.TagList),
Tags: tags,
}

if instance.DBInstanceStatus != nil {
Expand Down
10 changes: 7 additions & 3 deletions sources/rds/db_subnet_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ func dBSubnetGroupOutputMapper(ctx context.Context, client rdsClient, scope stri
items := make([]*sdp.Item, 0)

for _, sg := range output.DBSubnetGroups {
var tags map[string]string

// Get tags
tagsOut, err := client.ListTagsForResource(ctx, &rds.ListTagsForResourceInput{
ResourceName: sg.DBSubnetGroupArn,
})

if err != nil {
return nil, err
if err == nil {
tags = tagsToMap(tagsOut.TagList)
} else {
tags = sources.HandleTagsError(ctx, err)
}

attributes, err := sources.ToAttributesCase(sg)
Expand All @@ -33,7 +37,7 @@ func dBSubnetGroupOutputMapper(ctx context.Context, client rdsClient, scope stri
UniqueAttribute: "dBSubnetGroupName",
Attributes: attributes,
Scope: scope,
Tags: tagsToMap(tagsOut.TagList),
Tags: tags,
}

var a *sources.ARN
Expand Down
Loading
Loading