Skip to content

Commit

Permalink
refactor(redshift-snapshot-schedule): convert to libnuke resource format
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Sep 30, 2024
1 parent 24b5532 commit b12f63a
Showing 1 changed file with 45 additions and 23 deletions.
68 changes: 45 additions & 23 deletions resources/redshift-snapshot-schedule.go
Original file line number Diff line number Diff line change
@@ -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(&registry.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),
Expand All @@ -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,
})
}
Expand All @@ -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),
})
Expand All @@ -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
}

0 comments on commit b12f63a

Please sign in to comment.