From 24b5532ab55a9fdf24d073d9c6667b8a3517f903 Mon Sep 17 00:00:00 2001 From: Mike Schouw Date: Wed, 30 Aug 2023 15:43:27 +0200 Subject: [PATCH 1/3] feat(resource): add redshift snapshot schedule --- resources/redshift-snapshot-schedule.go | 81 +++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 resources/redshift-snapshot-schedule.go diff --git a/resources/redshift-snapshot-schedule.go b/resources/redshift-snapshot-schedule.go new file mode 100644 index 00000000..c0855f50 --- /dev/null +++ b/resources/redshift-snapshot-schedule.go @@ -0,0 +1,81 @@ +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/redshift" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type RedshiftSnapshotSchedule struct { + svc *redshift.Redshift + scheduleID *string + associatedClusters []*redshift.ClusterAssociatedToSchedule +} + +func init() { + register("RedshiftSnapshotSchedule", ListRedshiftSnapshotSchedule) +} + +func ListRedshiftSnapshotSchedule(sess *session.Session) ([]Resource, error) { + svc := redshift.New(sess) + resources := []Resource{} + + params := &redshift.DescribeSnapshotSchedulesInput{ + MaxRecords: aws.Int64(100), + } + + for { + output, err := svc.DescribeSnapshotSchedules(params) + if err != nil { + return nil, err + } + + for _, snapshotSchedule := range output.SnapshotSchedules { + resources = append(resources, &RedshiftSnapshotSchedule{ + svc: svc, + scheduleID: snapshotSchedule.ScheduleIdentifier, + associatedClusters: snapshotSchedule.AssociatedClusters, + }) + } + + if output.Marker == nil { + break + } + + params.Marker = output.Marker + } + + return resources, nil +} + +func (f *RedshiftSnapshotSchedule) Properties() types.Properties { + associatedClusters := make([]string, len(f.associatedClusters)) + for i, cluster := range f.associatedClusters { + associatedClusters[i] = *cluster.ClusterIdentifier + } + properties := types.NewProperties() + properties.Set("scheduleID", f.scheduleID) + properties.Set("associatedClusters", associatedClusters) + return properties +} + +func (f *RedshiftSnapshotSchedule) Remove() error { + for _, associatedCluster := range f.associatedClusters { + _, disassociateErr := f.svc.ModifyClusterSnapshotSchedule(&redshift.ModifyClusterSnapshotScheduleInput{ + ScheduleIdentifier: f.scheduleID, + ClusterIdentifier: associatedCluster.ClusterIdentifier, + DisassociateSchedule: aws.Bool(true), + }) + + if disassociateErr != nil { + return disassociateErr + } + } + + _, err := f.svc.DeleteSnapshotSchedule(&redshift.DeleteSnapshotScheduleInput{ + ScheduleIdentifier: f.scheduleID, + }) + + return err +} From b12f63a66c000056da55a43d476e85c5ae9cc9a9 Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Mon, 30 Sep 2024 17:06:57 -0600 Subject: [PATCH 2/3] refactor(redshift-snapshot-schedule): convert to libnuke resource format --- resources/redshift-snapshot-schedule.go | 68 ++++++++++++++++--------- 1 file changed, 45 insertions(+), 23 deletions(-) diff --git a/resources/redshift-snapshot-schedule.go b/resources/redshift-snapshot-schedule.go index c0855f50..760c363d 100644 --- a/resources/redshift-snapshot-schedule.go +++ b/resources/redshift-snapshot-schedule.go @@ -1,25 +1,35 @@ package resources import ( + "context" + "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/redshift" - "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/v3/pkg/nuke" ) -type RedshiftSnapshotSchedule struct { - svc *redshift.Redshift - scheduleID *string - associatedClusters []*redshift.ClusterAssociatedToSchedule -} +const RedshiftSnapshotScheduleResource = "RedshiftSnapshotSchedule" func init() { - register("RedshiftSnapshotSchedule", ListRedshiftSnapshotSchedule) + registry.Register(®istry.Registration{ + Name: RedshiftSnapshotScheduleResource, + Scope: nuke.Account, + Lister: &RedshiftSnapshotScheduleLister{}, + }) } -func ListRedshiftSnapshotSchedule(sess *session.Session) ([]Resource, error) { - svc := redshift.New(sess) - resources := []Resource{} +type RedshiftSnapshotScheduleLister struct{} + +func (l *RedshiftSnapshotScheduleLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) { + opts := o.(*nuke.ListerOpts) + + svc := redshift.New(opts.Session) + resources := make([]resource.Resource, 0) params := &redshift.DescribeSnapshotSchedulesInput{ MaxRecords: aws.Int64(100), @@ -34,7 +44,8 @@ func ListRedshiftSnapshotSchedule(sess *session.Session) ([]Resource, error) { for _, snapshotSchedule := range output.SnapshotSchedules { resources = append(resources, &RedshiftSnapshotSchedule{ svc: svc, - scheduleID: snapshotSchedule.ScheduleIdentifier, + ID: snapshotSchedule.ScheduleIdentifier, + Tags: snapshotSchedule.Tags, associatedClusters: snapshotSchedule.AssociatedClusters, }) } @@ -49,21 +60,28 @@ func ListRedshiftSnapshotSchedule(sess *session.Session) ([]Resource, error) { return resources, nil } -func (f *RedshiftSnapshotSchedule) Properties() types.Properties { - associatedClusters := make([]string, len(f.associatedClusters)) - for i, cluster := range f.associatedClusters { +type RedshiftSnapshotSchedule struct { + svc *redshift.Redshift + ID *string + Tags []*redshift.Tag + associatedClusters []*redshift.ClusterAssociatedToSchedule +} + +func (r *RedshiftSnapshotSchedule) Properties() types.Properties { + associatedClusters := make([]string, len(r.associatedClusters)) + for i, cluster := range r.associatedClusters { associatedClusters[i] = *cluster.ClusterIdentifier } properties := types.NewProperties() - properties.Set("scheduleID", f.scheduleID) - properties.Set("associatedClusters", associatedClusters) + properties.Set("ID", r.ID) + properties.Set("AssociatedClusters", associatedClusters) return properties } -func (f *RedshiftSnapshotSchedule) Remove() error { - for _, associatedCluster := range f.associatedClusters { - _, disassociateErr := f.svc.ModifyClusterSnapshotSchedule(&redshift.ModifyClusterSnapshotScheduleInput{ - ScheduleIdentifier: f.scheduleID, +func (r *RedshiftSnapshotSchedule) Remove(_ context.Context) error { + for _, associatedCluster := range r.associatedClusters { + _, disassociateErr := r.svc.ModifyClusterSnapshotSchedule(&redshift.ModifyClusterSnapshotScheduleInput{ + ScheduleIdentifier: r.ID, ClusterIdentifier: associatedCluster.ClusterIdentifier, DisassociateSchedule: aws.Bool(true), }) @@ -73,9 +91,13 @@ func (f *RedshiftSnapshotSchedule) Remove() error { } } - _, err := f.svc.DeleteSnapshotSchedule(&redshift.DeleteSnapshotScheduleInput{ - ScheduleIdentifier: f.scheduleID, + _, err := r.svc.DeleteSnapshotSchedule(&redshift.DeleteSnapshotScheduleInput{ + ScheduleIdentifier: r.ID, }) return err } + +func (r *RedshiftSnapshotSchedule) String() string { + return *r.ID +} From b7731991db65c320267f256010c5e201be97e21b Mon Sep 17 00:00:00 2001 From: Erik Kristensen Date: Mon, 30 Sep 2024 17:07:28 -0600 Subject: [PATCH 3/3] feat(redshift-snapshot-schedule): add tags --- resources/redshift-snapshot-schedule.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/resources/redshift-snapshot-schedule.go b/resources/redshift-snapshot-schedule.go index 760c363d..52c421a6 100644 --- a/resources/redshift-snapshot-schedule.go +++ b/resources/redshift-snapshot-schedule.go @@ -75,6 +75,9 @@ func (r *RedshiftSnapshotSchedule) Properties() types.Properties { properties := types.NewProperties() properties.Set("ID", r.ID) properties.Set("AssociatedClusters", associatedClusters) + for _, tag := range r.Tags { + properties.SetTag(tag.Key, tag.Value) + } return properties }