From 52d581ebe75a92d5ed6957733456c34094ea8425 Mon Sep 17 00:00:00 2001 From: Mike Schouw Date: Wed, 30 Aug 2023 15:43:27 +0200 Subject: [PATCH 1/3] add 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 000000000..c0855f500 --- /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 dd2be49755ece7ac993a0c1fe7d1c6ad68d721ad Mon Sep 17 00:00:00 2001 From: Mike Schouw Date: Sat, 18 Nov 2023 14:09:02 +0100 Subject: [PATCH 2/3] add cloudfront cache policy --- resources/cloudfront-cache-policy.go | 68 ++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 resources/cloudfront-cache-policy.go diff --git a/resources/cloudfront-cache-policy.go b/resources/cloudfront-cache-policy.go new file mode 100644 index 000000000..2732beeec --- /dev/null +++ b/resources/cloudfront-cache-policy.go @@ -0,0 +1,68 @@ +package resources + +import ( + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/cloudfront" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CloudFrontCachePolicy struct { + svc *cloudfront.CloudFront + ID *string +} + +func init() { + register("CloudFrontCachePolicy", ListCloudFrontCachePolicy) +} + +func ListCloudFrontCachePolicy(sess *session.Session) ([]Resource, error) { + svc := cloudfront.New(sess) + resources := []Resource{} + params := &cloudfront.ListCachePoliciesInput{} + + for { + resp, err := svc.ListCachePolicies(params) + if err != nil { + return nil, err + } + + for _, item := range resp.CachePolicyList.Items { + if *item.Type == "custom" { + resources = append(resources, &CloudFrontCachePolicy{ + svc: svc, + ID: item.CachePolicy.Id, + }) + } + } + + if resp.CachePolicyList.NextMarker == nil { + break + } + + params.Marker = resp.CachePolicyList.NextMarker + } + + return resources, nil +} + +func (f *CloudFrontCachePolicy) Remove() error { + resp, err := f.svc.GetCachePolicy(&cloudfront.GetCachePolicyInput{ + Id: f.ID, + }) + if err != nil { + return err + } + + _, err = f.svc.DeleteCachePolicy(&cloudfront.DeleteCachePolicyInput{ + Id: f.ID, + IfMatch: resp.ETag, + }) + + return err +} + +func (f *CloudFrontCachePolicy) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("ID", f.ID) + return properties +} From 10425001caa53c16c53740d32450cb1d9626d0e8 Mon Sep 17 00:00:00 2001 From: Mike Schouw Date: Sat, 18 Nov 2023 14:16:48 +0100 Subject: [PATCH 3/3] clean up --- resources/redshift-snapshot-schedule.go | 81 ------------------------- 1 file changed, 81 deletions(-) delete mode 100644 resources/redshift-snapshot-schedule.go diff --git a/resources/redshift-snapshot-schedule.go b/resources/redshift-snapshot-schedule.go deleted file mode 100644 index c0855f500..000000000 --- a/resources/redshift-snapshot-schedule.go +++ /dev/null @@ -1,81 +0,0 @@ -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 -}