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

feat: add tags to aws_backup_plan table #2336

Merged
merged 3 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions aws/backup_tags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package aws

import (
"context"
"errors"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/backup"
"github.com/aws/smithy-go"
"github.com/turbot/steampipe-plugin-sdk/v5/plugin"
)

func getAwsBackupResourceTags(ctx context.Context, d *plugin.QueryData, arn string) (interface{}, error) {
// Create Session
svc, err := BackupClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("backup_tags.getAwsBackupResourceTags", "connection_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}

params := &backup.ListTagsInput{
ResourceArn: aws.String(arn),
}

op, err := svc.ListTags(ctx, params)
plugin.Logger(ctx).Debug("backup_tags.getAwsBackupResourceTags", "ListTagsOutput", op)
if err != nil {
var ae smithy.APIError
if errors.As(err, &ae) {
plugin.Logger(ctx).Debug("backup_tags.getAwsBackupResourceTags", "smithy.APIError", ae)
if ae.ErrorCode() == "ResourceNotFoundException" {
return &backup.ListTagsOutput{
Tags: map[string]string{},
}, nil
}
}
plugin.Logger(ctx).Error("backup_tags.getAwsBackupResourceTags", "api_error", err)
return nil, err
}

if op.Tags == nil {
op.Tags = map[string]string{}
}

return op, nil
}
26 changes: 26 additions & 0 deletions aws/table_aws_backup_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func tableAwsBackupPlan(_ context.Context) *plugin.Table {
Func: getAwsBackupPlan,
Tags: map[string]string{"service": "backup", "action": "GetBackupPlan"},
},
{
Func: getAwsBackupPlanTags,
Tags: map[string]string{"service": "backup", "action": "ListTags"},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(backupv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
Expand Down Expand Up @@ -101,6 +105,12 @@ func tableAwsBackupPlan(_ context.Context) *plugin.Table {
Type: proto.ColumnType_STRING,
Transform: transform.FromField("BackupPlanName", "BackupPlan.BackupPlanName"),
},
{
Name: "tags",
Description: resourceInterfaceDescription("tags"),
Type: proto.ColumnType_JSON,
Hydrate: getAwsBackupPlanTags,
},
{
Name: "akas",
Description: resourceInterfaceDescription("akas"),
Expand Down Expand Up @@ -213,3 +223,19 @@ func getAwsBackupPlan(ctx context.Context, d *plugin.QueryData, h *plugin.Hydrat

return op, nil
}

func getAwsBackupPlanTags(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
arn := backupPlanArn(h.Item)

return getAwsBackupResourceTags(ctx, d, arn)
}

func backupPlanArn(item interface{}) string {
switch item := item.(type) {
case types.BackupPlansListMember:
return *item.BackupPlanArn
case backup.GetBackupPlanOutput:
return *item.BackupPlanArn
}
return ""
}
52 changes: 5 additions & 47 deletions aws/table_aws_backup_recovery_point.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ package aws

import (
"context"
"errors"
"regexp"
"strings"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/arn"
"github.com/aws/aws-sdk-go-v2/service/backup"
"github.com/aws/aws-sdk-go-v2/service/backup/types"
"github.com/aws/smithy-go"

backupv1 "github.com/aws/aws-sdk-go/service/backup"

Expand Down Expand Up @@ -57,6 +54,10 @@ func tableAwsBackupRecoveryPoint(_ context.Context) *plugin.Table {
Func: getAwsBackupRecoveryPoint,
Tags: map[string]string{"service": "backup", "action": "DescribeRecoveryPoint"},
},
{
Func: getAwsBackupRecoveryPointTags,
Tags: map[string]string{"service": "backup", "action": "ListTags"},
},
},
GetMatrixItemFunc: SupportedRegionMatrix(backupv1.EndpointsID),
Columns: awsRegionalColumns([]*plugin.Column{
Expand Down Expand Up @@ -327,50 +328,7 @@ func getAwsBackupRecoveryPoint(ctx context.Context, d *plugin.QueryData, h *plug
func getAwsBackupRecoveryPointTags(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
arn := recoveryPointArn(h.Item)

// Define the regex pattern for the recovery point ARN
pattern := `arn:aws:backup:[a-z0-9\-]+:[0-9]{12}:recovery-point:.*`

// Create a regular expression object
re := regexp.MustCompile(pattern)

// Only return the tags associated with the resovery point
if !re.MatchString(arn) {
return nil, nil
}

// Create Session
svc, err := BackupClient(ctx, d)
if err != nil {
plugin.Logger(ctx).Error("aws_backup_recovery_point.getAwsBackupRecoveryPointTags", "connection_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}

params := &backup.ListTagsInput{
ResourceArn: aws.String(arn),
}

op, err := svc.ListTags(ctx, params)
if err != nil {

var ae smithy.APIError
if errors.As(err, &ae) {
if ae.ErrorCode() == "ResourceNotFoundException" {
return &backup.GetBackupVaultNotificationsOutput{}, nil
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type was incorrect, copied/pasted from above function.

}
}
plugin.Logger(ctx).Error("aws_backup_recovery_point.getAwsBackupRecoveryPointTags", "api_error", err)
return nil, err
}

if op.Tags == nil {
return nil, nil
}

return op, nil
return getAwsBackupResourceTags(ctx, d, arn)
}

func recoveryPointArn(item interface{}) string {
Expand Down
33 changes: 1 addition & 32 deletions aws/table_aws_backup_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,6 @@ func getAwsBackupVaultNotification(ctx context.Context, d *plugin.QueryData, h *
plugin.Logger(ctx).Error("aws_backup_vault.getAwsBackupVaultNotification", "connection_error", err)
return nil, err
}
if svc == nil {
// Unsupported region, return no data
return nil, nil
}

if svc == nil {
// Unsupported region, return no data
Expand Down Expand Up @@ -294,36 +290,9 @@ func getAwsBackupVaultNotification(ctx context.Context, d *plugin.QueryData, h *
}

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

arn := vaultArn(h.Item)

params := &backup.ListTagsInput{
ResourceArn: aws.String(arn),
}

op, err := svc.ListTags(ctx, params)
if err != nil {

var ae smithy.APIError
if errors.As(err, &ae) {
if ae.ErrorCode() == "ResourceNotFoundException" {
return &backup.ListTagsOutput{}, nil
}
}
plugin.Logger(ctx).Error("aws_backup_vault.getAwsBackupVaultTags", "api_error", err)
return nil, err
}
return op, nil
return getAwsBackupResourceTags(ctx, d, arn)
}

func getAwsBackupVaultAccessPolicy(ctx context.Context, d *plugin.QueryData, h *plugin.HydrateData) (interface{}, error) {
Expand Down
Loading