-
Notifications
You must be signed in to change notification settings - Fork 110
Add table aws_cloudwatch_event_rule #2487
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e3fc8f0
Add table aws_cloudwatch_event_rule
ParthaI e745742
update the doc and tested the table end to end
ParthaI ab38c0d
Addressed copilot review comment
ParthaI b593c7a
Update the limit logic
ParthaI bd2a3b1
Merge branch 'main' of github.com:turbot/steampipe-plugin-aws into ad…
ParthaI File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,367 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/eventbridge" | ||
|
||
"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 tableAwsCloudwatchEventRule(_ context.Context) *plugin.Table { | ||
return &plugin.Table{ | ||
Name: "aws_cloudwatch_event_rule", | ||
Description: "AWS CloudWatch Event Rule", | ||
Get: &plugin.GetConfig{ | ||
KeyColumns: plugin.AllColumns([]string{"name", "event_bus_name"}), | ||
IgnoreConfig: &plugin.IgnoreConfig{ | ||
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"ResourceNotFoundException"}), | ||
}, | ||
Hydrate: getAwsCloudWatchEventRule, | ||
Tags: map[string]string{"service": "events", "action": "DescribeRule"}, | ||
}, | ||
List: &plugin.ListConfig{ | ||
Hydrate: listAwsCloudWatchEventRules, | ||
Tags: map[string]string{"service": "events", "action": "ListRules"}, | ||
KeyColumns: []*plugin.KeyColumn{ | ||
{Name: "event_bus_name", Require: plugin.Optional}, | ||
{Name: "name_prefix", Require: plugin.Optional}, | ||
}, | ||
}, | ||
HydrateConfig: []plugin.HydrateConfig{ | ||
{ | ||
Func: getAwsCloudWatchEventTargetsByRule, | ||
Tags: map[string]string{"service": "events", "action": "ListTargetsByRule"}, | ||
}, | ||
{ | ||
Func: getAwsCloudWatchEventRuleTags, | ||
Tags: map[string]string{"service": "events", "action": "ListTagsForResource"}, | ||
}, | ||
}, | ||
GetMatrixItemFunc: SupportedRegionMatrix(AWS_EVENTS_SERVICE_ID), | ||
Columns: awsRegionalColumns([]*plugin.Column{ | ||
{ | ||
Name: "name", | ||
Description: "The name of the rule.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "arn", | ||
Description: "The Amazon Resource Name (ARN) of the rule.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "description", | ||
Description: "The description of the rule.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "state", | ||
Description: "The state of the rule.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "event_bus_name", | ||
Description: "The name or ARN of the event bus associated with the rule.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "created_by", | ||
Description: "The account ID of the user that created the rule.", | ||
Type: proto.ColumnType_STRING, | ||
Hydrate: getAwsCloudWatchEventRule, | ||
}, | ||
{ | ||
Name: "role_arn", | ||
Description: "The Amazon Resource Name (ARN) of the IAM role associated with the rule.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "schedule_expression", | ||
Description: "The scheduling expression. For example, 'cron(0 20 * * ? *)', 'rate(5 minutes)'.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "managed_by", | ||
Description: "If this is a managed rule, created by an AWS service on your behalf, this field displays the principal name of the AWS service that created the rule.", | ||
Type: proto.ColumnType_STRING, | ||
}, | ||
{ | ||
Name: "event_pattern", | ||
Description: "The event pattern of the rule.", | ||
Type: proto.ColumnType_JSON, | ||
}, | ||
{ | ||
Name: "name_prefix", | ||
Description: "Specifying this limits the results to only those event rules with names that start with the specified prefix.", | ||
Type: proto.ColumnType_STRING, | ||
Hydrate: getCloudWatchNamePrefixValue, | ||
Transform: transform.FromValue(), | ||
}, | ||
{ | ||
Name: "targets", | ||
Description: "The targets assigned to the rule.", | ||
Type: proto.ColumnType_JSON, | ||
Hydrate: getAwsCloudWatchEventTargetsByRule, | ||
Transform: transform.FromField("Targets"), | ||
}, | ||
{ | ||
Name: "tags_src", | ||
Description: "A list of tags assigned to the rule.", | ||
Type: proto.ColumnType_JSON, | ||
Hydrate: getAwsCloudWatchEventRuleTags, | ||
Transform: transform.FromField("Tags"), | ||
}, | ||
|
||
// Standard columns for all tables | ||
{ | ||
Name: "title", | ||
Description: resourceInterfaceDescription("title"), | ||
Type: proto.ColumnType_STRING, | ||
Transform: transform.FromField("Name"), | ||
}, | ||
{ | ||
Name: "tags", | ||
Description: resourceInterfaceDescription("tags"), | ||
Type: proto.ColumnType_JSON, | ||
Hydrate: getAwsCloudWatchEventRuleTags, | ||
Transform: transform.FromField("Tags").Transform(cloudWatchEventTagListToTurbotTags), | ||
}, | ||
{ | ||
Name: "akas", | ||
Description: resourceInterfaceDescription("akas"), | ||
Type: proto.ColumnType_JSON, | ||
Transform: transform.FromField("Arn").Transform(transform.EnsureStringArray), | ||
}, | ||
}), | ||
} | ||
} | ||
|
||
//// LIST FUNCTION | ||
|
||
func listAwsCloudWatchEventRules(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
// Get client | ||
svc, err := EventBridgeClient(ctx, d) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.listAwsCloudWatchEventRules", "get_client_error", err) | ||
return nil, err | ||
} | ||
if svc == nil { | ||
// Unsupported region, return no data | ||
return nil, nil | ||
} | ||
|
||
// Limiting the results | ||
maxLimit := int32(100) | ||
if d.QueryContext.Limit != nil { | ||
limit := int32(*d.QueryContext.Limit) | ||
// AWS API enforces a minimum limit of 1 and a default of 100, so adjust if a lower value is provided. | ||
if limit < maxLimit { | ||
maxLimit = limit | ||
} | ||
} | ||
|
||
pagesLeft := true | ||
params := &eventbridge.ListRulesInput{ | ||
// Default to the maximum allowed | ||
Limit: aws.Int32(maxLimit), | ||
} | ||
|
||
equalQuals := d.EqualsQuals | ||
if equalQuals["event_bus_name"] != nil { | ||
params.EventBusName = aws.String(equalQuals["event_bus_name"].GetStringValue()) | ||
} | ||
if equalQuals["name_prefix"] != nil { | ||
params.NamePrefix = aws.String(equalQuals["name_prefix"].GetStringValue()) | ||
} | ||
// API doesn't support aws-go-sdk-v2 paginator as of date | ||
for pagesLeft { | ||
// apply rate limiting | ||
d.WaitForListRateLimit(ctx) | ||
|
||
output, err := svc.ListRules(ctx, params) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.listAwsCloudWatchEventRules", "api_error", err) | ||
return nil, err | ||
} | ||
for _, rule := range output.Rules { | ||
d.StreamListItem(ctx, &eventbridge.DescribeRuleOutput{ | ||
Name: rule.Name, | ||
Arn: rule.Arn, | ||
Description: rule.Description, | ||
State: rule.State, | ||
EventBusName: rule.EventBusName, | ||
ManagedBy: rule.ManagedBy, | ||
ScheduleExpression: rule.ScheduleExpression, | ||
RoleArn: rule.RoleArn, | ||
}) | ||
|
||
// Context may get cancelled due to manual cancellation or if the limit has been reached | ||
if d.RowsRemaining(ctx) == 0 { | ||
return nil, nil | ||
} | ||
} | ||
if output.NextToken != nil { | ||
pagesLeft = true | ||
params.NextToken = output.NextToken | ||
} else { | ||
pagesLeft = false | ||
} | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
//// HYDRATE FUNCTIONS | ||
|
||
func getAwsCloudWatchEventRule(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
ParthaI marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var name, eventBusName string | ||
|
||
if h.Item != nil { | ||
item, ok := h.Item.(*eventbridge.DescribeRuleOutput) | ||
if !ok { | ||
return nil, nil | ||
} | ||
name = *item.Name | ||
if item.EventBusName != nil { | ||
eventBusName = *item.EventBusName | ||
} | ||
} else { | ||
name = d.EqualsQuals["name"].GetStringValue() | ||
eventBusName = d.EqualsQuals["event_bus_name"].GetStringValue() | ||
} | ||
|
||
// Create Session | ||
svc, err := EventBridgeClient(ctx, d) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.getAwsCloudWatchEventRule", "get_client_error", err) | ||
return nil, err | ||
} | ||
if svc == nil { | ||
// Unsupported region, return no data | ||
return nil, nil | ||
} | ||
|
||
// Build the params | ||
params := &eventbridge.DescribeRuleInput{ | ||
Name: &name, | ||
} | ||
|
||
// Use the event bus name if specified | ||
if eventBusName != "" { | ||
params.EventBusName = &eventBusName | ||
} | ||
|
||
// Get call | ||
data, err := svc.DescribeRule(ctx, params) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.getAwsCloudWatchEventRule", "api_error", err) | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func getAwsCloudWatchEventTargetsByRule(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
rule := h.Item.(*eventbridge.DescribeRuleOutput) | ||
|
||
name := rule.Name | ||
var eventBusName *string | ||
|
||
if rule.EventBusName != nil { | ||
eventBusName = rule.EventBusName | ||
} | ||
|
||
// Create Session | ||
svc, err := EventBridgeClient(ctx, d) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.getAwsCloudWatchEventTargetsByRule", "get_client_error", err) | ||
return nil, err | ||
} | ||
if svc == nil { | ||
// Unsupported region, return no data | ||
return nil, nil | ||
} | ||
|
||
// Build the params | ||
params := &eventbridge.ListTargetsByRuleInput{ | ||
Rule: name, | ||
} | ||
|
||
// Use event bus name if available | ||
if eventBusName != nil { | ||
params.EventBusName = eventBusName | ||
} | ||
|
||
data, err := svc.ListTargetsByRule(ctx, params) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.getAwsCloudWatchEventTargetsByRule", "api_error", err) | ||
return nil, err | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func getAwsCloudWatchEventRuleTags(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
item := h.Item.(*eventbridge.DescribeRuleOutput) | ||
|
||
arn := item.Arn | ||
|
||
// Create Session | ||
svc, err := EventBridgeClient(ctx, d) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.getAwsCloudWatchEventRuleTags", "get_client_error", err) | ||
return nil, err | ||
} | ||
if svc == nil { | ||
// Unsupported region, return no data | ||
return nil, nil | ||
} | ||
|
||
// Build the params | ||
params := &eventbridge.ListTagsForResourceInput{ | ||
ResourceARN: arn, | ||
} | ||
|
||
// Get call | ||
op, err := svc.ListTagsForResource(ctx, params) | ||
if err != nil { | ||
plugin.Logger(ctx).Error("aws_cloudwatch_event_rule.getAwsCloudWatchEventRuleTags", "api_error", err) | ||
return nil, err | ||
} | ||
|
||
return op, nil | ||
} | ||
|
||
//// TRANSFORM FUNCTIONS | ||
|
||
func cloudWatchEventTagListToTurbotTags(ctx context.Context, d *transform.TransformData) (interface{}, error) { | ||
tagList := d.HydrateItem.(*eventbridge.ListTagsForResourceOutput) | ||
|
||
if tagList.Tags == nil { | ||
return nil, nil | ||
} | ||
|
||
// Mapping the resource tags inside turbotTags | ||
turbotTagsMap := map[string]string{} | ||
for _, i := range tagList.Tags { | ||
turbotTagsMap[*i.Key] = *i.Value | ||
} | ||
|
||
return turbotTagsMap, nil | ||
} | ||
|
||
//// UTILITY FUNCTIONS | ||
|
||
func getCloudWatchNamePrefixValue(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) { | ||
if d.EqualsQuals["name_prefix"].GetStringValue() != "" { | ||
return d.EqualsQuals["name_prefix"].GetStringValue(), nil | ||
} else { | ||
return h.Item.(*eventbridge.DescribeRuleOutput).Name, nil | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.