-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
308 lines (238 loc) · 8.46 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package sdk
import (
"encoding/json"
"errors"
"fmt"
"github.com/kataras/golog"
"github.com/paradeum-team/chainstorage-sdk/code"
"github.com/paradeum-team/chainstorage-sdk/consts"
"github.com/paradeum-team/chainstorage-sdk/model"
"github.com/ulule/deepcopier"
"net/http"
"net/url"
"regexp"
"strings"
)
type Bucket struct {
Config *Configuration
Client *RestyClient
logger *golog.Logger
}
// region 桶数据
// 获取桶数据列表
func (b *Bucket) GetBucketList(bucketName string, pageSize, pageIndex int) (model.BucketPageResponse, error) {
response := model.BucketPageResponse{}
// 参数设置
urlQuery := ""
if len(bucketName) != 0 {
if err := checkBucketName(bucketName); err != nil {
return response, err
}
urlQuery += fmt.Sprintf("bucketName=%s&", url.QueryEscape(bucketName))
}
if pageSize > 0 && pageSize <= 1000 {
urlQuery += fmt.Sprintf("pageSize=%d&", pageSize)
}
if pageIndex > 0 && pageIndex <= 1000 {
urlQuery += fmt.Sprintf("pageIndex=%d&", pageIndex)
}
// 请求Url
urlQuery = strings.TrimSuffix(urlQuery, "&")
//apiBaseAddress := conf.cssConfig.ChainStorageApiEndpoint
apiBaseAddress := b.Config.ChainStorageApiEndpoint
apiPath := "api/v1/buckets"
apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath)
if len(urlQuery) != 0 {
apiUrl += "?" + urlQuery
}
// API调用
httpStatus, body, err := b.Client.RestyGet(apiUrl)
//httpStatus, body, err := client.RestyGet(apiUrl)
if err != nil {
//utils.LogError(fmt.Sprintf("API:GetBucketList:HttpGet, apiUrl:%s, httpStatus:%d, err:%+v\n", apiUrl, httpStatus, err))
b.logger.Errorf(fmt.Sprintf("API:GetBucketList:HttpGet, apiUrl:%s, httpStatus:%d, err:%+v\n", apiUrl, httpStatus, err))
return response, err
}
if httpStatus != http.StatusOK {
b.logger.Errorf(fmt.Sprintf("API:GetBucketList:HttpGet, apiUrl:%s, httpStatus:%d, body:%s\n", apiUrl, httpStatus, string(body)))
return response, errors.New(string(body))
}
// 响应数据解析
err = json.Unmarshal(body, &response)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:GetBucketList:JsonUnmarshal, body:%s, err:%+v\n", string(body), err))
return response, err
}
return response, nil
}
// 创建桶数据
func (b *Bucket) CreateBucket(bucketName string, storageNetworkCode, bucketPrincipleCode int) (model.BucketCreateResponse, error) {
response := model.BucketCreateResponse{}
// 参数设置
if err := checkBucketName(bucketName); err != nil {
return response, err
}
if err := checkStorageNetworkCode(storageNetworkCode); err != nil {
return response, err
}
if err := checkBucketPrincipleCode(bucketPrincipleCode); err != nil {
return response, err
}
params := map[string]interface{}{
"bucketName": bucketName,
"storageNetworkCode": storageNetworkCode,
"bucketPrincipleCode": bucketPrincipleCode,
}
// 请求Url
apiBaseAddress := b.Config.ChainStorageApiEndpoint
apiPath := "api/v1/bucket"
apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath)
// API调用
httpStatus, body, err := b.Client.RestyPost(apiUrl, params)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:CreateBucket:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, err:%+v\n", apiUrl, params, httpStatus, err))
return response, err
}
if httpStatus != http.StatusOK {
b.logger.Errorf(fmt.Sprintf("API:CreateBucket:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, body:%s\n", apiUrl, params, httpStatus, string(body)))
return response, errors.New(string(body))
}
// 响应数据解析
err = json.Unmarshal(body, &response)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:CreateBucket:JsonUnmarshal, body:%s, err:%+v\n", string(body), err))
return response, err
}
return response, nil
}
// 清空桶数据
func (b *Bucket) EmptyBucket(bucketId int) (model.BucketEmptyResponse, error) {
response := model.BucketEmptyResponse{}
// 参数设置
if bucketId <= 0 {
return response, code.ErrInvalidBucketId
}
params := map[string]interface{}{
"id": bucketId,
}
// 请求Url
apiBaseAddress := b.Config.ChainStorageApiEndpoint
apiPath := "api/v1/bucket/status/clean"
apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath)
// API调用
httpStatus, body, err := b.Client.RestyPost(apiUrl, params)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:EmptyBucket:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, err:%+v\n", apiUrl, params, httpStatus, err))
return response, err
}
if httpStatus != http.StatusOK {
b.logger.Errorf(fmt.Sprintf("API:EmptyBucket:HttpPost, apiUrl:%s, params:%+v, httpStatus:%d, body:%s\n", apiUrl, params, httpStatus, string(body)))
return response, errors.New(string(body))
}
// 响应数据解析
err = json.Unmarshal(body, &response)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:EmptyBucket:JsonUnmarshal, body:%s, err:%+v\n", string(body), err))
return response, err
}
return response, nil
}
// 删除桶数据
func (b *Bucket) RemoveBucket(bucketId int, autoEmptyBucketData bool) (model.BucketRemoveResponse, error) {
response := model.BucketRemoveResponse{}
// 参数设置
if bucketId <= 0 {
return response, code.ErrInvalidBucketId
}
// 自动清空数据
if autoEmptyBucketData {
bucketEmptyResponse, err := b.EmptyBucket(bucketId)
if err != nil {
deepcopier.Copy(&bucketEmptyResponse).To(&response)
b.logger.Errorf(fmt.Sprintf("API:RemoveBucket:EmptyBucket, bucketId:%d, bucketEmptyResponse:%+v, err:%+v\n", bucketId, bucketEmptyResponse, err))
return response, err
}
}
// 请求Url
apiBaseAddress := b.Config.ChainStorageApiEndpoint
apiPath := fmt.Sprintf("api/v1/bucket/%d", bucketId)
apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath)
// API调用
httpStatus, body, err := b.Client.RestyDelete(apiUrl, nil)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:RemoveBucket:HttpDelete, apiUrl:%s, bucketId:%d, httpStatus:%d, err:%+v\n", apiUrl, bucketId, httpStatus, err))
return response, err
}
if httpStatus != http.StatusOK {
b.logger.Errorf(fmt.Sprintf("API:RemoveBucket:HttpDelete, apiUrl:%s, bucketId:%d, httpStatus:%d, body:%s\n", apiUrl, bucketId, httpStatus, string(body)))
return response, errors.New(string(body))
}
// 响应数据解析
err = json.Unmarshal(body, &response)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:RemoveBucket:JsonUnmarshal, body:%s, err:%+v\n", string(body), err))
return response, err
}
return response, nil
}
// 根据桶名称获取桶数据
func (b *Bucket) GetBucketByName(bucketName string) (model.BucketCreateResponse, error) {
response := model.BucketCreateResponse{}
// 参数设置
if err := checkBucketName(bucketName); err != nil {
return response, err
}
apiBaseAddress := b.Config.ChainStorageApiEndpoint
apiPath := fmt.Sprintf("api/v1/bucket/name/%s", url.QueryEscape(bucketName))
apiUrl := fmt.Sprintf("%s%s", apiBaseAddress, apiPath)
// API调用
httpStatus, body, err := b.Client.RestyGet(apiUrl)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:GetBucketByName:HttpGet, apiUrl:%s, httpStatus:%d, err:%+v\n", apiUrl, httpStatus, err))
return response, err
}
if httpStatus != http.StatusOK {
b.logger.Errorf(fmt.Sprintf("API:GetBucketByName:HttpGet, apiUrl:%s, httpStatus:%d, body:%s\n", apiUrl, httpStatus, string(body)))
return response, errors.New(string(body))
}
// 响应数据解析
err = json.Unmarshal(body, &response)
if err != nil {
b.logger.Errorf(fmt.Sprintf("API:GetBucketByName:JsonUnmarshal, body:%s, err:%+v\n", string(body), err))
return response, err
}
return response, nil
}
// endregion 桶数据
// 检查桶名称
func checkBucketName(bucketName string) error {
if len(bucketName) < 3 || len(bucketName) > 63 {
return code.ErrInvalidBucketName
}
// 桶名称异常,名称范围必须在 3-63 个字符之间并且只能包含小写字符、数字和破折号,请重新尝试
isMatch := regexp.MustCompile(`^[a-z0-9-]*$`).MatchString(bucketName)
if !isMatch {
return code.ErrInvalidBucketName
}
return nil
}
// 检查存储网络编码
func checkStorageNetworkCode(storageNetworkCode int) error {
// 存储网络编码必须设置
storageNetworkCodeMapping := consts.StorageNetworkCodeMapping
_, exist := storageNetworkCodeMapping[storageNetworkCode]
if !exist {
return code.ErrStorageNetworkMustSet
}
return nil
}
// 检查存储网络编码
func checkBucketPrincipleCode(bucketPrincipleCode int) error {
// 桶策略编码必须设置
bucketPrincipleCodeMapping := consts.BucketPrincipleCodeMapping
_, exist := bucketPrincipleCodeMapping[bucketPrincipleCode]
if !exist {
return code.ErrBucketPrincipleMustSet
}
return nil
}