Skip to content

Commit

Permalink
Fixed aws_cloudwatch_log_group table for the error key column is not …
Browse files Browse the repository at this point in the history
…globally unique Closes #1975 (#1976)
  • Loading branch information
ParthaI authored Jan 22, 2024
1 parent d7ba7cc commit 732ab6d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
2 changes: 1 addition & 1 deletion aws-test/tests/aws_cloudwatch_log_group/test-get-query.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
select name
from aws.aws_cloudwatch_log_group
where name = '{{ resourceName }}'
where name = '{{ resourceName }}' and region = '{{ output.region_name.value }}'
28 changes: 21 additions & 7 deletions aws/table_aws_cloudwatch_log_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@ func tableAwsCloudwatchLogGroup(_ context.Context) *plugin.Table {
Name: "aws_cloudwatch_log_group",
Description: "AWS CloudWatch Log Group",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("name"),
// Log groups with the same name can exist in different regions for an account, we should filter the results by region in such cases to resolve the error: "Error: get call returned 2 results - the key column is not globally unique".
KeyColumns: plugin.AllColumns([]string{"name", "region"}),
Hydrate: getCloudwatchLogGroup,
Tags: map[string]string{"service": "logs", "action": "DescribeLogGroups"},
},
List: &plugin.ListConfig{
Hydrate: listCloudwatchLogGroups,
Tags: map[string]string{"service": "logs", "action": "DescribeLogGroups"},
KeyColumns: plugin.KeyColumnSlice{
{
Name: "name",
Require: plugin.Optional,
},
},
},
HydrateConfig: []plugin.HydrateConfig{
{
Expand Down Expand Up @@ -127,6 +134,8 @@ func listCloudwatchLogGroups(ctx context.Context, d *plugin.QueryData, _ *plugin
return nil, err
}

name := d.EqualsQualString("name")

maxItems := int32(50)

// Reduce the basic request limit down if the user has only requested a small number
Expand All @@ -145,6 +154,10 @@ func listCloudwatchLogGroups(ctx context.Context, d *plugin.QueryData, _ *plugin
Limit: &maxItems,
}

if name != "" {
input.LogGroupNamePrefix = aws.String(name)
}

paginator := cloudwatchlogs.NewDescribeLogGroupsPaginator(svc, input, func(o *cloudwatchlogs.DescribeLogGroupsPaginatorOptions) {
o.Limit = maxItems
o.StopOnDuplicateToken = true
Expand Down Expand Up @@ -176,19 +189,20 @@ func listCloudwatchLogGroups(ctx context.Context, d *plugin.QueryData, _ *plugin
//// HYDRATE FUNCTIONS

func getCloudwatchLogGroup(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
name := d.EqualsQualString("name")

// check if name is empty
if strings.TrimSpace(name) == "" {
return nil, nil
}

// Get client
svc, err := CloudWatchLogsClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_cloudwatch_log_group.getCloudwatchLogGroup", "client_error", err)
return nil, err
}

// check if name is empty
name := d.EqualsQuals["name"].GetStringValue()
if strings.TrimSpace(name) == "" {
return nil, nil
}

params := &cloudwatchlogs.DescribeLogGroupsInput{
LogGroupNamePrefix: aws.String(name),
}
Expand Down

0 comments on commit 732ab6d

Please sign in to comment.