diff --git a/.github/release.yml b/.github/release.yml new file mode 100644 index 000000000..4d213e6f4 --- /dev/null +++ b/.github/release.yml @@ -0,0 +1,13 @@ +# https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes#configuring-automatically-generated-release-notes + +changelog: + categories: + - title: Notable changes + labels: + - '*' + exclude: + labels: + - dependencies + - title: Dependency updates + labels: + - dependencies diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 078944ac9..a62b68ca9 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -7,6 +7,7 @@ on: types: [opened, reopened, synchronize] schedule: - cron: '15 3 * * 0' + workflow_dispatch: jobs: build: @@ -14,9 +15,9 @@ jobs: runs-on: ubuntu-22.04 steps: - name: Setup Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: - go-version: '1.21' + go-version: '1.22' - name: Setup tools run: | go install golang.org/x/lint/golint@latest @@ -61,30 +62,30 @@ jobs: - name: Set up QEMU if: github.event_name != 'pull_request' id: qemu - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 with: platforms: arm64 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 with: install: true - name: Login to Docker Hub - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Login to Quay.io - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: quay.io username: ${{ secrets.QUAY_USERNAME }} password: ${{ secrets.QUAY_PASSWORD }} - name: Build and push - uses: docker/build-push-action@v3 + uses: docker/build-push-action@v5 with: context: . push: true diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 04a79068a..d48a0f4e8 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -22,7 +22,7 @@ jobs: sed -r -i "s/aws-nuke:v[0-9]+\.[0-9]+\.[0-9]+/aws-nuke:${{ github.ref_name }}/" README.md sed -r -i "s/aws-nuke-v[0-9]+\.[0-9]+\.[0-9]+/aws-nuke-${{ github.ref_name }}/" README.md sed -r -i "s/\/v[0-9]+\.[0-9]+\.[0-9]+\//\/${{ github.ref_name }}\//" README.md - - uses: peter-evans/create-pull-request@v5 + - uses: peter-evans/create-pull-request@v6 name: Create Pull Request with: title: Update readme for ${{ github.ref_name }} release @@ -36,9 +36,9 @@ jobs: runs-on: ubuntu-22.04 steps: - name: Setup Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: - go-version: '1.21' + go-version: '1.22' - name: Checkout code uses: actions/checkout@v4 with: @@ -73,30 +73,30 @@ jobs: - name: Set up QEMU id: qemu - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 with: platforms: arm64 - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 with: install: true - name: Login to Docker Hub - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Login to Quay.io - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: quay.io username: ${{ secrets.QUAY_USERNAME }} password: ${{ secrets.QUAY_PASSWORD }} - name: Build and push - uses: docker/build-push-action@v3 + uses: docker/build-push-action@v5 with: context: . push: true diff --git a/go.mod b/go.mod index a442f3855..2664a0f51 100644 --- a/go.mod +++ b/go.mod @@ -3,8 +3,8 @@ module github.com/rebuy-de/aws-nuke/v2 go 1.21 require ( - github.com/aws/aws-sdk-go v1.50.6 - github.com/fatih/color v1.16.0 + github.com/aws/aws-sdk-go v1.53.15 + github.com/fatih/color v1.17.0 github.com/golang/mock v1.6.0 github.com/google/uuid v1.6.0 github.com/mb0/glob v0.0.0-20160210091149-1eb79d2de6c4 @@ -12,8 +12,8 @@ require ( github.com/rebuy-de/rebuy-go-sdk/v4 v4.5.1 github.com/sirupsen/logrus v1.9.3 github.com/spf13/cobra v1.8.0 - github.com/stretchr/testify v1.8.4 - golang.org/x/sync v0.6.0 + github.com/stretchr/testify v1.9.0 + golang.org/x/sync v0.7.0 gopkg.in/yaml.v3 v3.0.1 ) @@ -28,7 +28,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect golang.org/x/mod v0.9.0 // indirect - golang.org/x/sys v0.14.0 // indirect + golang.org/x/sys v0.18.0 // indirect golang.org/x/tools v0.7.0 // indirect gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect ) diff --git a/go.sum b/go.sum index 3eb5fc38c..490f52452 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,12 @@ -github.com/aws/aws-sdk-go v1.50.6 h1:FaXvNwHG3Ri1paUEW16Ahk9zLVqSAdqa1M3phjZR35Q= -github.com/aws/aws-sdk-go v1.50.6/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= +github.com/aws/aws-sdk-go v1.53.15 h1:FtZmkg7xM8RfP2oY6p7xdKBYrRgkITk9yve2QV7N938= +github.com/aws/aws-sdk-go v1.53.15/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= -github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4= +github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI= github.com/gemnasium/logrus-graylog-hook/v3 v3.1.0 h1:SLtCnpI5ZZaz4l7RSatEhppB1BBhUEu+DqGANJzJdEA= github.com/gemnasium/logrus-graylog-hook/v3 v3.1.0/go.mod h1:wi1zWv9tIvyLSMLCAzgRP+YR24oLVQVBHfPPKjtht44= github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= @@ -52,8 +52,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= @@ -68,8 +68,8 @@ golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM= golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -79,8 +79,8 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= -golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/resources/cloudfront-response-headers-policies.go b/resources/cloudfront-response-headers-policies.go new file mode 100644 index 000000000..e075b5d6a --- /dev/null +++ b/resources/cloudfront-response-headers-policies.go @@ -0,0 +1,83 @@ +package resources + +import ( + "fmt" + "strings" + + "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 CloudFrontResponseHeadersPolicy struct { + svc *cloudfront.CloudFront + ID *string + name *string +} + +func init() { + register("CloudFrontResponseHeadersPolicy", ListCloudFrontResponseHeadersPolicies) +} + +func ListCloudFrontResponseHeadersPolicies(sess *session.Session) ([]Resource, error) { + svc := cloudfront.New(sess) + resources := []Resource{} + params := &cloudfront.ListResponseHeadersPoliciesInput{} + + for { + resp, err := svc.ListResponseHeadersPolicies(params) + if err != nil { + return nil, err + } + + for _, item := range resp.ResponseHeadersPolicyList.Items { + resources = append(resources, &CloudFrontResponseHeadersPolicy{ + svc: svc, + ID: item.ResponseHeadersPolicy.Id, + name: item.ResponseHeadersPolicy.ResponseHeadersPolicyConfig.Name, + }) + } + + if resp.ResponseHeadersPolicyList.NextMarker == nil { + break + } + + params.Marker = resp.ResponseHeadersPolicyList.NextMarker + } + + return resources, nil +} + +func (f *CloudFrontResponseHeadersPolicy) Filter() error { + if strings.HasPrefix(*f.name, "Managed-") { + return fmt.Errorf("Cannot delete default CloudFront Response headers policy") + } + return nil +} + +func (f *CloudFrontResponseHeadersPolicy) Remove() error { + resp, err := f.svc.GetResponseHeadersPolicy(&cloudfront.GetResponseHeadersPolicyInput{ + Id: f.ID, + }) + if err != nil { + return err + } + + _, err = f.svc.DeleteResponseHeadersPolicy(&cloudfront.DeleteResponseHeadersPolicyInput{ + Id: f.ID, + IfMatch: resp.ETag, + }) + + return err +} + +func (f *CloudFrontResponseHeadersPolicy) String() string { + return *f.name +} + +func (f *CloudFrontResponseHeadersPolicy) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("ID", f.ID) + properties.Set("Name", f.name) + return properties +} diff --git a/resources/elbv2-listenerrule.go b/resources/elbv2-listenerrule.go new file mode 100644 index 000000000..a4fe640e9 --- /dev/null +++ b/resources/elbv2-listenerrule.go @@ -0,0 +1,141 @@ +package resources + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/aws/session" + "github.com/aws/aws-sdk-go/service/elbv2" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" + "github.com/sirupsen/logrus" +) + +var elbv2ListenerRulePageSize int64 = 400 // AWS has a limit of 100 rules per listener + +type ELBv2ListenerRule struct { + svc *elbv2.ELBV2 + ruleArn *string + lbName *string + listenerArn *string + tags []*elbv2.Tag +} + +func init() { + register("ELBv2ListenerRule", ListELBv2ListenerRules) +} + +func ListELBv2ListenerRules(sess *session.Session) ([]Resource, error) { + svc := elbv2.New(sess) + + // We need to retrieve ELBs then Listeners then Rules + lbs := make([]*elbv2.LoadBalancer, 0) + err := svc.DescribeLoadBalancersPages( + nil, + func(page *elbv2.DescribeLoadBalancersOutput, lastPage bool) bool { + for _, elbv2 := range page.LoadBalancers { + lbs = append(lbs, elbv2) + } + return !lastPage + }, + ) + if err != nil { + return nil, err + } + + // Required for batched tag retrieval later + ruleArns := make([]*string, 0) + ruleArnToResource := make(map[string]*ELBv2ListenerRule) + + resources := make([]Resource, 0) + for _, lb := range lbs { + err := svc.DescribeListenersPages( + &elbv2.DescribeListenersInput{ + LoadBalancerArn: lb.LoadBalancerArn, + }, + func(page *elbv2.DescribeListenersOutput, lastPage bool) bool { + for _, listener := range page.Listeners { + rules, err := svc.DescribeRules(&elbv2.DescribeRulesInput{ + ListenerArn: listener.ListenerArn, + PageSize: &elbv2ListenerRulePageSize, + }) + if err == nil { + for _, rule := range rules.Rules { + // Skip default rules as they cannot be deleted + if rule.IsDefault != nil && *rule.IsDefault { + continue + } + + listenerRule := &ELBv2ListenerRule{ + svc: svc, + ruleArn: rule.RuleArn, + lbName: lb.LoadBalancerName, + listenerArn: listener.ListenerArn, + } + + ruleArns = append(ruleArns, rule.RuleArn) + resources = append(resources, listenerRule) + ruleArnToResource[*rule.RuleArn] = listenerRule + } + } else { + logrus. + WithError(err). + WithField("listenerArn", listener.ListenerArn). + Error("Failed to list listener rules for listener") + } + } + + return !lastPage + }, + ) + if err != nil { + logrus. + WithError(err). + WithField("loadBalancerArn", lb.LoadBalancerArn). + Error("Failed to list listeners for load balancer") + } + } + + // Tags for Rules need to be fetched separately + // We can only specify up to 20 in a single call + // See: https://github.com/aws/aws-sdk-go/blob/0e8c61841163762f870f6976775800ded4a789b0/service/elbv2/api.go#L5398 + for _, ruleChunk := range Chunk(ruleArns, 20) { + tagResp, err := svc.DescribeTags(&elbv2.DescribeTagsInput{ + ResourceArns: ruleChunk, + }) + if err != nil { + return nil, err + } + for _, elbv2TagInfo := range tagResp.TagDescriptions { + rule := ruleArnToResource[*elbv2TagInfo.ResourceArn] + rule.tags = elbv2TagInfo.Tags + } + } + + return resources, nil +} + +func (e *ELBv2ListenerRule) Remove() error { + _, err := e.svc.DeleteRule(&elbv2.DeleteRuleInput{ + RuleArn: e.ruleArn, + }) + if err != nil { + return err + } + + return nil +} + +func (e *ELBv2ListenerRule) Properties() types.Properties { + properties := types.NewProperties(). + Set("ARN", e.ruleArn). + Set("ListenerARN", e.listenerArn). + Set("LoadBalancerName", e.lbName) + + for _, tagValue := range e.tags { + properties.SetTag(tagValue.Key, tagValue.Value) + } + return properties +} + +func (e *ELBv2ListenerRule) String() string { + return fmt.Sprintf("%s -> %s", *e.lbName, *e.ruleArn) +} diff --git a/resources/redshiftserverless-namespaces.go b/resources/redshiftserverless-namespaces.go new file mode 100644 index 000000000..adfaae07e --- /dev/null +++ b/resources/redshiftserverless-namespaces.go @@ -0,0 +1,68 @@ +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/redshiftserverless" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type RedshiftServerlessNamespace struct { + svc *redshiftserverless.RedshiftServerless + namespace *redshiftserverless.Namespace +} + +func init() { + register("RedshiftServerlessNamespace", ListRedshiftServerlessNamespaces) +} + +func ListRedshiftServerlessNamespaces(sess *session.Session) ([]Resource, error) { + svc := redshiftserverless.New(sess) + resources := []Resource{} + + params := &redshiftserverless.ListNamespacesInput{ + MaxResults: aws.Int64(100), + } + + for { + output, err := svc.ListNamespaces(params) + if err != nil { + return nil, err + } + + for _, namespace := range output.Namespaces { + resources = append(resources, &RedshiftServerlessNamespace{ + svc: svc, + namespace: namespace, + }) + } + + if output.NextToken == nil { + break + } + + params.NextToken = output.NextToken + } + + return resources, nil +} + +func (n *RedshiftServerlessNamespace) Properties() types.Properties { + properties := types.NewProperties(). + Set("CreationDate", n.namespace.CreationDate). + Set("NamespaceName", n.namespace.NamespaceName) + + return properties +} + +func (n *RedshiftServerlessNamespace) Remove() error { + _, err := n.svc.DeleteNamespace(&redshiftserverless.DeleteNamespaceInput{ + NamespaceName: n.namespace.NamespaceName, + }) + + return err +} + +func (n *RedshiftServerlessNamespace) String() string { + return *n.namespace.NamespaceName +} diff --git a/resources/redshiftserverless-snapshots.go b/resources/redshiftserverless-snapshots.go new file mode 100644 index 000000000..9f56c8e62 --- /dev/null +++ b/resources/redshiftserverless-snapshots.go @@ -0,0 +1,69 @@ +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/redshiftserverless" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type RedshiftServerlessSnapshot struct { + svc *redshiftserverless.RedshiftServerless + snapshot *redshiftserverless.Snapshot +} + +func init() { + register("RedshiftServerlessSnapshot", ListRedshiftServerlessSnapshots) +} + +func ListRedshiftServerlessSnapshots(sess *session.Session) ([]Resource, error) { + svc := redshiftserverless.New(sess) + resources := []Resource{} + + params := &redshiftserverless.ListSnapshotsInput{ + MaxResults: aws.Int64(100), + } + + for { + output, err := svc.ListSnapshots(params) + if err != nil { + return nil, err + } + + for _, snapshot := range output.Snapshots { + resources = append(resources, &RedshiftServerlessSnapshot{ + svc: svc, + snapshot: snapshot, + }) + } + + if output.NextToken == nil { + break + } + + params.NextToken = output.NextToken + } + + return resources, nil +} + +func (s *RedshiftServerlessSnapshot) Properties() types.Properties { + properties := types.NewProperties(). + Set("CreateTime", s.snapshot.SnapshotCreateTime). + Set("Namespace", s.snapshot.NamespaceName). + Set("SnapshotName", s.snapshot.SnapshotName) + + return properties +} + +func (s *RedshiftServerlessSnapshot) Remove() error { + _, err := s.svc.DeleteSnapshot(&redshiftserverless.DeleteSnapshotInput{ + SnapshotName: s.snapshot.SnapshotName, + }) + + return err +} + +func (s *RedshiftServerlessSnapshot) String() string { + return *s.snapshot.SnapshotName +} diff --git a/resources/redshiftserverless-workgroups.go b/resources/redshiftserverless-workgroups.go new file mode 100644 index 000000000..a88c8d16a --- /dev/null +++ b/resources/redshiftserverless-workgroups.go @@ -0,0 +1,69 @@ +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/redshiftserverless" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type RedshiftServerlessWorkgroup struct { + svc *redshiftserverless.RedshiftServerless + workgroup *redshiftserverless.Workgroup +} + +func init() { + register("RedshiftServerlessWorkgroup", ListRedshiftServerlessWorkgroups) +} + +func ListRedshiftServerlessWorkgroups(sess *session.Session) ([]Resource, error) { + svc := redshiftserverless.New(sess) + resources := []Resource{} + + params := &redshiftserverless.ListWorkgroupsInput{ + MaxResults: aws.Int64(100), + } + + for { + output, err := svc.ListWorkgroups(params) + if err != nil { + return nil, err + } + + for _, workgroup := range output.Workgroups { + resources = append(resources, &RedshiftServerlessWorkgroup{ + svc: svc, + workgroup: workgroup, + }) + } + + if output.NextToken == nil { + break + } + + params.NextToken = output.NextToken + } + + return resources, nil +} + +func (w *RedshiftServerlessWorkgroup) Properties() types.Properties { + properties := types.NewProperties(). + Set("CreationDate", w.workgroup.CreationDate). + Set("Namespace", w.workgroup.NamespaceName). + Set("WorkgroupName", w.workgroup.WorkgroupName) + + return properties +} + +func (w *RedshiftServerlessWorkgroup) Remove() error { + _, err := w.svc.DeleteWorkgroup(&redshiftserverless.DeleteWorkgroupInput{ + WorkgroupName: w.workgroup.WorkgroupName, + }) + + return err +} + +func (w *RedshiftServerlessWorkgroup) String() string { + return *w.workgroup.WorkgroupName +} diff --git a/resources/util.go b/resources/util.go index 22a3e1b2e..b61b83966 100644 --- a/resources/util.go +++ b/resources/util.go @@ -36,3 +36,21 @@ func IsAWSError(err error, code string) bool { return aerr.Code() == code } + +func Chunk[T any](slice []T, size int) [][]T { + var chunks [][]T + for i := 0; i < len(slice); { + // Clamp the last chunk to the slice bound as necessary. + end := size + if l := len(slice[i:]); l < size { + end = l + } + + // Set the capacity of each chunk so that appending to a chunk does not + // modify the original slice. + chunks = append(chunks, slice[i:i+end:i+end]) + i += end + } + + return chunks +}