Google Cloud Bigtable C++ Client  1.33.0
A C++ Client Library for Google Cloud Bigtable
instance_admin_client.cc
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 #include "google/cloud/bigtable/instance_admin_client.h"
16 #include "google/cloud/bigtable/internal/common_client.h"
17 #include "google/cloud/bigtable/internal/logging_instance_admin_client.h"
18 #include <google/longrunning/operations.grpc.pb.h>
19 
20 namespace google {
21 namespace cloud {
22 namespace bigtable {
24 namespace {
25 
26 namespace btadmin = ::google::bigtable::admin::v2;
27 
28 /**
29  * An InstanceAdminClient for single-threaded programs that refreshes
30  * credentials on all gRPC errors.
31  *
32  * This class should not be used by multiple threads, it makes no attempt to
33  * protect its critical sections. While it is rare that the admin interface
34  * will be used by multiple threads, we should use the same approach here and in
35  * the regular client to support multi-threaded programs.
36  *
37  * The class also aggressively reconnects on any gRPC errors. A future version
38  * should only reconnect on those errors that indicate the credentials or
39  * connections need refreshing.
40  */
41 class DefaultInstanceAdminClient : public InstanceAdminClient {
42  public:
43  DefaultInstanceAdminClient(std::string project, Options options)
44  : project_(std::move(project)), impl_(std::move(options)) {}
45 
46  std::string const& project() const override { return project_; }
47  std::shared_ptr<grpc::Channel> Channel() override { return impl_.Channel(); }
48  void reset() override { return impl_.reset(); }
49 
50  grpc::Status ListInstances(
51  grpc::ClientContext* context,
52  btadmin::ListInstancesRequest const& request,
53  btadmin::ListInstancesResponse* response) override {
54  return impl_.Stub()->ListInstances(context, request, response);
55  }
56 
57  grpc::Status CreateInstance(
58  grpc::ClientContext* context,
59  btadmin::CreateInstanceRequest const& request,
60  google::longrunning::Operation* response) override {
61  return impl_.Stub()->CreateInstance(context, request, response);
62  }
63 
64  grpc::Status UpdateInstance(
65  grpc::ClientContext* context,
66  btadmin::PartialUpdateInstanceRequest const& request,
67  google::longrunning::Operation* response) override {
68  return impl_.Stub()->PartialUpdateInstance(context, request, response);
69  }
70 
71  grpc::Status GetOperation(
72  grpc::ClientContext* context,
73  google::longrunning::GetOperationRequest const& request,
74  google::longrunning::Operation* response) override {
75  auto stub = google::longrunning::Operations::NewStub(Channel());
76  return stub->GetOperation(context, request, response);
77  }
78 
79  grpc::Status GetInstance(grpc::ClientContext* context,
80  btadmin::GetInstanceRequest const& request,
81  btadmin::Instance* response) override {
82  return impl_.Stub()->GetInstance(context, request, response);
83  }
84 
85  grpc::Status DeleteInstance(grpc::ClientContext* context,
86  btadmin::DeleteInstanceRequest const& request,
87  google::protobuf::Empty* response) override {
88  return impl_.Stub()->DeleteInstance(context, request, response);
89  }
90 
91  grpc::Status ListClusters(grpc::ClientContext* context,
92  btadmin::ListClustersRequest const& request,
93  btadmin::ListClustersResponse* response) override {
94  return impl_.Stub()->ListClusters(context, request, response);
95  }
96 
97  grpc::Status GetCluster(grpc::ClientContext* context,
98  btadmin::GetClusterRequest const& request,
99  btadmin::Cluster* response) override {
100  return impl_.Stub()->GetCluster(context, request, response);
101  }
102 
103  grpc::Status DeleteCluster(grpc::ClientContext* context,
104  btadmin::DeleteClusterRequest const& request,
105  google::protobuf::Empty* response) override {
106  return impl_.Stub()->DeleteCluster(context, request, response);
107  }
108 
109  grpc::Status CreateCluster(
110  grpc::ClientContext* context,
111  btadmin::CreateClusterRequest const& request,
112  google::longrunning::Operation* response) override {
113  return impl_.Stub()->CreateCluster(context, request, response);
114  }
115 
116  grpc::Status UpdateCluster(
117  grpc::ClientContext* context, btadmin::Cluster const& request,
118  google::longrunning::Operation* response) override {
119  return impl_.Stub()->UpdateCluster(context, request, response);
120  }
121 
122  grpc::Status CreateAppProfile(grpc::ClientContext* context,
123  btadmin::CreateAppProfileRequest const& request,
124  btadmin::AppProfile* response) override {
125  return impl_.Stub()->CreateAppProfile(context, request, response);
126  }
127 
128  grpc::Status GetAppProfile(grpc::ClientContext* context,
129  btadmin::GetAppProfileRequest const& request,
130  btadmin::AppProfile* response) override {
131  return impl_.Stub()->GetAppProfile(context, request, response);
132  }
133 
134  grpc::Status ListAppProfiles(
135  grpc::ClientContext* context,
136  btadmin::ListAppProfilesRequest const& request,
137  btadmin::ListAppProfilesResponse* response) override {
138  return impl_.Stub()->ListAppProfiles(context, request, response);
139  }
140 
141  grpc::Status UpdateAppProfile(
142  grpc::ClientContext* context,
143  btadmin::UpdateAppProfileRequest const& request,
144  google::longrunning::Operation* response) override {
145  return impl_.Stub()->UpdateAppProfile(context, request, response);
146  }
147 
148  grpc::Status DeleteAppProfile(grpc::ClientContext* context,
149  btadmin::DeleteAppProfileRequest const& request,
150  google::protobuf::Empty* response) override {
151  return impl_.Stub()->DeleteAppProfile(context, request, response);
152  }
153 
154  grpc::Status GetIamPolicy(grpc::ClientContext* context,
155  google::iam::v1::GetIamPolicyRequest const& request,
156  google::iam::v1::Policy* response) override {
157  return impl_.Stub()->GetIamPolicy(context, request, response);
158  }
159 
160  grpc::Status SetIamPolicy(grpc::ClientContext* context,
161  google::iam::v1::SetIamPolicyRequest const& request,
162  google::iam::v1::Policy* response) override {
163  return impl_.Stub()->SetIamPolicy(context, request, response);
164  }
165 
166  grpc::Status TestIamPermissions(
167  grpc::ClientContext* context,
168  google::iam::v1::TestIamPermissionsRequest const& request,
169  google::iam::v1::TestIamPermissionsResponse* response) override {
170  return impl_.Stub()->TestIamPermissions(context, request, response);
171  }
172 
173  std::unique_ptr<
174  grpc::ClientAsyncResponseReaderInterface<btadmin::ListInstancesResponse>>
175  AsyncListInstances(grpc::ClientContext* context,
176  btadmin::ListInstancesRequest const& request,
177  grpc::CompletionQueue* cq) override {
178  return impl_.Stub()->AsyncListInstances(context, request, cq);
179  }
180 
181  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Instance>>
182  AsyncGetInstance(grpc::ClientContext* context,
183  btadmin::GetInstanceRequest const& request,
184  grpc::CompletionQueue* cq) override {
185  return impl_.Stub()->AsyncGetInstance(context, request, cq);
186  }
187 
188  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Cluster>>
189  AsyncGetCluster(grpc::ClientContext* context,
190  btadmin::GetClusterRequest const& request,
191  grpc::CompletionQueue* cq) override {
192  return impl_.Stub()->AsyncGetCluster(context, request, cq);
193  }
194 
195  std::unique_ptr<
196  grpc::ClientAsyncResponseReaderInterface<google::protobuf::Empty>>
197  AsyncDeleteCluster(grpc::ClientContext* context,
198  btadmin::DeleteClusterRequest const& request,
199  grpc::CompletionQueue* cq) override {
200  return impl_.Stub()->AsyncDeleteCluster(context, request, cq);
201  }
202 
203  std::unique_ptr<
204  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
205  AsyncCreateCluster(grpc::ClientContext* context,
206  btadmin::CreateClusterRequest const& request,
207  grpc::CompletionQueue* cq) override {
208  return impl_.Stub()->AsyncCreateCluster(context, request, cq);
209  }
210 
211  std::unique_ptr<
212  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
213  AsyncCreateInstance(grpc::ClientContext* context,
214  btadmin::CreateInstanceRequest const& request,
215  grpc::CompletionQueue* cq) override {
216  return impl_.Stub()->AsyncCreateInstance(context, request, cq);
217  }
218 
219  std::unique_ptr<
220  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
221  AsyncUpdateInstance(grpc::ClientContext* context,
222  btadmin::PartialUpdateInstanceRequest const& request,
223  grpc::CompletionQueue* cq) override {
224  return impl_.Stub()->AsyncPartialUpdateInstance(context, request, cq);
225  }
226 
227  std::unique_ptr<
228  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
229  AsyncUpdateCluster(grpc::ClientContext* context,
230  btadmin::Cluster const& request,
231  grpc::CompletionQueue* cq) override {
232  return impl_.Stub()->AsyncUpdateCluster(context, request, cq);
233  }
234 
235  std::unique_ptr<
236  grpc::ClientAsyncResponseReaderInterface<google::protobuf::Empty>>
237  AsyncDeleteInstance(grpc::ClientContext* context,
238  btadmin::DeleteInstanceRequest const& request,
239  grpc::CompletionQueue* cq) override {
240  return impl_.Stub()->AsyncDeleteInstance(context, request, cq);
241  }
242 
243  std::unique_ptr<
244  grpc::ClientAsyncResponseReaderInterface<btadmin::ListClustersResponse>>
245  AsyncListClusters(grpc::ClientContext* context,
246  btadmin::ListClustersRequest const& request,
247  grpc::CompletionQueue* cq) override {
248  return impl_.Stub()->AsyncListClusters(context, request, cq);
249  }
250 
251  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::AppProfile>>
252  AsyncGetAppProfile(grpc::ClientContext* context,
253  btadmin::GetAppProfileRequest const& request,
254  grpc::CompletionQueue* cq) override {
255  return impl_.Stub()->AsyncGetAppProfile(context, request, cq);
256  }
257 
258  std::unique_ptr<
259  grpc::ClientAsyncResponseReaderInterface<google::protobuf::Empty>>
260  AsyncDeleteAppProfile(grpc::ClientContext* context,
261  btadmin::DeleteAppProfileRequest const& request,
262  grpc::CompletionQueue* cq) override {
263  return impl_.Stub()->AsyncDeleteAppProfile(context, request, cq);
264  }
265 
266  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::AppProfile>>
267  AsyncCreateAppProfile(grpc::ClientContext* context,
268  btadmin::CreateAppProfileRequest const& request,
269  grpc::CompletionQueue* cq) override {
270  return impl_.Stub()->AsyncCreateAppProfile(context, request, cq);
271  }
272 
273  std::unique_ptr<
274  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
275  AsyncUpdateAppProfile(grpc::ClientContext* context,
276  btadmin::UpdateAppProfileRequest const& request,
277  grpc::CompletionQueue* cq) override {
278  return impl_.Stub()->AsyncUpdateAppProfile(context, request, cq);
279  }
280 
281  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
282  btadmin::ListAppProfilesResponse>>
283  AsyncListAppProfiles(grpc::ClientContext* context,
284  btadmin::ListAppProfilesRequest const& request,
285  grpc::CompletionQueue* cq) override {
286  return impl_.Stub()->AsyncListAppProfiles(context, request, cq);
287  }
288 
289  std::unique_ptr<
290  grpc::ClientAsyncResponseReaderInterface<google::iam::v1::Policy>>
291  AsyncGetIamPolicy(grpc::ClientContext* context,
292  google::iam::v1::GetIamPolicyRequest const& request,
293  grpc::CompletionQueue* cq) override {
294  return impl_.Stub()->AsyncGetIamPolicy(context, request, cq);
295  }
296 
297  std::unique_ptr<
298  grpc::ClientAsyncResponseReaderInterface<google::iam::v1::Policy>>
299  AsyncSetIamPolicy(grpc::ClientContext* context,
300  google::iam::v1::SetIamPolicyRequest const& request,
301  grpc::CompletionQueue* cq) override {
302  return impl_.Stub()->AsyncSetIamPolicy(context, request, cq);
303  }
304 
305  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
306  google::iam::v1::TestIamPermissionsResponse>>
307  AsyncTestIamPermissions(
308  grpc::ClientContext* context,
309  google::iam::v1::TestIamPermissionsRequest const& request,
310  grpc::CompletionQueue* cq) override {
311  return impl_.Stub()->AsyncTestIamPermissions(context, request, cq);
312  }
313 
314  std::unique_ptr<
315  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
316  AsyncGetOperation(grpc::ClientContext* context,
317  google::longrunning::GetOperationRequest const& request,
318  grpc::CompletionQueue* cq) override {
319  auto stub = google::longrunning::Operations::NewStub(Channel());
320  return std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
321  google::longrunning::Operation>>(
322  stub->AsyncGetOperation(context, request, cq).release());
323  }
324 
325  private:
326  google::cloud::BackgroundThreadsFactory BackgroundThreadsFactory() override {
327  return impl_.BackgroundThreadsFactory();
328  }
329 
330  struct Traits {
331  static std::string const& Endpoint(Options const& options) {
333  }
334  };
335 
336  std::string project_;
337  internal::CommonClient<Traits, btadmin::BigtableInstanceAdmin> impl_;
338 };
339 
340 } // anonymous namespace
341 
343  std::string project, Options options) {
344  options = internal::DefaultOptions(std::move(options));
345  bool tracing_enabled = google::cloud::internal::Contains(
346  options.get<TracingComponentsOption>(), "rpc");
347  auto tracing_options = options.get<GrpcTracingOptionsOption>();
348 
349  std::shared_ptr<InstanceAdminClient> client =
350  std::make_shared<DefaultInstanceAdminClient>(std::move(project),
351  std::move(options));
352  if (tracing_enabled) {
353  GCP_LOG(INFO) << "Enabled logging for gRPC calls";
354  client = std::make_shared<internal::LoggingInstanceAdminClient>(
355  std::move(client), std::move(tracing_options));
356  }
357  return client;
358 }
359 
361  std::string project, ClientOptions options) {
362  return MakeInstanceAdminClient(std::move(project),
363  internal::MakeOptions(std::move(options)));
364 }
365 
367 } // namespace bigtable
368 } // namespace cloud
369 } // namespace google