-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathlifecycle_rule.h
More file actions
373 lines (316 loc) · 12.5 KB
/
lifecycle_rule.h
File metadata and controls
373 lines (316 loc) · 12.5 KB
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H
#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H
#include "google/cloud/storage/storage_class.h"
#include "google/cloud/storage/version.h"
#include "google/cloud/internal/parse_rfc3339.h"
#include "google/cloud/optional.h"
#include "google/cloud/status_or.h"
#include "absl/time/civil_time.h"
#include "absl/types/optional.h"
#include <iosfwd>
#include <string>
#include <utility>
#include <vector>
namespace google {
namespace cloud {
namespace storage {
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
/// Implement a wrapper for Lifecycle Rules actions.
struct LifecycleRuleAction {
std::string type;
std::string storage_class;
};
inline bool operator==(LifecycleRuleAction const& lhs,
LifecycleRuleAction const& rhs) {
return std::tie(lhs.type, lhs.storage_class) ==
std::tie(rhs.type, rhs.storage_class);
}
inline bool operator<(LifecycleRuleAction const& lhs,
LifecycleRuleAction const& rhs) {
return std::tie(lhs.type, lhs.storage_class) <
std::tie(rhs.type, rhs.storage_class);
}
inline bool operator!=(LifecycleRuleAction const& lhs,
LifecycleRuleAction const& rhs) {
return std::rel_ops::operator!=(lhs, rhs);
}
inline bool operator>(LifecycleRuleAction const& lhs,
LifecycleRuleAction const& rhs) {
return std::rel_ops::operator>(lhs, rhs);
}
inline bool operator<=(LifecycleRuleAction const& lhs,
LifecycleRuleAction const& rhs) {
return std::rel_ops::operator<=(lhs, rhs);
}
inline bool operator>=(LifecycleRuleAction const& lhs,
LifecycleRuleAction const& rhs) {
return std::rel_ops::operator>=(lhs, rhs);
}
std::ostream& operator<<(std::ostream& os, LifecycleRuleAction const& rhs);
/// Implement a wrapper for Lifecycle Conditions.
struct LifecycleRuleCondition {
absl::optional<std::int32_t> age;
absl::optional<absl::CivilDay> created_before;
absl::optional<bool> is_live;
absl::optional<std::vector<std::string>> matches_storage_class;
absl::optional<std::int32_t> num_newer_versions;
absl::optional<std::int32_t> days_since_noncurrent_time;
absl::optional<absl::CivilDay> noncurrent_time_before;
absl::optional<std::int32_t> days_since_custom_time;
absl::optional<absl::CivilDay> custom_time_before;
absl::optional<std::vector<std::string>> matches_prefix;
absl::optional<std::vector<std::string>> matches_suffix;
};
bool operator==(LifecycleRuleCondition const& lhs,
LifecycleRuleCondition const& rhs);
bool operator<(LifecycleRuleCondition const& lhs,
LifecycleRuleCondition const& rhs);
inline bool operator!=(LifecycleRuleCondition const& lhs,
LifecycleRuleCondition const& rhs) {
return std::rel_ops::operator!=(lhs, rhs);
}
inline bool operator>(LifecycleRuleCondition const& lhs,
LifecycleRuleCondition const& rhs) {
return std::rel_ops::operator>(lhs, rhs);
}
inline bool operator<=(LifecycleRuleCondition const& lhs,
LifecycleRuleCondition const& rhs) {
return std::rel_ops::operator<=(lhs, rhs);
}
inline bool operator>=(LifecycleRuleCondition const& lhs,
LifecycleRuleCondition const& rhs) {
return std::rel_ops::operator>=(lhs, rhs);
}
std::ostream& operator<<(std::ostream& os, LifecycleRuleCondition const& rhs);
/**
* Defines objects to read, create, and modify Object Lifecycle Rules.
*
* Object Lifecycle Rules allow to configure a Bucket to automatically delete
* or change the storage class of objects as they go through lifecycle events.
*
* @see https://cloud.google.com/storage/docs/lifecycle for general information
* on Object Lifecycle Management in Google Cloud Storage.
*/
class LifecycleRule {
public:
explicit LifecycleRule(LifecycleRuleCondition condition,
LifecycleRuleAction action)
: action_(std::move(action)), condition_(std::move(condition)) {}
LifecycleRuleAction const& action() const { return action_; }
LifecycleRuleCondition const& condition() const { return condition_; }
/**
* @name Creates different types of LifecycleRule actions.
*/
///@{
static LifecycleRuleAction Delete();
static LifecycleRuleAction AbortIncompleteMultipartUpload();
static LifecycleRuleAction SetStorageClassStandard();
static LifecycleRuleAction SetStorageClassMultiRegional();
static LifecycleRuleAction SetStorageClassRegional();
static LifecycleRuleAction SetStorageClassNearline();
static LifecycleRuleAction SetStorageClassColdline();
static LifecycleRuleAction SetStorageClassDurableReducedAvailability();
static LifecycleRuleAction SetStorageClassArchive();
static LifecycleRuleAction SetStorageClass(std::string storage_class);
///@}
/**
* @name Creates different types of LifecycleRule rules.
*/
///@{
static LifecycleRuleCondition MaxAge(std::int32_t days) {
LifecycleRuleCondition result;
result.age.emplace(std::move(days));
return result;
}
static LifecycleRuleCondition CreatedBefore(absl::CivilDay date) {
LifecycleRuleCondition result;
result.created_before.emplace(std::move(date));
return result;
}
static LifecycleRuleCondition IsLive(bool value) {
LifecycleRuleCondition result;
result.is_live.emplace(std::move(value));
return result;
}
static LifecycleRuleCondition MatchesStorageClass(std::string storage_class) {
std::vector<std::string> value;
value.emplace_back(std::move(storage_class));
LifecycleRuleCondition result;
result.matches_storage_class.emplace(std::move(value));
return result;
}
static LifecycleRuleCondition MatchesStorageClasses(
std::initializer_list<std::string> list) {
std::vector<std::string> classes(std::move(list));
LifecycleRuleCondition result;
result.matches_storage_class.emplace(std::move(classes));
return result;
}
template <typename Iterator>
static LifecycleRuleCondition MatchesStorageClasses(Iterator begin,
Iterator end) {
std::vector<std::string> classes(begin, end);
LifecycleRuleCondition result;
result.matches_storage_class.emplace(std::move(classes));
return result;
}
static LifecycleRuleCondition MatchesStorageClassStandard() {
return MatchesStorageClass(storage_class::Standard());
}
static LifecycleRuleCondition MatchesStorageClassMultiRegional() {
return MatchesStorageClass(storage_class::MultiRegional());
}
static LifecycleRuleCondition MatchesStorageClassRegional() {
return MatchesStorageClass(storage_class::Regional());
}
static LifecycleRuleCondition MatchesStorageClassNearline() {
return MatchesStorageClass(storage_class::Nearline());
}
static LifecycleRuleCondition MatchesStorageClassColdline() {
return MatchesStorageClass(storage_class::Coldline());
}
static LifecycleRuleCondition
MatchesStorageClassDurableReducedAvailability() {
return MatchesStorageClass(storage_class::DurableReducedAvailability());
}
static LifecycleRuleCondition MatchesStorageClassArchive() {
return MatchesStorageClass(storage_class::Archive());
}
static LifecycleRuleCondition NumNewerVersions(std::int32_t days) {
LifecycleRuleCondition result;
result.num_newer_versions.emplace(std::move(days));
return result;
}
static LifecycleRuleCondition DaysSinceNoncurrentTime(std::int32_t days) {
LifecycleRuleCondition result;
result.days_since_noncurrent_time = days;
return result;
}
static LifecycleRuleCondition NoncurrentTimeBefore(absl::CivilDay date) {
LifecycleRuleCondition result;
result.noncurrent_time_before = date;
return result;
}
static LifecycleRuleCondition DaysSinceCustomTime(std::int32_t days) {
LifecycleRuleCondition result;
result.days_since_custom_time = days;
return result;
}
static LifecycleRuleCondition CustomTimeBefore(absl::CivilDay date) {
LifecycleRuleCondition result;
result.custom_time_before = date;
return result;
}
static LifecycleRuleCondition MatchesPrefix(std::string prefix) {
std::vector<std::string> value;
value.emplace_back(std::move(prefix));
LifecycleRuleCondition result;
result.matches_prefix.emplace(std::move(value));
return result;
}
static LifecycleRuleCondition MatchesPrefixes(
std::initializer_list<std::string> list) {
std::vector<std::string> classes(std::move(list));
LifecycleRuleCondition result;
result.matches_prefix.emplace(std::move(classes));
return result;
}
static LifecycleRuleCondition MatchesSuffix(std::string suffix) {
std::vector<std::string> value;
value.emplace_back(std::move(suffix));
LifecycleRuleCondition result;
result.matches_suffix.emplace(std::move(value));
return result;
}
static LifecycleRuleCondition MatchesSuffixes(
std::initializer_list<std::string> list) {
std::vector<std::string> classes(std::move(list));
LifecycleRuleCondition result;
result.matches_suffix.emplace(std::move(classes));
return result;
}
///@}
/**
* Combines multiple LifecycleRule conditions using conjunction.
*
* Create a condition that require all the @p condition parameters to be met
* to take effect.
*
* @par Example
*
* @code
* // Affect objects that are in the STANDARD storage class, have at
* // least 2 new versions, are at least 7 days old, and are alive.
* LifecycleRuleCondition condition = LifecycleRule::ConditionConjunction(
* LifecycleRule::NumNewerVersions(2),
* LifecycleRule::MatchesStorageClassStandard(),
* LifecycleRule::MaxAge(7), LifecycleRule::IsLive(true));
* @endcode
*
* @throws std::invalid_argument if the list of parameters is contradictory,
* for example, `IsLive(true)` and `IsLive(false)` are in the @p condition
* list.
* @return a LifecycleRuleCondition that is satisfied when all the
* @p condition conditions are satisfied.
* @tparam Condition the types of the conditions, they must all be convertible
* to `LifecycleRuleCondition`.
*/
template <typename... Condition>
static LifecycleRuleCondition ConditionConjunction(Condition&&... condition) {
LifecycleRuleCondition result;
MergeConditions(result, std::forward<Condition>(condition)...);
return result;
}
private:
LifecycleRule() = default;
static void MergeConditions(LifecycleRuleCondition& result,
LifecycleRuleCondition const& rhs);
template <typename... Condition>
static void MergeConditions(LifecycleRuleCondition& result,
LifecycleRuleCondition const& head,
Condition&&... tail) {
MergeConditions(result, head);
MergeConditions(result, std::forward<Condition>(tail)...);
}
LifecycleRuleAction action_;
LifecycleRuleCondition condition_;
};
inline bool operator==(LifecycleRule const& lhs, LifecycleRule const& rhs) {
return std::tie(lhs.condition(), lhs.action()) ==
std::tie(rhs.condition(), rhs.action());
}
inline bool operator<(LifecycleRule const& lhs, LifecycleRule const& rhs) {
return std::tie(lhs.action(), lhs.condition()) <
std::tie(rhs.action(), rhs.condition());
}
inline bool operator!=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
return std::rel_ops::operator!=(lhs, rhs);
}
inline bool operator>(LifecycleRule const& lhs, LifecycleRule const& rhs) {
return std::rel_ops::operator>(lhs, rhs);
}
inline bool operator<=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
return std::rel_ops::operator<=(lhs, rhs);
}
inline bool operator>=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
return std::rel_ops::operator>=(lhs, rhs);
}
std::ostream& operator<<(std::ostream& os, LifecycleRule const& rhs);
GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
} // namespace storage
} // namespace cloud
} // namespace google
#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H