Google Cloud Storage C++ Client  1.32.1
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 // http://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 {
33 inline namespace STORAGE_CLIENT_NS {
34 namespace internal {
35 struct LifecycleRuleParser;
36 class GrpcClient;
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 };
91 
92 inline bool operator==(LifecycleRuleCondition const& lhs,
93  LifecycleRuleCondition const& rhs) {
94  return lhs.age == rhs.age && lhs.created_before == rhs.created_before &&
95  lhs.is_live == rhs.is_live &&
102 }
103 
104 inline bool operator<(LifecycleRuleCondition const& lhs,
105  LifecycleRuleCondition const& rhs) {
106  return std::tie(lhs.age, lhs.created_before, lhs.is_live,
110  std::tie(rhs.age, rhs.created_before, rhs.is_live,
114 }
115 
116 inline bool operator!=(LifecycleRuleCondition const& lhs,
117  LifecycleRuleCondition const& rhs) {
118  return std::rel_ops::operator!=(lhs, rhs);
119 }
120 
121 inline bool operator>(LifecycleRuleCondition const& lhs,
122  LifecycleRuleCondition const& rhs) {
123  return std::rel_ops::operator>(lhs, rhs);
124 }
125 
126 inline bool operator<=(LifecycleRuleCondition const& lhs,
127  LifecycleRuleCondition const& rhs) {
128  return std::rel_ops::operator<=(lhs, rhs);
129 }
130 
131 inline bool operator>=(LifecycleRuleCondition const& lhs,
132  LifecycleRuleCondition const& rhs) {
133  return std::rel_ops::operator>=(lhs, rhs);
134 }
135 
136 std::ostream& operator<<(std::ostream& os, LifecycleRuleCondition const& rhs);
137 
138 /**
139  * Defines objects to read, create, and modify Object Lifecycle Rules.
140  *
141  * Object Lifecycle Rules allow to configure a Bucket to automatically delete
142  * or change the storage class of objects as they go through lifecycle events.
143  *
144  * @see https://cloud.google.com/storage/docs/lifecycle for general information
145  * on Object Lifecycle Management in Google Cloud Storage.
146  */
148  public:
150  LifecycleRuleAction action)
151  : action_(std::move(action)), condition_(std::move(condition)) {}
152 
153  LifecycleRuleAction const& action() const { return action_; }
154  LifecycleRuleCondition const& condition() const { return condition_; }
155 
156  //@{
157  /**
158  * @name Creates different types of LifecycleRule actions.
159  */
160  static LifecycleRuleAction Delete();
168  static LifecycleRuleAction SetStorageClass(std::string storage_class);
169  //@}
170 
171  //@{
172  /**
173  * @name Creates different types of LifecycleRule rules.
174  */
175  static LifecycleRuleCondition MaxAge(std::int32_t days) {
176  LifecycleRuleCondition result;
177  result.age.emplace(std::move(days));
178  return result;
179  }
180 
181  static LifecycleRuleCondition CreatedBefore(absl::CivilDay date) {
182  LifecycleRuleCondition result;
183  result.created_before.emplace(std::move(date));
184  return result;
185  }
186 
187  static LifecycleRuleCondition IsLive(bool value) {
188  LifecycleRuleCondition result;
189  result.is_live.emplace(std::move(value));
190  return result;
191  }
192 
193  static LifecycleRuleCondition MatchesStorageClass(std::string storage_class) {
194  std::vector<std::string> value;
195  value.emplace_back(std::move(storage_class));
196  LifecycleRuleCondition result;
197  result.matches_storage_class.emplace(std::move(value));
198  return result;
199  }
200 
202  std::initializer_list<std::string> list) {
203  std::vector<std::string> classes(std::move(list));
204  LifecycleRuleCondition result;
205  result.matches_storage_class.emplace(std::move(classes));
206  return result;
207  }
208 
209  template <typename Iterator>
211  Iterator end) {
212  std::vector<std::string> classes(begin, end);
213  LifecycleRuleCondition result;
214  result.matches_storage_class.emplace(std::move(classes));
215  return result;
216  }
217 
220  }
221 
224  }
225 
228  }
229 
232  }
233 
236  }
237 
241  }
242 
245  }
246 
247  static LifecycleRuleCondition NumNewerVersions(std::int32_t days) {
248  LifecycleRuleCondition result;
249  result.num_newer_versions.emplace(std::move(days));
250  return result;
251  }
252 
254  LifecycleRuleCondition result;
255  result.days_since_noncurrent_time = days;
256  return result;
257  }
258 
259  static LifecycleRuleCondition NoncurrentTimeBefore(absl::CivilDay date) {
260  LifecycleRuleCondition result;
261  result.noncurrent_time_before = date;
262  return result;
263  }
264 
265  static LifecycleRuleCondition DaysSinceCustomTime(std::int32_t days) {
266  LifecycleRuleCondition result;
267  result.days_since_custom_time = days;
268  return result;
269  }
270 
271  static LifecycleRuleCondition CustomTimeBefore(absl::CivilDay date) {
272  LifecycleRuleCondition result;
273  result.custom_time_before = date;
274  return result;
275  }
276  //@}
277 
278  /**
279  * Combines multiple LifecycleRule conditions using conjunction.
280  *
281  * Create a condition that require all the @p condition parameters to be met
282  * to take effect.
283  *
284  * @par Example
285  *
286  * @code
287  * // Affect objects that are in the STANDARD storage class, have at
288  * // least 2 new versions, are at least 7 days old, and are alive.
289  * LifecycleRuleCondition condition = LifecycleRule::ConditionConjunction(
290  * LifecycleRule::NumNewerVersions(2),
291  * LifecycleRule::MatchesStorageClassStandard(),
292  * LifecycleRule::MaxAge(7), LifecycleRule::IsLive(true));
293  * @endcode
294  *
295  * @throws std::invalid_argument if the list of parameters is contradictory,
296  * for example, `IsLive(true)` and `IsLive(false)` are in the @p condition
297  * list.
298  * @return a LifecycleRuleCondition that is satisfied when all the
299  * @p condition conditions are satisfied.
300  * @tparam Condition the types of the conditions, they must all be convertible
301  * to `LifecycleRuleCondition`.
302  */
303  template <typename... Condition>
304  static LifecycleRuleCondition ConditionConjunction(Condition&&... condition) {
305  LifecycleRuleCondition result;
306  MergeConditions(result, std::forward<Condition>(condition)...);
307  return result;
308  }
309 
310  private:
311  friend struct internal::LifecycleRuleParser;
312  friend class internal::GrpcClient;
313 
314  LifecycleRule() = default;
315 
316  static void MergeConditions(LifecycleRuleCondition& result,
317  LifecycleRuleCondition const& rhs);
318 
319  template <typename... Condition>
320  static void MergeConditions(LifecycleRuleCondition& result,
321  LifecycleRuleCondition const& head,
322  Condition&&... tail) {
323  MergeConditions(result, head);
324  MergeConditions(result, std::forward<Condition>(tail)...);
325  }
326 
327  LifecycleRuleAction action_;
328  LifecycleRuleCondition condition_;
329 };
330 
331 inline bool operator==(LifecycleRule const& lhs, LifecycleRule const& rhs) {
332  return std::tie(lhs.condition(), lhs.action()) ==
333  std::tie(rhs.condition(), rhs.action());
334 }
335 
336 inline bool operator<(LifecycleRule const& lhs, LifecycleRule const& rhs) {
337  return std::tie(lhs.action(), lhs.condition()) <
338  std::tie(rhs.action(), rhs.condition());
339 }
340 
341 inline bool operator!=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
342  return std::rel_ops::operator!=(lhs, rhs);
343 }
344 
345 inline bool operator>(LifecycleRule const& lhs, LifecycleRule const& rhs) {
346  return std::rel_ops::operator>(lhs, rhs);
347 }
348 
349 inline bool operator<=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
350  return std::rel_ops::operator<=(lhs, rhs);
351 }
352 
353 inline bool operator>=(LifecycleRule const& lhs, LifecycleRule const& rhs) {
354  return std::rel_ops::operator>=(lhs, rhs);
355 }
356 
357 std::ostream& operator<<(std::ostream& os, LifecycleRule const& rhs);
358 } // namespace STORAGE_CLIENT_NS
359 } // namespace storage
360 } // namespace cloud
361 } // namespace google
362 
363 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_LIFECYCLE_RULE_H