Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
feat: Add tags to sns fetching (#810)
Browse files Browse the repository at this point in the history
* feat: add tags to sns fetching

#803
This adds an extra api call to retrieve the tags of each SNS topic.

Co-authored-by: Ron <[email protected]>
Co-authored-by: Andrew Herrington <[email protected]>
  • Loading branch information
3 people authored May 1, 2022
1 parent f102bb2 commit 8e3e46b
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ crash.log
cloudquery
cloudquery.db
cloudquery.zip
# goenv
.go-version
/config.yml
dist/
vendor/
Expand Down
20 changes: 20 additions & 0 deletions client/mocks/mock_sns.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ type SnsClient interface {
ListTopics(ctx context.Context, params *sns.ListTopicsInput, optFns ...func(*sns.Options)) (*sns.ListTopicsOutput, error)
ListSubscriptions(ctx context.Context, params *sns.ListSubscriptionsInput, optFns ...func(*sns.Options)) (*sns.ListSubscriptionsOutput, error)
GetTopicAttributes(ctx context.Context, params *sns.GetTopicAttributesInput, optFns ...func(*sns.Options)) (*sns.GetTopicAttributesOutput, error)
ListTagsForResource(ctx context.Context, params *sns.ListTagsForResourceInput, optFns ...func(*sns.Options)) (*sns.ListTagsForResourceOutput, error)
}

//go:generate mockgen -package=mocks -destination=./mocks/mock_ecs.go . EcsClient
Expand Down
1 change: 1 addition & 0 deletions docs/tables/aws_sns_topics.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ AWS SNS topic
|content_based_deduplication|boolean|Enables content-based deduplication for FIFO topics.|
|kms_master_key_id|text|The ID of an AWS managed customer master key (CMK) for Amazon SNS or a custom CMK|
|arn|text|The topic's ARN.|
|tags|jsonb|Topic tags.|
1 change: 1 addition & 0 deletions resources/provider/migrations/postgres/28_v0.11.2.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE IF EXISTS "aws_sns_topics" DROP COLUMN IF EXISTS "tags";
1 change: 1 addition & 0 deletions resources/provider/migrations/postgres/28_v0.11.2.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE IF EXISTS "aws_sns_topics" ADD COLUMN IF NOT EXISTS "tags" jsonb;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE IF EXISTS "aws_sns_topics" DROP COLUMN IF EXISTS "tags";
1 change: 1 addition & 0 deletions resources/provider/migrations/timescale/28_v0.11.2.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE IF EXISTS "aws_sns_topics" ADD COLUMN IF NOT EXISTS "tags" jsonb;
22 changes: 22 additions & 0 deletions resources/services/sns/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ func SnsTopics() *schema.Table {
Type: schema.TypeString,
Resolver: schema.PathResolver("TopicArn"),
},
{
Name: "tags",
Description: "Topic tags.",
Type: schema.TypeJSON,
Resolver: resolveTopicTags,
},
},
}
}
Expand Down Expand Up @@ -183,3 +189,19 @@ func resolveTopicAttributes(ctx context.Context, meta schema.ClientMeta, resourc

return nil
}

func resolveTopicTags(ctx context.Context, meta schema.ClientMeta, resource *schema.Resource, col schema.Column) error {
topic := resource.Item.(types.Topic)
c := meta.(*client.Client)
svc := c.Services().SNS
tagParams := sns.ListTagsForResourceInput{
ResourceArn: topic.TopicArn,
}
tags, err := svc.ListTagsForResource(ctx, &tagParams, func(o *sns.Options) {
o.Region = c.Region
})
if err != nil {
return diag.WrapError(err)
}
return diag.WrapError(resource.Set(col.Name, client.TagsToMap(tags.Tags)))
}
9 changes: 9 additions & 0 deletions resources/services/sns/topics_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ import (
func buildSnsTopics(t *testing.T, ctrl *gomock.Controller) client.Services {
m := mocks.NewMockSnsClient(ctrl)
topic := types.Topic{}
tag := types.Tag{}
err := faker.FakeData(&topic)
if err != nil {
t.Fatal(err)
}
tagerr := faker.FakeData(&tag)
if tagerr != nil {
t.Fatal(tagerr)
}

m.EXPECT().ListTopics(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&sns.ListTopicsOutput{
Expand All @@ -39,6 +44,10 @@ func buildSnsTopics(t *testing.T, ctrl *gomock.Controller) client.Services {
"EffectiveDeliveryPolicy": `{"stuff": 3}`,
},
}, nil)
m.EXPECT().ListTagsForResource(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&sns.ListTagsForResourceOutput{
Tags: []types.Tag{tag},
}, nil)
return client.Services{
SNS: m,
}
Expand Down
4 changes: 4 additions & 0 deletions terraform/sns/modules/test/sns.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ module "sns" {
name_prefix = "${var.prefix}-sns-cq-provider"
display_name = "${var.prefix}-sns-cq-provider"
kms_master_key_id = aws_kms_key.sns_kms_key.id
tags = {
tag1 = "foo"
tag2 = "bar"
}
}

0 comments on commit 8e3e46b

Please sign in to comment.