/
ilm-rule-add.go
241 lines (215 loc) · 7.13 KB
/
ilm-rule-add.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
// Copyright (c) 2015-2022 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"context"
"github.com/minio/cli"
json "github.com/minio/colorjson"
"github.com/kirolous/mc/cmd/ilm"
"github.com/kirolous/mc/pkg/probe"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/lifecycle"
"github.com/minio/pkg/console"
)
var ilmAddCmd = cli.Command{
Name: "add",
Usage: "add a lifecycle configuration rule for a bucket",
Action: mainILMAdd,
OnUsageError: onUsageError,
Before: setGlobalsFromContext,
Flags: append(ilmAddFlags, globalFlags...),
CustomHelpTemplate: `NAME:
{{.HelpName}} - {{.Usage}}
USAGE:
{{.HelpName}} [COMMAND FLAGS] TARGET
FLAGS:
{{range .VisibleFlags}}{{.}}
{{end}}
DESCRIPTION:
Add a lifecycle configuration rule.
EXAMPLES:
1. Add a lifecycle rule with a transition and a noncurrent version transition action for objects with prefix doc/ in mybucket.
Tiers must exist in MinIO. Use existing tiers or add new tiers.
{{.Prompt}} mc ilm tier add minio myminio MINIOTIER-1 --endpoint https://warm-minio-1.com \
--access-key ACCESSKEY --secret-key SECRETKEY --bucket bucket1 --prefix prefix1
{{.Prompt}} {{.HelpName}} --prefix "doc/" --transition-days "90" --transition-tier "MINIOTIER-1" \
--noncurrent-transition-days "45" --noncurrent-transition-tier "MINIOTIER-1" \
myminio/mybucket/
2. Add a lifecycle rule with an expiration action for all objects in mybucket.
{{.Prompt}} {{.HelpName}} --expire-days "200" myminio/mybucket
3. Add a lifecycle rule with an expiration and a noncurrent version expiration action for all objects with prefix doc/ in mybucket.
{{.Prompt}} {{.HelpName}} --prefix "doc/" --expire-days "300" --noncurrent-expire-days "100" \
myminio/mybucket/
`,
}
var ilmAddFlags = []cli.Flag{
cli.StringFlag{
Name: "prefix",
Usage: "object prefix",
},
cli.StringFlag{
Name: "tags",
Usage: "key value pairs of the form '<key1>=<value1>&<key2>=<value2>&<key3>=<value3>'",
},
cli.StringFlag{
Name: "expiry-date",
Usage: "format 'YYYY-MM-DD' the date of expiration",
Hidden: true,
},
cli.StringFlag{
Name: "expiry-days",
Usage: "the number of days to expiration",
Hidden: true,
},
cli.StringFlag{
Name: "expire-days",
Usage: "number of days to expire",
},
cli.BoolFlag{
Name: "expired-object-delete-marker",
Usage: "remove delete markers with no parallel versions",
Hidden: true,
},
cli.BoolFlag{
Name: "expire-delete-marker",
Usage: "expire zombie delete markers",
},
cli.StringFlag{
Name: "transition-date",
Usage: "format 'YYYY-MM-DD' for the date to transition",
Hidden: true,
},
cli.StringFlag{
Name: "transition-days",
Usage: "number of days to transition",
},
cli.StringFlag{
Name: "storage-class",
Usage: "storage class for current version to transition into. MinIO supports tiers configured via `mc-admin-tier-add`.",
Hidden: true,
},
cli.StringFlag{
Name: "tier",
Usage: "remote tier where current versions transition to",
Hidden: true,
},
cli.StringFlag{
Name: "transition-tier",
Usage: "remote tier name to transition",
},
cli.IntFlag{
Name: "noncurrentversion-expiration-days",
Usage: "the number of days to remove noncurrent versions",
Hidden: true,
},
cli.StringFlag{
Name: "noncurrent-expire-days",
Usage: "number of days to expire noncurrent versions",
},
cli.IntFlag{
Name: "newer-noncurrentversions-expiration",
Usage: "the number of noncurrent versions to retain",
Hidden: true,
},
cli.IntFlag{
Name: "noncurrent-expire-newer",
Usage: "number of newer noncurrent versions to retain",
},
cli.IntFlag{
Name: "noncurrentversion-transition-days",
Usage: "the number of days to transition noncurrent versions",
Hidden: true,
},
cli.IntFlag{
Name: "noncurrent-transition-days",
Usage: "number of days to transition noncurrent versions",
},
cli.IntFlag{
Name: "newer-noncurrentversions-transition",
Usage: "the number of noncurrent versions to retain. If there are this many more recent noncurrent versions they will be transitioned",
Hidden: true,
},
cli.IntFlag{
Name: "noncurrent-transition-newer",
Usage: "number of noncurrent versions to retain in hot tier",
},
cli.StringFlag{
Name: "noncurrentversion-transition-storage-class",
Usage: "storage class for noncurrent versions to transition into",
Hidden: true,
},
cli.StringFlag{
Name: "noncurrentversion-tier",
Usage: "remote tier where noncurrent versions transition to",
Hidden: true,
},
cli.StringFlag{
Name: "noncurrent-transition-tier",
Usage: "remote tier name to transition",
},
}
type ilmAddMessage struct {
Status string `json:"status"`
Target string `json:"target"`
ID string `json:"id"`
}
func (i ilmAddMessage) String() string {
return console.Colorize(ilmThemeResultSuccess, "Lifecycle configuration rule added with ID `"+i.ID+"` to "+i.Target+".")
}
func (i ilmAddMessage) JSON() string {
msgBytes, e := json.MarshalIndent(i, "", " ")
fatalIf(probe.NewError(e), "Unable to encode as JSON.")
return string(msgBytes)
}
// Validate user given arguments
func checkILMAddSyntax(ctx *cli.Context) {
if len(ctx.Args()) != 1 {
showCommandHelpAndExit(ctx, globalErrorExitStatus)
}
}
// Calls SetBucketLifecycle with the XML representation of lifecycleConfiguration type.
func mainILMAdd(cliCtx *cli.Context) error {
ctx, cancelILMAdd := context.WithCancel(globalContext)
defer cancelILMAdd()
checkILMAddSyntax(cliCtx)
setILMDisplayColorScheme()
args := cliCtx.Args()
urlStr := args.Get(0)
client, err := newClient(urlStr)
fatalIf(err.Trace(urlStr), "Unable to initialize client for "+urlStr)
// Configuration that is already set.
lfcCfg, err := client.GetLifecycle(ctx)
if err != nil {
if e := err.ToGoError(); minio.ToErrorResponse(e).Code == "NoSuchLifecycleConfiguration" {
lfcCfg = lifecycle.NewConfiguration()
} else {
fatalIf(err.Trace(args...), "Unable to fetch lifecycle rules for "+urlStr)
}
}
opts, err := ilm.GetLifecycleOptions(cliCtx)
fatalIf(err.Trace(args...), "Unable to generate new lifecycle rules for the input")
newRule, err := opts.ToILMRule()
fatalIf(err.Trace(args...), "Unable to generate new lifecycle rules for the input")
lfcCfg.Rules = append(lfcCfg.Rules, newRule)
fatalIf(client.SetLifecycle(ctx, lfcCfg).Trace(urlStr), "Unable to add this lifecycle rule")
printMsg(ilmAddMessage{
Status: "success",
Target: urlStr,
ID: opts.ID,
})
return nil
}