Skip to content

Commit

Permalink
Add aws_codestar_notificaton_rule table (#2198) (#2217)
Browse files Browse the repository at this point in the history
Co-authored-by: Gert van Valkenhoef <[email protected]>
Co-authored-by: ParthaI <[email protected]>
  • Loading branch information
3 people authored Jun 24, 2024
1 parent f94896e commit 148a724
Show file tree
Hide file tree
Showing 6 changed files with 422 additions and 0 deletions.
1 change: 1 addition & 0 deletions aws/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"aws_codedeploy_deployment_config": tableAwsCodeDeployDeploymentConfig(ctx),
"aws_codedeploy_deployment_group": tableAwsCodeDeployDeploymentGroup(ctx),
"aws_codepipeline_pipeline": tableAwsCodepipelinePipeline(ctx),
"aws_codestar_notification_rule": tableAwsCodestarNotificationRule(ctx),
"aws_cognito_identity_pool": tableAwsCognitoIdentityPool(ctx),
"aws_cognito_identity_provider": tableAwsCognitoIdentityProvider(ctx),
"aws_cognito_user_pool": tableAwsCognitoUserPool(ctx),
Expand Down
9 changes: 9 additions & 0 deletions aws/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/codecommit"
"github.com/aws/aws-sdk-go-v2/service/codedeploy"
"github.com/aws/aws-sdk-go-v2/service/codepipeline"
"github.com/aws/aws-sdk-go-v2/service/codestarnotifications"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentity"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentityprovider"
"github.com/aws/aws-sdk-go-v2/service/configservice"
Expand Down Expand Up @@ -500,6 +501,14 @@ func CodePipelineClient(ctx context.Context, d *plugin.QueryData) (*codepipeline
return codepipeline.NewFromConfig(*cfg), nil
}

func CodeStarNotificationsClient(ctx context.Context, d *plugin.QueryData) (*codestarnotifications.Client, error) {
cfg, err := getClientForQueryRegion(ctx, d)
if err != nil {
return nil, err
}
return codestarnotifications.NewFromConfig(*cfg), nil
}

func CognitoIdentityClient(ctx context.Context, d *plugin.QueryData) (*cognitoidentity.Client, error) {
cfg, err := getClientForQuerySupportedRegion(ctx, d, cognitoidentityEndpoint.EndpointsID)
if err != nil {
Expand Down
258 changes: 258 additions & 0 deletions aws/table_aws_codestar_notification_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
package aws

import (
"context"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/codestarnotifications"
"github.com/aws/aws-sdk-go-v2/service/codestarnotifications/types"

turbot_types "github.com/turbot/go-kit/types"

codestarv1 "github.com/aws/aws-sdk-go/service/codestarnotifications"
"github.com/turbot/steampipe-plugin-sdk/v5/grpc/proto"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin/transform"
)

//// TABLE DEFINITION

func tableAwsCodestarNotificationRule(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_codestar_notification_rule",
Description: "AWS CodeStar Notification Rule",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("arn"),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"ResourceNotFoundException"}),
},
Hydrate: getCodeStarNotificationRule,
Tags: map[string]string{"service": "codestar-notifications", "action": "DescribeNotificationRule"},
},
List: &plugin.ListConfig{
Hydrate: listCodeStarNotificationRules,
KeyColumns: plugin.KeyColumnSlice{
{Name: "event_type_id", Require: plugin.Optional},
{Name: "created_by", Require: plugin.Optional},
{Name: "resource", Require: plugin.Optional},
{Name: "target_address", Require: plugin.Optional},
},
Tags: map[string]string{"service": "codestar-notifications", "action": "ListNotificationRules"},
},
GetMatrixItemFunc: SupportedRegionMatrix(codestarv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the notification rule.",
Type: proto.ColumnType_STRING,
},
{
Name: "id",
Description: "The unique ID of the notification rule.",
Type: proto.ColumnType_STRING,
},
{
Name: "name",
Description: "The name of the notification rule.",
Type: proto.ColumnType_STRING,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "resource",
Description: "The Amazon Resource Name (ARN) of the resource associated with the notification rule.",
Type: proto.ColumnType_STRING,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "detail_type",
Description: "The level of detail included in the notifications for this resource. BASIC will include only the contents of the event as it would appear in Amazon CloudWatch. FULL will include any supplemental information provided by AWS CodeStar Notifications and/or the service for the resource for which the notification is created.",
Type: proto.ColumnType_STRING,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "status",
Description: "The status of the notification rule. Valid statuses are on (sending notifications) or off (not sending notifications).",
Type: proto.ColumnType_STRING,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "created_by",
Description: "The name or email alias of the person who created the notification rule.",
Type: proto.ColumnType_STRING,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "event_type_id",
Description: "Specifies that only notification rules with the given event type enabled are returned.",
Type: proto.ColumnType_STRING,
Transform: transform.FromQual("event_type_id"),
},
{
Name: "target_address",
Description: "Specifies that only notification rules with a target with the given address are returned.",
Type: proto.ColumnType_STRING,
Transform: transform.FromQual("target_address"),
},
{
Name: "created_timestamp",
Description: "The date and time the notification rule was created.",
Type: proto.ColumnType_TIMESTAMP,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "last_modified_timestamp",
Description: "The date and time the notification rule was most recently updated.",
Type: proto.ColumnType_TIMESTAMP,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "event_types",
Description: "A list of the event types associated with the notification rule.",
Type: proto.ColumnType_JSON,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "targets",
Description: "A list of targets associated with the notification rule.",
Type: proto.ColumnType_JSON,
Hydrate: getCodeStarNotificationRule,
},

// Steampipe standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Hydrate: getCodeStarNotificationRule,
Transform: transform.FromField("Name"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getCodeStarNotificationRule,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Hydrate: getCodeStarNotificationRule,
Transform: transform.FromField("Arn").Transform(transform.EnsureStringArray),
},
}),
}
}

//// LIST FUNCTION

func listCodeStarNotificationRules(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Get client
svc, err := CodeStarNotificationsClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_codestar_notification_rule.listCodeStarNotificationRules", "connection_error", err)
return nil, err
}

// Limiting the results
maxLimit := int32(100)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxLimit {
maxLimit = limit
}
}

params := &codestarnotifications.ListNotificationRulesInput{
MaxResults: &maxLimit,
}

filters := buildCodeStarNotificationRulesFilter(d.EqualsQuals)
if len(filters) != 0 {
params.Filters = filters
}

paginator := codestarnotifications.NewListNotificationRulesPaginator(svc, params, func(o *codestarnotifications.ListNotificationRulesPaginatorOptions) {
o.StopOnDuplicateToken = true
})

for paginator.HasMorePages() {
// apply rate limiting
d.WaitForListRateLimit(ctx)

output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_codestar_notification_rule.listCodeStarNotificationRules", "api_error", err)
return nil, err
}
for _, rule := range output.NotificationRules {
d.StreamListItem(ctx, rule)

// Context may get cancelled due to manual cancellation or if the limit has been reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}

return nil, err
}

//// HYDRATE FUNCTIONS

func getCodeStarNotificationRule(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
var arn string
if h.Item != nil {
data := h.Item.(types.NotificationRuleSummary)
arn = turbot_types.SafeString(data.Arn)
} else {
arn = d.EqualsQuals["arn"].GetStringValue()
}

// Empty check
if arn == "" {
return nil, nil
}

// Get client
svc, err := CodeStarNotificationsClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_codestar_notification_rule.getCodeStarNotificationRule", "connection_error", err)
return nil, err
}

// Build params
params := &codestarnotifications.DescribeNotificationRuleInput{
Arn: aws.String(arn),
}

op, err := svc.DescribeNotificationRule(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_codestar_notification_rule.getCodeStarNotificationRule", "api_error", err)
return nil, err
}
return op, nil
}

//// UTILITY FUNCTIONS

func buildCodeStarNotificationRulesFilter(equalQuals plugin.KeyColumnEqualsQualMap) []types.ListNotificationRulesFilter {
filters := make([]types.ListNotificationRulesFilter, 0)

filterQuals := map[string]types.ListNotificationRulesFilterName{
"event_type_id": types.ListNotificationRulesFilterNameEventTypeId,
"created_by": types.ListNotificationRulesFilterNameCreatedBy,
"resource": types.ListNotificationRulesFilterNameResource,
"target_address": types.ListNotificationRulesFilterNameTargetAddress,
}

for columnName, filterName := range filterQuals {
if equalQuals[columnName] != nil {
filter := types.ListNotificationRulesFilter{
Name: filterName,
Value: aws.String(equalQuals[columnName].GetStringValue()),
}
filters = append(filters, filter)
}
}
return filters
}
Loading

0 comments on commit 148a724

Please sign in to comment.