Using C#, I am trying to list the available databases on a Google Spanner Instance. There seems little to no decent documentation that actually explains how to use the Google.Spanner
Nuget libs.
From what I can tell there are a few libraries to use. The first is the main SpannerClient which gives you a client to execute sql queries on a given database. I have this working fine. However in order to present the list of available databases to my users, I'd like to get the list of available databases. I have found the package Google.Cloud.Spanner.Admin.Instance.V1
; but almost no docs to explain how to use. So I've been coding in the dark, so far I've written the following test code:
var credentials = GoogleCredential.FromJson(_apiKey);
var client = new InstanceAdminClientBuilder();
client.Credential = credentials;
var clientInst = client.Build();
ListInstancesRequest listInstanceRequest = new ListInstancesRequest();
listInstanceRequest.Parent = _projectId;
var res = clientInst.ListInstances(listInstanceRequest);
I get an :
"InvalidArgument - Invalid ListInstances request."
which suggests I've not done it right but I can't find any decent docs.
So any help welcome!!
CodePudding user response:
Listing instances isn't the same as listing databases. If you want to list databases, you should use the Google.Cloud.Spanner.Admin.Database.V1
package, with DatabaseAdminClient
.
Next, the Parent
property in any list operation should be the full resource name, e.g. projects/my_project_id
rather than just the project ID. Normally the best way of approaching this is to use the generated resource name types.
So for example:
var client = DatabaseAdminClient.Create();
var instanceName = new InstanceName(projectId, instanceId);
var databases = client.ListDatabases(instanceName);
foreach (var database in databases)
{
Console.WriteLine(database.Name);
}
If you want to list the instances for the project, you'd use:
var client = InstanceAdminClient.Create();
var projectName = new ProjectName(projectId);
var instances = client.ListInstances(projectName);
foreach (var instance in instances)
{
Console.WriteLine(instance.Name);
}