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

CL-858 Adding Athena support for data catalogs and prepared statements #23

Merged
merged 1 commit into from
Mar 29, 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
77 changes: 77 additions & 0 deletions resources/athena-data-catalogs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
)

type AthenaDataCatalog struct {
svc *athena.Athena
name *string
}

func init() {
register("AthenaDataCatalog", ListAthenaDataCatalogs)
}

func ListAthenaDataCatalogs(sess *session.Session) ([]Resource, error) {
svc := athena.New(sess)
resources := []Resource{}

params := &athena.ListDataCatalogsInput{
MaxResults: aws.Int64(50),
}

for {
output, err := svc.ListDataCatalogs(params)
if err != nil {
return nil, err
}

for _, catalog := range output.DataCatalogsSummary {
resources = append(resources, &AthenaDataCatalog{
svc: svc,
name: catalog.CatalogName,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}

return resources, nil
}

func (f *AthenaDataCatalog) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("Name", f.name)

return properties
}

func (f *AthenaDataCatalog) Remove() error {

_, err := f.svc.DeleteDataCatalog(&athena.DeleteDataCatalogInput{
Name: f.name,
})

return err
}

func (f *AthenaDataCatalog) Filter() error {
if *f.name == "AwsDataCatalog" {
return fmt.Errorf("cannot delete default data source")
}
return nil
}

func (f *AthenaDataCatalog) String() string {
return *f.name
}
80 changes: 80 additions & 0 deletions resources/athena-prepared-statements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package resources

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
)

type AthenaPreparedStatement struct {
svc *athena.Athena
workGroup *string
name *string
}

func init() {
register("AthenaPreparedStatement", ListAthenaPreparedStatements)
}

func ListAthenaPreparedStatements(sess *session.Session) ([]Resource, error) {
svc := athena.New(sess)
resources := []Resource{}

workgroups, err := svc.ListWorkGroups(&athena.ListWorkGroupsInput{})
if err != nil {
return nil, err
}

for _, workgroup := range workgroups.WorkGroups {
params := &athena.ListPreparedStatementsInput{
WorkGroup: workgroup.Name,
MaxResults: aws.Int64(50),
}

for {
output, err := svc.ListPreparedStatements(params)
if err != nil {
return nil, err
}

for _, statement := range output.PreparedStatements {
resources = append(resources, &AthenaPreparedStatement{
svc: svc,
workGroup: workgroup.Name,
name: statement.StatementName,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}
}

return resources, nil
}

func (f *AthenaPreparedStatement) Properties() types.Properties {
properties := types.NewProperties()
properties.Set("StatementName", f.name)
properties.Set("WorkGroup", f.workGroup)

return properties
}

func (f *AthenaPreparedStatement) Remove() error {

_, err := f.svc.DeletePreparedStatement(&athena.DeletePreparedStatementInput{
StatementName: f.name,
WorkGroup: f.workGroup,
})

return err
}

func (f *AthenaPreparedStatement) String() string {
return *f.name
}
Loading