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