16 #include "google/cloud/internal/disable_deprecation_warnings.inc"
17 #include "google/cloud/spanner/database_admin_connection.h"
18 #include "google/cloud/spanner/internal/defaults.h"
19 #include "google/cloud/spanner/options.h"
20 #include "google/cloud/spanner/timestamp.h"
21 #include "google/cloud/common_options.h"
22 #include "google/cloud/grpc_options.h"
23 #include "google/cloud/internal/async_long_running_operation.h"
24 #include "google/cloud/internal/retry_loop.h"
25 #include "google/cloud/options.h"
26 #include <grpcpp/grpcpp.h>
34 namespace gcsa = ::
google::spanner::admin::database::v1;
39 future<StatusOr<
google::spanner::admin::database::v1::Backup>>
46 future<StatusOr<
google::spanner::admin::database::v1::Database>>
53 StatusOr<
google::spanner::admin::database::v1::Backup>
66 return google::
cloud::internal::MakePaginationRange<ListBackupsRange>(
67 gcsa::ListBackupsRequest{},
68 [](gcsa::ListBackupsRequest
const&) {
69 return StatusOr<gcsa::ListBackupsResponse
>(
72 [](gcsa::ListBackupsResponse
const&) {
73 return std::vector<gcsa::Backup>{};
77 StatusOr<
google::spanner::admin::database::v1::Backup>
86 return google::
cloud::internal::MakePaginationRange<
87 ListBackupOperationsRange>(
88 gcsa::ListBackupOperationsRequest{},
89 [](gcsa::ListBackupOperationsRequest
const&) {
90 return StatusOr<gcsa::ListBackupOperationsResponse
>(
93 [](gcsa::ListBackupOperationsResponse
const&) {
94 return std::vector<
google::longrunning::Operation>{};
101 return google::
cloud::internal::MakePaginationRange<
102 ListDatabaseOperationsRange>(
103 gcsa::ListDatabaseOperationsRequest{},
104 [](gcsa::ListDatabaseOperationsRequest
const&) {
105 return StatusOr<gcsa::ListDatabaseOperationsResponse
>(
108 [](gcsa::ListDatabaseOperationsResponse
const&) {
109 return std::vector<
google::longrunning::Operation>{};
119 explicit DatabaseAdminConnectionImpl(
120 std::shared_ptr<spanner_internal::DatabaseAdminStub> stub,
122 : stub_(std::move(stub)),
124 backoff_policy_prototype_(
126 polling_policy_prototype_(
128 background_threads_(internal::MakeBackgroundThreadsFactory(opts)()) {}
130 ~DatabaseAdminConnectionImpl()
override =
default;
132 future<StatusOr<
google::spanner::admin::database::v1::Database>>
134 gcsa::CreateDatabaseRequest request;
136 request.set_create_statement(
"CREATE DATABASE `" +
138 struct EncryptionVisitor {
139 explicit EncryptionVisitor(gcsa::CreateDatabaseRequest& request)
140 : request_(request) {}
148 auto* config = request_.mutable_encryption_config();
151 gcsa::CreateDatabaseRequest& request_;
155 *request.add_extra_statements() = std::move(s);
158 return google::
cloud::internal::AsyncLongRunningOperation<gcsa::Database>(
159 background_threads_->
cq(), std::move(request),
161 std::unique_ptr<grpc::ClientContext> context,
162 gcsa::CreateDatabaseRequest
const& request) {
163 return stub->AsyncCreateDatabase(cq, std::move(context), request);
166 std::unique_ptr<grpc::ClientContext> context,
167 google::longrunning::GetOperationRequest
const& request) {
168 return stub->AsyncGetOperation(cq, std::move(context), request);
171 std::unique_ptr<grpc::ClientContext> context,
172 google::longrunning::CancelOperationRequest
const& request) {
173 return stub->AsyncCancelOperation(cq, std::move(context), request);
175 &
google::
cloud::internal::ExtractLongRunningResultResponse<
177 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
178 Idempotency::kNonIdempotent, polling_policy_prototype_->
clone(),
182 StatusOr<
google::spanner::admin::database::v1::Database> GetDatabase(
184 gcsa::GetDatabaseRequest request;
187 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
188 Idempotency::kIdempotent,
189 [
this](grpc::ClientContext& context,
190 gcsa::GetDatabaseRequest
const& request) {
191 return stub_->GetDatabase(context, request);
196 StatusOr<
google::spanner::admin::database::v1::GetDatabaseDdlResponse>
198 gcsa::GetDatabaseDdlRequest request;
201 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
202 Idempotency::kIdempotent,
203 [
this](grpc::ClientContext& context,
204 gcsa::GetDatabaseDdlRequest
const& request) {
205 return stub_->GetDatabaseDdl(context, request);
211 StatusOr<
google::spanner::admin::database::v1::UpdateDatabaseDdlMetadata>>
213 gcsa::UpdateDatabaseDdlRequest request;
216 *request.add_statements() = std::move(s);
219 return google::
cloud::internal::AsyncLongRunningOperation<
220 gcsa::UpdateDatabaseDdlMetadata>(
221 background_threads_->
cq(), std::move(request),
223 std::unique_ptr<grpc::ClientContext> context,
224 gcsa::UpdateDatabaseDdlRequest
const& request) {
225 return stub->AsyncUpdateDatabaseDdl(cq, std::move(context), request);
228 std::unique_ptr<grpc::ClientContext> context,
229 google::longrunning::GetOperationRequest
const& request) {
230 return stub->AsyncGetOperation(cq, std::move(context), request);
233 std::unique_ptr<grpc::ClientContext> context,
234 google::longrunning::CancelOperationRequest
const& request) {
235 return stub->AsyncCancelOperation(cq, std::move(context), request);
237 &
google::
cloud::internal::ExtractLongRunningResultMetadata<
238 gcsa::UpdateDatabaseDdlMetadata>,
239 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
240 Idempotency::kNonIdempotent, polling_policy_prototype_->
clone(),
245 google::spanner::admin::database::v1::DropDatabaseRequest request;
248 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
249 Idempotency::kIdempotent,
250 [
this](grpc::ClientContext& context,
251 gcsa::DropDatabaseRequest
const& request) {
252 return stub_->DropDatabase(context, request);
258 gcsa::ListDatabasesRequest request;
260 request.clear_page_token();
265 std::shared_ptr<RetryPolicy
const>(retry_policy_prototype_->clone());
266 auto backoff = std::shared_ptr<BackoffPolicy
const>(
267 backoff_policy_prototype_->clone());
269 char const* function_name =
__func__;
270 return google::
cloud::internal::MakePaginationRange<ListDatabaseRange>(
272 [stub, retry, backoff,
273 function_name](gcsa::ListDatabasesRequest
const& r) {
275 retry->clone(), backoff->clone(), Idempotency::kIdempotent,
276 [stub](grpc::ClientContext& context,
277 gcsa::ListDatabasesRequest
const& request) {
278 return stub->ListDatabases(context, request);
282 [](gcsa::ListDatabasesResponse r) {
283 std::vector<gcsa::Database> result(r.databases().size());
284 auto& dbs = *r.mutable_databases();
285 std::move(dbs.begin(), dbs.end(), result.begin());
290 future<StatusOr<
google::spanner::admin::database::v1::Database>>
292 gcsa::RestoreDatabaseRequest request;
296 struct EncryptionVisitor {
297 explicit EncryptionVisitor(gcsa::RestoreDatabaseRequest& request)
298 : request_(request) {}
304 auto* config = request_.mutable_encryption_config();
305 config->set_encryption_type(
306 gcsa::RestoreDatabaseEncryptionConfig::GOOGLE_DEFAULT_ENCRYPTION);
309 auto* config = request_.mutable_encryption_config();
310 config->set_encryption_type(
311 gcsa::RestoreDatabaseEncryptionConfig::CUSTOMER_MANAGED_ENCRYPTION);
314 gcsa::RestoreDatabaseRequest& request_;
318 return google::
cloud::internal::AsyncLongRunningOperation<gcsa::Database>(
319 background_threads_->
cq(), std::move(request),
321 std::unique_ptr<grpc::ClientContext> context,
322 gcsa::RestoreDatabaseRequest
const& request) {
323 return stub->AsyncRestoreDatabase(cq, std::move(context), request);
326 std::unique_ptr<grpc::ClientContext> context,
327 google::longrunning::GetOperationRequest
const& request) {
328 return stub->AsyncGetOperation(cq, std::move(context), request);
331 std::unique_ptr<grpc::ClientContext> context,
332 google::longrunning::CancelOperationRequest
const& request) {
333 return stub->AsyncCancelOperation(cq, std::move(context), request);
335 &
google::
cloud::internal::ExtractLongRunningResultResponse<
337 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
338 Idempotency::kNonIdempotent, polling_policy_prototype_->
clone(),
342 StatusOr<
google::iam::v1::Policy> GetIamPolicy(
344 google::iam::v1::GetIamPolicyRequest request;
347 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
348 Idempotency::kIdempotent,
349 [
this](grpc::ClientContext& context,
350 google::iam::v1::GetIamPolicyRequest
const& request) {
351 return stub_->GetIamPolicy(context, request);
356 StatusOr<
google::iam::v1::Policy> SetIamPolicy(
358 google::iam::v1::SetIamPolicyRequest request;
360 *request.mutable_policy() = std::move(p
.policy);
361 auto const idempotency = request.policy().etag().empty()
362 ? Idempotency::kNonIdempotent
363 : Idempotency::kIdempotent;
365 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
367 [
this](grpc::ClientContext& context,
368 google::iam::v1::SetIamPolicyRequest
const& request) {
369 return stub_->SetIamPolicy(context, request);
374 StatusOr<
google::iam::v1::TestIamPermissionsResponse> TestIamPermissions(
376 google::iam::v1::TestIamPermissionsRequest request;
379 request.add_permissions(std::move(permission));
382 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
383 Idempotency::kIdempotent,
384 [
this](grpc::ClientContext& context,
385 google::iam::v1::TestIamPermissionsRequest
const& request) {
386 return stub_->TestIamPermissions(context, request);
392 gcsa::CreateBackupRequest request;
395 auto& backup = *request.mutable_backup();
398 *backup.mutable_expire_time() =
401 *backup.mutable_version_time() =
404 struct EncryptionVisitor {
405 explicit EncryptionVisitor(gcsa::CreateBackupRequest& request)
406 : request_(request) {}
412 auto* config = request_.mutable_encryption_config();
413 config->set_encryption_type(
414 gcsa::CreateBackupEncryptionConfig::GOOGLE_DEFAULT_ENCRYPTION);
417 auto* config = request_.mutable_encryption_config();
418 config->set_encryption_type(
419 gcsa::CreateBackupEncryptionConfig::CUSTOMER_MANAGED_ENCRYPTION);
422 gcsa::CreateBackupRequest& request_;
426 return google::
cloud::internal::AsyncLongRunningOperation<gcsa::Backup>(
427 background_threads_->
cq(), std::move(request),
429 std::unique_ptr<grpc::ClientContext> context,
430 gcsa::CreateBackupRequest
const& request) {
431 return stub->AsyncCreateBackup(cq, std::move(context), request);
434 std::unique_ptr<grpc::ClientContext> context,
435 google::longrunning::GetOperationRequest
const& request) {
436 return stub->AsyncGetOperation(cq, std::move(context), request);
439 std::unique_ptr<grpc::ClientContext> context,
440 google::longrunning::CancelOperationRequest
const& request) {
441 return stub->AsyncCancelOperation(cq, std::move(context), request);
443 &
google::
cloud::internal::ExtractLongRunningResultResponse<
445 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
446 Idempotency::kNonIdempotent, polling_policy_prototype_->
clone(),
450 StatusOr<
google::spanner::admin::database::v1::Backup> GetBackup(
452 gcsa::GetBackupRequest request;
455 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
456 Idempotency::kIdempotent,
457 [
this](grpc::ClientContext& context,
458 gcsa::GetBackupRequest
const& request) {
459 return stub_->GetBackup(context, request);
465 google::spanner::admin::database::v1::DeleteBackupRequest request;
468 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
469 Idempotency::kIdempotent,
470 [
this](grpc::ClientContext& context,
471 gcsa::DeleteBackupRequest
const& request) {
472 return stub_->DeleteBackup(context, request);
478 gcsa::ListBackupsRequest request;
480 request.set_filter(std::move(p
.filter));
485 std::shared_ptr<RetryPolicy
const>(retry_policy_prototype_->clone());
486 auto backoff = std::shared_ptr<BackoffPolicy
const>(
487 backoff_policy_prototype_->clone());
489 char const* function_name =
__func__;
490 return google::
cloud::internal::MakePaginationRange<ListBackupsRange>(
492 [stub, retry, backoff,
493 function_name](gcsa::ListBackupsRequest
const& r) {
495 retry->clone(), backoff->clone(), Idempotency::kIdempotent,
496 [stub](grpc::ClientContext& context,
497 gcsa::ListBackupsRequest
const& request) {
498 return stub->ListBackups(context, request);
502 [](gcsa::ListBackupsResponse r) {
503 std::vector<gcsa::Backup> result(r.backups().size());
504 auto& backups = *r.mutable_backups();
505 std::move(backups.begin(), backups.end(), result.begin());
510 StatusOr<
google::spanner::admin::database::v1::Backup> UpdateBackup(
513 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
514 Idempotency::kIdempotent,
515 [
this](grpc::ClientContext& context,
516 gcsa::UpdateBackupRequest
const& request) {
517 return stub_->UpdateBackup(context, request);
522 ListBackupOperationsRange ListBackupOperations(
524 gcsa::ListBackupOperationsRequest request;
526 request.set_filter(std::move(p
.filter));
531 std::shared_ptr<RetryPolicy
const>(retry_policy_prototype_->clone());
532 auto backoff = std::shared_ptr<BackoffPolicy
const>(
533 backoff_policy_prototype_->clone());
535 char const* function_name =
__func__;
536 return google::
cloud::internal::MakePaginationRange<
537 ListBackupOperationsRange>(
539 [stub, retry, backoff,
540 function_name](gcsa::ListBackupOperationsRequest
const& r) {
542 retry->clone(), backoff->clone(), Idempotency::kIdempotent,
543 [stub](grpc::ClientContext& context,
544 gcsa::ListBackupOperationsRequest
const& request) {
545 return stub->ListBackupOperations(context, request);
549 [](gcsa::ListBackupOperationsResponse r) {
550 std::vector<
google::longrunning::Operation> result(
551 r.operations().size());
552 auto& operations = *r.mutable_operations();
553 std::move(operations.begin(), operations.end(), result.begin());
558 ListDatabaseOperationsRange ListDatabaseOperations(
560 gcsa::ListDatabaseOperationsRequest request;
562 request.set_filter(std::move(p
.filter));
567 std::shared_ptr<RetryPolicy
const>(retry_policy_prototype_->clone());
568 auto backoff = std::shared_ptr<BackoffPolicy
const>(
569 backoff_policy_prototype_->clone());
571 char const* function_name =
__func__;
572 return google::
cloud::internal::MakePaginationRange<
573 ListDatabaseOperationsRange>(
575 [stub, retry, backoff,
576 function_name](gcsa::ListDatabaseOperationsRequest
const& r) {
578 retry->clone(), backoff->clone(), Idempotency::kIdempotent,
579 [stub](grpc::ClientContext& context,
580 gcsa::ListDatabaseOperationsRequest
const& request) {
581 return stub->ListDatabaseOperations(context, request);
585 [](gcsa::ListDatabaseOperationsResponse r) {
586 std::vector<
google::longrunning::Operation> result(
587 r.operations().size());
588 auto& operations = *r.mutable_operations();
589 std::move(operations.begin(), operations.end(), result.begin());
595 std::shared_ptr<spanner_internal::DatabaseAdminStub> stub_;
596 std::unique_ptr<RetryPolicy
const> retry_policy_prototype_;
597 std::unique_ptr<BackoffPolicy
const> backoff_policy_prototype_;
598 std::unique_ptr<PollingPolicy
const> polling_policy_prototype_;
613 internal::CheckExpectedOptions<CommonOptionList, GrpcOptionList,
614 SpannerPolicyOptionList>(opts,
__func__);
615 opts
= spanner_internal::DefaultAdminOptions(std::move(opts));
616 auto stub = spanner_internal::CreateDefaultDatabaseAdminStub(opts);
617 return std::make_shared<
spanner::DatabaseAdminConnectionImpl>(
618 std::move(stub), std::move(opts));
622 ConnectionOptions
const& options) {
627 ConnectionOptions
const& options, std::unique_ptr<RetryPolicy> retry_policy,
628 std::unique_ptr<BackoffPolicy> backoff_policy,
629 std::unique_ptr<PollingPolicy> polling_policy) {
630 auto opts = internal::MakeOptions(options);
640 namespace spanner_internal {
644 MakeDatabaseAdminConnectionForTesting(std::shared_ptr<DatabaseAdminStub> stub,
646 opts
= spanner_internal::DefaultAdminOptions(std::move(opts));
647 return std::make_shared<
spanner::DatabaseAdminConnectionImpl>(
648 std::move(stub), std::move(opts));