Google Cloud Spanner C++ Client  1.32.0
A C++ Client Library for Google Cloud Spanner
create_instance_request_builder.h
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 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CREATE_INSTANCE_REQUEST_BUILDER_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CREATE_INSTANCE_REQUEST_BUILDER_H
17 
18 #include "google/cloud/spanner/instance.h"
19 #include "google/cloud/spanner/version.h"
20 #include <google/spanner/admin/instance/v1/spanner_instance_admin.pb.h>
21 #include <map>
22 #include <string>
23 
24 namespace google {
25 namespace cloud {
26 namespace spanner {
27 inline namespace SPANNER_CLIENT_NS {
28 
29 /**
30  * CreateInstanceRequestBuilder is a builder class for
31  * `google::spanner::admin::instance::v1::CreateInstanceRequest`
32  *
33  * This is useful when calling the `InstanceAdminClient::CreateInstance()`
34  * function.
35  *
36  * @par Example
37  * @snippet samples.cc create-instance
38  */
40  public:
41  // Copy and move.
45  default;
47  default;
48 
49  /**
50  * Constructor requires Instance and Cloud Spanner instance config name.
51  * The display_name is set to a default value of in.instance_id().
52  */
53  CreateInstanceRequestBuilder(Instance const& in, std::string config) {
54  request_.set_parent(in.project().FullName());
55  request_.set_instance_id(in.instance_id());
56  request_.mutable_instance()->set_name(in.FullName());
57  request_.mutable_instance()->set_display_name(in.instance_id());
58  request_.mutable_instance()->set_config(std::move(config));
59  }
60 
61  CreateInstanceRequestBuilder& SetDisplayName(std::string display_name) & {
62  request_.mutable_instance()->set_display_name(std::move(display_name));
63  return *this;
64  }
65 
66  CreateInstanceRequestBuilder&& SetDisplayName(std::string display_name) && {
67  request_.mutable_instance()->set_display_name(std::move(display_name));
68  return std::move(*this);
69  }
70 
72  request_.mutable_instance()->set_node_count(node_count);
73  return *this;
74  }
75 
77  request_.mutable_instance()->set_node_count(node_count);
78  return std::move(*this);
79  }
80 
82  request_.mutable_instance()->set_processing_units(processing_units);
83  return *this;
84  }
85 
86  CreateInstanceRequestBuilder&& SetProcessingUnits(int processing_units) && {
87  request_.mutable_instance()->set_processing_units(processing_units);
88  return std::move(*this);
89  }
90 
92  std::map<std::string, std::string> const& labels) & {
93  for (auto const& pair : labels) {
94  request_.mutable_instance()->mutable_labels()->insert(
95  {pair.first, pair.second});
96  }
97  return *this;
98  }
99 
101  std::map<std::string, std::string> const& labels) && {
102  for (auto const& pair : labels) {
103  request_.mutable_instance()->mutable_labels()->insert(
104  {pair.first, pair.second});
105  }
106  return std::move(*this);
107  }
108 
109  google::spanner::admin::instance::v1::CreateInstanceRequest& Build() & {
110  // Preserve original behavior of defaulting node_count to 1.
111  if (request_.instance().processing_units() == 0) {
112  if (request_.instance().node_count() == 0) {
113  request_.mutable_instance()->set_node_count(1);
114  }
115  }
116  return request_;
117  }
118  google::spanner::admin::instance::v1::CreateInstanceRequest&& Build() && {
119  // Preserve original behavior of defaulting node_count to 1.
120  if (request_.instance().processing_units() == 0) {
121  if (request_.instance().node_count() == 0) {
122  request_.mutable_instance()->set_node_count(1);
123  }
124  }
125  return std::move(request_);
126  }
127 
128  private:
129  google::spanner::admin::instance::v1::CreateInstanceRequest request_;
130 };
131 
132 } // namespace SPANNER_CLIENT_NS
133 } // namespace spanner
134 } // namespace cloud
135 } // namespace google
136 
137 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_CREATE_INSTANCE_REQUEST_BUILDER_H