-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
238 lines (176 loc) · 5.26 KB
/
bucket.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
log "github.com/sirupsen/logrus"
"sort"
"regexp"
"time"
)
type BucketInfo struct {
Name string
Provider string
}
var signUrlValidMin time.Duration = 15
func orderStringSlice (selectElement string, elements []string) ([]string) {
log.Trace(selectElement, elements)
// Order slice but having the selected element in the first position
selectElementPos := 0
firstBucketValue := elements[0]
for index, bucket := range elements {
if bucket == selectElement {
selectElementPos = index
break
}
}
elements[0] = selectElement
elements[selectElementPos] = firstBucketValue
sort.Strings(elements[1:])
log.Trace(elements)
return elements
}
func orderIntSlice (selectElement int, elements []int) ([]int) {
log.Trace(selectElement, elements)
// Order slice but having the selected element in the first position
selectElementPos := 0
firstBucketValue := elements[0]
for index, bucket := range elements {
if bucket == selectElement {
selectElementPos = index
break
}
}
elements[0] = selectElement
elements[selectElementPos] = firstBucketValue
sort.Ints(elements[1:])
log.Trace(elements)
return elements
}
func getMatchedBucketUserAws (bucketsAws []string) ([]string) {
log.Trace(bucketsAws)
var matchBuckets []string
semaphoreAws <- struct{}{}
allBuckets := awsListBuckets
<-semaphoreAws
for _, bucketRemote := range allBuckets {
for _, bucketLocal := range bucketsAws {
found, err := regexp.MatchString(bucketLocal, bucketRemote)
if err != nil {
log.Error(err)
continue
}
if found {
matchBuckets = append(matchBuckets, bucketRemote)
}
}
}
log.Trace(matchBuckets)
return matchBuckets
}
func getMatchedBucketUserGcp (bucketsGcp []string) ([]string) {
log.Trace(bucketsGcp)
var matchBuckets []string
semaphoreGcp <- struct{}{}
allBuckets := gcpListBuckets
<-semaphoreGcp
for _, bucketRemote := range allBuckets {
for _, bucketLocal := range bucketsGcp {
found, err := regexp.MatchString(bucketLocal, bucketRemote)
if err != nil {
log.Error(err)
continue
}
if found {
matchBuckets = append(matchBuckets, bucketRemote)
}
}
}
log.Trace(matchBuckets)
return matchBuckets
}
func getAllBuckets () {
err, allBuckets := AwsS3ListBuckets()
if err != nil {
log.Error(err)
return
}
semaphoreAws <- struct{}{}
awsListBuckets = allBuckets
<-semaphoreAws
log.Info(allBuckets)
err, allBuckets = GcpListBuckets()
if err != nil {
log.Error(err)
return
}
semaphoreGcp <- struct{}{}
gcpListBuckets = allBuckets
<-semaphoreGcp
log.Info(allBuckets)
}
func getListBucketUserMatching (bucketsAws, bucketsGcp []string) ([]BucketInfo) {
log.Trace(bucketsAws, bucketsGcp)
var matchAwsBuckets []string
var matchGcpBuckets []string
//ListBucketsAws
if len(bucketsAws) > 0 {
matchAwsBuckets = getMatchedBucketUserAws(bucketsAws)
}
//ListBucketsGcp
if len(bucketsGcp) > 0 {
matchGcpBuckets = getMatchedBucketUserGcp(bucketsGcp)
}
log.Trace(matchAwsBuckets, matchGcpBuckets)
matchBuckets := make([]BucketInfo, len(matchAwsBuckets)+len(matchGcpBuckets))
for i,name := range matchAwsBuckets {
matchBuckets[i].Name = name
matchBuckets[i].Provider = "aws"
}
for i,name := range matchGcpBuckets {
matchBuckets[i+len(matchAwsBuckets)].Name = name
matchBuckets[i+len(matchAwsBuckets)].Provider = "gcp"
}
log.Trace(matchBuckets)
return matchBuckets
}
func getSignedBucketUrl (bucketList []BucketInfo, bucket, object string) (string) {
log.Trace(bucketList, bucket, object)
var presignUrl string
var err error
for _,b := range bucketList {
if b.Name == bucket {
if b.Provider == "aws" {
err, presignUrl = AwsS3PresignObjectGet(bucket, object)
if err != nil {
log.Error(err)
return ""
}
return presignUrl
} else if b.Provider == "gcp" {
err, presignUrl = GcpPresignObjectGet(bucket, object)
if err != nil {
log.Error(err)
return presignUrl
}
return presignUrl
} else {
log.Error("unknown provider to signedUrl ", b.Provider, b.Name)
return ""
}
}
}
log.Trace(presignUrl)
return ""
}
func getBucketObjectsList (bucketList []BucketInfo, bucket string, order string) ([]string) {
log.Trace(bucketList, bucket)
var objectsList []string
for _,b := range bucketList {
if b.Name == bucket {
objectsList = getBucketObjectsCache(bucket,b.Provider)
}
}
if order == "zA" {
sort.Sort(sort.Reverse(sort.StringSlice(objectsList)))
}
log.Trace(objectsList)
return objectsList
}