Google Cloud Bigtable C++ Client  1.1.0
A C++ Client Library for Google Cloud Bigtable
metadata_update_policy.h
Go to the documentation of this file.
1 // Copyright 2018 Google Inc.
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_BIGTABLE_METADATA_UPDATE_POLICY_H_
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_METADATA_UPDATE_POLICY_H_
17 
18 #include "google/cloud/bigtable/version.h"
19 #include <grpcpp/grpcpp.h>
20 #include <memory>
21 #include <utility>
22 
23 namespace google {
24 namespace cloud {
25 namespace bigtable {
26 inline namespace BIGTABLE_CLIENT_NS {
27 /**
28  * Define the class for governing x-goog-request-params metadata value.
29  *
30  * The value of x-goog-request-params starts with one of the following suffix
31  * "parent=" : Operation in instance, e.g. TableAdmin::CreateTable.
32  * "table_name=" : table_id is known at the time of creation, e.g.
33  * Table::Apply.
34  * "name=" : this is used when table|_id is known only in the RPC call, e.g.
35  * TableAdmin::GetTable.
36  * "resource=" : this is used to set IAM policies for bigtable resource.
37  *
38  * The Setup function also adds x-goog-api-client header for analytics purpose.
39  */
40 class MetadataParamTypes final {
41  public:
42  static MetadataParamTypes const PARENT;
43  static MetadataParamTypes const NAME;
44  static MetadataParamTypes const RESOURCE;
45  static MetadataParamTypes const TABLE_NAME;
46 
47  std::string const& type() const { return type_; }
48 
49  private:
50  std::string type_;
51  MetadataParamTypes(std::string type) : type_(std::move(type)) {}
52 };
53 
54 inline bool operator==(MetadataParamTypes const& lhs,
55  MetadataParamTypes const& rhs) {
56  return lhs.type() == rhs.type();
57 }
58 
59 inline bool operator!=(MetadataParamTypes const& lhs,
60  MetadataParamTypes const& rhs) {
61  return std::rel_ops::operator!=(lhs, rhs);
62 }
63 
64 /// MetadataUpdatePolicy holds supported metadata and setup ClientContext
65 class MetadataUpdatePolicy {
66  public:
67  /**
68  * Constructor with default metadata pair.
69  *
70  * @param resource_name hierarchical name of resource, including project id,
71  * instance id and/or table_id.
72  * @param metadata_param_type type to decide prefix for the value of
73  * x-goog-request-params
74  */
75  MetadataUpdatePolicy(std::string const& resource_name,
76  MetadataParamTypes const& metadata_param_type);
77 
78  /**
79  * Constructor with default metadata pair.
80  *
81  * @param resource_name hierarchical name of resource, including project id,
82  * instance id and/or table_id.
83  * @param metadata_param_type type to decide prefix for the value of
84  * x-goog-request-params.
85  * @param table_id table_id used in RPC call.
86  */
87  static MetadataUpdatePolicy FromTableId(
88  std::string const& resource_name,
89  MetadataParamTypes const& metadata_param_type,
90  std::string const& table_id);
91 
92  // TODO(#2704) - this seems to be used only in tests, remove or use.
93  /**
94  * Constructor with default metadata pair.
95  *
96  * @param resource_name hierarchical name of resource, including project id,
97  * instance id and/or table_id.
98  * @param metadata_param_type type to decide prefix for the value of
99  * x-goog-request-params.
100  * @param cluster_id cluster_id of the cluster.
101  */
102  static MetadataUpdatePolicy FromClusterId(
103  std::string const& resource_name,
104  MetadataParamTypes const& metadata_param_type,
105  std::string const& cluster_id);
106 
107  MetadataUpdatePolicy(MetadataUpdatePolicy&&) noexcept = default;
108  MetadataUpdatePolicy(MetadataUpdatePolicy const&) = default;
109  MetadataUpdatePolicy& operator=(MetadataUpdatePolicy const&) = default;
110 
111  // Update the ClientContext for the next call.
112  void Setup(grpc::ClientContext& context) const;
113 
114  std::string const& value() const { return value_; }
115  std::string const& api_client_header() const { return api_client_header_; }
116 
117  private:
118  std::string value_;
119  std::string api_client_header_;
120 };
121 
122 } // namespace BIGTABLE_CLIENT_NS
123 } // namespace bigtable
124 } // namespace cloud
125 } // namespace google
126 
127 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_BIGTABLE_METADATA_UPDATE_POLICY_H_
#define BIGTABLE_CLIENT_NS
Definition: version.h:22