Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
instance_admin_client.cc
Go to the documentation of this file.
1 // Copyright 2019 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 // TODO(#7356): Remove this file after the deprecation period expires
16 #include "google/cloud/internal/disable_deprecation_warnings.inc"
17 #include "google/cloud/spanner/instance_admin_client.h"
18 
19 namespace google {
20 namespace cloud {
21 namespace spanner {
22 inline namespace SPANNER_CLIENT_NS {
23 
24 StatusOr<google::spanner::admin::instance::v1::Instance>
26  return conn_->GetInstance({in.FullName()});
27 }
28 
29 future<StatusOr<google::spanner::admin::instance::v1::Instance>>
31  google::spanner::admin::instance::v1::CreateInstanceRequest const&
32  request) {
33  return conn_->CreateInstance({request});
34 }
35 
36 future<StatusOr<google::spanner::admin::instance::v1::Instance>>
38  google::spanner::admin::instance::v1::UpdateInstanceRequest const&
39  request) {
40  return conn_->UpdateInstance({request});
41 }
42 
44  return conn_->DeleteInstance({in.FullName()});
45 }
46 
47 StatusOr<google::spanner::admin::instance::v1::InstanceConfig>
48 InstanceAdminClient::GetInstanceConfig(std::string const& name) {
49  return conn_->GetInstanceConfig({name});
50 }
51 
52 ListInstanceConfigsRange InstanceAdminClient::ListInstanceConfigs(
53  std::string project_id) {
54  return conn_->ListInstanceConfigs({std::move(project_id)});
55 }
56 
57 ListInstancesRange InstanceAdminClient::ListInstances(std::string project_id,
58  std::string filter) {
59  return conn_->ListInstances({std::move(project_id), std::move(filter)});
60 }
61 
62 StatusOr<google::iam::v1::Policy> InstanceAdminClient::GetIamPolicy(
63  Instance const& in) {
64  return conn_->GetIamPolicy({in.FullName()});
65 }
66 
67 StatusOr<google::iam::v1::Policy> InstanceAdminClient::SetIamPolicy(
68  Instance const& in, google::iam::v1::Policy policy) {
69  return conn_->SetIamPolicy({in.FullName(), std::move(policy)});
70 }
71 
72 StatusOr<google::iam::v1::Policy> InstanceAdminClient::SetIamPolicy(
73  Instance const& in, IamUpdater const& updater) {
74  auto const rerun_maximum_duration = std::chrono::minutes(15);
75  auto default_rerun_policy =
76  LimitedTimeTransactionRerunPolicy(rerun_maximum_duration).clone();
77 
78  auto const backoff_initial_delay = std::chrono::milliseconds(1000);
79  auto const backoff_maximum_delay = std::chrono::minutes(5);
80  auto const backoff_scaling = 2.0;
81  auto default_backoff_policy =
82  ExponentialBackoffPolicy(backoff_initial_delay, backoff_maximum_delay,
83  backoff_scaling)
84  .clone();
85 
86  return SetIamPolicy(in, updater, std::move(default_rerun_policy),
87  std::move(default_backoff_policy));
88 }
89 
90 StatusOr<google::iam::v1::Policy> InstanceAdminClient::SetIamPolicy(
91  Instance const& in, IamUpdater const& updater,
92  std::unique_ptr<TransactionRerunPolicy> rerun_policy,
93  std::unique_ptr<BackoffPolicy> backoff_policy) {
94  using RerunnablePolicy = spanner_internal::SafeTransactionRerun;
95 
96  Status last_status;
97  do {
98  auto current_policy = GetIamPolicy(in);
99  if (!current_policy) {
100  last_status = std::move(current_policy).status();
101  } else {
102  auto etag = current_policy->etag();
103  auto desired = updater(*current_policy);
104  if (!desired.has_value()) {
105  return current_policy;
106  }
107  desired->set_etag(std::move(etag));
108  auto result = SetIamPolicy(in, *std::move(desired));
109  if (RerunnablePolicy::IsOk(result.status())) {
110  return result;
111  }
112  last_status = std::move(result).status();
113  }
114  if (!rerun_policy->OnFailure(last_status)) break;
115  std::this_thread::sleep_for(backoff_policy->OnCompletion());
116  } while (!rerun_policy->IsExhausted());
117  return last_status;
118 }
119 
120 StatusOr<google::iam::v1::TestIamPermissionsResponse>
122  std::vector<std::string> permissions) {
123  return conn_->TestIamPermissions({in.FullName(), std::move(permissions)});
124 }
125 
126 } // namespace SPANNER_CLIENT_NS
127 } // namespace spanner
128 } // namespace cloud
129 } // namespace google