Google Cloud Bigtable C++ Client  1.33.0
A C++ Client Library for Google Cloud Bigtable
table_admin.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/table_admin.h"
16 #include "google/cloud/bigtable/internal/async_retry_multi_page.h"
17 #include "google/cloud/bigtable/internal/async_retry_op.h"
18 #include "google/cloud/bigtable/internal/async_retry_unary_rpc_and_poll.h"
19 #include "google/cloud/bigtable/internal/unary_client_utils.h"
20 #include "google/cloud/grpc_error_delegate.h"
21 #include "google/cloud/internal/async_retry_unary_rpc.h"
22 #include "google/cloud/internal/retry_policy.h"
23 #include "google/cloud/internal/time_utils.h"
24 #include <google/protobuf/duration.pb.h>
25 #include <sstream>
26 
27 namespace btadmin = ::google::bigtable::admin::v2;
28 
29 namespace google {
30 namespace cloud {
31 namespace bigtable {
33 static_assert(std::is_copy_constructible<bigtable::TableAdmin>::value,
34  "bigtable::TableAdmin must be constructible");
35 static_assert(std::is_copy_assignable<bigtable::TableAdmin>::value,
36  "bigtable::TableAdmin must be assignable");
37 
38 // NOLINTNEXTLINE(readability-identifier-naming)
39 constexpr TableAdmin::TableView TableAdmin::ENCRYPTION_VIEW;
40 // NOLINTNEXTLINE(readability-identifier-naming)
41 constexpr TableAdmin::TableView TableAdmin::FULL;
42 // NOLINTNEXTLINE(readability-identifier-naming)
43 constexpr TableAdmin::TableView TableAdmin::NAME_ONLY;
44 // NOLINTNEXTLINE(readability-identifier-naming)
45 constexpr TableAdmin::TableView TableAdmin::REPLICATION_VIEW;
46 // NOLINTNEXTLINE(readability-identifier-naming)
47 constexpr TableAdmin::TableView TableAdmin::SCHEMA_VIEW;
48 // NOLINTNEXTLINE(readability-identifier-naming)
49 constexpr TableAdmin::TableView TableAdmin::VIEW_UNSPECIFIED;
50 
51 /// Shortcuts to avoid typing long names over and over.
52 using ClientUtils = bigtable::internal::UnaryClientUtils<AdminClient>;
53 using ::google::cloud::internal::Idempotency;
54 
55 StatusOr<btadmin::Table> TableAdmin::CreateTable(std::string table_id,
56  TableConfig config) {
57  grpc::Status status;
58 
59  auto request = std::move(config).as_proto();
60  request.set_parent(instance_name());
61  request.set_table_id(std::move(table_id));
62 
63  // This is a non-idempotent API, use the correct retry loop for this type of
64  // operation.
65  auto result = ClientUtils::MakeNonIdempotentCall(
66  *client_, clone_rpc_retry_policy(), clone_metadata_update_policy(),
67  &AdminClient::CreateTable, request, "CreateTable", status);
68 
69  if (!status.ok()) {
71  }
72  return result;
73 }
74 
75 StatusOr<std::vector<btadmin::Table>> TableAdmin::ListTables(
76  btadmin::Table::View view) {
77  grpc::Status status;
78 
79  // Copy the policies in effect for the operation.
80  auto rpc_policy = clone_rpc_retry_policy();
81  auto backoff_policy = clone_rpc_backoff_policy();
82 
83  // Build the RPC request, try to minimize copying.
84  std::vector<btadmin::Table> result;
85  std::string page_token;
86  do {
87  btadmin::ListTablesRequest request;
88  request.set_page_token(std::move(page_token));
89  request.set_parent(instance_name());
90  request.set_view(view);
91 
92  auto response = ClientUtils::MakeCall(
93  *client_, *rpc_policy, *backoff_policy, clone_metadata_update_policy(),
94  &AdminClient::ListTables, request, "TableAdmin", status,
95  Idempotency::kIdempotent);
96 
97  if (!status.ok()) {
99  }
100 
101  for (auto& x : *response.mutable_tables()) {
102  result.emplace_back(std::move(x));
103  }
104  page_token = std::move(*response.mutable_next_page_token());
105  } while (!page_token.empty());
106  return result;
107 }
108 
109 StatusOr<btadmin::Table> TableAdmin::GetTable(std::string const& table_id,
110  btadmin::Table::View view) {
111  grpc::Status status;
112  btadmin::GetTableRequest request;
113  auto name = TableName(table_id);
114  request.set_name(name);
115  request.set_view(view);
116 
117  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
118 
119  auto result = ClientUtils::MakeCall(
120  *client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
121  metadata_update_policy, &AdminClient::GetTable, request, "GetTable",
122  status, Idempotency::kIdempotent);
123  if (!status.ok()) {
125  }
126 
127  return result;
128 }
129 
130 Status TableAdmin::DeleteTable(std::string const& table_id) {
131  grpc::Status status;
132  btadmin::DeleteTableRequest request;
133  auto name = TableName(table_id);
134  request.set_name(name);
135 
136  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
137 
138  // This is a non-idempotent API, use the correct retry loop for this type of
139  // operation.
140  ClientUtils::MakeNonIdempotentCall(
141  *client_, clone_rpc_retry_policy(), metadata_update_policy,
142  &AdminClient::DeleteTable, request, "DeleteTable", status);
143 
145 }
146 
147 google::bigtable::admin::v2::CreateBackupRequest
148 TableAdmin::CreateBackupParams::AsProto(std::string instance_name) const {
149  google::bigtable::admin::v2::CreateBackupRequest proto;
150  proto.set_parent(instance_name + "/clusters/" + cluster_id);
151  proto.set_backup_id(backup_id);
152  proto.mutable_backup()->set_source_table(std::move(instance_name) +
153  "/tables/" + table_name);
154  *proto.mutable_backup()->mutable_expire_time() =
155  google::cloud::internal::ToProtoTimestamp(expire_time);
156  return proto;
157 }
158 
159 StatusOr<google::bigtable::admin::v2::Backup> TableAdmin::CreateBackup(
160  CreateBackupParams const& params) {
161  auto cq = background_threads_->cq();
162  return AsyncCreateBackupImpl(cq, params).get();
163 }
164 
165 future<StatusOr<google::bigtable::admin::v2::Backup>>
166 TableAdmin::AsyncCreateBackupImpl(CompletionQueue& cq,
167  CreateBackupParams const& params) {
168  auto request = params.AsProto(instance_name());
169  MetadataUpdatePolicy metadata_update_policy(request.parent(),
170  MetadataParamTypes::PARENT);
171  auto client = client_;
172  return internal::AsyncStartPollAfterRetryUnaryRpc<
173  google::bigtable::admin::v2::Backup>(
174  __func__, clone_polling_policy(), clone_rpc_retry_policy(),
175  clone_rpc_backoff_policy(),
176  internal::ConstantIdempotencyPolicy(Idempotency::kNonIdempotent),
177  metadata_update_policy, client,
178  [client](grpc::ClientContext* context,
179  google::bigtable::admin::v2::CreateBackupRequest const& request,
180  grpc::CompletionQueue* cq) {
181  return client->AsyncCreateBackup(context, request, cq);
182  },
183  std::move(request), cq);
184 }
185 
186 StatusOr<google::bigtable::admin::v2::Backup> TableAdmin::GetBackup(
187  std::string const& cluster_id, std::string const& backup_id) {
188  grpc::Status status;
189  btadmin::GetBackupRequest request;
191  cluster_id, backup_id);
192  request.set_name(name);
193 
194  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
195 
196  auto result = ClientUtils::MakeCall(
197  *client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
198  metadata_update_policy, &AdminClient::GetBackup, request, "GetBackup",
199  status, Idempotency::kIdempotent);
200  if (!status.ok()) {
202  }
203 
204  return result;
205 }
206 
207 google::bigtable::admin::v2::UpdateBackupRequest
209  std::string const& instance_name) const {
210  google::bigtable::admin::v2::UpdateBackupRequest proto;
211  proto.mutable_backup()->set_name(instance_name + "/clusters/" + cluster_id +
212  "/backups/" + backup_name);
213  *proto.mutable_backup()->mutable_expire_time() =
214  google::cloud::internal::ToProtoTimestamp(expire_time);
215  proto.mutable_update_mask()->add_paths("expire_time");
216  return proto;
217 }
218 
219 StatusOr<google::bigtable::admin::v2::Backup> TableAdmin::UpdateBackup(
220  UpdateBackupParams const& params) {
221  grpc::Status status;
222  btadmin::UpdateBackupRequest request = params.AsProto(instance_name());
223 
224  MetadataUpdatePolicy metadata_update_policy(request.backup().name(),
225  MetadataParamTypes::BACKUP_NAME);
226 
227  auto result = ClientUtils::MakeCall(
228  *client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
229  metadata_update_policy, &AdminClient::UpdateBackup, request,
230  "UpdateBackup", status, Idempotency::kIdempotent);
231  if (!status.ok()) {
233  }
234 
235  return result;
236 }
237 
239  google::bigtable::admin::v2::Backup const& backup) {
240  grpc::Status status;
241  btadmin::DeleteBackupRequest request;
242  request.set_name(backup.name());
243  MetadataUpdatePolicy metadata_update_policy(request.name(),
244  MetadataParamTypes::NAME);
245  auto result = ClientUtils::MakeCall(
246  *client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
247  metadata_update_policy, &AdminClient::DeleteBackup, request,
248  "DeleteBackup", status, Idempotency::kIdempotent);
249  if (!status.ok()) {
251  }
252 
253  return {};
254 }
255 
256 Status TableAdmin::DeleteBackup(std::string const& cluster_id,
257  std::string const& backup_id) {
258  grpc::Status status;
259  btadmin::DeleteBackupRequest request;
261  cluster_id, backup_id));
262 
263  MetadataUpdatePolicy metadata_update_policy(request.name(),
264  MetadataParamTypes::NAME);
265 
266  auto result = ClientUtils::MakeCall(
267  *client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
268  metadata_update_policy, &AdminClient::DeleteBackup, request,
269  "DeleteBackup", status, Idempotency::kIdempotent);
270  if (!status.ok()) {
272  }
273 
274  return {};
275 }
276 
277 google::bigtable::admin::v2::ListBackupsRequest
278 TableAdmin::ListBackupsParams::AsProto(std::string const& instance_name) const {
279  google::bigtable::admin::v2::ListBackupsRequest proto;
280  proto.set_parent(cluster_id ? instance_name + "/clusters/" + *cluster_id
281  : instance_name + "/clusters/-");
282  if (filter) *proto.mutable_filter() = *filter;
283  if (order_by) *proto.mutable_order_by() = *order_by;
284  return proto;
285 }
286 
287 StatusOr<std::vector<google::bigtable::admin::v2::Backup>>
289  grpc::Status status;
290 
291  // Copy the policies in effect for the operation.
292  auto rpc_policy = clone_rpc_retry_policy();
293  auto backoff_policy = clone_rpc_backoff_policy();
294 
295  // Build the RPC request, try to minimize copying.
296  std::vector<btadmin::Backup> result;
297  btadmin::ListBackupsRequest request = params.AsProto(instance_name());
298 
299  MetadataUpdatePolicy metadata_update_policy(request.parent(),
300  MetadataParamTypes::PARENT);
301 
302  std::string page_token;
303  do {
304  request.set_page_token(std::move(page_token));
305 
306  auto response = ClientUtils::MakeCall(
307  *client_, *rpc_policy, *backoff_policy, metadata_update_policy,
308  &AdminClient::ListBackups, request, "TableAdmin", status,
309  Idempotency::kIdempotent);
310 
311  if (!status.ok()) {
313  }
314 
315  for (auto& x : *response.mutable_backups()) {
316  result.emplace_back(std::move(x));
317  }
318  page_token = std::move(*response.mutable_next_page_token());
319  } while (!page_token.empty());
320  return result;
321 }
322 
323 google::bigtable::admin::v2::RestoreTableRequest
325  std::string const& instance_name) const {
326  google::bigtable::admin::v2::RestoreTableRequest proto;
327  proto.set_parent(instance_name);
328  proto.set_table_id(table_id);
329  proto.set_backup(instance_name + "/clusters/" + cluster_id + "/backups/" +
330  backup_id);
331  return proto;
332 }
333 
334 StatusOr<google::bigtable::admin::v2::Table> TableAdmin::RestoreTable(
335  RestoreTableParams const& params) {
336  auto cq = background_threads_->cq();
337  return AsyncRestoreTableImpl(cq, params).get();
338 }
339 
340 future<StatusOr<google::bigtable::admin::v2::Table>>
341 TableAdmin::AsyncRestoreTableImpl(CompletionQueue& cq,
342  RestoreTableParams const& params) {
343  return AsyncRestoreTableImpl(
344  cq,
346  params.table_id, BackupName(params.cluster_id, params.backup_id)});
347 }
348 
349 google::bigtable::admin::v2::RestoreTableRequest AsProto(
350  std::string const& instance_name,
352  google::bigtable::admin::v2::RestoreTableRequest proto;
353  proto.set_parent(instance_name);
354  proto.set_table_id(std::move(p.table_id));
355  proto.set_backup(std::move(p.backup_name));
356  return proto;
357 }
358 
359 StatusOr<google::bigtable::admin::v2::Table> TableAdmin::RestoreTable(
361  auto cq = background_threads_->cq();
362  return AsyncRestoreTableImpl(cq, std::move(params)).get();
363 }
364 
365 future<StatusOr<google::bigtable::admin::v2::Table>>
366 TableAdmin::AsyncRestoreTableImpl(CompletionQueue& cq,
368  MetadataUpdatePolicy metadata_update_policy(instance_name(),
369  MetadataParamTypes::PARENT);
370  auto client = client_;
371  return internal::AsyncStartPollAfterRetryUnaryRpc<
372  google::bigtable::admin::v2::Table>(
373  __func__, clone_polling_policy(), clone_rpc_retry_policy(),
374  clone_rpc_backoff_policy(),
375  internal::ConstantIdempotencyPolicy(Idempotency::kNonIdempotent),
376  metadata_update_policy, client,
377  [client](grpc::ClientContext* context,
378  google::bigtable::admin::v2::RestoreTableRequest const& request,
379  grpc::CompletionQueue* cq) {
380  return client->AsyncRestoreTable(context, request, cq);
381  },
382  AsProto(instance_name(), std::move(params)), cq);
383 }
384 
385 StatusOr<btadmin::Table> TableAdmin::ModifyColumnFamilies(
386  std::string const& table_id,
387  std::vector<ColumnFamilyModification> modifications) {
388  grpc::Status status;
389 
390  btadmin::ModifyColumnFamiliesRequest request;
391  auto name = TableName(table_id);
392  request.set_name(name);
393  for (auto& m : modifications) {
394  *request.add_modifications() = std::move(m).as_proto();
395  }
396  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
397  auto result = ClientUtils::MakeNonIdempotentCall(
398  *client_, clone_rpc_retry_policy(), metadata_update_policy,
399  &AdminClient::ModifyColumnFamilies, request, "ModifyColumnFamilies",
400  status);
401 
402  if (!status.ok()) {
404  }
405  return result;
406 }
407 
408 Status TableAdmin::DropRowsByPrefix(std::string const& table_id,
409  std::string row_key_prefix) {
410  grpc::Status status;
411  btadmin::DropRowRangeRequest request;
412  auto name = TableName(table_id);
413  request.set_name(name);
414  request.set_row_key_prefix(std::move(row_key_prefix));
415  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
416  ClientUtils::MakeNonIdempotentCall(
417  *client_, clone_rpc_retry_policy(), metadata_update_policy,
418  &AdminClient::DropRowRange, request, "DropRowByPrefix", status);
419 
421 }
422 
424  std::string const& table_id, std::string const& consistency_token) {
425  auto cq = background_threads_->cq();
426  return AsyncWaitForConsistencyImpl(cq, table_id, consistency_token);
427 }
428 
429 google::cloud::future<StatusOr<Consistency>>
430 TableAdmin::AsyncWaitForConsistencyImpl(CompletionQueue& cq,
431  std::string const& table_id,
432  std::string const& consistency_token) {
433  class AsyncWaitForConsistencyState
434  : public std::enable_shared_from_this<AsyncWaitForConsistencyState> {
435  public:
436  static future<StatusOr<Consistency>> Create(
437  CompletionQueue cq, std::string table_id, std::string consistency_token,
438  TableAdmin const& table_admin,
439  std::unique_ptr<PollingPolicy> polling_policy) {
440  std::shared_ptr<AsyncWaitForConsistencyState> state(
441  new AsyncWaitForConsistencyState(
442  std::move(cq), std::move(table_id), std::move(consistency_token),
443  table_admin, std::move(polling_policy)));
444 
445  state->StartIteration();
446  return state->promise_.get_future();
447  }
448 
449  private:
450  AsyncWaitForConsistencyState(CompletionQueue cq, std::string table_id,
451  std::string consistency_token,
452  TableAdmin const& table_admin,
453  std::unique_ptr<PollingPolicy> polling_policy)
454  : cq_(std::move(cq)),
455  table_id_(std::move(table_id)),
456  consistency_token_(std::move(consistency_token)),
457  table_admin_(table_admin),
458  polling_policy_(std::move(polling_policy)) {}
459 
460  void StartIteration() {
461  auto self = shared_from_this();
462  table_admin_.AsyncCheckConsistency(cq_, table_id_, consistency_token_)
463  .then([self](future<StatusOr<Consistency>> f) {
464  self->OnCheckConsistency(f.get());
465  });
466  }
467 
468  void OnCheckConsistency(StatusOr<Consistency> consistent) {
469  auto self = shared_from_this();
470  if (consistent && *consistent == Consistency::kConsistent) {
471  promise_.set_value(*consistent);
472  return;
473  }
474  auto status = std::move(consistent).status();
475  if (!polling_policy_->OnFailure(status)) {
476  promise_.set_value(std::move(status));
477  return;
478  }
479  cq_.MakeRelativeTimer(polling_policy_->WaitPeriod())
480  .then([self](future<StatusOr<std::chrono::system_clock::time_point>>
481  result) {
482  if (auto tp = result.get()) {
483  self->StartIteration();
484  } else {
485  self->promise_.set_value(tp.status());
486  }
487  });
488  }
489 
490  CompletionQueue cq_;
491  std::string table_id_;
492  std::string consistency_token_;
493  TableAdmin table_admin_;
494  std::unique_ptr<PollingPolicy> polling_policy_;
495  google::cloud::promise<StatusOr<Consistency>> promise_;
496  };
497 
498  return AsyncWaitForConsistencyState::Create(cq, table_id, consistency_token,
499  *this, clone_polling_policy());
500 }
501 
502 Status TableAdmin::DropAllRows(std::string const& table_id) {
503  grpc::Status status;
504  btadmin::DropRowRangeRequest request;
505  auto name = TableName(table_id);
506  request.set_name(name);
507  request.set_delete_all_data_from_table(true);
508  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
509  ClientUtils::MakeNonIdempotentCall(
510  *client_, clone_rpc_retry_policy(), metadata_update_policy,
511  &AdminClient::DropRowRange, request, "DropAllRows", status);
512 
514 }
515 
516 StatusOr<std::string> TableAdmin::GenerateConsistencyToken(
517  std::string const& table_id) {
518  grpc::Status status;
519  btadmin::GenerateConsistencyTokenRequest request;
520  auto name = TableName(table_id);
521  request.set_name(name);
522  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
523 
524  auto response = ClientUtils::MakeCall(
525  *client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
526  metadata_update_policy, &AdminClient::GenerateConsistencyToken, request,
527  "GenerateConsistencyToken", status, Idempotency::kIdempotent);
528 
529  if (!status.ok()) {
531  }
532  return std::move(*response.mutable_consistency_token());
533 }
534 
536  std::string const& table_id, std::string const& consistency_token) {
537  grpc::Status status;
538  btadmin::CheckConsistencyRequest request;
539  auto name = TableName(table_id);
540  request.set_name(name);
541  request.set_consistency_token(consistency_token);
542  MetadataUpdatePolicy metadata_update_policy(name, MetadataParamTypes::NAME);
543 
544  auto response = ClientUtils::MakeCall(
545  *client_, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
546  metadata_update_policy, &AdminClient::CheckConsistency, request,
547  "CheckConsistency", status, Idempotency::kIdempotent);
548 
549  if (!status.ok()) {
551  }
552 
553  return response.consistent() ? Consistency::kConsistent
555 }
556 
557 future<StatusOr<Consistency>> TableAdmin::AsyncCheckConsistency(
558  CompletionQueue& cq, std::string const& table_id,
559  std::string const& consistency_token) {
560  btadmin::CheckConsistencyRequest request;
561  auto name = TableName(table_id);
562  request.set_name(name);
563  request.set_consistency_token(consistency_token);
564 
565  auto client = client_;
566  return google::cloud::internal::StartRetryAsyncUnaryRpc(
567  cq, __func__, clone_rpc_retry_policy(), clone_rpc_backoff_policy(),
568  Idempotency::kIdempotent,
569  [client, name](grpc::ClientContext* context,
570  btadmin::CheckConsistencyRequest const& request,
571  grpc::CompletionQueue* cq) {
572  MetadataUpdatePolicy(name, MetadataParamTypes::NAME)
573  .Setup(*context);
574  return client->AsyncCheckConsistency(context, request, cq);
575  },
576  std::move(request))
577  .then([](future<StatusOr<btadmin::CheckConsistencyResponse>> fut)
578  -> StatusOr<Consistency> {
579  auto result = fut.get();
580  if (!result) {
581  return result.status();
582  }
583 
584  return result->consistent() ? Consistency::kConsistent
586  });
587 }
588 
589 StatusOr<google::iam::v1::Policy> TableAdmin::GetIamPolicy(
590  std::string const& table_id) {
591  grpc::Status status;
592 
593  ::google::iam::v1::GetIamPolicyRequest request;
594  auto resource = TableName(table_id);
595  request.set_resource(resource);
596 
597  MetadataUpdatePolicy metadata_update_policy(resource,
598  MetadataParamTypes::RESOURCE);
599 
600  auto proto = ClientUtils::MakeCall(
601  *client_, *clone_rpc_retry_policy(), *clone_rpc_backoff_policy(),
602  metadata_update_policy, &AdminClient::GetIamPolicy, request,
603  "GetIamPolicy", status, Idempotency::kIdempotent);
604 
605  if (!status.ok()) {
606  return MakeStatusFromRpcError(status);
607  }
608 
609  return proto;
610 }
611 
612 StatusOr<google::iam::v1::Policy> TableAdmin::GetIamPolicy(
613  std::string const& cluster_id, std::string const& backup_id) {
614  grpc::Status status;
615 
616  ::google::iam::v1::GetIamPolicyRequest request;
617  auto resource = BackupName(cluster_id, backup_id);
618  request.set_resource(resource);
619 
620  MetadataUpdatePolicy metadata_update_policy(resource,
621  MetadataParamTypes::RESOURCE);
622 
623  auto proto = ClientUtils::MakeCall(
624  *client_, *clone_rpc_retry_policy(), *clone_rpc_backoff_policy(),
625  metadata_update_policy, &AdminClient::GetIamPolicy, request,
626  "GetIamPolicy", status, Idempotency::kIdempotent);
627 
628  if (!status.ok()) {
629  return MakeStatusFromRpcError(status);
630  }
631 
632  return proto;
633 }
634 
635 StatusOr<google::iam::v1::Policy> TableAdmin::SetIamPolicy(
636  std::string const& table_id, google::iam::v1::Policy const& iam_policy) {
637  grpc::Status status;
638 
639  ::google::iam::v1::SetIamPolicyRequest request;
640  auto resource = TableName(table_id);
641  request.set_resource(resource);
642  *request.mutable_policy() = iam_policy;
643 
644  MetadataUpdatePolicy metadata_update_policy(resource,
645  MetadataParamTypes::RESOURCE);
646 
647  auto proto = ClientUtils::MakeCall(
648  *client_, *clone_rpc_retry_policy(), *clone_rpc_backoff_policy(),
649  metadata_update_policy, &AdminClient::SetIamPolicy, request,
650  "SetIamPolicy", status, Idempotency::kIdempotent);
651 
652  if (!status.ok()) {
653  return MakeStatusFromRpcError(status);
654  }
655 
656  return proto;
657 }
658 
659 StatusOr<google::iam::v1::Policy> TableAdmin::SetIamPolicy(
660  std::string const& cluster_id, std::string const& backup_id,
661  google::iam::v1::Policy const& iam_policy) {
662  grpc::Status status;
663 
664  ::google::iam::v1::SetIamPolicyRequest request;
665  auto resource = BackupName(cluster_id, backup_id);
666  request.set_resource(resource);
667  *request.mutable_policy() = iam_policy;
668 
669  MetadataUpdatePolicy metadata_update_policy(resource,
670  MetadataParamTypes::RESOURCE);
671 
672  auto proto = ClientUtils::MakeCall(
673  *client_, *clone_rpc_retry_policy(), *clone_rpc_backoff_policy(),
674  metadata_update_policy, &AdminClient::SetIamPolicy, request,
675  "SetIamPolicy", status, Idempotency::kIdempotent);
676 
677  if (!status.ok()) {
678  return MakeStatusFromRpcError(status);
679  }
680 
681  return proto;
682 }
683 
684 StatusOr<std::vector<std::string>> TableAdmin::TestIamPermissions(
685  std::string const& table_id, std::vector<std::string> const& permissions) {
686  grpc::Status status;
687  ::google::iam::v1::TestIamPermissionsRequest request;
688  auto resource = TableName(table_id);
689  request.set_resource(resource);
690 
691  for (auto const& permission : permissions) {
692  request.add_permissions(permission);
693  }
694 
695  MetadataUpdatePolicy metadata_update_policy(resource,
696  MetadataParamTypes::RESOURCE);
697 
698  auto response = ClientUtils::MakeCall(
699  *client_, *clone_rpc_retry_policy(), *clone_rpc_backoff_policy(),
700  metadata_update_policy, &AdminClient::TestIamPermissions, request,
701  "TestIamPermissions", status, Idempotency::kIdempotent);
702 
703  std::vector<std::string> resource_permissions;
704 
705  for (auto& permission : *response.mutable_permissions()) {
706  resource_permissions.push_back(permission);
707  }
708 
709  if (!status.ok()) {
710  return MakeStatusFromRpcError(status);
711  }
712 
713  return resource_permissions;
714 }
715 
716 StatusOr<std::vector<std::string>> TableAdmin::TestIamPermissions(
717  std::string const& cluster_id, std::string const& backup_id,
718  std::vector<std::string> const& permissions) {
719  grpc::Status status;
720  ::google::iam::v1::TestIamPermissionsRequest request;
721  auto resource = BackupName(cluster_id, backup_id);
722  request.set_resource(resource);
723 
724  for (auto const& permission : permissions) {
725  request.add_permissions(permission);
726  }
727 
728  MetadataUpdatePolicy metadata_update_policy(resource,
729  MetadataParamTypes::RESOURCE);
730 
731  auto response = ClientUtils::MakeCall(
732  *client_, *clone_rpc_retry_policy(), *clone_rpc_backoff_policy(),
733  metadata_update_policy, &AdminClient::TestIamPermissions, request,
734  "TestIamPermissions", status, Idempotency::kIdempotent);
735 
736  std::vector<std::string> resource_permissions;
737 
738  for (auto& permission : *response.mutable_permissions()) {
739  resource_permissions.push_back(permission);
740  }
741 
742  if (!status.ok()) {
743  return MakeStatusFromRpcError(status);
744  }
745 
746  return resource_permissions;
747 }
748 
749 std::string TableAdmin::InstanceName() const {
751  instance_id_);
752 }
753 
755 } // namespace bigtable
756 } // namespace cloud
757 } // namespace google