Google Cloud Storage C++ Client  1.40.2
A C++ Client Library for Google Cloud Storage
bucket_metadata.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_BUCKET_METADATA_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_BUCKET_METADATA_H
17 
18 #include "google/cloud/storage/bucket_access_control.h"
19 #include "google/cloud/storage/internal/common_metadata.h"
20 #include "google/cloud/storage/internal/patch_builder.h"
21 #include "google/cloud/storage/lifecycle_rule.h"
22 #include "google/cloud/storage/object_access_control.h"
23 #include "google/cloud/storage/version.h"
24 #include "google/cloud/optional.h"
25 #include "absl/types/optional.h"
26 #include <chrono>
27 #include <map>
28 #include <string>
29 #include <tuple>
30 #include <utility>
31 #include <vector>
32 
33 namespace google {
34 namespace cloud {
35 namespace storage {
37 namespace internal {
38 struct BucketMetadataParser;
39 struct GrpcBucketMetadataParser;
40 struct GrpcBucketRequestParser;
41 } // namespace internal
42 
43 /**
44  * The billing configuration for a Bucket.
45  *
46  * @see https://cloud.google.com/storage/docs/requester-pays for general
47  * information on "Requester Pays" billing.
48  */
49 struct BucketBilling {
50  BucketBilling() = default;
51  // NOLINTNEXTLINE(google-explicit-constructor)
52  BucketBilling(bool v) : requester_pays(v) {}
53 
54  bool requester_pays{false};
55 };
56 
57 inline bool operator==(BucketBilling const& lhs, BucketBilling const& rhs) {
58  return lhs.requester_pays == rhs.requester_pays;
59 }
60 
61 inline bool operator<(BucketBilling const& lhs, BucketBilling const& rhs) {
62  return !lhs.requester_pays && rhs.requester_pays;
63 }
64 
65 inline bool operator!=(BucketBilling const& lhs, BucketBilling const& rhs) {
66  return std::rel_ops::operator!=(lhs, rhs);
67 }
68 
69 inline bool operator>(BucketBilling const& lhs, BucketBilling const& rhs) {
70  return std::rel_ops::operator>(lhs, rhs);
71 }
72 
73 inline bool operator<=(BucketBilling const& lhs, BucketBilling const& rhs) {
74  return std::rel_ops::operator<=(lhs, rhs);
75 }
76 
77 inline bool operator>=(BucketBilling const& lhs, BucketBilling const& rhs) {
78  return std::rel_ops::operator>=(lhs, rhs);
79 }
80 
81 /**
82  * An entry in the CORS list.
83  *
84  * CORS (Cross-Origin Resource Sharing) is a mechanism to enable client-side
85  * cross-origin requests. An entry in the configuration has a maximum age and a
86  * list of allowed origin and methods, as well as a list of returned response
87  * headers.
88  *
89  * @see https://en.wikipedia.org/wiki/Cross-origin_resource_sharing for general
90  * information on CORS.
91  *
92  * @see https://cloud.google.com/storage/docs/cross-origin for general
93  * information about CORS in the context of Google Cloud Storage.
94  *
95  * @see https://cloud.google.com/storage/docs/configuring-cors for information
96  * on how to set and troubleshoot CORS settings.
97  */
98 struct CorsEntry {
99  absl::optional<std::int64_t> max_age_seconds;
100  std::vector<std::string> method;
101  std::vector<std::string> origin;
102  std::vector<std::string> response_header;
103 };
104 
105 //@{
106 /// @name Comparison operators for CorsEntry.
107 inline bool operator==(CorsEntry const& lhs, CorsEntry const& rhs) {
108  return std::tie(lhs.max_age_seconds, lhs.method, lhs.origin,
109  lhs.response_header) == std::tie(rhs.max_age_seconds,
110  rhs.method, rhs.origin,
111  rhs.response_header);
112 }
113 
114 inline bool operator<(CorsEntry const& lhs, CorsEntry const& rhs) {
115  return std::tie(lhs.max_age_seconds, lhs.method, lhs.origin,
116  lhs.response_header) < std::tie(rhs.max_age_seconds,
117  rhs.method, rhs.origin,
118  rhs.response_header);
119 }
120 
121 inline bool operator!=(CorsEntry const& lhs, CorsEntry const& rhs) {
122  return std::rel_ops::operator!=(lhs, rhs);
123 }
124 
125 inline bool operator>(CorsEntry const& lhs, CorsEntry const& rhs) {
126  return std::rel_ops::operator>(lhs, rhs);
127 }
128 
129 inline bool operator<=(CorsEntry const& lhs, CorsEntry const& rhs) {
130  return std::rel_ops::operator<=(lhs, rhs);
131 }
132 
133 inline bool operator>=(CorsEntry const& lhs, CorsEntry const& rhs) {
134  return std::rel_ops::operator>=(lhs, rhs);
135 }
136 //@}
137 
138 std::ostream& operator<<(std::ostream& os, CorsEntry const& rhs);
139 
140 /**
141  * Configure if only the IAM policies are used for access control.
142  *
143  * @see Before enabling Uniform Bucket Level Access please
144  * review the [feature documentation][ubla-link], as well as
145  * ["Should you use uniform bucket-level access ?"][ubla-should-link].
146  *
147  * [ubla-link]:
148  * https://cloud.google.com/storage/docs/uniform-bucket-level-access
149  * [ubla-should-link]:
150  * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
151  */
153  bool enabled;
154  std::chrono::system_clock::time_point locked_time;
155 };
156 using BucketPolicyOnly = UniformBucketLevelAccess;
157 
158 //@{
159 /// @name Comparison operators For UniformBucketLevelAccess
160 inline bool operator==(UniformBucketLevelAccess const& lhs,
161  UniformBucketLevelAccess const& rhs) {
162  return std::tie(lhs.enabled, lhs.locked_time) ==
163  std::tie(rhs.enabled, rhs.locked_time);
164 }
165 
166 inline bool operator<(UniformBucketLevelAccess const& lhs,
167  UniformBucketLevelAccess const& rhs) {
168  return std::tie(lhs.enabled, lhs.locked_time) <
169  std::tie(rhs.enabled, rhs.locked_time);
170 }
171 
172 inline bool operator!=(UniformBucketLevelAccess const& lhs,
173  UniformBucketLevelAccess const& rhs) {
174  return std::rel_ops::operator!=(lhs, rhs);
175 }
176 
177 inline bool operator>(UniformBucketLevelAccess const& lhs,
178  UniformBucketLevelAccess const& rhs) {
179  return std::rel_ops::operator>(lhs, rhs);
180 }
181 
182 inline bool operator<=(UniformBucketLevelAccess const& lhs,
183  UniformBucketLevelAccess const& rhs) {
184  return std::rel_ops::operator<=(lhs, rhs);
185 }
186 
187 inline bool operator>=(UniformBucketLevelAccess const& lhs,
188  UniformBucketLevelAccess const& rhs) {
189  return std::rel_ops::operator>=(lhs, rhs);
190 }
191 //@}
192 
193 std::ostream& operator<<(std::ostream& os, UniformBucketLevelAccess const& rhs);
194 
195 /**
196  * The IAM configuration for a Bucket.
197  *
198  * Currently this only holds the UniformBucketLevelAccess. In the future, we may
199  * define additional IAM which would be included in this object.
200  *
201  * @see Before enabling Uniform Bucket Level Access please review the
202  * [feature documentation][ubla-link], as well as
203  * ["Should you use uniform bucket-level access ?"][ubla-should-link].
204  *
205  * [ubla-link]:
206  * https://cloud.google.com/storage/docs/uniform-bucket-level-access
207  * [ubla-should-link]:
208  * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
209  */
212  absl::optional<std::string> public_access_prevention;
213 };
214 
215 //@{
216 /// @name Public Access Prevention helper functions
217 inline std::string PublicAccessPreventionEnforced() { return "enforced"; }
218 inline std::string PublicAccessPreventionInherited() { return "inherited"; }
219 GOOGLE_CLOUD_CPP_DEPRECATED("Use PublicAccessPreventionInherited()")
220 inline std::string PublicAccessPreventionUnspecified() { return "unspecified"; }
221 //@}
222 
223 //@{
224 /// @name Comparison operators for BucketIamConfiguration.
225 inline bool operator==(BucketIamConfiguration const& lhs,
226  BucketIamConfiguration const& rhs) {
227  return std::tie(lhs.uniform_bucket_level_access,
229  std::tie(rhs.uniform_bucket_level_access,
231 }
232 
233 inline bool operator<(BucketIamConfiguration const& lhs,
234  BucketIamConfiguration const& rhs) {
235  return std::tie(lhs.uniform_bucket_level_access,
237  std::tie(rhs.uniform_bucket_level_access,
239 }
240 
241 inline bool operator!=(BucketIamConfiguration const& lhs,
242  BucketIamConfiguration const& rhs) {
243  return std::rel_ops::operator!=(lhs, rhs);
244 }
245 
246 inline bool operator>(BucketIamConfiguration const& lhs,
247  BucketIamConfiguration const& rhs) {
248  return std::rel_ops::operator>(lhs, rhs);
249 }
250 
251 inline bool operator<=(BucketIamConfiguration const& lhs,
252  BucketIamConfiguration const& rhs) {
253  return std::rel_ops::operator<=(lhs, rhs);
254 }
255 
256 inline bool operator>=(BucketIamConfiguration const& lhs,
257  BucketIamConfiguration const& rhs) {
258  return std::rel_ops::operator>=(lhs, rhs);
259 }
260 //@}
261 
262 std::ostream& operator<<(std::ostream& os, BucketIamConfiguration const& rhs);
263 
264 /**
265  * The Object Lifecycle configuration for a Bucket.
266  *
267  * @see https://cloud.google.com/storage/docs/managing-lifecycles for general
268  * information on object lifecycle rules.
269  */
271  std::vector<LifecycleRule> rule;
272 };
273 
274 //@{
275 /// @name Comparison operators for BucketLifecycle.
276 inline bool operator==(BucketLifecycle const& lhs, BucketLifecycle const& rhs) {
277  return lhs.rule == rhs.rule;
278 }
279 
280 inline bool operator<(BucketLifecycle const& lhs, BucketLifecycle const& rhs) {
281  return lhs.rule < rhs.rule;
282 }
283 
284 inline bool operator!=(BucketLifecycle const& lhs, BucketLifecycle const& rhs) {
285  return std::rel_ops::operator!=(lhs, rhs);
286 }
287 
288 inline bool operator>(BucketLifecycle const& lhs, BucketLifecycle const& rhs) {
289  return std::rel_ops::operator>(lhs, rhs);
290 }
291 
292 inline bool operator<=(BucketLifecycle const& lhs, BucketLifecycle const& rhs) {
293  return std::rel_ops::operator<=(lhs, rhs);
294 }
295 
296 inline bool operator>=(BucketLifecycle const& lhs, BucketLifecycle const& rhs) {
297  return std::rel_ops::operator>=(lhs, rhs);
298 }
299 //@}
300 
301 /**
302  * The Logging configuration for a Bucket.
303  *
304  * @see https://cloud.google.com/storage/docs/access-logs for general
305  * information about using access logs with Google Cloud Storage.
306  */
308  std::string log_bucket;
309  std::string log_object_prefix;
310 };
311 
312 inline bool operator==(BucketLogging const& lhs, BucketLogging const& rhs) {
313  return std::tie(lhs.log_bucket, lhs.log_object_prefix) ==
314  std::tie(rhs.log_bucket, rhs.log_object_prefix);
315 }
316 
317 inline bool operator<(BucketLogging const& lhs, BucketLogging const& rhs) {
318  return std::tie(lhs.log_bucket, lhs.log_object_prefix) <
319  std::tie(rhs.log_bucket, rhs.log_object_prefix);
320 }
321 
322 inline bool operator!=(BucketLogging const& lhs, BucketLogging const& rhs) {
323  return std::rel_ops::operator!=(lhs, rhs);
324 }
325 
326 inline bool operator>(BucketLogging const& lhs, BucketLogging const& rhs) {
327  return std::rel_ops::operator>(lhs, rhs);
328 }
329 
330 inline bool operator<=(BucketLogging const& lhs, BucketLogging const& rhs) {
331  return std::rel_ops::operator<=(lhs, rhs);
332 }
333 
334 inline bool operator>=(BucketLogging const& lhs, BucketLogging const& rhs) {
335  return std::rel_ops::operator>=(lhs, rhs);
336 }
337 
338 std::ostream& operator<<(std::ostream& os, BucketLogging const& rhs);
339 
340 /**
341  * Describes the default customer managed encryption key for a bucket.
342  *
343  * Customer managed encryption keys (CMEK) are encryption keys selected by the
344  * user and generated by Google Cloud Key Management Service.
345  *
346  * @see https://cloud.google.com/storage/docs/encryption/customer-managed-keys
347  * for a general description of CMEK in Google Cloud Storage.
348  *
349  * @see https://cloud.google.com/kms/ for details about the Cloud Key Management
350  * Service.
351  */
353  std::string default_kms_key_name;
354 };
355 
356 inline bool operator==(BucketEncryption const& lhs,
357  BucketEncryption const& rhs) {
359 }
360 
361 inline bool operator<(BucketEncryption const& lhs,
362  BucketEncryption const& rhs) {
364 }
365 
366 inline bool operator!=(BucketEncryption const& lhs,
367  BucketEncryption const& rhs) {
368  return std::rel_ops::operator!=(lhs, rhs);
369 }
370 
371 inline bool operator>(BucketEncryption const& lhs,
372  BucketEncryption const& rhs) {
373  return std::rel_ops::operator>(lhs, rhs);
374 }
375 
376 inline bool operator<=(BucketEncryption const& lhs,
377  BucketEncryption const& rhs) {
378  return std::rel_ops::operator<=(lhs, rhs);
379 }
380 
381 inline bool operator>=(BucketEncryption const& lhs,
382  BucketEncryption const& rhs) {
383  return std::rel_ops::operator>=(lhs, rhs);
384 }
385 
386 /**
387  * The retention policy for a bucket.
388  *
389  * The Bucket Lock feature of Google Cloud Storage allows you to configure a
390  * data retention policy for a Cloud Storage bucket. This policy governs how
391  * long objects in the bucket must be retained. The feature also allows you to
392  * lock the data retention policy, permanently preventing the policy from from
393  * being reduced or removed.
394  *
395  * @see https://cloud.google.com/storage/docs/bucket-lock for a general
396  * overview
397  */
399  std::chrono::seconds retention_period;
400  std::chrono::system_clock::time_point effective_time;
401  bool is_locked;
402 };
403 
404 inline bool operator==(BucketRetentionPolicy const& lhs,
405  BucketRetentionPolicy const& rhs) {
406  return std::tie(lhs.retention_period, lhs.effective_time, lhs.is_locked) ==
407  std::tie(rhs.retention_period, rhs.effective_time, rhs.is_locked);
408 }
409 
410 inline bool operator<(BucketRetentionPolicy const& lhs,
411  BucketRetentionPolicy const& rhs) {
412  return std::tie(lhs.retention_period, lhs.effective_time, lhs.is_locked) <
413  std::tie(rhs.retention_period, rhs.effective_time, rhs.is_locked);
414 }
415 
416 inline bool operator!=(BucketRetentionPolicy const& lhs,
417  BucketRetentionPolicy const& rhs) {
418  return std::rel_ops::operator!=(lhs, rhs);
419 }
420 
421 inline bool operator>(BucketRetentionPolicy const& lhs,
422  BucketRetentionPolicy const& rhs) {
423  return std::rel_ops::operator>(lhs, rhs);
424 }
425 
426 inline bool operator<=(BucketRetentionPolicy const& lhs,
427  BucketRetentionPolicy const& rhs) {
428  return std::rel_ops::operator<=(lhs, rhs);
429 }
430 
431 inline bool operator>=(BucketRetentionPolicy const& lhs,
432  BucketRetentionPolicy const& rhs) {
433  return std::rel_ops::operator>=(lhs, rhs);
434 }
435 
436 std::ostream& operator<<(std::ostream& os, BucketRetentionPolicy const& rhs);
437 
438 /// A helper function to avoid typos in the RPO configuration
439 inline std::string RpoDefault() { return "DEFAULT"; }
440 
441 /// A helper function to avoid typos in the RPO configuration
442 inline std::string RpoAsyncTurbo() { return "ASYNC_TURBO"; }
443 
444 /**
445  * The versioning configuration for a Bucket.
446  *
447  * @see https://cloud.google.com/storage/docs/requester-pays for general
448  * information on "Requester Pays" billing.
449  */
451  BucketVersioning() = default;
452  explicit BucketVersioning(bool flag) : enabled(flag) {}
453 
454  bool enabled{true};
455 };
456 
457 inline bool operator==(BucketVersioning const& lhs,
458  BucketVersioning const& rhs) {
459  return lhs.enabled == rhs.enabled;
460 }
461 
462 inline bool operator<(BucketVersioning const& lhs,
463  BucketVersioning const& rhs) {
464  return !lhs.enabled && rhs.enabled;
465 }
466 
467 inline bool operator!=(BucketVersioning const& lhs,
468  BucketVersioning const& rhs) {
469  return std::rel_ops::operator!=(lhs, rhs);
470 }
471 
472 inline bool operator>(BucketVersioning const& lhs,
473  BucketVersioning const& rhs) {
474  return std::rel_ops::operator>(lhs, rhs);
475 }
476 
477 inline bool operator<=(BucketVersioning const& lhs,
478  BucketVersioning const& rhs) {
479  return std::rel_ops::operator<=(lhs, rhs);
480 }
481 
482 inline bool operator>=(BucketVersioning const& lhs,
483  BucketVersioning const& rhs) {
484  return std::rel_ops::operator>=(lhs, rhs);
485 }
486 
487 /**
488  * The website configuration for a Bucket.
489  *
490  * @see https://cloud.google.com/storage/docs/static-website for information on
491  * how to configure Buckets to serve as a static website.
492  */
494  std::string main_page_suffix;
495  std::string not_found_page;
496 };
497 
498 inline bool operator==(BucketWebsite const& lhs, BucketWebsite const& rhs) {
499  return std::tie(lhs.main_page_suffix, lhs.not_found_page) ==
500  std::tie(rhs.main_page_suffix, rhs.not_found_page);
501 }
502 
503 inline bool operator<(BucketWebsite const& lhs, BucketWebsite const& rhs) {
504  return std::tie(lhs.main_page_suffix, lhs.not_found_page) <
505  std::tie(rhs.main_page_suffix, rhs.not_found_page);
506 }
507 
508 inline bool operator!=(BucketWebsite const& lhs, BucketWebsite const& rhs) {
509  return std::rel_ops::operator!=(lhs, rhs);
510 }
511 
512 inline bool operator>(BucketWebsite const& lhs, BucketWebsite const& rhs) {
513  return std::rel_ops::operator>(lhs, rhs);
514 }
515 
516 inline bool operator<=(BucketWebsite const& lhs, BucketWebsite const& rhs) {
517  return std::rel_ops::operator<=(lhs, rhs);
518 }
519 
520 inline bool operator>=(BucketWebsite const& lhs, BucketWebsite const& rhs) {
521  return std::rel_ops::operator>=(lhs, rhs);
522 }
523 
524 /**
525  * Represents a Google Cloud Storage Bucket Metadata object.
526  */
527 class BucketMetadata : private internal::CommonMetadata<BucketMetadata> {
528  public:
529  BucketMetadata() = default;
530 
531  // Please keep these in alphabetical order, that make it easier to verify we
532  // have actually implemented all of them.
533  /**
534  * @name Get and set Bucket Access Control Lists.
535  *
536  * @see https://cloud.google.com/storage/docs/access-control/lists
537  */
538  ///@{
539  std::vector<BucketAccessControl> const& acl() const { return acl_; }
540  std::vector<BucketAccessControl>& mutable_acl() { return acl_; }
542  acl_ = std::move(acl);
543  return *this;
544  }
545  ///@}
546 
547  /**
548  * @name Get and set billing configuration for the Bucket.
549  *
550  * @see https://cloud.google.com/storage/docs/requester-pays
551  */
552  ///@{
553  bool has_billing() const { return billing_.has_value(); }
554  BucketBilling const& billing() const { return *billing_; }
555  absl::optional<BucketBilling> const& billing_as_optional() const {
556  return billing_;
557  }
559  billing_ = v;
560  return *this;
561  }
563  billing_.reset();
564  return *this;
565  }
566  ///@}
567 
568  /**
569  * @name Get and set the default event based hold for the Bucket.
570  *
571  * Objects may have an event-based hold associated with them. If a Bucket
572  * has the `default_event_based_hold()` parameter set, and you create a new
573  * object in the bucket without specifying its event-event based hold then the
574  * object gets the value set in the bucket.
575  *
576  * @see https://cloud.google.com/storage/docs/bucket-lock for generation
577  * information on retention policies. The section on
578  * [Object
579  * holds](https://cloud.google.com/storage/docs/bucket-lock#object-holds) is
580  * particularly relevant.
581  *
582  * @see https://cloud.google.com/storage/docs/holding-objects for examples
583  * of using default event-based hold policy.
584  */
585  ///@{
586  bool default_event_based_hold() const { return default_event_based_hold_; }
588  default_event_based_hold_ = v;
589  return *this;
590  }
591  ///@}
592 
593  /**
594  * @name Get and set CORS configuration for the Bucket.
595  *
596  * @see https://en.wikipedia.org/wiki/Cross-origin_resource_sharing for
597  * general information on CORS.
598  *
599  * @see https://cloud.google.com/storage/docs/cross-origin for general
600  * information about CORS in the context of Google Cloud Storage.
601  *
602  * @see https://cloud.google.com/storage/docs/configuring-cors for information
603  * on how to set and troubleshoot CORS settings.
604  */
605  ///@{
606  std::vector<CorsEntry> const& cors() const { return cors_; }
607  std::vector<CorsEntry>& mutable_cors() { return cors_; }
608  BucketMetadata& set_cors(std::vector<CorsEntry> cors) {
609  cors_ = std::move(cors);
610  return *this;
611  }
612  ///@}
613 
614  /**
615  * @name Get and set the Default Object Access Control Lists.
616  *
617  * @see https://cloud.google.com/storage/docs/access-control/lists#default for
618  * general information of default ACLs.
619  *
620  * @see
621  * https://cloud.google.com/storage/docs/access-control/create-manage-lists#defaultobjects
622  * for information on how to set the default ACLs.
623  */
624  ///@{
625  std::vector<ObjectAccessControl> const& default_acl() const {
626  return default_acl_;
627  }
629  return default_acl_;
630  }
632  default_acl_ = std::move(acl);
633  return *this;
634  }
635  ///@}
636 
637  /**
638  * @name Get and set the Default Object Access Control Lists.
639  *
640  * @see https://cloud.google.com/storage/docs/access-control/lists#default for
641  * general information of default ACLs.
642  *
643  * @see
644  * https://cloud.google.com/storage/docs/encryption/customer-managed-keys
645  * for information on Customer-Managed Encryption Keys.
646  */
647  ///@{
648  bool has_encryption() const { return encryption_.has_value(); }
649  BucketEncryption const& encryption() const { return *encryption_; }
650  absl::optional<BucketEncryption> const& encryption_as_optional() const {
651  return encryption_;
652  }
654  encryption_ = std::move(v);
655  return *this;
656  }
658  encryption_.reset();
659  return *this;
660  }
661  ///@}
662 
663  using CommonMetadata::etag;
664 
665  /**
666  * @name Get and set the IAM configuration.
667  *
668  * @see Before enabling Uniform Bucket Level Access please review the
669  * [feature documentation][ubla-link], as well as
670  * ["Should you use uniform bucket-level access ?"][ubla-should-link].
671  *
672  * [ubla-link]:
673  * https://cloud.google.com/storage/docs/uniform-bucket-level-access
674  * [ubla-should-link]:
675  * https://cloud.google.com/storage/docs/uniform-bucket-level-access#should-you-use
676  */
677  ///@{
678  bool has_iam_configuration() const { return iam_configuration_.has_value(); }
680  return *iam_configuration_;
681  }
683  const {
684  return iam_configuration_;
685  }
687  iam_configuration_ = std::move(v);
688  return *this;
689  }
691  iam_configuration_.reset();
692  return *this;
693  }
694  ///@}
695 
696  using CommonMetadata::id;
697  using CommonMetadata::kind;
698 
699  /// @name Accessors and modifiers to the `labels`.
700  ///@{
701 
702  /// Returns `true` if the key is present in the Bucket's metadata labels.
703  bool has_label(std::string const& key) const {
704  return labels_.end() != labels_.find(key);
705  }
706 
707  /**
708  * Returns the value of @p key in the Bucket's metadata labels.
709  *
710  * It is undefined behavior to call `label(key)` if `has_label(key) == false`.
711  */
712  std::string const& label(std::string const& key) const {
713  return labels_.at(key);
714  }
715 
716  /// Delete a label. This is a no-op if the key does not exist.
717  BucketMetadata& delete_label(std::string const& key) {
718  auto i = labels_.find(key);
719  if (i == labels_.end()) {
720  return *this;
721  }
722  labels_.erase(i);
723  return *this;
724  }
725 
726  /// Insert or update the label entry.
727  BucketMetadata& upsert_label(std::string key, std::string value) {
728  auto i = labels_.lower_bound(key);
729  if (i == labels_.end() || i->first != key) {
730  labels_.emplace_hint(i, std::move(key), std::move(value));
731  } else {
732  i->second = std::move(value);
733  }
734  return *this;
735  }
736 
737  /// Returns all the Bucket's labels.
738  std::map<std::string, std::string> const& labels() const { return labels_; }
739 
740  /// Returns all the Bucket's labels.
741  std::map<std::string, std::string>& mutable_labels() { return labels_; }
742  ///@}
743 
744  /**
745  * @name Accessors and modifiers for object lifecycle rules.
746  *
747  * @see https://cloud.google.com/storage/docs/managing-lifecycles for general
748  * information on object lifecycle rules.
749  */
750  ///@{
751  bool has_lifecycle() const { return lifecycle_.has_value(); }
752  BucketLifecycle const& lifecycle() const { return *lifecycle_; }
753  absl::optional<BucketLifecycle> const& lifecycle_as_optional() const {
754  return lifecycle_;
755  }
757  lifecycle_ = std::move(v);
758  return *this;
759  }
761  lifecycle_.reset();
762  return *this;
763  }
764  ///@}
765 
766  std::string const& location() const { return location_; }
767  BucketMetadata& set_location(std::string v) {
768  location_ = std::move(v);
769  return *this;
770  }
771 
772  std::string const& location_type() const { return location_type_; }
773 
774  /// @name Accessors and modifiers for logging configuration.
775  ///@{
776  bool has_logging() const { return logging_.has_value(); }
777  BucketLogging const& logging() const { return *logging_; }
778  absl::optional<BucketLogging> const& logging_as_optional() const {
779  return logging_;
780  }
782  logging_ = std::move(v);
783  return *this;
784  }
786  logging_.reset();
787  return *this;
788  }
789  ///@}
790 
791  /// The bucket metageneration.
792  using CommonMetadata::metageneration;
793 
794  /// The bucket name.
795  using CommonMetadata::name;
796 
797  /**
798  * Changes the name.
799  *
800  * @note Bucket names can only be set during bucket creation. This modifier
801  * is used to set the name when using a `BucketMetadata` object that changes
802  * some other attribute.
803  */
804  BucketMetadata& set_name(std::string v) {
805  CommonMetadata::set_name(std::move(v));
806  return *this;
807  }
808 
809  /// Returns true if the bucket `owner` attribute is present.
810  using CommonMetadata::has_owner;
811  using CommonMetadata::owner;
812 
813  std::int64_t const& project_number() const { return project_number_; }
814 
815  using CommonMetadata::self_link;
816 
817  /// @name Accessors and modifiers for retention policy configuration.
818  ///@{
819  bool has_retention_policy() const { return retention_policy_.has_value(); }
821  return *retention_policy_;
822  }
824  const {
825  return retention_policy_;
826  }
828  retention_policy_ = std::move(v);
829  return *this;
830  }
831 
832  /**
833  * Sets the retention period.
834  *
835  * The retention period is the only writable attribute in a retention policy.
836  * This function makes it easier to set the retention policy when the
837  * `BucketMetadata` object is used to update or patch the bucket.
838  */
839  BucketMetadata& set_retention_policy(std::chrono::seconds retention_period) {
841  retention_period, std::chrono::system_clock::time_point{}, false});
842  }
843 
845  retention_policy_.reset();
846  return *this;
847  }
848  ///@}
849 
850  /// @name Accessors and modifiers for the Recovery Point Objective.
851  ///@{
852  std::string const& rpo() const { return rpo_; }
853  BucketMetadata& set_rpo(std::string v) {
854  rpo_ = std::move(v);
855  return *this;
856  }
857  ///@}
858 
859  /// @name Access and modify the default storage class attribute.
860  ///@{
861  using CommonMetadata::storage_class;
863  CommonMetadata::set_storage_class(std::move(v));
864  return *this;
865  }
866  ///@}
867 
868  /// Returns the bucket creation timestamp.
869  using CommonMetadata::time_created;
870 
871  /// Returns the timestamp for the last bucket metadata update.
872  using CommonMetadata::updated;
873 
874  /// @name Accessors and modifiers for versioning configuration.
875  ///@{
876  absl::optional<BucketVersioning> const& versioning() const {
877  return versioning_;
878  }
879  bool has_versioning() const { return versioning_.has_value(); }
881  versioning_.emplace(BucketVersioning{true});
882  return *this;
883  }
885  versioning_.emplace(BucketVersioning{false});
886  return *this;
887  }
889  versioning_.reset();
890  return *this;
891  }
893  versioning_ = std::move(v);
894  return *this;
895  }
896  ///@}
897 
898  /// @name Accessors and modifiers for website configuration.
899  ///@{
900  bool has_website() const { return website_.has_value(); }
901  BucketWebsite const& website() const { return *website_; }
902  absl::optional<BucketWebsite> const& website_as_optional() const {
903  return website_;
904  }
906  website_ = std::move(v);
907  return *this;
908  }
910  website_.reset();
911  return *this;
912  }
913  ///@}
914 
915  friend bool operator==(BucketMetadata const& lhs, BucketMetadata const& rhs);
916  friend bool operator!=(BucketMetadata const& lhs, BucketMetadata const& rhs) {
917  return !(lhs == rhs);
918  }
919 
920  private:
921  friend struct internal::BucketMetadataParser;
922  friend struct internal::GrpcBucketMetadataParser;
923 
924  friend std::ostream& operator<<(std::ostream& os, BucketMetadata const& rhs);
925  // Keep the fields in alphabetical order.
926  std::vector<BucketAccessControl> acl_;
927  absl::optional<BucketBilling> billing_;
928  std::vector<CorsEntry> cors_;
929  bool default_event_based_hold_ = false;
930  std::vector<ObjectAccessControl> default_acl_;
931  absl::optional<BucketEncryption> encryption_;
932  absl::optional<BucketIamConfiguration> iam_configuration_;
933  std::map<std::string, std::string> labels_;
934  absl::optional<BucketLifecycle> lifecycle_;
935  std::string location_;
936  std::string location_type_;
937  absl::optional<BucketLogging> logging_;
938  std::int64_t project_number_ = 0;
939  absl::optional<BucketRetentionPolicy> retention_policy_;
940  std::string rpo_;
941  absl::optional<BucketVersioning> versioning_;
942  absl::optional<BucketWebsite> website_;
943 };
944 
945 std::ostream& operator<<(std::ostream& os, BucketMetadata const& rhs);
946 
947 /**
948  * Prepares a patch for the Bucket resource.
949  *
950  * The Bucket resource has many modifiable fields. The application may send a
951  * patch request to change (or delete) a small fraction of these fields by using
952  * this object.
953  *
954  * @see
955  * https://cloud.google.com/storage/docs/json_api/v1/how-tos/performance#patch
956  * for general information on PATCH requests for the Google Cloud Storage
957  * JSON API.
958  */
960  public:
962 
963  std::string BuildPatch() const;
964 
966 
967  /**
968  * Clears the ACL for the Bucket.
969  *
970  * @warning Currently the server ignores requests to reset the full ACL.
971  */
973 
976 
979 
982 
984  std::vector<ObjectAccessControl> const& v);
985 
986  /**
987  * Clears the default object ACL for the Bucket.
988  *
989  * @warning Currently the server ignores requests to reset the full ACL.
990  */
992 
994  BucketIamConfiguration const& v);
996 
999 
1000  BucketMetadataPatchBuilder& SetLabel(std::string const& label,
1001  std::string const& value);
1002  BucketMetadataPatchBuilder& ResetLabel(std::string const& label);
1004 
1007 
1010 
1011  BucketMetadataPatchBuilder& SetName(std::string const& v);
1013 
1015  BucketRetentionPolicy const& v);
1017  std::chrono::seconds retention_period) {
1018  // This is the only parameter that the application can set, so make it easy
1019  // for them to set it.
1021  retention_period, std::chrono::system_clock::time_point{}, false});
1022  }
1024 
1025  BucketMetadataPatchBuilder& SetRpo(std::string const& v);
1027 
1030 
1033 
1036 
1037  private:
1038  friend struct internal::GrpcBucketRequestParser;
1039 
1040  internal::PatchBuilder impl_;
1041  bool labels_subpatch_dirty_{false};
1042  internal::PatchBuilder labels_subpatch_;
1043 };
1044 
1046 } // namespace storage
1047 } // namespace cloud
1048 } // namespace google
1049 
1050 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_BUCKET_METADATA_H