/
deleteBucket.go
125 lines (112 loc) · 3.8 KB
/
deleteBucket.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2022 Antrea Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"context"
"fmt"
"time"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/spf13/cobra"
s3client "antrea.io/theia/snowflake/pkg/aws/client/s3"
)
// deleteBucketCmd represents the delete-bucket command
var deleteBucketCmd = &cobra.Command{
Use: "delete-bucket",
Short: "Delete an AWS S3 bucket",
Long: `This command deletes an existing S3 bucket in your AWS
account. For example:
"theia-sf delete-bucket --name <YOUR BUCKET NAME>"
If the bucket is not empty, you will need to provide the "--force" flag, which
will cause all objects in the bucket to be deleted.`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
region, _ := cmd.Flags().GetString("region")
bucketName, _ := cmd.Flags().GetString("name")
force, _ := cmd.Flags().GetBool("force")
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
awsCfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(region))
if err != nil {
return fmt.Errorf("unable to load AWS SDK config: %w", err)
}
s3Client := s3client.GetClient(awsCfg)
if force {
if err := deleteS3Objects(ctx, s3Client, bucketName); err != nil {
return err
}
}
return deleteBucket(ctx, s3Client, bucketName)
},
}
func deleteBucket(ctx context.Context, s3Client s3client.Interface, name string) error {
logger := logger.WithValues("bucket", name)
logger.Info("Deleting S3 bucket")
if _, err := s3Client.DeleteBucket(ctx, &s3.DeleteBucketInput{
Bucket: &name,
}); err != nil {
return fmt.Errorf("error when deleting bucket '%s': %w", name, err)
}
logger.Info("Deleted S3 bucket")
return nil
}
func deleteS3Objects(ctx context.Context, s3Client s3client.Interface, bucketName string) error {
logger := logger.WithValues("bucket", bucketName)
prefix := ""
var nextToken *string
logger.Info("Deleting all objects in S3 bucket")
for {
output, err := s3Client.ListObjectsV2(ctx, &s3.ListObjectsV2Input{
Bucket: &bucketName,
ContinuationToken: nextToken,
Prefix: &prefix,
})
if err != nil {
return fmt.Errorf("error when listing objects: %w", err)
}
keys := make([]s3types.ObjectIdentifier, 0, len(output.Contents))
for _, obj := range output.Contents {
keys = append(keys, s3types.ObjectIdentifier{
Key: obj.Key,
})
}
if len(keys) == 0 {
break
}
logger.Info("Deleting objects", "count", len(keys))
_, err = s3Client.DeleteObjects(ctx, &s3.DeleteObjectsInput{
Bucket: &bucketName,
Delete: &s3types.Delete{
Objects: keys,
},
})
if err != nil {
return fmt.Errorf("error when deleting objects: %w", err)
}
nextToken = output.ContinuationToken
if nextToken == nil {
break
}
}
logger.Info("Deleted all objects in S3 bucket")
return nil
}
func init() {
rootCmd.AddCommand(deleteBucketCmd)
deleteBucketCmd.Flags().String("region", GetEnv("AWS_REGION", defaultRegion), "region to use for deleting the bucket")
deleteBucketCmd.Flags().String("name", "", "name of bucket to delete")
deleteBucketCmd.MarkFlagRequired("name")
deleteBucketCmd.Flags().Bool("force", false, "delete all objects if bucket is not empty")
}