Google Cloud Storage C++ Client  1.42.0
A C++ Client Library for Google Cloud Storage
lifecycle_rule.h
Go to the documentation of this file.
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 
30 namespace google {
31 namespace cloud {
32 namespace storage {
34 namespace internal {
35 struct LifecycleRuleParser;
36 struct GrpcBucketMetadataParser;
37 } // namespace internal
38 
39 /// Implement a wrapper for Lifecycle Rules actions.
41  std::string type;
42  std::string storage_class;
43 };
44 
45 inline bool operator==(LifecycleRuleAction const& lhs,
46  LifecycleRuleAction const& rhs) {
47  return std::tie(lhs.type, lhs.storage_class) ==
48  std::tie(rhs.type, rhs.storage_class);
49 }
50 
51 inline bool operator<(LifecycleRuleAction const& lhs,
52  LifecycleRuleAction const& rhs) {
53  return std::tie(lhs.type, lhs.storage_class) <
54  std::tie(rhs.type, rhs.storage_class);
55 }
56 
57 inline bool operator!=(LifecycleRuleAction const& lhs,
58  LifecycleRuleAction const& rhs) {
59  return std::rel_ops::operator!=(lhs, rhs);
60 }
61 
62 inline bool operator>(LifecycleRuleAction const& lhs,
63  LifecycleRuleAction const& rhs) {
64  return std::rel_ops::operator>(lhs, rhs);
65 }
66 
67 inline bool operator<=(LifecycleRuleAction const& lhs,
68  LifecycleRuleAction const& rhs) {
69  return std::rel_ops::operator<=(lhs, rhs);
70 }
71 
72 inline bool operator>=(LifecycleRuleAction const& lhs,
73  LifecycleRuleAction const& rhs) {
74  return std::rel_ops::operator>=(lhs, rhs);
75 }
76 
77 std::ostream& operator<<(std::ostream& os, LifecycleRuleAction const& rhs);
78 
79 /// Implement a wrapper for Lifecycle Conditions.
81  absl::optional<std::int32_t> age;
82  absl::optional<absl::CivilDay> created_before;
83  absl::optional<bool> is_live;
84  absl::optional<std::vector<std::string>> matches_storage_class;
85  absl::optional<std::int32_t> num_newer_versions;
86  absl::optional<std::int32_t> days_since_noncurrent_time;
87  absl::optional<absl::CivilDay> noncurrent_time_before;
88  absl::optional<std::int32_t> days_since_custom_time;
89  absl::optional<absl::CivilDay> custom_time_before;
90  absl::optional<std::vector<std::string>> matches_prefix;
91  absl::optional<std::vector<std::string>> matches_suffix;
92 };
93 
94 bool operator==(LifecycleRuleCondition const& lhs,
95  LifecycleRuleCondition const& rhs);
96 
98  LifecycleRuleCondition const& rhs);
99 
100 inline bool operator!=(LifecycleRuleCondition const& lhs,
101  LifecycleRuleCondition const& rhs) {
102  return std::rel_ops::operator!=(lhs, rhs);
103 }
104 
105 inline bool operator>(LifecycleRuleCondition const& lhs,
106  LifecycleRuleCondition const& rhs) {
107  return std::rel_ops::operator>(lhs, rhs);
108 }
109 
110 inline bool operator<=(LifecycleRuleCondition const& lhs,
111  LifecycleRuleCondition const& rhs) {
112  return std::rel_ops::operator<=(lhs, rhs);
113 }
114 
115 inline bool operator>=(LifecycleRuleCondition const& lhs,
116  LifecycleRuleCondition const& rhs) {
117  return std::rel_ops::operator>=(lhs, rhs);
118 }
119 
120 std::ostream& operator<<(std::ostream& os, LifecycleRuleCondition const& rhs);
121 
122 /**
123  * Defines objects to read, create, and modify Object Lifecycle Rules.
124  *
125  * Object Lifecycle Rules allow to configure a Bucket to automatically delete
126  * or change the storage class of objects as they go through lifecycle events.
127  *
128  * @see https://cloud.google.com/storage/docs/lifecycle for general information
129  * on Object Lifecycle Management in Google Cloud Storage.
130  */
132  public:
134  LifecycleRuleAction action)
135  : action_(std::move(action)), condition_(std::move(condition)) {}
136 
137  LifecycleRuleAction const& action() const { return action_; }
138  LifecycleRuleCondition const& condition() const { return condition_; }
139 
140  /**
141  * @name Creates different types of LifecycleRule actions.
142  */
143  ///@{
153  static LifecycleRuleAction SetStorageClass(std::string storage_class);
154  ///@}
155 
156  /**
157  * @name Creates different types of LifecycleRule rules.
158  */
159  ///@{
160  static LifecycleRuleCondition MaxAge(std::int32_t days) {
161  LifecycleRuleCondition result;
162  result.age.emplace(std::move(days));
163  return result;
164  }
165 
166  static LifecycleRuleCondition CreatedBefore(absl::CivilDay date) {
167  LifecycleRuleCondition result;
168  result.created_before.emplace(std::move(date));
169  return result;
170  }
171 
172  static LifecycleRuleCondition IsLive(bool value) {
173  LifecycleRuleCondition result;
174  result.is_live.emplace(std::move(value));
175  return result;
176  }
177 
178  static LifecycleRuleCondition MatchesStorageClass(std::string storage_class) {
179  std::vector<std::string> value;
180  value.emplace_back(std::move(storage_class));
181  LifecycleRuleCondition result;
182  result.matches_storage_class.emplace(std::move(value));
183  return result;
184  }
185 
187  std::initializer_list<std::string> list) {
188  std::vector<std::string> classes(std::move(list));
189  LifecycleRuleCondition result;
190  result.matches_storage_class.emplace(std::move(classes));
191  return result;
192  }
193 
194  template <typename Iterator>
196  Iterator end) {
197  std::vector<std::string> classes(begin, end);
198  LifecycleRuleCondition result;
199  result.matches_storage_class.emplace(std::move(classes));
200  return result;
201  }
202 
205  }
206 
209  }
210 
213  }
214 
217  }
218 
221  }
222 
226  }
227 
230  }
231 
232  static LifecycleRuleCondition NumNewerVersions(std::int32_t days) {
233  LifecycleRuleCondition result;
234  result.num_newer_versions.emplace(std::move(days));
235  return result;
236  }
237 
239  LifecycleRuleCondition result;
240  result.days_since_noncurrent_time = days;
241  return result;
242  }
243 
244  static LifecycleRuleCondition NoncurrentTimeBefore(absl::CivilDay date) {
245  LifecycleRuleCondition result;
246  result.noncurrent_time_before = date;
247  return result;
248  }
249 
250  static LifecycleRuleCondition DaysSinceCustomTime(std::int32_t days) {
251  LifecycleRuleCondition result;
252  result.days_since_custom_time = days;
253  return result;
254  }
255 
256  static LifecycleRuleCondition CustomTimeBefore(absl::CivilDay date) {
257  LifecycleRuleCondition result;
258  result.custom_time_before = date;
259  return result;
260  }
261 
262  static LifecycleRuleCondition MatchesPrefix(std::string prefix) {
263  std::vector<std::string> value;
264  value.emplace_back(std::move(prefix));
265  LifecycleRuleCondition result;
266  result.matches_prefix.emplace(std::move(value));
267  return result;
268  }
269 
271  std::initializer_list<std::string> list) {
272  std::vector<std::string> classes(std::move(list));
273  LifecycleRuleCondition result;
274  result.matches_prefix.emplace(std::move(classes));
275  return result;
276  }
277 
278  static LifecycleRuleCondition MatchesSuffix(std::string suffix) {
279  std::vector<std::string> value;
280  value.emplace_back(std::move(suffix));
281  LifecycleRuleCondition result;
282  result.matches_suffix.emplace(std::move(value));
283  return result;
284  }
285 
287  std::initializer_list<std::string> list) {
288  std::vector<std::string> classes(std::move(list));
289  LifecycleRuleCondition result;
290  result.matches_suffix.emplace(std::move(classes));
291  return result;
292  }
293  ///@}
294 
295  /**
296  * Combines multiple LifecycleRule conditions using conjunction.
297  *
298  * Create a condition that require all the @p condition parameters to be met
299  * to take effect.
300  *
301  * @par Example
302  *
303  * @code
304  * // Affect objects that are in the STANDARD storage class, have at
305  * // least 2 new versions, are at least 7 days old, and are alive.
306  * LifecycleRuleCondition condition = LifecycleRule::ConditionConjunction(
307  * LifecycleRule::NumNewerVersions(2),
308  * LifecycleRule::MatchesStorageClassStandard(),
309  * LifecycleRule::MaxAge(7), LifecycleRule::IsLive(true));
310  * @endcode
311  *
312  * @throws std::invalid_argument if the list of parameters is contradictory,
313  * for example, `IsLive(true)` and `IsLive(false)` are in the @p condition
314  * list.
315  * @return a LifecycleRuleCondition that is satisfied when all the
316  * @p condition conditions are satisfied.
317  * @tparam Condition the types of the conditions, they must all be convertible
318  * to `LifecycleRuleCondition`.
319  */
320  template <typename... Condition>
321  static LifecycleRuleCondition ConditionConjunction(Condition&&... condition) {
322  LifecycleRuleCondition result;
323  MergeConditions(result, std::forward<Condition>(condition)...);
324  return result;
325  }
326 
327  private:
328  friend struct internal::LifecycleRuleParser;
329  friend struct internal::GrpcBucketMetadataParser;
330 
331  LifecycleRule() = default;
332 
333  static void MergeConditions(LifecycleRuleCondition& result,
334  LifecycleRuleCondition const& rhs);
335 
336  template <typename... Condition>
337  static void MergeConditions(LifecycleRuleCondition& result,
338  LifecycleRuleCondition const& head,
339  Condition&&... tail) {
340  MergeConditions(result, head);
341  MergeConditions(result, std::forward<Condition>(tail)...);
342  }
343 
344  LifecycleRuleAction action_;
345  LifecycleRuleCondition condition_;
346 };
347 
348 inline bool operator==(LifecycleRule const& lhs, LifecycleRule const& rhs) {
349  return std::tie(lhs.condition(), lhs.action()) ==
350  std::tie(rhs.condition(), rhs.action());
351 }
352 
353 inline bool operator<(LifecycleRule const& lhs, LifecycleRule const& rhs) {
354  return std::tie(lhs.action(), lhs.condition()) <
355  std::tie(rhs.action(), rhs.condition());
356 }
357 
358 inline bool operator!=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
359  return std::rel_ops::operator!=(lhs, rhs);
360 }
361 
362 inline bool operator>(LifecycleRule const& lhs, LifecycleRule const& rhs) {
363  return std::rel_ops::operator>(lhs, rhs);
364 }
365 
366 inline bool operator<=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
367  return std::rel_ops::operator<=(lhs, rhs);
368 }
369 
370 inline bool operator>=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
371  return std::rel_ops::operator>=(lhs, rhs);
372 }
373 
374 std::ostream& operator<<(std::ostream& os, LifecycleRule const& rhs);
376 } // namespace storage
377 } // namespace cloud
378 } // namespace google
379 
380 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H