-
Notifications
You must be signed in to change notification settings - Fork 100
/
bucket_worm.go
261 lines (211 loc) · 6.76 KB
/
bucket_worm.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
package lib
import (
"encoding/xml"
"fmt"
"strconv"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
var specChineseWorm = SpecText{
synopsisText: "设置、删除、修改、提交bucket的Worm配置",
paramText: "command_name bucket_url [days] [wormId] [options]",
syntaxText: `
ossutil worm init oss://bucket days
ossutil worm abort oss://bucket
ossutil worm complete oss://bucket wormId
ossuitl worm extend oss://bucket days wormId
ossuitl worm get oss://bucket
`,
detailHelpText: `
worm命令通过设置第一个参数为init、abort、complete、extend、get,可以创建、删除、提交、修改或者查询bucket的worm配置
用法:
该命令有五种用法:
1) ossutil worm init oss://bucket days
这个命令创建worm配置,Object的保留天数为days
2) ossutil worm abort oss://bucket
这个命令删除bucket的Worm配置
3) ossutil worm complete oss://bucket wormId
这个命令提交worm配置,成功后worm状态将由InProgress变为Locked
4) ossutil worm extend oss://bucket days wormId
这个命令修改worm配置,将Object的保留天数修改为days
5) ossutil worm get oss://bucket
这个命令查询worm配置
`,
sampleText: `
`,
}
var specEnglishWorm = SpecText{
synopsisText: "set、delete、complete、get bucket's worm configuration",
paramText: "command_name bucket_url [days] [wormId] [options]",
syntaxText: `
ossutil worm init oss://bucket days
ossutil worm abort oss://bucket
ossutil worm complete oss://bucket wormId
ossuitl worm extend oss://bucket days wormId
ossuitl worm get oss://bucket
`,
detailHelpText: `
The worm command can create, delete, complete, modify or get the worm configuration of the bucket
by setting the first parameter to init, abort, complete, extend, and get
Usage:
There are 5 usages for this command:
1) ossutil worm init oss://bucket days
This command creates a worm configuration, the object's retention period is days
2) ossutil worm abort oss://bucket
This command deletes the worm configuration of the bucket
3) ossutil worm complete oss://bucket wormId
This command complete the worm configuration.
After success, the worm status will change from InProgress to Locked
4) ossutil worm extend oss://bucket days wormId
This command modifies the worm configuration and changes the retention period of objects to days
5) ossutil worm get oss://bucket
This command get worm configuration
`,
sampleText: `
`,
}
type wormOptionType struct {
bucketName string
wormConfig oss.WormConfiguration
}
type WormCommand struct {
command Command
wmOption wormOptionType
}
var wormCommand = WormCommand{
command: Command{
name: "worm",
nameAlias: []string{"worm"},
minArgc: 2,
maxArgc: 4,
specChinese: specChineseWorm,
specEnglish: specEnglishWorm,
group: GroupTypeNormalCommand,
validOptionNames: []string{
OptionConfigFile,
OptionEndpoint,
OptionAccessKeyID,
OptionAccessKeySecret,
OptionSTSToken,
OptionProxyHost,
OptionProxyUser,
OptionProxyPwd,
OptionLogLevel,
},
},
}
// function for FormatHelper interface
func (wormc *WormCommand) formatHelpForWhole() string {
return wormc.command.formatHelpForWhole()
}
func (wormc *WormCommand) formatIndependHelp() string {
return wormc.command.formatIndependHelp()
}
// Init simulate inheritance, and polymorphism
func (wormc *WormCommand) Init(args []string, options OptionMapType) error {
return wormc.command.Init(args, options, wormc)
}
// RunCommand simulate inheritance, and polymorphism
func (wormc *WormCommand) RunCommand() error {
// init all command name
commandDict := make(map[string]string)
commandDict["init"] = "init"
commandDict["abort"] = "abort"
commandDict["complete"] = "complete"
commandDict["extend"] = "extend"
commandDict["get"] = "get"
// check command name
strCommand := wormc.command.args[0]
_, ok := commandDict[strCommand]
if !ok {
return fmt.Errorf("invalid parameter %s,which must be init, abort, complete, extend, get", strCommand)
}
bucketUrL, err := StorageURLFromString(wormc.command.args[1], "")
if err != nil {
return err
}
if !bucketUrL.IsCloudURL() {
return fmt.Errorf("parameter is not a cloud url,url is %s", bucketUrL.ToString())
}
cloudUrl := bucketUrL.(CloudURL)
if cloudUrl.bucket == "" {
return fmt.Errorf("bucket name is empty,url is %s", bucketUrL.ToString())
}
wormc.wmOption.bucketName = cloudUrl.bucket
if strCommand == "init" {
err = wormc.InitiateBucketWorm()
} else if strCommand == "abort" {
err = wormc.AbortBucketWorm()
} else if strCommand == "complete" {
err = wormc.CompleteBucketWorm()
} else if strCommand == "extend" {
err = wormc.ExtendBucketWorm()
} else if strCommand == "get" {
err = wormc.GetBucketWorm()
}
return err
}
func (wormc *WormCommand) InitiateBucketWorm() error {
if len(wormc.command.args) < 3 {
return fmt.Errorf("missing parameter,the parameter day is empty")
}
retentionDays, err := strconv.Atoi(wormc.command.args[2])
if err != nil {
return err
}
client, err := wormc.command.ossClient(wormc.wmOption.bucketName)
if err != nil {
return err
}
wormID, err := client.InitiateBucketWorm(wormc.wmOption.bucketName, retentionDays)
if wormID != "" {
fmt.Printf("init success,worm id is %s", wormID)
}
return err
}
func (wormc *WormCommand) AbortBucketWorm() error {
client, err := wormc.command.ossClient(wormc.wmOption.bucketName)
if err != nil {
return err
}
return client.AbortBucketWorm(wormc.wmOption.bucketName)
}
func (wormc *WormCommand) CompleteBucketWorm() error {
if len(wormc.command.args) < 3 {
return fmt.Errorf("missing parameter,the wormId is empty")
}
client, err := wormc.command.ossClient(wormc.wmOption.bucketName)
if err != nil {
return err
}
return client.CompleteBucketWorm(wormc.wmOption.bucketName, wormc.command.args[2])
}
func (wormc *WormCommand) ExtendBucketWorm() error {
if len(wormc.command.args) < 4 {
return fmt.Errorf("missing parameter, need 4 parameters")
}
retentionDays, err := strconv.Atoi(wormc.command.args[2])
if err != nil {
return err
}
client, err := wormc.command.ossClient(wormc.wmOption.bucketName)
if err != nil {
return err
}
return client.ExtendBucketWorm(wormc.wmOption.bucketName, retentionDays, wormc.command.args[3])
}
func (wormc *WormCommand) GetBucketWorm() error {
client, err := wormc.command.ossClient(wormc.wmOption.bucketName)
if err != nil {
return err
}
wormConfig, err := client.GetBucketWorm(wormc.wmOption.bucketName)
if err != nil {
return err
}
wormc.wmOption.wormConfig = wormConfig
output, err := xml.MarshalIndent(wormConfig, " ", " ")
if err == nil {
fmt.Println(string(output))
}
return err
}