Skip to content

Commit

Permalink
Add table aws_fms_* resource Closes #1779 (#1851)
Browse files Browse the repository at this point in the history
Co-authored-by: Madhushree Ray <[email protected]>
  • Loading branch information
ParthaI and madhushreeray30 authored Nov 10, 2023
1 parent d849c0e commit 784a873
Show file tree
Hide file tree
Showing 8 changed files with 621 additions and 0 deletions.
2 changes: 2 additions & 0 deletions aws/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"aws_emr_instance": tableAwsEmrInstance(ctx),
"aws_eventbridge_bus": tableAwsEventBridgeBus(ctx),
"aws_eventbridge_rule": tableAwsEventBridgeRule(ctx),
"aws_fms_app_list": tableAwsFMSAppList(ctx),
"aws_fms_policy": tableAwsFMSPolicy(ctx),
"aws_fsx_file_system": tableAwsFsxFileSystem(ctx),
"aws_glacier_vault": tableAwsGlacierVault(ctx),
"aws_globalaccelerator_accelerator": tableAwsGlobalAcceleratorAccelerator(ctx),
Expand Down
9 changes: 9 additions & 0 deletions aws/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/emr"
"github.com/aws/aws-sdk-go-v2/service/eventbridge"
"github.com/aws/aws-sdk-go-v2/service/firehose"
"github.com/aws/aws-sdk-go-v2/service/fms"
"github.com/aws/aws-sdk-go-v2/service/fsx"
"github.com/aws/aws-sdk-go-v2/service/glacier"
"github.com/aws/aws-sdk-go-v2/service/globalaccelerator"
Expand Down Expand Up @@ -721,6 +722,14 @@ func FirehoseClient(ctx context.Context, d *plugin.QueryData) (*firehose.Client,
return firehose.NewFromConfig(*cfg), nil
}

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

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

import (
"context"

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

fmsv1 "github.com/aws/aws-sdk-go/service/fms"

"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 tableAwsFMSAppList(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_fms_app_list",
Description: "AWS FMS App List",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("list_id"),
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"ResourceNotFoundException"}),
},
Hydrate: getFmsAppList,
Tags: map[string]string{"service": "fms", "action": "GetAppsList"},
},
List: &plugin.ListConfig{
Hydrate: listFmsAppLists,
Tags: map[string]string{"service": "fms", "action": "ListAppsLists"},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: getFmsAppList,
Tags: map[string]string{"service": "fms", "action": "GetAppsList"},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(fmsv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "list_name",
Description: "The name of the applications list.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ListName", "AppsList.ListName"),
},
{
Name: "list_id",
Description: "The ID of the applications list.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ListId", "AppsList.ListId"),
},
{
Name: "arn",
Description: "The Amazon Resource Name (ARN) of the applications list.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ListArn", "AppsListArn"),
},
{
Name: "create_time",
Description: "The time that the Firewall Manager applications list was created.",
Type: proto.ColumnType_TIMESTAMP,
Hydrate: getFmsAppList,
},
{
Name: "last_update_time",
Description: "The time that the Firewall Manager applications list was last updated.",
Type: proto.ColumnType_TIMESTAMP,
Hydrate: getFmsAppList,
},
{
Name: "list_update_token",
Description: "A unique identifier for each update to the list. When you update the list, the update token must match the token of the current version of the application list.",
Type: proto.ColumnType_STRING,
Hydrate: getFmsAppList,
},
{
Name: "previous_apps_list",
Description: "A map of previous version numbers to their corresponding App object arrays.",
Type: proto.ColumnType_JSON,
Hydrate: getFmsAppList,
},
{
Name: "apps_list",
Description: "An array of applications in the Firewall Manager applications list.",
Type: proto.ColumnType_JSON,
Hydrate: getFmsAppList,
},

// Steampipe standard columns
{
Name: "title",
Description: resourceInterfaceDescription("title"),
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ListName", "AppsList.ListName"),
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Type: proto.ColumnType_JSON,
Transform: transform.FromField("ListArn", "AppsListArn").Transform(transform.EnsureStringArray),
},
}),
}
}

//// LIST FUNCTION

func listFmsAppLists(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create Session
svc, err := FMSClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_fms_app_list.listFmsAppLists", "connection_error", err)
return nil, err
}

if svc == nil {
// Unsupported region, return no data
return nil, nil
}

// Reduce the basic request limit down if the user has only requested a small number of rows
maxItems := int32(100)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxItems {
maxItems = int32(limit)
}
}

input := fms.ListAppsListsInput{
MaxResults: aws.Int32(maxItems),
}

paginator := fms.NewListAppsListsPaginator(svc, &input, func(o *fms.ListAppsListsPaginatorOptions) {
o.Limit = maxItems
o.StopOnDuplicateToken = true
})

for paginator.HasMorePages() {
output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_fms_app_list.listFmsAppLists", "api_error", err)
return nil, err
}

for _, app := range output.AppsLists {
d.StreamListItem(ctx, app)

// 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, nil
}

//// HYDRATE FUNCTIONS

func getFmsAppList(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
listId := ""

if h.Item != nil {
data := h.Item.(types.AppsListDataSummary)
listId = *data.ListId
} else {
listId = d.EqualsQualString("list_id")
}

if listId == "" {
return nil, nil
}
// Create service
svc, err := FMSClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_fms_app_list.getFmsAppList", "connection_error", err)
return nil, err
}

if svc == nil {
// Unsupported region, return no data
return nil, nil
}

params := &fms.GetAppsListInput{
ListId: &listId,
}

op, err := svc.GetAppsList(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_fms_app_list.getFmsAppList", "api_error", err)
return nil, err
}

if op != nil {
return op, nil
}

return nil, nil
}
Loading

0 comments on commit 784a873

Please sign in to comment.