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

feat: Add CloudWatch Logs Log Group Resource #1317

Merged
merged 9 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
40 changes: 40 additions & 0 deletions client/mocks/mock_cloudwatchlogs.go

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

2 changes: 2 additions & 0 deletions client/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ type CloudwatchClient interface {
//go:generate mockgen -package=mocks -destination=./mocks/mock_cloudwatchlogs.go . CloudwatchLogsClient
type CloudwatchLogsClient interface {
DescribeMetricFilters(ctx context.Context, params *cloudwatchlogs.DescribeMetricFiltersInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeMetricFiltersOutput, error)
DescribeLogGroups(ctx context.Context, params *cloudwatchlogs.DescribeLogGroupsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogGroupsOutput, error)
DescribeLogStreams(ctx context.Context, params *cloudwatchlogs.DescribeLogStreamsInput, optFns ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.DescribeLogStreamsOutput, error)
deep-spaced marked this conversation as resolved.
Show resolved Hide resolved
}

//go:generate mockgen -package=mocks -destination=./mocks/mock_cloudformation.go . CloudFormationClient
Expand Down
15 changes: 15 additions & 0 deletions docs/tables/aws_cloudwatchlogs_log_groups.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

# Table: aws_cloudwatchlogs_log_groups
Represents a log group.
## Columns
| Name | Type | Description |
| ------------- | ------------- | ----- |
|account_id|text|The AWS Account ID of the resource.|
|region|text|The AWS Region of the resource.|
|arn|text|The Amazon Resource Name (ARN) of the log group.|
|creation_time|bigint|The creation time of the log group, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC.|
|kms_key_id|text|The Amazon Resource Name (ARN) of the CMK to use when encrypting log data.|
|log_group_name|text|The name of the log group.|
|metric_filter_count|bigint|The number of metric filters.|
|retention_in_days|bigint|The number of days to retain the log events in the specified log group|
|stored_bytes|bigint|The number of bytes stored.|
1 change: 1 addition & 0 deletions resources/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func Provider() *provider.Provider {
"cloudtrail.trails": cloudtrail.CloudtrailTrails(),
"cloudwatch.alarms": cloudwatch.CloudwatchAlarms(),
"cloudwatchlogs.filters": cloudwatchlogs.CloudwatchlogsFilters(),
"cloudwatchlogs.log_groups": cloudwatchlogs.LogGroups(),
"codebuild.projects": codebuild.CodebuildProjects(),
"codepipeline.pipelines": codepipeline.Pipelines(),
"codepipeline.webhooks": codepipeline.Webhooks(),
Expand Down
41 changes: 41 additions & 0 deletions resources/services/cloudwatchlogs/gen.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
resource "aws" "cloudwatchlogs" "log_groups" {
path = "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types.LogGroup"
ignoreError "IgnoreAccessDenied" {
path = "github.com/cloudquery/cq-provider-aws/client.IgnoreAccessDeniedServiceDisabled"
}
deleteFilter "AccountRegionFilter" {
path = "github.com/cloudquery/cq-provider-aws/client.DeleteAccountRegionFilter"
}
multiplex "AwsAccountRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ServiceAccountRegionMultiplexer"
params = ["logs"]
}

userDefinedColumn "account_id" {
description = "The AWS Account ID of the resource."
type = "string"
resolver "resolveAWSAccount" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSAccount"
}
}

userDefinedColumn "region" {
type = "string"
description = "The AWS Region of the resource."
resolver "resolveAWSRegion" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveAWSRegion"
}
}
ignore_columns_in_tests = ["kms_key_id","retention_in_days"]

options {
primary_keys = ["arn"]
}

column "tags" {
type = "json"
resolver "resolveTags" {
path = "github.com/cloudquery/cq-provider-aws/client.ResolveTags"
}
}
deep-spaced marked this conversation as resolved.
Show resolved Hide resolved
}
98 changes: 98 additions & 0 deletions resources/services/cloudwatchlogs/log_groups.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package cloudwatchlogs

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-sdk/provider/diag"
"github.com/cloudquery/cq-provider-sdk/provider/schema"
)

func LogGroups() *schema.Table {
deep-spaced marked this conversation as resolved.
Show resolved Hide resolved
return &schema.Table{
Name: "aws_cloudwatchlogs_log_groups",
Description: "Represents a log group.",
Resolver: fetchCloudwatchlogsLogGroups,
Multiplex: client.ServiceAccountRegionMultiplexer("logs"),
IgnoreError: client.IgnoreAccessDeniedServiceDisabled,
DeleteFilter: client.DeleteAccountRegionFilter,
Options: schema.TableCreationOptions{PrimaryKeys: []string{"arn"}},
Columns: []schema.Column{
{
Name: "account_id",
Description: "The AWS Account ID of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSAccount,
},
{
Name: "region",
Description: "The AWS Region of the resource.",
Type: schema.TypeString,
Resolver: client.ResolveAWSRegion,
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the log group.",
Type: schema.TypeString,
},
{
Name: "creation_time",
Description: "The creation time of the log group, expressed as the number of milliseconds after Jan 1, 1970 00:00:00 UTC.",
Type: schema.TypeBigInt,
},
{
Name: "kms_key_id",
Description: "The Amazon Resource Name (ARN) of the CMK to use when encrypting log data.",
Type: schema.TypeString,
IgnoreInTests: true,
},
{
Name: "log_group_name",
Description: "The name of the log group.",
Type: schema.TypeString,
},
{
Name: "metric_filter_count",
Description: "The number of metric filters.",
Type: schema.TypeBigInt,
},
{
Name: "retention_in_days",
Description: "The number of days to retain the log events in the specified log group",
Type: schema.TypeBigInt,
IgnoreInTests: true,
},
{
Name: "stored_bytes",
Description: "The number of bytes stored.",
Type: schema.TypeBigInt,
},
},
}
}

// ====================================================================================================================
// Table Resolver Functions
// ====================================================================================================================

func fetchCloudwatchlogsLogGroups(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- interface{}) error {
var config cloudwatchlogs.DescribeLogGroupsInput
c := meta.(*client.Client)
svc := c.Services().CloudwatchLogs
for {
response, err := svc.DescribeLogGroups(ctx, &config, func(options *cloudwatchlogs.Options) {
options.Region = c.Region
})
if err != nil {
return diag.WrapError(err)
}
res <- response.LogGroups
if aws.ToString(response.NextToken) == "" {
break
}
config.NextToken = response.NextToken
}
return nil
}
32 changes: 32 additions & 0 deletions resources/services/cloudwatchlogs/log_groups_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package cloudwatchlogs

import (
"testing"

"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
"github.com/bxcodec/faker"
"github.com/cloudquery/cq-provider-aws/client"
"github.com/cloudquery/cq-provider-aws/client/mocks"
"github.com/golang/mock/gomock"
)

func buildCloudwatchLogsLogGroupsMock(t *testing.T, ctrl *gomock.Controller) client.Services {
m := mocks.NewMockCloudwatchLogsClient(ctrl)
l := types.LogGroup{}
err := faker.FakeData(&l)
if err != nil {
t.Fatal(err)
}
m.EXPECT().DescribeLogGroups(gomock.Any(), gomock.Any(), gomock.Any()).Return(
&cloudwatchlogs.DescribeLogGroupsOutput{
LogGroups: []types.LogGroup{l},
}, nil)
return client.Services{
deep-spaced marked this conversation as resolved.
Show resolved Hide resolved
CloudwatchLogs: m,
}
}

func TestCloudwatchlogsLogGroups(t *testing.T) {
deep-spaced marked this conversation as resolved.
Show resolved Hide resolved
client.AwsMockTestHelper(t, LogGroups(), buildCloudwatchLogsLogGroupsMock, client.TestOptions{})
}