|
| 1 | +package resources |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "slices" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/gotidy/ptr" |
| 10 | + |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/dsql" |
| 12 | + dsqltypes "github.com/aws/aws-sdk-go-v2/service/dsql/types" |
| 13 | + |
| 14 | + "github.com/ekristen/libnuke/pkg/registry" |
| 15 | + "github.com/ekristen/libnuke/pkg/resource" |
| 16 | + libsettings "github.com/ekristen/libnuke/pkg/settings" |
| 17 | + "github.com/ekristen/libnuke/pkg/types" |
| 18 | + |
| 19 | + "github.com/ekristen/aws-nuke/v3/pkg/nuke" |
| 20 | +) |
| 21 | + |
| 22 | +const DSQLClusterResource = "DSQLCluster" |
| 23 | + |
| 24 | +func init() { |
| 25 | + registry.Register(®istry.Registration{ |
| 26 | + Name: DSQLClusterResource, |
| 27 | + Scope: nuke.Account, |
| 28 | + Resource: &DSQLCluster{}, |
| 29 | + Lister: &DSQLClusterLister{}, |
| 30 | + Settings: []string{ |
| 31 | + "DisableDeletionProtection", |
| 32 | + }, |
| 33 | + }) |
| 34 | +} |
| 35 | + |
| 36 | +type DSQLClusterLister struct{} |
| 37 | + |
| 38 | +func (l *DSQLClusterLister) List(ctx context.Context, o interface{}) ([]resource.Resource, error) { |
| 39 | + opts := o.(*nuke.ListerOpts) |
| 40 | + svc := dsql.NewFromConfig(*opts.Config) |
| 41 | + var resources []resource.Resource |
| 42 | + |
| 43 | + if !l.IsSupportedRegion(opts.Region.Name) { |
| 44 | + return resources, nil |
| 45 | + } |
| 46 | + |
| 47 | + params := &dsql.ListClustersInput{ |
| 48 | + MaxResults: ptr.Int32(100), |
| 49 | + } |
| 50 | + |
| 51 | + for { |
| 52 | + res, err := svc.ListClusters(ctx, params) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + for _, clusterSummary := range res.Clusters { |
| 58 | + // get additional cluster metadata not returned in ListClusters |
| 59 | + cluster, err := svc.GetCluster(ctx, &dsql.GetClusterInput{ |
| 60 | + Identifier: clusterSummary.Identifier, |
| 61 | + }) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + // get cluster tags |
| 66 | + var tags map[string]string |
| 67 | + tagsRes, err := svc.ListTagsForResource(ctx, &dsql.ListTagsForResourceInput{ |
| 68 | + ResourceArn: clusterSummary.Arn, |
| 69 | + }) |
| 70 | + if err != nil { |
| 71 | + opts.Logger.Warnf("unable to fetch tags for dsql cluster: %s", ptr.ToString(clusterSummary.Arn)) |
| 72 | + } else { |
| 73 | + tags = tagsRes.Tags |
| 74 | + } |
| 75 | + |
| 76 | + resources = append(resources, &DSQLCluster{ |
| 77 | + svc: svc, |
| 78 | + Arn: clusterSummary.Arn, |
| 79 | + CreationTime: cluster.CreationTime, |
| 80 | + DeletionProtectionEnabled: cluster.DeletionProtectionEnabled, |
| 81 | + Identifier: clusterSummary.Identifier, |
| 82 | + Status: cluster.Status, |
| 83 | + Tags: tags, |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + if res.NextToken == nil { |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + params.NextToken = res.NextToken |
| 92 | + } |
| 93 | + |
| 94 | + return resources, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (l *DSQLClusterLister) IsSupportedRegion(region string) bool { |
| 98 | + // https://aws.amazon.com/rds/aurora/dsql/faqs/#:~:text=available%20in%20all-,AWS%20Regions,-%3F |
| 99 | + // NOTE: us-west-2 (Oregon) is available as a witness region, but clusters cannot be created in this region |
| 100 | + supportedRegions := []string{ |
| 101 | + "us-east-1", |
| 102 | + "us-east-2", |
| 103 | + } |
| 104 | + |
| 105 | + return slices.Contains(supportedRegions, region) |
| 106 | +} |
| 107 | + |
| 108 | +type DSQLCluster struct { |
| 109 | + svc *dsql.Client |
| 110 | + settings *libsettings.Setting |
| 111 | + Arn *string |
| 112 | + CreationTime *time.Time |
| 113 | + DeletionProtectionEnabled *bool |
| 114 | + Identifier *string |
| 115 | + Status dsqltypes.ClusterStatus |
| 116 | + Tags map[string]string |
| 117 | +} |
| 118 | + |
| 119 | +func (r *DSQLCluster) Remove(ctx context.Context) error { |
| 120 | + err := r.RemoveDeletionProtection(ctx) |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + _, err = r.svc.DeleteCluster(ctx, &dsql.DeleteClusterInput{ |
| 126 | + Identifier: r.Identifier, |
| 127 | + }) |
| 128 | + |
| 129 | + return err |
| 130 | +} |
| 131 | + |
| 132 | +func (r *DSQLCluster) Filter() error { |
| 133 | + if r.Status == dsqltypes.ClusterStatusDeleted { |
| 134 | + return errors.New("dsql cluster already deleted") |
| 135 | + } |
| 136 | + |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +func (r *DSQLCluster) RemoveDeletionProtection(ctx context.Context) error { |
| 141 | + if !r.settings.GetBool("DisableDeletionProtection") { |
| 142 | + return nil |
| 143 | + } |
| 144 | + |
| 145 | + _, err := r.svc.UpdateCluster(ctx, &dsql.UpdateClusterInput{ |
| 146 | + Identifier: r.Identifier, |
| 147 | + DeletionProtectionEnabled: ptr.Bool(false), |
| 148 | + }) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func (r *DSQLCluster) Settings(settings *libsettings.Setting) { |
| 157 | + r.settings = settings |
| 158 | +} |
| 159 | + |
| 160 | +func (r *DSQLCluster) Properties() types.Properties { |
| 161 | + return types.NewPropertiesFromStruct(r) |
| 162 | +} |
| 163 | + |
| 164 | +func (r *DSQLCluster) String() string { |
| 165 | + return *r.Arn |
| 166 | +} |
0 commit comments