Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
database_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/database_admin_client.h"
18 #include "google/cloud/spanner/timestamp.h"
19 #include <algorithm>
20 
21 namespace google {
22 namespace cloud {
23 namespace spanner {
24 inline namespace SPANNER_CLIENT_NS {
25 
26 namespace gcsa = ::google::spanner::admin::database::v1;
27 
28 DatabaseAdminClient::DatabaseAdminClient(ConnectionOptions const& options)
29  : conn_(MakeDatabaseAdminConnection(options)) {}
30 
31 future<StatusOr<gcsa::Database>> DatabaseAdminClient::CreateDatabase(
32  Database db, std::vector<std::string> extra_statements,
33  EncryptionConfig encryption_config) {
34  return conn_->CreateDatabase({std::move(db), std::move(extra_statements),
35  std::move(encryption_config)});
36 }
37 
38 StatusOr<gcsa::Database> DatabaseAdminClient::GetDatabase(Database db) {
39  return conn_->GetDatabase({std::move(db)});
40 }
41 
42 StatusOr<gcsa::GetDatabaseDdlResponse> DatabaseAdminClient::GetDatabaseDdl(
43  Database db) {
44  return conn_->GetDatabaseDdl({std::move(db)});
45 }
46 
47 future<StatusOr<gcsa::UpdateDatabaseDdlMetadata>>
49  std::vector<std::string> statements) {
50  return conn_->UpdateDatabase({std::move(db), std::move(statements)});
51 }
52 
53 ListDatabaseRange DatabaseAdminClient::ListDatabases(Instance in) {
54  return conn_->ListDatabases({std::move(in)});
55 }
56 
58  return conn_->DropDatabase({std::move(db)});
59 }
60 
61 future<StatusOr<gcsa::Database>> DatabaseAdminClient::RestoreDatabase(
62  Database db, Backup const& backup, EncryptionConfig encryption_config) {
63  return conn_->RestoreDatabase(
64  {std::move(db), backup.FullName(), std::move(encryption_config)});
65 }
66 
67 future<StatusOr<gcsa::Database>> DatabaseAdminClient::RestoreDatabase(
68  Database db, google::spanner::admin::database::v1::Backup const& backup,
69  EncryptionConfig encryption_config) {
70  return conn_->RestoreDatabase(
71  {std::move(db), backup.name(), std::move(encryption_config)});
72 }
73 
74 StatusOr<google::iam::v1::Policy> DatabaseAdminClient::GetIamPolicy(
75  Database db) {
76  return conn_->GetIamPolicy({std::move(db)});
77 }
78 
79 StatusOr<google::iam::v1::Policy> DatabaseAdminClient::SetIamPolicy(
80  Database db, google::iam::v1::Policy policy) {
81  return conn_->SetIamPolicy({std::move(db), std::move(policy)});
82 }
83 
84 StatusOr<google::iam::v1::Policy> DatabaseAdminClient::SetIamPolicy(
85  Database const& db, IamUpdater const& updater) {
86  auto const rerun_maximum_duration = std::chrono::minutes(15);
87  auto default_rerun_policy =
88  LimitedTimeTransactionRerunPolicy(rerun_maximum_duration).clone();
89 
90  auto const backoff_initial_delay = std::chrono::milliseconds(1000);
91  auto const backoff_maximum_delay = std::chrono::minutes(5);
92  auto const backoff_scaling = 2.0;
93  auto default_backoff_policy =
94  ExponentialBackoffPolicy(backoff_initial_delay, backoff_maximum_delay,
95  backoff_scaling)
96  .clone();
97 
98  return SetIamPolicy(db, updater, std::move(default_rerun_policy),
99  std::move(default_backoff_policy));
100 }
101 
102 StatusOr<google::iam::v1::Policy> DatabaseAdminClient::SetIamPolicy(
103  Database const& db, IamUpdater const& updater,
104  std::unique_ptr<TransactionRerunPolicy> rerun_policy,
105  std::unique_ptr<BackoffPolicy> backoff_policy) {
106  using RerunnablePolicy = spanner_internal::SafeTransactionRerun;
107 
108  Status last_status;
109  do {
110  auto current_policy = GetIamPolicy(db);
111  if (!current_policy) {
112  last_status = std::move(current_policy).status();
113  } else {
114  auto etag = current_policy->etag();
115  auto desired = updater(*current_policy);
116  if (!desired.has_value()) {
117  return current_policy;
118  }
119  desired->set_etag(std::move(etag));
120  auto result = SetIamPolicy(db, *std::move(desired));
121  if (RerunnablePolicy::IsOk(result.status())) {
122  return result;
123  }
124  last_status = std::move(result).status();
125  }
126  if (!rerun_policy->OnFailure(last_status)) break;
127  std::this_thread::sleep_for(backoff_policy->OnCompletion());
128  } while (!rerun_policy->IsExhausted());
129  return last_status;
130 }
131 
132 StatusOr<google::iam::v1::TestIamPermissionsResponse>
134  std::vector<std::string> permissions) {
135  return conn_->TestIamPermissions({std::move(db), std::move(permissions)});
136 }
137 
138 future<StatusOr<gcsa::Backup>> DatabaseAdminClient::CreateBackup(
139  Database db, std::string backup_id, Timestamp expire_time,
140  absl::optional<Timestamp> version_time,
141  EncryptionConfig encryption_config) {
142  auto expire_time_point =
143  expire_time.get<std::chrono::system_clock::time_point>();
144  if (!expire_time_point) {
145  expire_time_point = std::chrono::system_clock::time_point::max();
146  }
147  return conn_->CreateBackup(
148  {std::move(db), std::move(backup_id), *std::move(expire_time_point),
149  expire_time, std::move(version_time), std::move(encryption_config)});
150 }
151 
152 future<StatusOr<gcsa::Backup>> DatabaseAdminClient::CreateBackup(
153  Database db, std::string backup_id,
154  std::chrono::system_clock::time_point expire_time) {
155  auto ts = MakeTimestamp(expire_time);
156  if (!ts) return make_ready_future(StatusOr<gcsa::Backup>(ts.status()));
157  return CreateBackup(std::move(db), std::move(backup_id), *ts);
158 }
159 
160 StatusOr<gcsa::Backup> DatabaseAdminClient::GetBackup(Backup const& backup) {
161  return conn_->GetBackup({backup.FullName()});
162 }
163 
165  google::spanner::admin::database::v1::Backup const& backup) {
166  return conn_->DeleteBackup({backup.name()});
167 }
168 
170  return conn_->DeleteBackup({backup.FullName()});
171 }
172 
174  std::string filter) {
175  return conn_->ListBackups({std::move(in), std::move(filter)});
176 }
177 
179  google::spanner::admin::database::v1::Backup const& backup,
180  Timestamp expire_time) {
181  google::spanner::admin::database::v1::UpdateBackupRequest request;
182  request.mutable_backup()->set_name(backup.name());
183  *request.mutable_backup()->mutable_expire_time() =
184  expire_time.get<protobuf::Timestamp>().value();
185  request.mutable_update_mask()->add_paths("expire_time");
186  return conn_->UpdateBackup({request});
187 }
188 
190  Backup const& backup, Timestamp expire_time) {
191  google::spanner::admin::database::v1::UpdateBackupRequest request;
192  request.mutable_backup()->set_name(backup.FullName());
193  *request.mutable_backup()->mutable_expire_time() =
194  expire_time.get<protobuf::Timestamp>().value();
195  request.mutable_update_mask()->add_paths("expire_time");
196  return conn_->UpdateBackup({request});
197 }
198 
200  google::spanner::admin::database::v1::Backup const& backup,
201  std::chrono::system_clock::time_point const& expire_time) {
202  auto ts = MakeTimestamp(expire_time);
203  if (!ts) return ts.status();
204  return UpdateBackupExpireTime(backup, *ts);
205 }
206 
208  Backup const& backup,
209  std::chrono::system_clock::time_point const& expire_time) {
210  auto ts = MakeTimestamp(expire_time);
211  if (!ts) return ts.status();
212  return UpdateBackupExpireTime(backup, *ts);
213 }
214 
215 ListBackupOperationsRange DatabaseAdminClient::ListBackupOperations(
216  Instance in, std::string filter) {
217  return conn_->ListBackupOperations({std::move(in), std::move(filter)});
218 }
219 
220 ListDatabaseOperationsRange DatabaseAdminClient::ListDatabaseOperations(
221  Instance in, std::string filter) {
222  return conn_->ListDatabaseOperations({std::move(in), std::move(filter)});
223 }
224 
225 } // namespace SPANNER_CLIENT_NS
226 } // namespace spanner
227 } // namespace cloud
228 } // namespace google