/
minio.go
121 lines (101 loc) · 3.68 KB
/
minio.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
package common
import (
"context"
"github.com/minio/minio-go/v7"
log "github.com/sirupsen/logrus"
"time"
)
type MinioBucket struct {
CloudProviderResource
ObjectsInfos []minio.ObjectInfo
}
func listBuckets(bucketApi *minio.Client, tagName string, region string, withTags bool) []MinioBucket {
buckets, err := bucketApi.ListBuckets(context.TODO())
if err != nil {
log.Errorf("Can't list bucket for region %s: %s", region, err.Error())
return []MinioBucket{}
}
bucketLimit := 50
if len(buckets) < bucketLimit {
bucketLimit = len(buckets)
}
scwBuckets := []MinioBucket{}
for _, bucket := range buckets[:bucketLimit] {
essentialTags := EssentialTags{}
if withTags {
bucketTags := listBucketTags(bucketApi, context.TODO(), bucket.Name)
essentialTags = GetEssentialTags(bucketTags, tagName)
}
creationDate, _ := time.Parse(time.RFC3339, bucket.CreationDate.Format(time.RFC3339))
scwBuckets = append(scwBuckets, MinioBucket{
CloudProviderResource: CloudProviderResource{
Identifier: bucket.Name,
Description: "Bucket: " + bucket.Name,
CreationDate: creationDate,
TTL: essentialTags.TTL,
Tag: essentialTags.Tag,
IsProtected: false,
},
ObjectsInfos: nil,
})
}
return scwBuckets
}
func listBucketTags(bucketApi *minio.Client, ctx context.Context, bucketName string) []string {
objects, err := bucketApi.GetBucketTagging(ctx, bucketName)
tags := []string{}
if err != nil {
log.Errorf("Can't get tags for bucket %s: %s", bucketName, err.Error())
return tags
}
for _, value := range objects.ToMap() {
tags = append(tags, value)
}
return tags
}
func ListBucketObjects(bucketApi *minio.Client, ctx context.Context, bucketName string) []minio.ObjectInfo {
objects := bucketApi.ListObjects(ctx, bucketName, minio.ListObjectsOptions{Recursive: true})
objectsInfos := []minio.ObjectInfo{}
for object := range objects {
objectsInfos = append(objectsInfos, object)
}
return objectsInfos
}
func GetExpiredBuckets(bucketApi *minio.Client, tagName string, region string, tagValue string, disableTTLCheck bool) []MinioBucket {
buckets := listBuckets(bucketApi, tagName, region, true)
expiredBuckets := []MinioBucket{}
for _, bucket := range buckets {
if bucket.IsResourceExpired(tagValue, disableTTLCheck) || (len(bucket.ObjectsInfos) == 0 && time.Now().UTC().After(bucket.CreationDate.Add(4*time.Hour)) && bucket.TTL == -1) {
objectsInfos := ListBucketObjects(bucketApi, context.TODO(), bucket.Identifier)
bucket.ObjectsInfos = objectsInfos
expiredBuckets = append(expiredBuckets, bucket)
}
}
return expiredBuckets
}
func GetUnusedBuckets(bucketApi *minio.Client, tagName string, region string, isDestroyingCommand bool) []MinioBucket {
buckets := listBuckets(bucketApi, tagName, region, false)
expiredBuckets := []MinioBucket{}
for _, bucket := range buckets {
if isDestroyingCommand || bucket.CreationDate.UTC().Add(2*time.Hour).Before(time.Now().UTC()) {
expiredBuckets = append(expiredBuckets, bucket)
}
}
return expiredBuckets
}
func EmptyBucket(bucketApi *minio.Client, bucketName string, objects []minio.ObjectInfo) {
for _, object := range objects {
err := bucketApi.RemoveObject(context.TODO(), bucketName, object.Key, minio.RemoveObjectOptions{ForceDelete: true})
if err != nil {
log.Errorf("Can't delete object %s for bucket %s: %s", object.Key, bucketName, err.Error())
}
}
}
func DeleteBucket(bucketApi *minio.Client, bucket MinioBucket, region string) {
err := bucketApi.RemoveBucket(context.Background(), bucket.Identifier)
if err != nil {
log.Errorf("Can't delete bucket %s: %s", bucket.Identifier, err.Error())
} else {
log.Debugf("Bucket %s in %s deleted.", bucket.Identifier, region)
}
}