Google Cloud Spanner C++ Client
A C++ Client Library for Google Cloud Spanner
Public Member Functions | List of all members
google::cloud::spanner::v1::InstanceAdminClient Class Reference

Performs instance administration operations on Cloud Spanner. More...

#include <google/cloud/spanner/instance_admin_client.h>

Public Member Functions

 InstanceAdminClient (std::shared_ptr< InstanceAdminConnection > conn)
 
 InstanceAdminClient ()=delete
 No default construction. More...
 
StatusOr< google::spanner::admin::instance::v1::Instance > GetInstance (Instance const &in)
 Retrieve metadata information about a Cloud Spanner Instance. More...
 
future< StatusOr< google::spanner::admin::instance::v1::Instance > > CreateInstance (google::spanner::admin::instance::v1::CreateInstanceRequest const &)
 Creates a new Cloud Spanner instance in the given project. More...
 
future< StatusOr< google::spanner::admin::instance::v1::Instance > > UpdateInstance (google::spanner::admin::instance::v1::UpdateInstanceRequest const &)
 Updates a Cloud Spanner instance. More...
 
Status DeleteInstance (Instance const &in)
 Deletes an existing Cloud Spanner instance. More...
 
StatusOr< google::spanner::admin::instance::v1::InstanceConfig > GetInstanceConfig (std::string const &name)
 Retrieve information about a Cloud Spanner Instance Config. More...
 
ListInstanceConfigsRange ListInstanceConfigs (std::string project_id)
 Retrieve a list of instance configs for a given project. More...
 
ListInstancesRange ListInstances (std::string project_id, std::string filter)
 Retrieve a list of instances for a given project. More...
 
StatusOr< google::iam::v1::Policy > GetIamPolicy (Instance const &in)
 Get the IAM policy in effect for the given instance. More...
 
StatusOr< google::iam::v1::Policy > SetIamPolicy (Instance const &in, google::iam::v1::Policy policy)
 Set the IAM policy for the given instance. More...
 
StatusOr< google::iam::v1::Policy > SetIamPolicy (Instance const &in, IamUpdater const &updater)
 Updates the IAM policy for an instance using an optimistic concurrency control loop. More...
 
StatusOr< google::iam::v1::Policy > SetIamPolicy (Instance const &in, IamUpdater const &updater, std::unique_ptr< TransactionRerunPolicy > rerun_policy, std::unique_ptr< BackoffPolicy > backoff_policy)
 Updates the IAM policy for an instance using an optimistic concurrency control loop. More...
 
StatusOr< google::iam::v1::TestIamPermissionsResponse > TestIamPermissions (Instance const &in, std::vector< std::string > permissions)
 Get the subset of the permissions the caller has on the given instance. More...
 
 InstanceAdminClient (InstanceAdminClient const &)=default
 
InstanceAdminClientoperator= (InstanceAdminClient const &)=default
 
 InstanceAdminClient (InstanceAdminClient &&)=default
 
InstanceAdminClientoperator= (InstanceAdminClient &&)=default
 

Friends

bool operator== (InstanceAdminClient const &a, InstanceAdminClient const &b)
 
bool operator!= (InstanceAdminClient const &a, InstanceAdminClient const &b)
 

Detailed Description

Performs instance administration operations on Cloud Spanner.

Applications use this class to perform operations on Spanner Databases.

Performance

InstanceAdminClient objects are cheap to create, copy, and move. However, each InstanceAdminClient object must be created with a std::shared_ptr<InstanceAdminConnection>, which itself is relatively expensive to create. Therefore, connection instances should be shared when possible. See the MakeInstanceAdminConnection() method and the InstanceAdminConnection interface for more details.

Thread Safety

Instances of this class created via copy-construction or copy-assignment share the underlying pool of connections. Access to these copies via multiple threads is guaranteed to work. Two threads operating on the same instance of this class is not guaranteed to work.

Error Handling

This class uses StatusOr<T> to report errors. When an operation fails to perform its work the returned StatusOr<T> contains the error details. If the ok() member function in the StatusOr<T> returns true then it contains the expected result. Please consult the StatusOr<T> documentation for more details.

Definition at line 61 of file instance_admin_client.h.

Constructor & Destructor Documentation

◆ InstanceAdminClient() [1/4]

google::cloud::spanner::v1::InstanceAdminClient::InstanceAdminClient ( std::shared_ptr< InstanceAdminConnection conn)
inlineexplicit

Definition at line 63 of file instance_admin_client.h.

◆ InstanceAdminClient() [2/4]

google::cloud::spanner::v1::InstanceAdminClient::InstanceAdminClient ( )
delete

No default construction.

Use InstanceAdminClient(std::shared_ptr<InstanceAdminConnection>)

◆ InstanceAdminClient() [3/4]

google::cloud::spanner::v1::InstanceAdminClient::InstanceAdminClient ( InstanceAdminClient const &  )
default

◆ InstanceAdminClient() [4/4]

google::cloud::spanner::v1::InstanceAdminClient::InstanceAdminClient ( InstanceAdminClient &&  )
default

Member Function Documentation

◆ CreateInstance()

future< StatusOr< google::spanner::admin::instance::v1::Instance > > google::cloud::spanner::v1::InstanceAdminClient::CreateInstance ( google::spanner::admin::instance::v1::CreateInstanceRequest const &  request)

Creates a new Cloud Spanner instance in the given project.

Use CreateInstanceRequestBuilder to build the google::spanner::admin::instance::v1::CreateInstanceRequest object.

Note that the instance id must be between 2 and 64 characters long, it must start with a lowercase letter ([a-z]), it must end with a lowercase letter or a number ([a-z0-9]) and any characters between the beginning and ending characters must be lower case letters, numbers, or dashes (-), that is, they must belong to the [-a-z0-9] character set.

Example
void CreateInstance(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id,
std::string const& instance_id,
std::string const& display_name) {
using ::google::cloud::future;
using ::google::cloud::StatusOr;
google::cloud::spanner::Instance in(project_id, instance_id);
// Pick instance config that matches the regex, if there's no match, pick the
// first one.
std::string instance_config = [client, project_id]() mutable {
std::string ret{};
std::regex filter = std::regex(".*us-west.*");
for (auto const& instance_config : client.ListInstanceConfigs(project_id)) {
if (!instance_config) break;
if (ret.empty()) {
// fallback to the first element.
ret = instance_config->name();
}
if (std::regex_match(instance_config->name(), filter)) {
return instance_config->name();
}
}
return ret;
}();
if (instance_config.empty()) {
throw std::runtime_error("could not pick an instance config");
}
future<StatusOr<google::spanner::admin::instance::v1::Instance>> f =
client.CreateInstance(
google::cloud::spanner::CreateInstanceRequestBuilder(in,
instance_config)
.SetDisplayName(display_name)
.SetNodeCount(1)
.SetLabels({{"label-key", "label-value"}})
.Build());
StatusOr<google::spanner::admin::instance::v1::Instance> instance = f.get();
if (!instance) {
throw std::runtime_error(instance.status().message());
}
std::cout << "Created instance [" << in << "]\n";
}

Definition at line 28 of file instance_admin_client.cc.

◆ DeleteInstance()

Status google::cloud::spanner::v1::InstanceAdminClient::DeleteInstance ( Instance const &  in)

Deletes an existing Cloud Spanner instance.

Warning
Deleting an instance deletes all the databases in the instance. This is an unrecoverable operation.
Example
void DeleteInstance(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id,
std::string const& instance_id) {
google::cloud::spanner::Instance in(project_id, instance_id);
auto status = client.DeleteInstance(in);
if (!status.ok()) throw std::runtime_error(status.message());
std::cout << "Deleted instance [" << in << "]\n";
}

Definition at line 41 of file instance_admin_client.cc.

◆ GetIamPolicy()

StatusOr< google::iam::v1::Policy > google::cloud::spanner::v1::InstanceAdminClient::GetIamPolicy ( Instance const &  in)

Get the IAM policy in effect for the given instance.

This function retrieves the IAM policy configured in the given instance, that is, which roles are enabled in the instance, and what entities are members of each role.

Idempotency
This is a read-only operation and therefore it is always treated as idempotent.
Example
void InstanceGetIamPolicy(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id,
std::string const& instance_id) {
google::cloud::spanner::Instance in(project_id, instance_id);
auto actual = client.GetIamPolicy(in);
if (!actual) throw std::runtime_error(actual.status().message());
std::cout << "The IAM policy for instance " << instance_id << " is:\n"
<< actual->DebugString() << "\n";
}
See also
The Cloud Spanner documentation for a description of the roles and permissions supported by Cloud Spanner.
IAM Overview for an introduction to Identity and Access Management in Google Cloud Platform.

Definition at line 60 of file instance_admin_client.cc.

◆ GetInstance()

StatusOr< google::spanner::admin::instance::v1::Instance > google::cloud::spanner::v1::InstanceAdminClient::GetInstance ( Instance const &  in)

Retrieve metadata information about a Cloud Spanner Instance.

Idempotency
This is a read-only operation and therefore it is always treated as idempotent.
Example
void GetInstance(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id,
std::string const& instance_id) {
google::cloud::spanner::Instance in(project_id, instance_id);
auto instance = client.GetInstance(in);
if (!instance) throw std::runtime_error(instance.status().message());
std::cout << "The instance " << instance->name()
<< " exists and its metadata is:\n"
<< instance->DebugString() << "\n";
}

Definition at line 23 of file instance_admin_client.cc.

◆ GetInstanceConfig()

StatusOr< google::spanner::admin::instance::v1::InstanceConfig > google::cloud::spanner::v1::InstanceAdminClient::GetInstanceConfig ( std::string const &  name)

Retrieve information about a Cloud Spanner Instance Config.

Idempotency
This is a read-only operation and therefore it is always treated as idempotent.
Example
void GetInstanceConfig(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id,
std::string const& instance_config_name) {
auto instance_config = client.GetInstanceConfig(
"projects/" + project_id + "/instanceConfigs/" + instance_config_name);
if (!instance_config) {
throw std::runtime_error(instance_config.status().message());
}
std::cout << "The instanceConfig " << instance_config->name()
<< " exists and its metadata is:\n"
<< instance_config->DebugString() << "\n";
}

Definition at line 46 of file instance_admin_client.cc.

◆ ListInstanceConfigs()

ListInstanceConfigsRange google::cloud::spanner::v1::InstanceAdminClient::ListInstanceConfigs ( std::string  project_id)

Retrieve a list of instance configs for a given project.

Idempotency
This is a read-only operation and therefore it is always treated as idempotent.
Example
void ListInstanceConfigs(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id) {
int count = 0;
for (auto const& instance_config : client.ListInstanceConfigs(project_id)) {
if (!instance_config) {
throw std::runtime_error(instance_config.status().message());
}
++count;
std::cout << "Instance config [" << count << "]:\n"
<< instance_config->DebugString() << "\n";
}
if (count == 0) {
std::cout << "No instance configs found in project " << project_id;
}
}

Definition at line 50 of file instance_admin_client.cc.

◆ ListInstances()

ListInstancesRange google::cloud::spanner::v1::InstanceAdminClient::ListInstances ( std::string  project_id,
std::string  filter 
)

Retrieve a list of instances for a given project.

Idempotency
This is a read-only operation and therefore it is always treated as idempotent.
Example
void ListInstances(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id) {
int count = 0;
for (auto const& instance : client.ListInstances(project_id, "")) {
if (!instance) throw std::runtime_error(instance.status().message());
++count;
std::cout << "Instance [" << count << "]:\n"
<< instance->DebugString() << "\n";
}
if (count == 0) {
std::cout << "No instances found in project " << project_id;
}
}

Definition at line 55 of file instance_admin_client.cc.

◆ operator=() [1/2]

InstanceAdminClient& google::cloud::spanner::v1::InstanceAdminClient::operator= ( InstanceAdminClient const &  )
default

◆ operator=() [2/2]

InstanceAdminClient& google::cloud::spanner::v1::InstanceAdminClient::operator= ( InstanceAdminClient &&  )
default

◆ SetIamPolicy() [1/3]

StatusOr< google::iam::v1::Policy > google::cloud::spanner::v1::InstanceAdminClient::SetIamPolicy ( Instance const &  in,
google::iam::v1::Policy  policy 
)

Set the IAM policy for the given instance.

This function changes the IAM policy configured in the given instance to the value of policy.

Idempotency
This function is only idempotent if the etag field in policy is set. Therefore, the underlying RPCs are only retried if the field is set, and the function returns the first RPC error in any other case.
Example
void AddDatabaseReader(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id,
std::string const& instance_id,
std::string const& new_reader) {
google::cloud::spanner::Instance in(project_id, instance_id);
auto result = client.SetIamPolicy(
in,
[&new_reader](google::iam::v1::Policy current)
-> google::cloud::optional<google::iam::v1::Policy> {
// Find (or create) the binding for "roles/spanner.databaseReader".
auto& binding = [&current]() -> google::iam::v1::Binding& {
auto role_pos = std::find_if(
current.mutable_bindings()->begin(),
current.mutable_bindings()->end(),
[](google::iam::v1::Binding const& b) {
return b.role() == "roles/spanner.databaseReader" &&
!b.has_condition();
});
if (role_pos != current.mutable_bindings()->end()) {
return *role_pos;
}
auto& binding = *current.add_bindings();
binding.set_role("roles/spanner.databaseReader");
return binding;
}();
auto member_pos = std::find(binding.members().begin(),
binding.members().end(), new_reader);
if (member_pos != binding.members().end()) {
std::cout << "The entity " << new_reader
<< " is already a database reader:\n"
<< current.DebugString();
return {};
}
binding.add_members(new_reader);
return current;
});
if (!result) throw std::runtime_error(result.status().message());
std::cout << "Successfully added " << new_reader
<< " to the database reader role:\n"
<< result->DebugString() << "\n";
}
See also
The Cloud Spanner documentation for a description of the roles and permissions supported by Cloud Spanner.
IAM Overview for an introduction to Identity and Access Management in Google Cloud Platform.

Definition at line 65 of file instance_admin_client.cc.

◆ SetIamPolicy() [2/3]

StatusOr< google::iam::v1::Policy > google::cloud::spanner::v1::InstanceAdminClient::SetIamPolicy ( Instance const &  in,
IamUpdater const &  updater 
)

Updates the IAM policy for an instance using an optimistic concurrency control loop.

This function repeatedly reads the current IAM policy in in, and then calls the updater with the this policy. The updater returns an empty optional if no changes are required, or it returns the new desired value for the IAM policy. This function then updates the policy.

Updating an IAM policy can fail with retryable errors or can be aborted because there were simultaneous changes the to IAM policy. In these cases this function reruns the loop until it succeeds.

The function returns the final IAM policy, or an error if the rerun policy for the underlying connection has expired.

Idempotency
This function always sets the etag field on the policy, so the underlying RPCs are retried automatically.
Parameters
inthe identifier for the instance where you want to change the IAM policy.
updatera callback to modify the policy. Return an unset optional to indicate that no changes to the policy are needed.

Definition at line 70 of file instance_admin_client.cc.

◆ SetIamPolicy() [3/3]

StatusOr< google::iam::v1::Policy > google::cloud::spanner::v1::InstanceAdminClient::SetIamPolicy ( Instance const &  in,
IamUpdater const &  updater,
std::unique_ptr< TransactionRerunPolicy rerun_policy,
std::unique_ptr< BackoffPolicy backoff_policy 
)

Updates the IAM policy for an instance using an optimistic concurrency control loop.

This function repeatedly reads the current IAM policy in in, and then calls the updater with the this policy. The updater returns an empty optional if no changes are required, or it returns the new desired value for the IAM policy. This function then updates the policy.

Updating an IAM policy can fail with retryable errors or can be aborted because there were simultaneous changes the to IAM policy. In these cases this function reruns the loop until it succeeds.

The function returns the final IAM policy, or an error if the rerun policy for the underlying connection has expired.

Idempotency
This function always sets the etag field on the policy, so the underlying RPCs are retried automatically.
Parameters
inthe identifier for the instance where you want to change the IAM policy.
updatera callback to modify the policy. Return an unset optional to indicate that no changes to the policy are needed.
rerun_policycontrols for how long (or how many times) the updater will be rerun after the IAM policy update aborts.
backoff_policycontrols how long SetIamPolicy waits between reruns.

Definition at line 88 of file instance_admin_client.cc.

◆ TestIamPermissions()

StatusOr< google::iam::v1::TestIamPermissionsResponse > google::cloud::spanner::v1::InstanceAdminClient::TestIamPermissions ( Instance const &  in,
std::vector< std::string >  permissions 
)

Get the subset of the permissions the caller has on the given instance.

This function compares the given list of permissions against those permissions granted to the caller, and returns the subset of the list that the caller actually holds.

Note
Permission wildcards, such as spanner.* are not allowed.
Example
void InstanceTestIamPermissions(
google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id, std::string const& instance_id) {
google::cloud::spanner::Instance in(project_id, instance_id);
auto actual = client.TestIamPermissions(in, {"spanner.databases.list"});
if (!actual) throw std::runtime_error(actual.status().message());
char const* msg = actual->permissions().empty() ? "does not" : "does";
std::cout
<< "The caller " << msg
<< " have permission to list databases on the Cloud Spanner instance "
<< in.instance_id() << "\n";
}
See also
The Cloud Spanner documentation for a description of the roles and permissions supported by Cloud Spanner.
IAM Overview for an introduction to Identity and Access Management in Google Cloud Platform.

Definition at line 119 of file instance_admin_client.cc.

◆ UpdateInstance()

future< StatusOr< google::spanner::admin::instance::v1::Instance > > google::cloud::spanner::v1::InstanceAdminClient::UpdateInstance ( google::spanner::admin::instance::v1::UpdateInstanceRequest const &  request)

Updates a Cloud Spanner instance.

Use google::cloud::spanner::UpdateInstanceRequestBuilder to build the google::spanner::admin::instance::v1::UpdateInstanceRequest object.

Idempotency
This operation is idempotent as its result does not depend on the previous state of the instance. Note that, as is the case with all operations, it is subject to race conditions if multiple tasks are attempting to change the same fields in the same instance.
Example
void UpdateInstance(google::cloud::spanner::InstanceAdminClient client,
std::string const& project_id,
std::string const& instance_id,
std::string const& new_display_name) {
google::cloud::spanner::Instance in(project_id, instance_id);
auto f = client.UpdateInstance(
google::cloud::spanner::UpdateInstanceRequestBuilder(in)
.SetDisplayName(new_display_name)
.Build());
auto instance = f.get();
if (!instance) throw std::runtime_error(instance.status().message());
std::cout << "Updated instance [" << in << "]\n";
}

Definition at line 35 of file instance_admin_client.cc.

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( InstanceAdminClient const &  a,
InstanceAdminClient const &  b 
)
friend

Definition at line 84 of file instance_admin_client.h.

◆ operator==

bool operator== ( InstanceAdminClient const &  a,
InstanceAdminClient const &  b 
)
friend

Definition at line 80 of file instance_admin_client.h.