From 2cc68d015c274566ff2cce8fde9fc6b261568837 Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Thu, 12 Dec 2024 18:02:05 -0700 Subject: [PATCH] feat(neptune): support deletion protect, properties, tags --- resources/neptune-cluster.go | 49 +++++++++++++++++++++++---- resources/neptune-instance.go | 62 +++++++++++++++++++++++++++++------ 2 files changed, 94 insertions(+), 17 deletions(-) diff --git a/resources/neptune-cluster.go b/resources/neptune-cluster.go index abdda876..a20abe26 100644 --- a/resources/neptune-cluster.go +++ b/resources/neptune-cluster.go @@ -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" @@ -21,6 +25,12 @@ func init() { Scope: nuke.Account, Resource: &NeptuneCluster{}, Lister: &NeptuneClusterLister{}, + DependsOn: []string{ + NeptuneInstanceResource, + }, + Settings: []string{ + "DisableDeletionProtection", + }, }) } @@ -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, }) } @@ -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 diff --git a/resources/neptune-instance.go b/resources/neptune-instance.go index 0d846b60..e889e6c6 100644 --- a/resources/neptune-instance.go +++ b/resources/neptune-instance.go @@ -2,7 +2,8 @@ package resources import ( "context" - + "fmt" + "github.com/gotidy/ptr" "github.com/sirupsen/logrus" "github.com/aws/aws-sdk-go/aws" @@ -10,6 +11,7 @@ import ( "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" @@ -23,6 +25,10 @@ func init() { Scope: nuke.Account, Resource: &NeptuneInstance{}, Lister: &NeptuneInstanceLister{}, + Settings: []string{ + "DisableClusterDeletionProtection", + "DisableDeletionProtection", + }, }) } @@ -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, }) } @@ -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