|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/aws/aws-sdk-go/aws" |
| 5 | + "github.com/aws/aws-sdk-go/aws/session" |
| 6 | + "github.com/aws/aws-sdk-go/service/redshift" |
| 7 | + "github.com/gruntwork-io/cloud-nuke/config" |
| 8 | + "github.com/gruntwork-io/cloud-nuke/logging" |
| 9 | + "github.com/gruntwork-io/cloud-nuke/report" |
| 10 | + "github.com/gruntwork-io/cloud-nuke/telemetry" |
| 11 | + "github.com/gruntwork-io/go-commons/errors" |
| 12 | + commonTelemetry "github.com/gruntwork-io/go-commons/telemetry" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +func getAllRedshiftClusters(session *session.Session, region string, excludeAfter time.Time, configObj config.Config) ([]*string, error) { |
| 17 | + svc := redshift.New(session) |
| 18 | + var clusterIds []*string |
| 19 | + err := svc.DescribeClustersPages( |
| 20 | + &redshift.DescribeClustersInput{}, |
| 21 | + func(page *redshift.DescribeClustersOutput, lastPage bool) bool { |
| 22 | + for _, cluster := range page.Clusters { |
| 23 | + if shouldIncludeRedshiftCluster(cluster, excludeAfter, configObj) { |
| 24 | + clusterIds = append(clusterIds, cluster.ClusterIdentifier) |
| 25 | + } |
| 26 | + } |
| 27 | + return !lastPage |
| 28 | + }, |
| 29 | + ) |
| 30 | + return clusterIds, errors.WithStackTrace(err) |
| 31 | +} |
| 32 | + |
| 33 | +func shouldIncludeRedshiftCluster(cluster *redshift.Cluster, excludeAfter time.Time, configObj config.Config) bool { |
| 34 | + if cluster == nil { |
| 35 | + return false |
| 36 | + } |
| 37 | + if excludeAfter.Before(*cluster.ClusterCreateTime) { |
| 38 | + return false |
| 39 | + } |
| 40 | + return config.ShouldInclude( |
| 41 | + aws.StringValue(cluster.ClusterIdentifier), |
| 42 | + configObj.Redshift.IncludeRule.NamesRegExp, |
| 43 | + configObj.Redshift.ExcludeRule.NamesRegExp, |
| 44 | + ) |
| 45 | +} |
| 46 | + |
| 47 | +func nukeAllRedshiftClusters(session *session.Session, identifiers []*string) error { |
| 48 | + svc := redshift.New(session) |
| 49 | + if len(identifiers) == 0 { |
| 50 | + logging.Logger.Debugf("No Redshift Clusters to nuke in region %s", *session.Config.Region) |
| 51 | + return nil |
| 52 | + } |
| 53 | + logging.Logger.Debugf("Deleting all Redshift Clusters in region %s", *session.Config.Region) |
| 54 | + deletedIds := []*string{} |
| 55 | + for _, id := range identifiers { |
| 56 | + _, err := svc.DeleteCluster(&redshift.DeleteClusterInput{ClusterIdentifier: id, SkipFinalClusterSnapshot: aws.Bool(true)}) |
| 57 | + if err != nil { |
| 58 | + telemetry.TrackEvent(commonTelemetry.EventContext{ |
| 59 | + EventName: "Error Nuking RedshiftCluster", |
| 60 | + }, map[string]interface{}{ |
| 61 | + "region": *session.Config.Region, |
| 62 | + }) |
| 63 | + logging.Logger.Errorf("[Failed] %s: %s", *id, err) |
| 64 | + } else { |
| 65 | + deletedIds = append(deletedIds, id) |
| 66 | + logging.Logger.Debugf("Deleted Redshift Cluster: %s", aws.StringValue(id)) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if len(deletedIds) > 0 { |
| 71 | + for _, id := range deletedIds { |
| 72 | + err := svc.WaitUntilClusterDeleted(&redshift.DescribeClustersInput{ClusterIdentifier: id}) |
| 73 | + // Record status of this resource |
| 74 | + e := report.Entry{ |
| 75 | + Identifier: aws.StringValue(id), |
| 76 | + ResourceType: "Redshift Cluster", |
| 77 | + Error: err, |
| 78 | + } |
| 79 | + report.Record(e) |
| 80 | + if err != nil { |
| 81 | + telemetry.TrackEvent(commonTelemetry.EventContext{ |
| 82 | + EventName: "Error Nuking Redshift Cluster", |
| 83 | + }, map[string]interface{}{ |
| 84 | + "region": *session.Config.Region, |
| 85 | + }) |
| 86 | + logging.Logger.Errorf("[Failed] %s", err) |
| 87 | + return errors.WithStackTrace(err) |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + logging.Logger.Debugf("[OK] %d Redshift Cluster(s) deleted in %s", len(deletedIds), *session.Config.Region) |
| 92 | + return nil |
| 93 | +} |
0 commit comments