Google Cloud Spanner C++ Client  1.34.0
A C++ Client Library for Google Cloud Spanner
instance.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_INSTANCE_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_INSTANCE_H
17 
18 #include "google/cloud/spanner/version.h"
19 #include "google/cloud/project.h"
20 #include "google/cloud/status_or.h"
21 #include <iosfwd>
22 #include <string>
23 
24 namespace google {
25 namespace cloud {
26 namespace spanner {
28 
29 /**
30  * This class identifies a Cloud Spanner Instance.
31  *
32  * A Cloud Spanner instance is identified by its `project_id` and `instance_id`.
33  *
34  * @note This class makes no effort to validate the components of the
35  * database name. It is the application's responsibility to provide valid
36  * project, and instance ids. Passing invalid values will not be checked
37  * until the instance name is used in a RPC to spanner.
38  *
39  * For more info about the `instance_id` format, see
40  * https://cloud.google.com/spanner/docs/reference/rpc/google.spanner.admin.instance.v1#createinstancerequest
41  */
42 class Instance {
43  public:
44  /**
45  * Constructs an Instance object identified by the given @p project and
46  * @p instance_id.
47  */
48  Instance(Project project, std::string instance_id);
49 
50  /**
51  * Constructs an Instance object identified by the given IDs.
52  *
53  * This is equivalent to first constructing a `Project` from the given
54  * @p project_id and then calling the `Instance(Project, std::string)`
55  * constructor.
56  */
57  Instance(std::string project_id, std::string instance_id);
58 
59  /// @name Copy and move
60  //@{
61  Instance(Instance const&) = default;
62  Instance& operator=(Instance const&) = default;
63  Instance(Instance&&) = default;
64  Instance& operator=(Instance&&) = default;
65  //@}
66 
67  /// Returns the `Project` containing this instance.
68  Project const& project() const { return project_; }
69  std::string const& project_id() const { return project_.project_id(); }
70 
71  /// Returns the Instance ID.
72  std::string const& instance_id() const { return instance_id_; }
73 
74  /**
75  * Returns the fully qualified instance name as a string of the form:
76  * "projects/<project-id>/instances/<instance-id>"
77  */
78  std::string FullName() const;
79 
80  /// @name Equality operators
81  //@{
82  friend bool operator==(Instance const& a, Instance const& b);
83  friend bool operator!=(Instance const& a, Instance const& b);
84  //@}
85 
86  /// Output the `FullName()` format.
87  friend std::ostream& operator<<(std::ostream&, Instance const&);
88 
89  private:
90  Project project_;
91  std::string instance_id_;
92 };
93 
94 /**
95  * Constructs an `Instance` from the given @p full_name.
96  * Returns a non-OK Status if `full_name` is improperly formed.
97  */
98 StatusOr<Instance> MakeInstance(std::string const& full_name);
99 
101 } // namespace spanner
102 } // namespace cloud
103 } // namespace google
104 
105 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_INSTANCE_H