forked from rebuy-de/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from MikeSchouw/feature/redshift-automated-snap…
…shots add snapshot schedule
- Loading branch information
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |