Skip to content

Commit

Permalink
CL-858 Adding Athena support for data catalogs and prepared statements
Browse files Browse the repository at this point in the history
  • Loading branch information
swhite-oreilly committed Mar 26, 2024
1 parent 89dc604 commit fd49a6d
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
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
}

0 comments on commit fd49a6d

Please sign in to comment.