Skip to content
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

Add table aws_iot_greengrass_* Closes #1994 #2002

Closed
wants to merge 9 commits into from
Closed
3 changes: 3 additions & 0 deletions aws/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"aws_inspector_assessment_template": tableAwsInspectorAssessmentTemplate(ctx),
"aws_inspector_exclusion": tableAwsInspectorExclusion(ctx),
"aws_inspector_finding": tableAwsInspectorFinding(ctx),
"aws_iot_greengrass_component": tableAwsIotGreengrassComponent(ctx),
"aws_iot_greengrass_core_device": tableAwsIotGreengrassCoreDevice(ctx),
"aws_iot_greengrass_deployment": tableAwsIotGreengrassDeployment(ctx),
"aws_kinesis_consumer": tableAwsKinesisConsumer(ctx),
"aws_kinesis_firehose_delivery_stream": tableAwsKinesisFirehoseDeliveryStream(ctx),
"aws_kinesis_stream": tableAwsKinesisStream(ctx),
Expand Down
13 changes: 13 additions & 0 deletions aws/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/glacier"
"github.com/aws/aws-sdk-go-v2/service/globalaccelerator"
"github.com/aws/aws-sdk-go-v2/service/glue"
"github.com/aws/aws-sdk-go-v2/service/greengrassv2"
"github.com/aws/aws-sdk-go-v2/service/guardduty"
"github.com/aws/aws-sdk-go-v2/service/health"
"github.com/aws/aws-sdk-go-v2/service/iam"
Expand Down Expand Up @@ -163,6 +164,7 @@ import (
glacierEndpoint "github.com/aws/aws-sdk-go/service/glacier"
inspectorEndpoint "github.com/aws/aws-sdk-go/service/inspector"
inspector2Endpoint "github.com/aws/aws-sdk-go/service/inspector2"
greengrassEndpoint "github.com/aws/aws-sdk-go/service/greengrassv2"
kafkaEndpoint "github.com/aws/aws-sdk-go/service/kafka"
kinesisanalyticsv2Endpoint "github.com/aws/aws-sdk-go/service/kinesisanalyticsv2"
kinesisvideoEndpoint "github.com/aws/aws-sdk-go/service/kinesisvideo"
Expand Down Expand Up @@ -853,6 +855,17 @@ func Inspector2Client(ctx context.Context, d *plugin.QueryData) (*inspector2.Cli
return inspector2.NewFromConfig(*cfg), nil
}

func IoTGreengrassClient(ctx context.Context, d *plugin.QueryData) (*greengrassv2.Client, error) {
cfg, err := getClientForQuerySupportedRegion(ctx, d, greengrassEndpoint.EndpointsID)
if err != nil {
return nil, err
}
if cfg == nil {
return nil, nil
}
return greengrassv2.NewFromConfig(*cfg), nil
}

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

import (
"context"

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

greengrassv1 "github.com/aws/aws-sdk-go/service/greengrassv2"

"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 tableAwsIotGreengrassComponent(_ context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_iot_greengrass_component",
Description: "AWS IoT Greengrass Component",
List: &plugin.ListConfig{
Hydrate: listIotGreengrassComponents,
Tags: map[string]string{"service": "greengrassv2", "action": "ListComponents"},
},
HydrateConfig: []plugin.HydrateConfig{
{
Func: getIotGreengrassComponent,
Tags: map[string]string{"service": "greengrassv2", "action": "GetComponent"},
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"ResourceNotFoundException"}),
},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(greengrassv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "component_name",
Description: "The name of the component.",
Type: proto.ColumnType_STRING,
},
{
Name: "arn",
Description: "The ARN of the component version.",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ARN"),
},
{
Name: "recipe",
Description: "The recipe of the component version.",
Type: proto.ColumnType_STRING,
Hydrate: getIotGreengrassComponent,
},
{
Name: "recipe_output_format",
Description: "The format of the recipe.",
Type: proto.ColumnType_STRING,
Hydrate: getIotGreengrassComponent,
},

// JSON columns
{
Name: "latest_version",
Description: "The latest version of the component and its details.",
Type: proto.ColumnType_JSON,
},

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

//// LIST FUNCTION

func listIotGreengrassComponents(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
// Create Session
svc, err := IoTGreengrassClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_iot_greengrass_component.listIotGreengrassComponents", "connection_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}

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

input := &greengrassv2.ListComponentsInput{
MaxResults: aws.Int32(maxLimit),
}

paginator := greengrassv2.NewListComponentsPaginator(svc, input, func(o *greengrassv2.ListComponentsPaginatorOptions) {
o.Limit = maxLimit
o.StopOnDuplicateToken = true
})

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

output, err := paginator.NextPage(ctx)
if err != nil {
plugin.Logger(ctx).Error("aws_iot_greengrass_component.listIotGreengrassComponents", "api_error", err)
return nil, err
}

for _, item := range output.Components {
d.StreamListItem(ctx, item)

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

return nil, err
}

//// HYDRATE FUNCTIONS

func getIotGreengrassComponent(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
t := h.Item.(types.Component)
arn := *t.Arn

// Create service
svc, err := IoTGreengrassClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_iot_greengrass_component.getIotGreengrassComponent", "connection_error", err)
return nil, err
}

params := &greengrassv2.GetComponentInput{
Arn: aws.String(arn),
}

resp, err := svc.GetComponent(ctx, params)
if err != nil {
plugin.Logger(ctx).Error("aws_iot_greengrass_component.getIotGreengrassComponent", "api_error", err)
return nil, err
}

return resp, nil
}
Loading