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