From 39f4f9540dfb74725af3db269a6614e47c68744e Mon Sep 17 00:00:00 2001 From: Maarten Dirkse Date: Fri, 23 Feb 2024 15:34:59 +0100 Subject: [PATCH 1/2] Add support for Redshift Serverless namespaces, snapshots and workgroups. (#1194) --- resources/redshiftserverless-namespaces.go | 68 +++++++++++++++++++++ resources/redshiftserverless-snapshots.go | 69 ++++++++++++++++++++++ resources/redshiftserverless-workgroups.go | 69 ++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 resources/redshiftserverless-namespaces.go create mode 100644 resources/redshiftserverless-snapshots.go create mode 100644 resources/redshiftserverless-workgroups.go diff --git a/resources/redshiftserverless-namespaces.go b/resources/redshiftserverless-namespaces.go new file mode 100644 index 00000000..adfaae07 --- /dev/null +++ b/resources/redshiftserverless-namespaces.go @@ -0,0 +1,68 @@ +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/redshiftserverless" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type RedshiftServerlessNamespace struct { + svc *redshiftserverless.RedshiftServerless + namespace *redshiftserverless.Namespace +} + +func init() { + register("RedshiftServerlessNamespace", ListRedshiftServerlessNamespaces) +} + +func ListRedshiftServerlessNamespaces(sess *session.Session) ([]Resource, error) { + svc := redshiftserverless.New(sess) + resources := []Resource{} + + params := &redshiftserverless.ListNamespacesInput{ + MaxResults: aws.Int64(100), + } + + for { + output, err := svc.ListNamespaces(params) + if err != nil { + return nil, err + } + + for _, namespace := range output.Namespaces { + resources = append(resources, &RedshiftServerlessNamespace{ + svc: svc, + namespace: namespace, + }) + } + + if output.NextToken == nil { + break + } + + params.NextToken = output.NextToken + } + + return resources, nil +} + +func (n *RedshiftServerlessNamespace) Properties() types.Properties { + properties := types.NewProperties(). + Set("CreationDate", n.namespace.CreationDate). + Set("NamespaceName", n.namespace.NamespaceName) + + return properties +} + +func (n *RedshiftServerlessNamespace) Remove() error { + _, err := n.svc.DeleteNamespace(&redshiftserverless.DeleteNamespaceInput{ + NamespaceName: n.namespace.NamespaceName, + }) + + return err +} + +func (n *RedshiftServerlessNamespace) String() string { + return *n.namespace.NamespaceName +} diff --git a/resources/redshiftserverless-snapshots.go b/resources/redshiftserverless-snapshots.go new file mode 100644 index 00000000..9f56c8e6 --- /dev/null +++ b/resources/redshiftserverless-snapshots.go @@ -0,0 +1,69 @@ +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/redshiftserverless" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type RedshiftServerlessSnapshot struct { + svc *redshiftserverless.RedshiftServerless + snapshot *redshiftserverless.Snapshot +} + +func init() { + register("RedshiftServerlessSnapshot", ListRedshiftServerlessSnapshots) +} + +func ListRedshiftServerlessSnapshots(sess *session.Session) ([]Resource, error) { + svc := redshiftserverless.New(sess) + resources := []Resource{} + + params := &redshiftserverless.ListSnapshotsInput{ + MaxResults: aws.Int64(100), + } + + for { + output, err := svc.ListSnapshots(params) + if err != nil { + return nil, err + } + + for _, snapshot := range output.Snapshots { + resources = append(resources, &RedshiftServerlessSnapshot{ + svc: svc, + snapshot: snapshot, + }) + } + + if output.NextToken == nil { + break + } + + params.NextToken = output.NextToken + } + + return resources, nil +} + +func (s *RedshiftServerlessSnapshot) Properties() types.Properties { + properties := types.NewProperties(). + Set("CreateTime", s.snapshot.SnapshotCreateTime). + Set("Namespace", s.snapshot.NamespaceName). + Set("SnapshotName", s.snapshot.SnapshotName) + + return properties +} + +func (s *RedshiftServerlessSnapshot) Remove() error { + _, err := s.svc.DeleteSnapshot(&redshiftserverless.DeleteSnapshotInput{ + SnapshotName: s.snapshot.SnapshotName, + }) + + return err +} + +func (s *RedshiftServerlessSnapshot) String() string { + return *s.snapshot.SnapshotName +} diff --git a/resources/redshiftserverless-workgroups.go b/resources/redshiftserverless-workgroups.go new file mode 100644 index 00000000..a88c8d16 --- /dev/null +++ b/resources/redshiftserverless-workgroups.go @@ -0,0 +1,69 @@ +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/redshiftserverless" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type RedshiftServerlessWorkgroup struct { + svc *redshiftserverless.RedshiftServerless + workgroup *redshiftserverless.Workgroup +} + +func init() { + register("RedshiftServerlessWorkgroup", ListRedshiftServerlessWorkgroups) +} + +func ListRedshiftServerlessWorkgroups(sess *session.Session) ([]Resource, error) { + svc := redshiftserverless.New(sess) + resources := []Resource{} + + params := &redshiftserverless.ListWorkgroupsInput{ + MaxResults: aws.Int64(100), + } + + for { + output, err := svc.ListWorkgroups(params) + if err != nil { + return nil, err + } + + for _, workgroup := range output.Workgroups { + resources = append(resources, &RedshiftServerlessWorkgroup{ + svc: svc, + workgroup: workgroup, + }) + } + + if output.NextToken == nil { + break + } + + params.NextToken = output.NextToken + } + + return resources, nil +} + +func (w *RedshiftServerlessWorkgroup) Properties() types.Properties { + properties := types.NewProperties(). + Set("CreationDate", w.workgroup.CreationDate). + Set("Namespace", w.workgroup.NamespaceName). + Set("WorkgroupName", w.workgroup.WorkgroupName) + + return properties +} + +func (w *RedshiftServerlessWorkgroup) Remove() error { + _, err := w.svc.DeleteWorkgroup(&redshiftserverless.DeleteWorkgroupInput{ + WorkgroupName: w.workgroup.WorkgroupName, + }) + + return err +} + +func (w *RedshiftServerlessWorkgroup) String() string { + return *w.workgroup.WorkgroupName +} From 28d08c15c36142a2fb4c1f874c358f626189b295 Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Fri, 23 Feb 2024 07:51:52 -0700 Subject: [PATCH 2/2] chore: migrate to aws-nuke-v3/libnuke resource format --- resources/redshiftserverless-namespaces.go | 34 +++++++++++++++++----- resources/redshiftserverless-snapshots.go | 34 +++++++++++++++++----- resources/redshiftserverless-workgroups.go | 34 +++++++++++++++++----- 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/resources/redshiftserverless-namespaces.go b/resources/redshiftserverless-namespaces.go index adfaae07..84304073 100644 --- a/resources/redshiftserverless-namespaces.go +++ b/resources/redshiftserverless-namespaces.go @@ -1,10 +1,18 @@ package resources import ( + "context" + "github.com/gotidy/ptr" + "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/redshiftserverless" - "github.com/rebuy-de/aws-nuke/v2/pkg/types" + + "github.com/ekristen/libnuke/pkg/registry" + "github.com/ekristen/libnuke/pkg/resource" + "github.com/ekristen/libnuke/pkg/types" + + "github.com/ekristen/aws-nuke/pkg/nuke" ) type RedshiftServerlessNamespace struct { @@ -12,13 +20,23 @@ type RedshiftServerlessNamespace struct { namespace *redshiftserverless.Namespace } +const RedshiftServerlessNamespaceResource = "RedshiftServerlessNamespace" + func init() { - register("RedshiftServerlessNamespace", ListRedshiftServerlessNamespaces) + registry.Register(®istry.Registration{ + Name: RedshiftServerlessNamespaceResource, + Scope: nuke.Account, + Lister: &RedshiftServerlessNamespaceLister{}, + }) } -func ListRedshiftServerlessNamespaces(sess *session.Session) ([]Resource, error) { - svc := redshiftserverless.New(sess) - resources := []Resource{} +type RedshiftServerlessNamespaceLister struct{} + +func (l *RedshiftServerlessNamespaceLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { + opts := o.(*nuke.ListerOpts) + + svc := redshiftserverless.New(opts.Session) + resources := make([]resource.Resource, 0) params := &redshiftserverless.ListNamespacesInput{ MaxResults: aws.Int64(100), @@ -55,7 +73,7 @@ func (n *RedshiftServerlessNamespace) Properties() types.Properties { return properties } -func (n *RedshiftServerlessNamespace) Remove() error { +func (n *RedshiftServerlessNamespace) Remove(_ context.Context) error { _, err := n.svc.DeleteNamespace(&redshiftserverless.DeleteNamespaceInput{ NamespaceName: n.namespace.NamespaceName, }) @@ -64,5 +82,5 @@ func (n *RedshiftServerlessNamespace) Remove() error { } func (n *RedshiftServerlessNamespace) String() string { - return *n.namespace.NamespaceName + return ptr.ToString(n.namespace.NamespaceName) } diff --git a/resources/redshiftserverless-snapshots.go b/resources/redshiftserverless-snapshots.go index 9f56c8e6..d8209260 100644 --- a/resources/redshiftserverless-snapshots.go +++ b/resources/redshiftserverless-snapshots.go @@ -1,10 +1,18 @@ package resources import ( + "context" + "github.com/gotidy/ptr" + "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/redshiftserverless" - "github.com/rebuy-de/aws-nuke/v2/pkg/types" + + "github.com/ekristen/libnuke/pkg/registry" + "github.com/ekristen/libnuke/pkg/resource" + "github.com/ekristen/libnuke/pkg/types" + + "github.com/ekristen/aws-nuke/pkg/nuke" ) type RedshiftServerlessSnapshot struct { @@ -12,13 +20,23 @@ type RedshiftServerlessSnapshot struct { snapshot *redshiftserverless.Snapshot } +const RedshiftServerlessSnapshotResource = "RedshiftServerlessSnapshot" + func init() { - register("RedshiftServerlessSnapshot", ListRedshiftServerlessSnapshots) + registry.Register(®istry.Registration{ + Name: RedshiftServerlessSnapshotResource, + Scope: nuke.Account, + Lister: &RedshiftServerlessSnapshotLister{}, + }) } -func ListRedshiftServerlessSnapshots(sess *session.Session) ([]Resource, error) { - svc := redshiftserverless.New(sess) - resources := []Resource{} +type RedshiftServerlessSnapshotLister struct{} + +func (l *RedshiftServerlessSnapshotLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { + opts := o.(*nuke.ListerOpts) + + svc := redshiftserverless.New(opts.Session) + resources := make([]resource.Resource, 0) params := &redshiftserverless.ListSnapshotsInput{ MaxResults: aws.Int64(100), @@ -56,7 +74,7 @@ func (s *RedshiftServerlessSnapshot) Properties() types.Properties { return properties } -func (s *RedshiftServerlessSnapshot) Remove() error { +func (s *RedshiftServerlessSnapshot) Remove(_ context.Context) error { _, err := s.svc.DeleteSnapshot(&redshiftserverless.DeleteSnapshotInput{ SnapshotName: s.snapshot.SnapshotName, }) @@ -65,5 +83,5 @@ func (s *RedshiftServerlessSnapshot) Remove() error { } func (s *RedshiftServerlessSnapshot) String() string { - return *s.snapshot.SnapshotName + return ptr.ToString(s.snapshot.SnapshotName) } diff --git a/resources/redshiftserverless-workgroups.go b/resources/redshiftserverless-workgroups.go index a88c8d16..29e8d217 100644 --- a/resources/redshiftserverless-workgroups.go +++ b/resources/redshiftserverless-workgroups.go @@ -1,10 +1,18 @@ package resources import ( + "context" + "github.com/gotidy/ptr" + "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/redshiftserverless" - "github.com/rebuy-de/aws-nuke/v2/pkg/types" + + "github.com/ekristen/libnuke/pkg/registry" + "github.com/ekristen/libnuke/pkg/resource" + "github.com/ekristen/libnuke/pkg/types" + + "github.com/ekristen/aws-nuke/pkg/nuke" ) type RedshiftServerlessWorkgroup struct { @@ -12,13 +20,23 @@ type RedshiftServerlessWorkgroup struct { workgroup *redshiftserverless.Workgroup } +const RedshiftServerlessWorkgroupResource = "RedshiftServerlessWorkgroup" + func init() { - register("RedshiftServerlessWorkgroup", ListRedshiftServerlessWorkgroups) + registry.Register(®istry.Registration{ + Name: RedshiftServerlessWorkgroupResource, + Scope: nuke.Account, + Lister: &RedshiftServerlessWorkgroupLister{}, + }) } -func ListRedshiftServerlessWorkgroups(sess *session.Session) ([]Resource, error) { - svc := redshiftserverless.New(sess) - resources := []Resource{} +type RedshiftServerlessWorkgroupLister struct{} + +func (l *RedshiftServerlessWorkgroupLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { + opts := o.(*nuke.ListerOpts) + + svc := redshiftserverless.New(opts.Session) + resources := make([]resource.Resource, 0) params := &redshiftserverless.ListWorkgroupsInput{ MaxResults: aws.Int64(100), @@ -56,7 +74,7 @@ func (w *RedshiftServerlessWorkgroup) Properties() types.Properties { return properties } -func (w *RedshiftServerlessWorkgroup) Remove() error { +func (w *RedshiftServerlessWorkgroup) Remove(_ context.Context) error { _, err := w.svc.DeleteWorkgroup(&redshiftserverless.DeleteWorkgroupInput{ WorkgroupName: w.workgroup.WorkgroupName, }) @@ -65,5 +83,5 @@ func (w *RedshiftServerlessWorkgroup) Remove() error { } func (w *RedshiftServerlessWorkgroup) String() string { - return *w.workgroup.WorkgroupName + return ptr.ToString(w.workgroup.WorkgroupName) }