16 #include "google/cloud/internal/disable_deprecation_warnings.inc"
17 #include "google/cloud/spanner/instance_admin_connection.h"
18 #include "google/cloud/spanner/instance.h"
19 #include "google/cloud/spanner/internal/defaults.h"
20 #include "google/cloud/spanner/options.h"
21 #include "google/cloud/internal/async_long_running_operation.h"
22 #include "google/cloud/internal/retry_loop.h"
23 #include <grpcpp/grpcpp.h>
31 namespace gcsa = ::
google::spanner::admin::instance::v1;
32 namespace giam = ::
google::iam::v1;
39 InstanceAdminConnectionImpl(
40 std::shared_ptr<spanner_internal::InstanceAdminStub> stub,
42 : stub_(std::move(stub)),
44 backoff_policy_prototype_(
46 polling_policy_prototype_(
48 background_threads_(internal::MakeBackgroundThreadsFactory(opts)()) {}
50 ~InstanceAdminConnectionImpl()
override =
default;
53 gcsa::GetInstanceRequest request;
56 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
57 Idempotency::kIdempotent,
58 [
this](grpc::ClientContext& context,
59 gcsa::GetInstanceRequest
const& request) {
60 return stub_->GetInstance(context, request);
65 future<StatusOr<gcsa::Instance>> CreateInstance(
68 return google::
cloud::internal::AsyncLongRunningOperation<gcsa::Instance>(
71 std::unique_ptr<grpc::ClientContext> context,
72 gcsa::CreateInstanceRequest
const& request) {
73 return stub->AsyncCreateInstance(cq, std::move(context), request);
76 std::unique_ptr<grpc::ClientContext> context,
77 google::longrunning::GetOperationRequest
const& request) {
78 return stub->AsyncGetOperation(cq, std::move(context), request);
81 std::unique_ptr<grpc::ClientContext> context,
82 google::longrunning::CancelOperationRequest
const& request) {
83 return stub->AsyncCancelOperation(cq, std::move(context), request);
85 &
google::
cloud::internal::ExtractLongRunningResultResponse<
87 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
88 Idempotency::kNonIdempotent, polling_policy_prototype_->
clone(),
92 future<StatusOr<gcsa::Instance>> UpdateInstance(
95 return google::
cloud::internal::AsyncLongRunningOperation<gcsa::Instance>(
98 std::unique_ptr<grpc::ClientContext> context,
99 gcsa::UpdateInstanceRequest
const& request) {
100 return stub->AsyncUpdateInstance(cq, std::move(context), request);
103 std::unique_ptr<grpc::ClientContext> context,
104 google::longrunning::GetOperationRequest
const& request) {
105 return stub->AsyncGetOperation(cq, std::move(context), request);
108 std::unique_ptr<grpc::ClientContext> context,
109 google::longrunning::CancelOperationRequest
const& request) {
110 return stub->AsyncCancelOperation(cq, std::move(context), request);
112 &
google::
cloud::internal::ExtractLongRunningResultResponse<
114 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
115 Idempotency::kIdempotent, polling_policy_prototype_->
clone(),
__func__);
119 gcsa::DeleteInstanceRequest request;
122 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
123 Idempotency::kIdempotent,
124 [
this](grpc::ClientContext& context,
125 gcsa::DeleteInstanceRequest
const& request) {
126 return stub_->DeleteInstance(context, request);
131 StatusOr<gcsa::InstanceConfig> GetInstanceConfig(
133 gcsa::GetInstanceConfigRequest request;
136 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
137 Idempotency::kIdempotent,
138 [
this](grpc::ClientContext& context,
139 gcsa::GetInstanceConfigRequest
const& request) {
140 return stub_->GetInstanceConfig(context, request);
145 ListInstanceConfigsRange ListInstanceConfigs(
147 gcsa::ListInstanceConfigsRequest request;
148 request.set_parent(
"projects/" + params
.project_id);
149 request.clear_page_token();
153 auto retry = std::shared_ptr<RetryPolicy>(retry_policy_prototype_->clone());
155 std::shared_ptr<BackoffPolicy>(backoff_policy_prototype_->clone());
157 char const* function_name =
__func__;
158 return google::
cloud::internal::MakePaginationRange<
159 ListInstanceConfigsRange>(
161 [stub, retry, backoff,
162 function_name](gcsa::ListInstanceConfigsRequest
const& r) {
164 retry->clone(), backoff->clone(), Idempotency::kIdempotent,
165 [stub](grpc::ClientContext& context,
166 gcsa::ListInstanceConfigsRequest
const& request) {
167 return stub->ListInstanceConfigs(context, request);
171 [](gcsa::ListInstanceConfigsResponse r) {
172 std::vector<gcsa::InstanceConfig> result(r.instance_configs().size());
173 auto& configs = *r.mutable_instance_configs();
174 std::move(configs.begin(), configs.end(), result.begin());
180 gcsa::ListInstancesRequest request;
181 request.set_parent(
"projects/" + params
.project_id);
182 request.set_filter(std::move(params
.filter));
183 request.clear_page_token();
187 auto retry = std::shared_ptr<RetryPolicy>(retry_policy_prototype_->clone());
189 std::shared_ptr<BackoffPolicy>(backoff_policy_prototype_->clone());
191 char const* function_name =
__func__;
192 return google::
cloud::internal::MakePaginationRange<ListInstancesRange>(
194 [stub, retry, backoff,
195 function_name](gcsa::ListInstancesRequest
const& r) {
197 retry->clone(), backoff->clone(), Idempotency::kIdempotent,
198 [stub](grpc::ClientContext& context,
199 gcsa::ListInstancesRequest
const& request) {
200 return stub->ListInstances(context, request);
204 [](gcsa::ListInstancesResponse r) {
205 std::vector<gcsa::Instance> result(r.instances().size());
206 auto& instances = *r.mutable_instances();
207 std::move(instances.begin(), instances.end(), result.begin());
213 google::iam::v1::GetIamPolicyRequest request;
216 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
217 Idempotency::kIdempotent,
218 [
this](grpc::ClientContext& context,
219 giam::GetIamPolicyRequest
const& request) {
220 return stub_->GetIamPolicy(context, request);
226 google::iam::v1::SetIamPolicyRequest request;
228 *request.mutable_policy() = std::move(p
.policy);
229 auto const idempotency = request.policy().etag().empty()
230 ? Idempotency::kNonIdempotent
231 : Idempotency::kIdempotent;
233 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
235 [
this](grpc::ClientContext& context,
236 giam::SetIamPolicyRequest
const& request) {
237 return stub_->SetIamPolicy(context, request);
242 StatusOr<
google::iam::v1::TestIamPermissionsResponse> TestIamPermissions(
244 google::iam::v1::TestIamPermissionsRequest request;
247 request.add_permissions(std::move(permission));
250 retry_policy_prototype_->clone(), backoff_policy_prototype_->clone(),
251 Idempotency::kIdempotent,
252 [
this](grpc::ClientContext& context,
253 giam::TestIamPermissionsRequest
const& request) {
254 return stub_->TestIamPermissions(context, request);
260 std::shared_ptr<spanner_internal::InstanceAdminStub> stub_;
261 std::unique_ptr<RetryPolicy
const> retry_policy_prototype_;
262 std::unique_ptr<BackoffPolicy
const> backoff_policy_prototype_;
263 std::unique_ptr<PollingPolicy
const> polling_policy_prototype_;
278 internal::CheckExpectedOptions<CommonOptionList, GrpcOptionList,
279 SpannerPolicyOptionList>(opts,
__func__);
280 opts
= spanner_internal::DefaultAdminOptions(std::move(opts));
281 auto stub = spanner_internal::CreateDefaultInstanceAdminStub(opts);
282 return std::make_shared<
spanner::InstanceAdminConnectionImpl>(
283 std::move(stub), std::move(opts));
287 ConnectionOptions
const& options) {
292 ConnectionOptions
const& options, std::unique_ptr<RetryPolicy> retry_policy,
293 std::unique_ptr<BackoffPolicy> backoff_policy,
294 std::unique_ptr<PollingPolicy> polling_policy) {
295 auto opts = internal::MakeOptions(options);
305 namespace spanner_internal {
309 MakeInstanceAdminConnectionForTesting(std::shared_ptr<InstanceAdminStub> stub,
311 opts
= spanner_internal::DefaultAdminOptions(std::move(opts));
312 return std::make_shared<
spanner::InstanceAdminConnectionImpl>(
313 std::move(stub), std::move(opts));