Skip to content

Commit

Permalink
feat(neptune): support deletion protect, properties, tags
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Dec 13, 2024
1 parent e3a0fd3 commit 2cc68d0
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 17 deletions.
49 changes: 42 additions & 7 deletions resources/neptune-cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package resources

import (
"context"
"fmt"

"github.com/gotidy/ptr"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/neptune"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
libsettings "github.com/ekristen/libnuke/pkg/settings"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/v3/pkg/nuke"
Expand All @@ -21,6 +25,12 @@ func init() {
Scope: nuke.Account,
Resource: &NeptuneCluster{},
Lister: &NeptuneClusterLister{},
DependsOn: []string{
NeptuneInstanceResource,
},
Settings: []string{
"DisableDeletionProtection",
},
})
}

Expand Down Expand Up @@ -54,9 +64,10 @@ func (l *NeptuneClusterLister) List(_ context.Context, o interface{}) ([]resourc
}

resources = append(resources, &NeptuneCluster{
svc: svc,
ID: dbCluster.DBClusterIdentifier,
Tags: dbTags,
svc: svc,
ID: dbCluster.DBClusterIdentifier,
Status: dbCluster.Status,
Tags: dbTags,
})
}

Expand All @@ -71,15 +82,39 @@ func (l *NeptuneClusterLister) List(_ context.Context, o interface{}) ([]resourc
}

type NeptuneCluster struct {
svc *neptune.Neptune
ID *string
Tags []*neptune.Tag
svc *neptune.Neptune
settings *libsettings.Setting

ID *string
Status *string
Tags []*neptune.Tag
}

func (r *NeptuneCluster) Settings(settings *libsettings.Setting) {
r.settings = settings
}

func (r *NeptuneCluster) Filter() error {
if ptr.ToString(r.Status) == "deleting" {
return fmt.Errorf("already deleting")
}
return nil
}

func (r *NeptuneCluster) Remove(_ context.Context) error {
if r.settings.GetBool("DisableDeletionProtection") {
_, err := r.svc.ModifyDBCluster(&neptune.ModifyDBClusterInput{
DBClusterIdentifier: r.ID,
DeletionProtection: ptr.Bool(false),
})
if err != nil {
return err
}
}

_, err := r.svc.DeleteDBCluster(&neptune.DeleteDBClusterInput{
DBClusterIdentifier: r.ID,
SkipFinalSnapshot: aws.Bool(true),
SkipFinalSnapshot: ptr.Bool(true),
})

return err
Expand Down
62 changes: 52 additions & 10 deletions resources/neptune-instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ package resources

import (
"context"

"fmt"
"github.com/gotidy/ptr"
"github.com/sirupsen/logrus"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/neptune"

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
libsettings "github.com/ekristen/libnuke/pkg/settings"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/v3/pkg/nuke"
Expand All @@ -23,6 +25,10 @@ func init() {
Scope: nuke.Account,
Resource: &NeptuneInstance{},
Lister: &NeptuneInstanceLister{},
Settings: []string{
"DisableClusterDeletionProtection",
"DisableDeletionProtection",
},
})
}

Expand Down Expand Up @@ -63,10 +69,12 @@ func (l *NeptuneInstanceLister) List(_ context.Context, o interface{}) ([]resour
}

resources = append(resources, &NeptuneInstance{
svc: svc,
ID: dbInstance.DBInstanceIdentifier,
Name: dbInstance.DBName,
Tags: dbTags,
svc: svc,
ID: dbInstance.DBInstanceIdentifier,
ClusterID: dbInstance.DBClusterIdentifier,
Name: dbInstance.DBName,
Status: dbInstance.DBInstanceStatus,
Tags: dbTags,
})
}

Expand All @@ -81,16 +89,50 @@ func (l *NeptuneInstanceLister) List(_ context.Context, o interface{}) ([]resour
}

type NeptuneInstance struct {
svc *neptune.Neptune
ID *string
Name *string
Tags []*neptune.Tag
svc *neptune.Neptune
settings *libsettings.Setting
ID *string
ClusterID *string
Name *string
Status *string
Tags []*neptune.Tag
}

func (r *NeptuneInstance) Settings(settings *libsettings.Setting) {
r.settings = settings
}

func (r *NeptuneInstance) Filter() error {
if ptr.ToString(r.Status) == "deleting" {
return fmt.Errorf("already deleting")
}
return nil
}

func (r *NeptuneInstance) Remove(_ context.Context) error {
if r.settings.GetBool("DisableClusterDeletionProtection") {
_, err := r.svc.ModifyDBCluster(&neptune.ModifyDBClusterInput{
DBClusterIdentifier: r.ClusterID,
DeletionProtection: ptr.Bool(false),
})
if err != nil {
return err
}
}

if r.settings.GetBool("DisableDeletionProtection") {
_, err := r.svc.ModifyDBInstance(&neptune.ModifyDBInstanceInput{
DBInstanceIdentifier: r.ID,
DeletionProtection: ptr.Bool(false),
})
if err != nil {
return err
}
}

_, err := r.svc.DeleteDBInstance(&neptune.DeleteDBInstanceInput{
DBInstanceIdentifier: r.ID,
SkipFinalSnapshot: aws.Bool(true),
SkipFinalSnapshot: ptr.Bool(true),
})

return err
Expand Down

0 comments on commit 2cc68d0

Please sign in to comment.