Google Cloud Storage C++ Client 2.13.0
A C++ Client Library for Google Cloud Storage
Loading...
Searching...
No Matches
lifecycle_rule.h
1// Copyright 2018 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H
17
18#include "google/cloud/storage/storage_class.h"
19#include "google/cloud/storage/version.h"
20#include "google/cloud/internal/parse_rfc3339.h"
21#include "google/cloud/optional.h"
22#include "google/cloud/status_or.h"
23#include "absl/time/civil_time.h"
24#include "absl/types/optional.h"
25#include <iosfwd>
26#include <string>
27#include <utility>
28#include <vector>
29
30namespace google {
31namespace cloud {
32namespace storage {
33GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
34
35/// Implement a wrapper for Lifecycle Rules actions.
37 std::string type;
38 std::string storage_class;
39};
40
41inline bool operator==(LifecycleRuleAction const& lhs,
42 LifecycleRuleAction const& rhs) {
43 return std::tie(lhs.type, lhs.storage_class) ==
44 std::tie(rhs.type, rhs.storage_class);
45}
46
47inline bool operator<(LifecycleRuleAction const& lhs,
48 LifecycleRuleAction const& rhs) {
49 return std::tie(lhs.type, lhs.storage_class) <
50 std::tie(rhs.type, rhs.storage_class);
51}
52
53inline bool operator!=(LifecycleRuleAction const& lhs,
54 LifecycleRuleAction const& rhs) {
55 return std::rel_ops::operator!=(lhs, rhs);
56}
57
58inline bool operator>(LifecycleRuleAction const& lhs,
59 LifecycleRuleAction const& rhs) {
60 return std::rel_ops::operator>(lhs, rhs);
61}
62
63inline bool operator<=(LifecycleRuleAction const& lhs,
64 LifecycleRuleAction const& rhs) {
65 return std::rel_ops::operator<=(lhs, rhs);
66}
67
68inline bool operator>=(LifecycleRuleAction const& lhs,
69 LifecycleRuleAction const& rhs) {
70 return std::rel_ops::operator>=(lhs, rhs);
71}
72
73std::ostream& operator<<(std::ostream& os, LifecycleRuleAction const& rhs);
74
75/// Implement a wrapper for Lifecycle Conditions.
77 absl::optional<std::int32_t> age;
78 absl::optional<absl::CivilDay> created_before;
79 absl::optional<bool> is_live;
80 absl::optional<std::vector<std::string>> matches_storage_class;
81 absl::optional<std::int32_t> num_newer_versions;
82 absl::optional<std::int32_t> days_since_noncurrent_time;
83 absl::optional<absl::CivilDay> noncurrent_time_before;
84 absl::optional<std::int32_t> days_since_custom_time;
85 absl::optional<absl::CivilDay> custom_time_before;
86 absl::optional<std::vector<std::string>> matches_prefix;
87 absl::optional<std::vector<std::string>> matches_suffix;
88};
89
90bool operator==(LifecycleRuleCondition const& lhs,
91 LifecycleRuleCondition const& rhs);
92
93bool operator<(LifecycleRuleCondition const& lhs,
94 LifecycleRuleCondition const& rhs);
95
96inline bool operator!=(LifecycleRuleCondition const& lhs,
97 LifecycleRuleCondition const& rhs) {
98 return std::rel_ops::operator!=(lhs, rhs);
99}
100
101inline bool operator>(LifecycleRuleCondition const& lhs,
102 LifecycleRuleCondition const& rhs) {
103 return std::rel_ops::operator>(lhs, rhs);
104}
105
106inline bool operator<=(LifecycleRuleCondition const& lhs,
107 LifecycleRuleCondition const& rhs) {
108 return std::rel_ops::operator<=(lhs, rhs);
109}
110
111inline bool operator>=(LifecycleRuleCondition const& lhs,
112 LifecycleRuleCondition const& rhs) {
113 return std::rel_ops::operator>=(lhs, rhs);
114}
115
116std::ostream& operator<<(std::ostream& os, LifecycleRuleCondition const& rhs);
117
118/**
119 * Defines objects to read, create, and modify Object Lifecycle Rules.
120 *
121 * Object Lifecycle Rules allow to configure a Bucket to automatically delete
122 * or change the storage class of objects as they go through lifecycle events.
123 *
124 * @see https://cloud.google.com/storage/docs/lifecycle for general information
125 * on Object Lifecycle Management in Google Cloud Storage.
126 */
127class LifecycleRule {
128 public:
129 explicit LifecycleRule(LifecycleRuleCondition condition,
130 LifecycleRuleAction action)
131 : action_(std::move(action)), condition_(std::move(condition)) {}
132
133 LifecycleRuleAction const& action() const { return action_; }
134 LifecycleRuleCondition const& condition() const { return condition_; }
135
136 /**
137 * @name Creates different types of LifecycleRule actions.
138 */
139 ///@{
149 static LifecycleRuleAction SetStorageClass(std::string storage_class);
150 ///@}
151
152 /**
153 * @name Creates different types of LifecycleRule rules.
154 */
155 ///@{
156 static LifecycleRuleCondition MaxAge(std::int32_t days) {
158 result.age.emplace(std::move(days));
159 return result;
160 }
161
162 static LifecycleRuleCondition CreatedBefore(absl::CivilDay date) {
164 result.created_before.emplace(std::move(date));
165 return result;
166 }
167
168 static LifecycleRuleCondition IsLive(bool value) {
170 result.is_live.emplace(std::move(value));
171 return result;
172 }
173
174 static LifecycleRuleCondition MatchesStorageClass(std::string storage_class) {
175 std::vector<std::string> value;
176 value.emplace_back(std::move(storage_class));
178 result.matches_storage_class.emplace(std::move(value));
179 return result;
180 }
181
183 std::initializer_list<std::string> list) {
184 std::vector<std::string> classes(std::move(list));
186 result.matches_storage_class.emplace(std::move(classes));
187 return result;
188 }
189
190 template <typename Iterator>
191 static LifecycleRuleCondition MatchesStorageClasses(Iterator begin,
192 Iterator end) {
193 std::vector<std::string> classes(begin, end);
195 result.matches_storage_class.emplace(std::move(classes));
196 return result;
197 }
198
201 }
202
205 }
206
209 }
210
213 }
214
217 }
218
222 }
223
226 }
227
228 static LifecycleRuleCondition NumNewerVersions(std::int32_t days) {
230 result.num_newer_versions.emplace(std::move(days));
231 return result;
232 }
233
234 static LifecycleRuleCondition DaysSinceNoncurrentTime(std::int32_t days) {
236 result.days_since_noncurrent_time = days;
237 return result;
238 }
239
240 static LifecycleRuleCondition NoncurrentTimeBefore(absl::CivilDay date) {
242 result.noncurrent_time_before = date;
243 return result;
244 }
245
246 static LifecycleRuleCondition DaysSinceCustomTime(std::int32_t days) {
248 result.days_since_custom_time = days;
249 return result;
250 }
251
252 static LifecycleRuleCondition CustomTimeBefore(absl::CivilDay date) {
254 result.custom_time_before = date;
255 return result;
256 }
257
258 static LifecycleRuleCondition MatchesPrefix(std::string prefix) {
259 std::vector<std::string> value;
260 value.emplace_back(std::move(prefix));
262 result.matches_prefix.emplace(std::move(value));
263 return result;
264 }
265
267 std::initializer_list<std::string> list) {
268 std::vector<std::string> classes(std::move(list));
270 result.matches_prefix.emplace(std::move(classes));
271 return result;
272 }
273
274 static LifecycleRuleCondition MatchesSuffix(std::string suffix) {
275 std::vector<std::string> value;
276 value.emplace_back(std::move(suffix));
278 result.matches_suffix.emplace(std::move(value));
279 return result;
280 }
281
283 std::initializer_list<std::string> list) {
284 std::vector<std::string> classes(std::move(list));
286 result.matches_suffix.emplace(std::move(classes));
287 return result;
288 }
289 ///@}
290
291 /**
292 * Combines multiple LifecycleRule conditions using conjunction.
293 *
294 * Create a condition that require all the @p condition parameters to be met
295 * to take effect.
296 *
297 * @par Example
298 *
299 * @code
300 * // Affect objects that are in the STANDARD storage class, have at
301 * // least 2 new versions, are at least 7 days old, and are alive.
302 * LifecycleRuleCondition condition = LifecycleRule::ConditionConjunction(
303 * LifecycleRule::NumNewerVersions(2),
304 * LifecycleRule::MatchesStorageClassStandard(),
305 * LifecycleRule::MaxAge(7), LifecycleRule::IsLive(true));
306 * @endcode
307 *
308 * @throws std::invalid_argument if the list of parameters is contradictory,
309 * for example, `IsLive(true)` and `IsLive(false)` are in the @p condition
310 * list.
311 * @return a LifecycleRuleCondition that is satisfied when all the
312 * @p condition conditions are satisfied.
313 * @tparam Condition the types of the conditions, they must all be convertible
314 * to `LifecycleRuleCondition`.
315 */
316 template <typename... Condition>
317 static LifecycleRuleCondition ConditionConjunction(Condition&&... condition) {
319 MergeConditions(result, std::forward<Condition>(condition)...);
320 return result;
321 }
322
323 private:
324 LifecycleRule() = default;
325
326 static void MergeConditions(LifecycleRuleCondition& result,
327 LifecycleRuleCondition const& rhs);
328
329 template <typename... Condition>
330 static void MergeConditions(LifecycleRuleCondition& result,
331 LifecycleRuleCondition const& head,
332 Condition&&... tail) {
333 MergeConditions(result, head);
334 MergeConditions(result, std::forward<Condition>(tail)...);
335 }
336
337 LifecycleRuleAction action_;
338 LifecycleRuleCondition condition_;
339};
340
341inline bool operator==(LifecycleRule const& lhs, LifecycleRule const& rhs) {
342 return std::tie(lhs.condition(), lhs.action()) ==
343 std::tie(rhs.condition(), rhs.action());
344}
345
346inline bool operator<(LifecycleRule const& lhs, LifecycleRule const& rhs) {
347 return std::tie(lhs.action(), lhs.condition()) <
348 std::tie(rhs.action(), rhs.condition());
349}
350
351inline bool operator!=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
352 return std::rel_ops::operator!=(lhs, rhs);
353}
354
355inline bool operator>(LifecycleRule const& lhs, LifecycleRule const& rhs) {
356 return std::rel_ops::operator>(lhs, rhs);
357}
358
359inline bool operator<=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
360 return std::rel_ops::operator<=(lhs, rhs);
361}
362
363inline bool operator>=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
364 return std::rel_ops::operator>=(lhs, rhs);
365}
366
367std::ostream& operator<<(std::ostream& os, LifecycleRule const& rhs);
368GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
369} // namespace storage
370} // namespace cloud
371} // namespace google
372
373#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H
Defines objects to read, create, and modify Object Lifecycle Rules.
Definition: lifecycle_rule.h:127
static LifecycleRuleCondition CustomTimeBefore(absl::CivilDay date)
Definition: lifecycle_rule.h:252
static LifecycleRuleAction AbortIncompleteMultipartUpload()
static LifecycleRuleAction SetStorageClassRegional()
static LifecycleRuleCondition MatchesStorageClassArchive()
Definition: lifecycle_rule.h:224
static LifecycleRuleCondition NoncurrentTimeBefore(absl::CivilDay date)
Definition: lifecycle_rule.h:240
static LifecycleRuleCondition MatchesPrefixes(std::initializer_list< std::string > list)
Definition: lifecycle_rule.h:266
static LifecycleRuleCondition MatchesStorageClassColdline()
Definition: lifecycle_rule.h:215
static LifecycleRuleAction Delete()
static LifecycleRuleCondition MatchesPrefix(std::string prefix)
Definition: lifecycle_rule.h:258
LifecycleRuleCondition const & condition() const
Definition: lifecycle_rule.h:134
static LifecycleRuleCondition MatchesStorageClassNearline()
Definition: lifecycle_rule.h:211
static LifecycleRuleCondition DaysSinceNoncurrentTime(std::int32_t days)
Definition: lifecycle_rule.h:234
static LifecycleRuleAction SetStorageClassNearline()
static LifecycleRuleCondition MatchesStorageClassRegional()
Definition: lifecycle_rule.h:207
static LifecycleRuleCondition ConditionConjunction(Condition &&... condition)
Combines multiple LifecycleRule conditions using conjunction.
Definition: lifecycle_rule.h:317
static LifecycleRuleCondition MatchesStorageClassStandard()
Definition: lifecycle_rule.h:199
static LifecycleRuleCondition MatchesSuffix(std::string suffix)
Definition: lifecycle_rule.h:274
static LifecycleRuleCondition NumNewerVersions(std::int32_t days)
Definition: lifecycle_rule.h:228
static LifecycleRuleCondition MatchesStorageClasses(std::initializer_list< std::string > list)
Definition: lifecycle_rule.h:182
static LifecycleRuleCondition MatchesStorageClassDurableReducedAvailability()
Definition: lifecycle_rule.h:220
LifecycleRule(LifecycleRuleCondition condition, LifecycleRuleAction action)
Definition: lifecycle_rule.h:129
static LifecycleRuleCondition MatchesStorageClasses(Iterator begin, Iterator end)
Definition: lifecycle_rule.h:191
static LifecycleRuleAction SetStorageClassMultiRegional()
static LifecycleRuleAction SetStorageClassColdline()
static LifecycleRuleCondition IsLive(bool value)
Definition: lifecycle_rule.h:168
static LifecycleRuleAction SetStorageClass(std::string storage_class)
static LifecycleRuleAction SetStorageClassStandard()
static LifecycleRuleAction SetStorageClassArchive()
static LifecycleRuleCondition MatchesStorageClassMultiRegional()
Definition: lifecycle_rule.h:203
static LifecycleRuleCondition MatchesStorageClass(std::string storage_class)
Definition: lifecycle_rule.h:174
static LifecycleRuleCondition MatchesSuffixes(std::initializer_list< std::string > list)
Definition: lifecycle_rule.h:282
static LifecycleRuleAction SetStorageClassDurableReducedAvailability()
static LifecycleRuleCondition DaysSinceCustomTime(std::int32_t days)
Definition: lifecycle_rule.h:246
static LifecycleRuleCondition CreatedBefore(absl::CivilDay date)
Definition: lifecycle_rule.h:162
LifecycleRuleAction const & action() const
Definition: lifecycle_rule.h:133
static LifecycleRuleCondition MaxAge(std::int32_t days)
Definition: lifecycle_rule.h:156
Contains functions to return well-known storage class names.
Definition: storage_class.h:25
char const * Nearline()
Definition: storage_class.h:41
char const * MultiRegional()
Definition: storage_class.h:31
char const * DurableReducedAvailability()
Definition: storage_class.h:51
char const * Archive()
Definition: storage_class.h:56
char const * Standard()
Definition: storage_class.h:26
char const * Regional()
Definition: storage_class.h:36
char const * Coldline()
Definition: storage_class.h:46
Contains all the Google Cloud Storage C++ client APIs.
Definition: auto_finalize.h:24
bool operator<(LifecycleRuleAction const &lhs, LifecycleRuleAction const &rhs)
Definition: lifecycle_rule.h:47
bool operator!=(LifecycleRule const &lhs, LifecycleRule const &rhs)
Definition: lifecycle_rule.h:351
bool operator==(LifecycleRuleAction const &lhs, LifecycleRuleAction const &rhs)
Definition: lifecycle_rule.h:41
bool operator<=(LifecycleRuleCondition const &lhs, LifecycleRuleCondition const &rhs)
Definition: lifecycle_rule.h:106
bool operator==(LifecycleRuleCondition const &lhs, LifecycleRuleCondition const &rhs)
bool operator>(LifecycleRuleCondition const &lhs, LifecycleRuleCondition const &rhs)
Definition: lifecycle_rule.h:101
bool operator!=(LifecycleRuleCondition const &lhs, LifecycleRuleCondition const &rhs)
Definition: lifecycle_rule.h:96
bool operator!=(LifecycleRuleAction const &lhs, LifecycleRuleAction const &rhs)
Definition: lifecycle_rule.h:53
bool operator>(LifecycleRule const &lhs, LifecycleRule const &rhs)
Definition: lifecycle_rule.h:355
bool operator>=(LifecycleRuleAction const &lhs, LifecycleRuleAction const &rhs)
Definition: lifecycle_rule.h:68
bool operator>=(LifecycleRuleCondition const &lhs, LifecycleRuleCondition const &rhs)
Definition: lifecycle_rule.h:111
bool operator==(LifecycleRule const &lhs, LifecycleRule const &rhs)
Definition: lifecycle_rule.h:341
bool operator>(LifecycleRuleAction const &lhs, LifecycleRuleAction const &rhs)
Definition: lifecycle_rule.h:58
bool operator<=(LifecycleRuleAction const &lhs, LifecycleRuleAction const &rhs)
Definition: lifecycle_rule.h:63
bool operator<(LifecycleRuleCondition const &lhs, LifecycleRuleCondition const &rhs)
bool operator>=(LifecycleRule const &lhs, LifecycleRule const &rhs)
Definition: lifecycle_rule.h:363
bool operator<=(LifecycleRule const &lhs, LifecycleRule const &rhs)
Definition: lifecycle_rule.h:359
bool operator<(LifecycleRule const &lhs, LifecycleRule const &rhs)
Definition: lifecycle_rule.h:346
Implement a wrapper for Lifecycle Rules actions.
Definition: lifecycle_rule.h:36
std::string storage_class
Definition: lifecycle_rule.h:38
std::string type
Definition: lifecycle_rule.h:37
Implement a wrapper for Lifecycle Conditions.
Definition: lifecycle_rule.h:76
absl::optional< absl::CivilDay > custom_time_before
Definition: lifecycle_rule.h:85
absl::optional< std::int32_t > num_newer_versions
Definition: lifecycle_rule.h:81
absl::optional< std::vector< std::string > > matches_prefix
Definition: lifecycle_rule.h:86
absl::optional< std::int32_t > age
Definition: lifecycle_rule.h:77
absl::optional< std::vector< std::string > > matches_storage_class
Definition: lifecycle_rule.h:80
absl::optional< std::vector< std::string > > matches_suffix
Definition: lifecycle_rule.h:87
absl::optional< std::int32_t > days_since_noncurrent_time
Definition: lifecycle_rule.h:82
absl::optional< absl::CivilDay > created_before
Definition: lifecycle_rule.h:78
absl::optional< absl::CivilDay > noncurrent_time_before
Definition: lifecycle_rule.h:83
absl::optional< bool > is_live
Definition: lifecycle_rule.h:79
absl::optional< std::int32_t > days_since_custom_time
Definition: lifecycle_rule.h:84