Skip to content

Commit

Permalink
Add aws_keyspaces_keyspace and aws_keyspaces_table tables Closes #2264 (
Browse files Browse the repository at this point in the history
#2271)

Co-authored-by: Priyanka Chatterjee <[email protected]>
  • Loading branch information
ParthaI and Priyanka-Chatterjee-2000 authored Jan 2, 2025
1 parent 4966710 commit 7d017c6
Show file tree
Hide file tree
Showing 17 changed files with 859 additions and 0 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"akas": ["{{ output.resource_aka.value }}"],
"table_name": "{{resourceName}}",
"title": "{{resourceName}}"
}
]
3 changes: 3 additions & 0 deletions aws-test/tests/aws_keyspaces_table/test-get-call-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select title, akas, table_name
from aws.aws_keyspaces_table
where keyspace_name = '{{resourceName}}' and table_name = '{{resourceName}}';
10 changes: 10 additions & 0 deletions aws-test/tests/aws_keyspaces_table/test-list-call-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"akas": [
"{{ output.resource_aka.value }}"
],
"keyspace_name": "{{resourceName}}",
"table_name": "{{resourceName}}",
"title": "{{resourceName}}"
}
]
3 changes: 3 additions & 0 deletions aws-test/tests/aws_keyspaces_table/test-list-call-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select table_name, keyspace_name, title, akas
from aws.aws_keyspaces_table
where keyspace_name = '{{ resourceName }}';
8 changes: 8 additions & 0 deletions aws-test/tests/aws_keyspaces_table/test-turbot-expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"akas": [
"{{ output.resource_aka.value }}"
],
"title": "{{resourceName}}"
}
]
3 changes: 3 additions & 0 deletions aws-test/tests/aws_keyspaces_table/test-turbot-query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
select akas, title
from aws.aws_keyspaces_table
where keyspace_name = '{{ resourceName }}';
1 change: 1 addition & 0 deletions aws-test/tests/aws_keyspaces_table/variables.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
76 changes: 76 additions & 0 deletions aws-test/tests/aws_keyspaces_table/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@

variable "resource_name" {
type = string
default = "turbot-test-20200125-create-update"
description = "Name of the resource used throughout the test."
}

variable "aws_profile" {
type = string
default = "default"
description = "AWS credentials profile used for the test. Default is to use the default profile."
}

variable "aws_region" {
type = string
default = "us-east-1"
description = "AWS region used for the test. Does not work with default region in config, so must be defined here."
}

variable "aws_region_alternate" {
type = string
default = "us-east-2"
description = "Alternate AWS region used for tests that require two regions (e.g. DynamoDB global tables)."
}

provider "aws" {
profile = var.aws_profile
region = var.aws_region
}

provider "aws" {
alias = "alternate"
profile = var.aws_profile
region = var.aws_region_alternate
}

data "aws_partition" "current" {}
data "aws_caller_identity" "current" {}
data "aws_region" "primary" {}
data "aws_region" "alternate" {
provider = aws.alternate
}

data "null_data_source" "resource" {
inputs = {
scope = "arn:${data.aws_partition.current.partition}:::${data.aws_caller_identity.current.account_id}"
}
}

resource "aws_keyspaces_keyspace" "named_test_resource" {
name = var.resource_name
}

resource "aws_keyspaces_table" "named_test_resource" {
keyspace_name = aws_keyspaces_keyspace.named_test_resource.name
table_name = var.resource_name

schema_definition {
column {
name = "test_id"
type = "ascii"
}

partition_key {
name = "test_id"
}
}
}

output "resource_aka" {
value = aws_keyspaces_table.named_test_resource.arn
}

output "resource_name" {
value = var.resource_name
}
2 changes: 2 additions & 0 deletions aws/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ func Plugin(ctx context.Context) *plugin.Plugin {
"aws_iot_thing": tableAwsIoTThing(ctx),
"aws_iot_thing_group": tableAwsIoTThingGroup(ctx),
"aws_iot_thing_type": tableAwsIoTThingType(ctx),
"aws_keyspaces_keyspace": tableAwsKeyspacesKeyspace(ctx),
"aws_keyspaces_table": tableAwsKeyspacesTable(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 @@ -85,6 +85,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/inspector2"
"github.com/aws/aws-sdk-go-v2/service/iot"
"github.com/aws/aws-sdk-go-v2/service/kafka"
"github.com/aws/aws-sdk-go-v2/service/keyspaces"
"github.com/aws/aws-sdk-go-v2/service/kinesis"
"github.com/aws/aws-sdk-go-v2/service/kinesisanalyticsv2"
"github.com/aws/aws-sdk-go-v2/service/kinesisvideo"
Expand Down Expand Up @@ -179,6 +180,7 @@ import (
inspector2Endpoint "github.com/aws/aws-sdk-go/service/inspector2"
iotEndpoint "github.com/aws/aws-sdk-go/service/iot"
kafkaEndpoint "github.com/aws/aws-sdk-go/service/kafka"
keyspacesEndpoint "github.com/aws/aws-sdk-go/service/keyspaces"
kinesisanalyticsv2Endpoint "github.com/aws/aws-sdk-go/service/kinesisanalyticsv2"
kinesisvideoEndpoint "github.com/aws/aws-sdk-go/service/kinesisvideo"
kmsEndpoint "github.com/aws/aws-sdk-go/service/kms"
Expand Down Expand Up @@ -930,6 +932,17 @@ func KafkaClient(ctx context.Context, d *plugin.QueryData) (*kafka.Client, error
return kafka.NewFromConfig(*cfg), nil
}

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

func KinesisClient(ctx context.Context, d *plugin.QueryData) (*kinesis.Client, error) {
cfg, err := getClientForQueryRegion(ctx, d)
if err != nil {
Expand Down
165 changes: 165 additions & 0 deletions aws/table_aws_keyspaces_keyspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package aws

import (
"context"

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

"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 tableAwsKeyspacesKeyspace(ctx context.Context) *plugin.Table {
return &plugin.Table{
Name: "aws_keyspaces_keyspace",
Description: "AWS Keyspaces Keyspace",
Get: &plugin.GetConfig{
KeyColumns: plugin.SingleColumn("keyspace_name"), // Identify the keyspace by its name
Hydrate: getKeyspacesKeyspace, // Get function
IgnoreConfig: &plugin.IgnoreConfig{
ShouldIgnoreErrorFunc: shouldIgnoreErrors([]string{"ResourceNotFoundException"}),
},
Tags: map[string]string{"service": "keyspaces", "action": "GetKeyspace"},
},
List: &plugin.ListConfig{
Hydrate: listKeyspacesKeyspaces, // Parent hydrate function
Tags: map[string]string{"service": "keyspaces", "action": "ListKeyspaces"},
},
GetMatrixItemFunc: SupportedRegionMatrix(keyspacesv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
{
Name: "keyspace_name",
Description: "The name of the keyspace.",
Type: proto.ColumnType_STRING,
},
{
Name: "arn",
Description: "The unique identifier of the keyspace in the format of an Amazon Resource Name (ARN).",
Type: proto.ColumnType_STRING,
Transform: transform.FromField("ResourceArn"),
},
{
Name: "replication_strategy",
Description: "Returns the replication strategy of the keyspace. The options are SINGLE_REGION or MULTI_REGION .",
Type: proto.ColumnType_STRING,
},
{
Name: "replication_regions",
Description: "If the replication strategy of the keyspace is MULTI_REGION, a list of replication regions is returned.",
Type: proto.ColumnType_JSON,
},

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

//// LIST FUNCTION

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

if svc == nil {
// Unsupported region check
return nil, nil
}

// Define max results for the request
maxItems := int32(1000)
if d.QueryContext.Limit != nil {
limit := int32(*d.QueryContext.Limit)
if limit < maxItems {
maxItems = limit
}
}

input := &keyspaces.ListKeyspacesInput{
MaxResults: &maxItems,
}

paginator := keyspaces.NewListKeyspacesPaginator(svc, input, func(o *keyspaces.ListKeyspacesPaginatorOptions) {
o.Limit = maxItems
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_keyspaces_keyspace.listKeyspacesKeyspaces", "api_error", err)
return nil, err
}

for _, keyspace := range output.Keyspaces {
d.StreamListItem(ctx, keyspace)

// Stop processing if context is canceled or limit is reached
if d.RowsRemaining(ctx) == 0 {
return nil, nil
}
}
}

return nil, nil
}

//// GET FUNCTION

func getKeyspacesKeyspace(ctx context.Context, d *plugin.QueryData, _ *plugin.HydrateData) (interface{}, error) {
keyspaceName := d.EqualsQualString("keyspace_name")

// Empty check for keyspace name
if keyspaceName == "" {
return nil, nil
}

// Create session
svc, err := KeyspacesClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_keyspaces_keyspace.getKeyspacesKeyspace", "connection_error", err)
return nil, err
}

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

// Build the input parameters
input := &keyspaces.GetKeyspaceInput{
KeyspaceName: &keyspaceName,
}

// Make the GetKeyspace API call
result, err := svc.GetKeyspace(ctx, input)
if err != nil {
plugin.Logger(ctx).Error("aws_keyspaces_keyspace.getKeyspacesKeyspace", "api_error", err)
return nil, err
}

return result, nil
}
Loading

0 comments on commit 7d017c6

Please sign in to comment.