Google Cloud Bigtable C++ Client  1.33.0
A C++ Client Library for Google Cloud Bigtable
admin_client.cc
Go to the documentation of this file.
1 // Copyright 2017 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/admin_client.h"
16 #include "google/cloud/bigtable/internal/common_client.h"
17 #include "google/cloud/bigtable/internal/logging_admin_client.h"
18 #include "google/cloud/log.h"
19 #include <google/longrunning/operations.grpc.pb.h>
20 
21 namespace google {
22 namespace cloud {
23 namespace bigtable {
25 
26 namespace btadmin = ::google::bigtable::admin::v2;
27 
28 grpc::Status AdminClient::CreateBackup(grpc::ClientContext*,
29  btadmin::CreateBackupRequest const&,
30  google::longrunning::Operation*) {
31  return {grpc::StatusCode::UNIMPLEMENTED, "Not implemented"};
32 }
33 
34 grpc::Status AdminClient::GetBackup(grpc::ClientContext*,
35  btadmin::GetBackupRequest const&,
36  btadmin::Backup*) {
37  return {grpc::StatusCode::UNIMPLEMENTED, "Not implemented"};
38 }
39 
40 grpc::Status AdminClient::UpdateBackup(grpc::ClientContext*,
41  btadmin::UpdateBackupRequest const&,
42  btadmin::Backup*) {
43  return {grpc::StatusCode::UNIMPLEMENTED, "Not implemented"};
44 }
45 
46 grpc::Status AdminClient::DeleteBackup(grpc::ClientContext*,
47  btadmin::DeleteBackupRequest const&,
48  google::protobuf::Empty*) {
49  return {grpc::StatusCode::UNIMPLEMENTED, "Not implemented"};
50 }
51 
52 grpc::Status AdminClient::ListBackups(grpc::ClientContext*,
53  btadmin::ListBackupsRequest const&,
54  btadmin::ListBackupsResponse*) {
55  return {grpc::StatusCode::UNIMPLEMENTED, "Not implemented"};
56 }
57 
58 grpc::Status AdminClient::RestoreTable(grpc::ClientContext*,
59  btadmin::RestoreTableRequest const&,
60  google::longrunning::Operation*) {
61  return {grpc::StatusCode::UNIMPLEMENTED, "Not implemented"};
62 }
63 
64 std::unique_ptr<
65  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
66 AdminClient::AsyncCreateBackup(grpc::ClientContext*,
67  btadmin::CreateBackupRequest const&,
68  grpc::CompletionQueue*) {
69  return {};
70 }
71 
72 std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Backup>>
73 AdminClient::AsyncGetBackup(grpc::ClientContext*,
74  btadmin::GetBackupRequest const&,
75  grpc::CompletionQueue*) {
76  return {};
77 }
78 
79 std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Backup>>
80 AdminClient::AsyncUpdateBackup(grpc::ClientContext*,
81  btadmin::UpdateBackupRequest const&,
82  grpc::CompletionQueue*) {
83  return {};
84 }
85 
86 std::unique_ptr<
87  grpc::ClientAsyncResponseReaderInterface<google::protobuf::Empty>>
88 AdminClient::AsyncDeleteBackup(grpc::ClientContext*,
89  btadmin::DeleteBackupRequest const&,
90  grpc::CompletionQueue*) {
91  return {};
92 }
93 
94 std::unique_ptr<
95  grpc::ClientAsyncResponseReaderInterface<btadmin::ListBackupsResponse>>
96 AdminClient::AsyncListBackups(grpc::ClientContext*,
97  btadmin::ListBackupsRequest const&,
98  grpc::CompletionQueue*) {
99  return {};
100 }
101 
102 std::unique_ptr<
103  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
104 AdminClient::AsyncRestoreTable(grpc::ClientContext*,
105  btadmin::RestoreTableRequest const&,
106  grpc::CompletionQueue*) {
107  return {};
108 }
109 
110 namespace {
111 
112 /**
113  * An AdminClient for single-threaded programs that refreshes credentials on all
114  * gRPC errors.
115  *
116  * This class should not be used by multiple threads, it makes no attempt to
117  * protect its critical sections. While it is rare that the admin interface
118  * will be used by multiple threads, we should use the same approach here and in
119  * the regular client to support multi-threaded programs.
120  *
121  * The class also aggressively reconnects on any gRPC errors. A future version
122  * should only reconnect on those errors that indicate the credentials or
123  * connections need refreshing.
124  */
125 class DefaultAdminClient : public google::cloud::bigtable::AdminClient {
126  public:
127  DefaultAdminClient(std::string project, Options options)
128  : project_(std::move(project)), impl_(std::move(options)) {}
129 
130  std::string const& project() const override { return project_; }
131  std::shared_ptr<grpc::Channel> Channel() override { return impl_.Channel(); }
132  void reset() override { return impl_.reset(); }
133 
134  grpc::Status CreateTable(grpc::ClientContext* context,
135  btadmin::CreateTableRequest const& request,
136  btadmin::Table* response) override {
137  return impl_.Stub()->CreateTable(context, request, response);
138  }
139 
140  grpc::Status ListTables(grpc::ClientContext* context,
141  btadmin::ListTablesRequest const& request,
142  btadmin::ListTablesResponse* response) override {
143  return impl_.Stub()->ListTables(context, request, response);
144  }
145 
146  std::unique_ptr<
147  grpc::ClientAsyncResponseReaderInterface<btadmin::ListTablesResponse>>
148  AsyncListTables(grpc::ClientContext* context,
149  btadmin::ListTablesRequest const& request,
150  grpc::CompletionQueue* cq) override {
151  return impl_.Stub()->AsyncListTables(context, request, cq);
152  }
153 
154  grpc::Status GetTable(grpc::ClientContext* context,
155  btadmin::GetTableRequest const& request,
156  btadmin::Table* response) override {
157  return impl_.Stub()->GetTable(context, request, response);
158  }
159 
160  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Table>>
161  AsyncGetTable(grpc::ClientContext* context,
162  btadmin::GetTableRequest const& request,
163  grpc::CompletionQueue* cq) override {
164  return impl_.Stub()->AsyncGetTable(context, request, cq);
165  }
166 
167  grpc::Status DeleteTable(grpc::ClientContext* context,
168  btadmin::DeleteTableRequest const& request,
169  google::protobuf::Empty* response) override {
170  return impl_.Stub()->DeleteTable(context, request, response);
171  }
172 
173  grpc::Status CreateBackup(grpc::ClientContext* context,
174  btadmin::CreateBackupRequest const& request,
175  google::longrunning::Operation* response) override {
176  return impl_.Stub()->CreateBackup(context, request, response);
177  }
178 
179  grpc::Status GetBackup(grpc::ClientContext* context,
180  btadmin::GetBackupRequest const& request,
181  btadmin::Backup* response) override {
182  return impl_.Stub()->GetBackup(context, request, response);
183  }
184 
185  grpc::Status UpdateBackup(grpc::ClientContext* context,
186  btadmin::UpdateBackupRequest const& request,
187  btadmin::Backup* response) override {
188  return impl_.Stub()->UpdateBackup(context, request, response);
189  }
190 
191  grpc::Status DeleteBackup(grpc::ClientContext* context,
192  btadmin::DeleteBackupRequest const& request,
193  google::protobuf::Empty* response) override {
194  return impl_.Stub()->DeleteBackup(context, request, response);
195  }
196 
197  grpc::Status ListBackups(grpc::ClientContext* context,
198  btadmin::ListBackupsRequest const& request,
199  btadmin::ListBackupsResponse* response) override {
200  return impl_.Stub()->ListBackups(context, request, response);
201  }
202 
203  grpc::Status RestoreTable(grpc::ClientContext* context,
204  btadmin::RestoreTableRequest const& request,
205  google::longrunning::Operation* response) override {
206  return impl_.Stub()->RestoreTable(context, request, response);
207  }
208 
209  grpc::Status ModifyColumnFamilies(
210  grpc::ClientContext* context,
211  btadmin::ModifyColumnFamiliesRequest const& request,
212  btadmin::Table* response) override {
213  return impl_.Stub()->ModifyColumnFamilies(context, request, response);
214  }
215 
216  grpc::Status DropRowRange(grpc::ClientContext* context,
217  btadmin::DropRowRangeRequest const& request,
218  google::protobuf::Empty* response) override {
219  return impl_.Stub()->DropRowRange(context, request, response);
220  }
221 
222  grpc::Status GenerateConsistencyToken(
223  grpc::ClientContext* context,
224  btadmin::GenerateConsistencyTokenRequest const& request,
225  btadmin::GenerateConsistencyTokenResponse* response) override {
226  return impl_.Stub()->GenerateConsistencyToken(context, request, response);
227  }
228 
229  grpc::Status CheckConsistency(
230  grpc::ClientContext* context,
231  btadmin::CheckConsistencyRequest const& request,
232  btadmin::CheckConsistencyResponse* response) override {
233  return impl_.Stub()->CheckConsistency(context, request, response);
234  }
235 
236  grpc::Status GetOperation(
237  grpc::ClientContext* context,
238  google::longrunning::GetOperationRequest const& request,
239  google::longrunning::Operation* response) override {
240  auto stub = google::longrunning::Operations::NewStub(Channel());
241  return stub->GetOperation(context, request, response);
242  }
243 
244  grpc::Status GetIamPolicy(grpc::ClientContext* context,
245  google::iam::v1::GetIamPolicyRequest const& request,
246  google::iam::v1::Policy* response) override {
247  return impl_.Stub()->GetIamPolicy(context, request, response);
248  }
249 
250  grpc::Status SetIamPolicy(grpc::ClientContext* context,
251  google::iam::v1::SetIamPolicyRequest const& request,
252  google::iam::v1::Policy* response) override {
253  return impl_.Stub()->SetIamPolicy(context, request, response);
254  }
255 
256  grpc::Status TestIamPermissions(
257  grpc::ClientContext* context,
258  google::iam::v1::TestIamPermissionsRequest const& request,
259  google::iam::v1::TestIamPermissionsResponse* response) override {
260  return impl_.Stub()->TestIamPermissions(context, request, response);
261  }
262 
263  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Table>>
264  AsyncCreateTable(grpc::ClientContext* context,
265  btadmin::CreateTableRequest const& request,
266  grpc::CompletionQueue* cq) override {
267  return impl_.Stub()->AsyncCreateTable(context, request, cq);
268  }
269 
270  std::unique_ptr<
271  grpc::ClientAsyncResponseReaderInterface<::google::protobuf::Empty>>
272  AsyncDeleteTable(grpc::ClientContext* context,
273  btadmin::DeleteTableRequest const& request,
274  grpc::CompletionQueue* cq) override {
275  return impl_.Stub()->AsyncDeleteTable(context, request, cq);
276  }
277 
278  std::unique_ptr<
279  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
280  AsyncCreateBackup(grpc::ClientContext* context,
281  btadmin::CreateBackupRequest const& request,
282  grpc::CompletionQueue* cq) override {
283  return impl_.Stub()->AsyncCreateBackup(context, request, cq);
284  }
285 
286  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Backup>>
287  AsyncGetBackup(grpc::ClientContext* context,
288  btadmin::GetBackupRequest const& request,
289  grpc::CompletionQueue* cq) override {
290  return impl_.Stub()->AsyncGetBackup(context, request, cq);
291  }
292 
293  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Backup>>
294  AsyncUpdateBackup(grpc::ClientContext* context,
295  btadmin::UpdateBackupRequest const& request,
296  grpc::CompletionQueue* cq) override {
297  return impl_.Stub()->AsyncUpdateBackup(context, request, cq);
298  }
299 
300  std::unique_ptr<
301  grpc::ClientAsyncResponseReaderInterface<google::protobuf::Empty>>
302  AsyncDeleteBackup(grpc::ClientContext* context,
303  btadmin::DeleteBackupRequest const& request,
304  grpc::CompletionQueue* cq) override {
305  return impl_.Stub()->AsyncDeleteBackup(context, request, cq);
306  }
307 
308  std::unique_ptr<
309  grpc::ClientAsyncResponseReaderInterface<btadmin::ListBackupsResponse>>
310  AsyncListBackups(grpc::ClientContext* context,
311  btadmin::ListBackupsRequest const& request,
312  grpc::CompletionQueue* cq) override {
313  return impl_.Stub()->AsyncListBackups(context, request, cq);
314  }
315 
316  std::unique_ptr<
317  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
318  AsyncRestoreTable(grpc::ClientContext* context,
319  btadmin::RestoreTableRequest const& request,
320  grpc::CompletionQueue* cq) override {
321  return impl_.Stub()->AsyncRestoreTable(context, request, cq);
322  }
323 
324  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<btadmin::Table>>
325  AsyncModifyColumnFamilies(grpc::ClientContext* context,
326  btadmin::ModifyColumnFamiliesRequest const& request,
327  grpc::CompletionQueue* cq) override {
328  return impl_.Stub()->AsyncModifyColumnFamilies(context, request, cq);
329  }
330 
331  std::unique_ptr<
332  grpc::ClientAsyncResponseReaderInterface<google::protobuf::Empty>>
333  AsyncDropRowRange(grpc::ClientContext* context,
334  btadmin::DropRowRangeRequest const& request,
335  grpc::CompletionQueue* cq) override {
336  return impl_.Stub()->AsyncDropRowRange(context, request, cq);
337  };
338 
339  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
340  btadmin::GenerateConsistencyTokenResponse>>
341  AsyncGenerateConsistencyToken(
342  grpc::ClientContext* context,
343  btadmin::GenerateConsistencyTokenRequest const& request,
344  grpc::CompletionQueue* cq) override {
345  return impl_.Stub()->AsyncGenerateConsistencyToken(context, request, cq);
346  }
347 
348  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
349  btadmin::CheckConsistencyResponse>>
350  AsyncCheckConsistency(grpc::ClientContext* context,
351  btadmin::CheckConsistencyRequest const& request,
352  grpc::CompletionQueue* cq) override {
353  return impl_.Stub()->AsyncCheckConsistency(context, request, cq);
354  }
355 
356  std::unique_ptr<
357  grpc::ClientAsyncResponseReaderInterface<google::iam::v1::Policy>>
358  AsyncGetIamPolicy(grpc::ClientContext* context,
359  google::iam::v1::GetIamPolicyRequest const& request,
360  grpc::CompletionQueue* cq) override {
361  return impl_.Stub()->AsyncGetIamPolicy(context, request, cq);
362  }
363 
364  std::unique_ptr<
365  grpc::ClientAsyncResponseReaderInterface<google::iam::v1::Policy>>
366  AsyncSetIamPolicy(grpc::ClientContext* context,
367  google::iam::v1::SetIamPolicyRequest const& request,
368  grpc::CompletionQueue* cq) override {
369  return impl_.Stub()->AsyncSetIamPolicy(context, request, cq);
370  }
371 
372  std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
373  google::iam::v1::TestIamPermissionsResponse>>
374  AsyncTestIamPermissions(
375  grpc::ClientContext* context,
376  google::iam::v1::TestIamPermissionsRequest const& request,
377  grpc::CompletionQueue* cq) override {
378  return impl_.Stub()->AsyncTestIamPermissions(context, request, cq);
379  }
380 
381  std::unique_ptr<
382  grpc::ClientAsyncResponseReaderInterface<google::longrunning::Operation>>
383  AsyncGetOperation(grpc::ClientContext* context,
384  google::longrunning::GetOperationRequest const& request,
385  grpc::CompletionQueue* cq) override {
386  auto stub = google::longrunning::Operations::NewStub(Channel());
387  return std::unique_ptr<grpc::ClientAsyncResponseReaderInterface<
388  google::longrunning::Operation>>(
389  stub->AsyncGetOperation(context, request, cq).release());
390  }
391 
392  private:
393  google::cloud::BackgroundThreadsFactory BackgroundThreadsFactory() override {
394  return impl_.BackgroundThreadsFactory();
395  }
396 
397  struct Traits {
398  static std::string const& Endpoint(Options const& options) {
399  return options.get<AdminEndpointOption>();
400  }
401  };
402 
403  std::string project_;
404  internal::CommonClient<Traits, btadmin::BigtableTableAdmin> impl_;
405 };
406 
407 } // namespace
408 
409 std::shared_ptr<AdminClient> MakeAdminClient(std::string project,
410  Options options) {
411  options = internal::DefaultOptions(std::move(options));
412  bool tracing_enabled = google::cloud::internal::Contains(
413  options.get<TracingComponentsOption>(), "rpc");
414  auto tracing_options = options.get<GrpcTracingOptionsOption>();
415 
416  std::shared_ptr<AdminClient> client = std::make_shared<DefaultAdminClient>(
417  std::move(project), std::move(options));
418  if (tracing_enabled) {
419  GCP_LOG(INFO) << "Enabled logging for gRPC calls";
420  client = std::make_shared<internal::LoggingAdminClient>(
421  std::move(client), std::move(tracing_options));
422  }
423  return client;
424 }
425 
426 std::shared_ptr<AdminClient> CreateDefaultAdminClient(std::string project,
427  ClientOptions options) {
428  return MakeAdminClient(std::move(project),
429  internal::MakeOptions(std::move(options)));
430 }
431 
433 } // namespace bigtable
434 } // namespace cloud
435 } // namespace google