use of com.google.cloud.spanner.InstanceConfigId in project java-spanner by googleapis.
the class CreateDatabaseWithDefaultLeaderSampleIT method testCreateDatabaseWithDefaultLeader.
@Test
public void testCreateDatabaseWithDefaultLeader() throws Exception {
final String databaseId = idGenerator.generateDatabaseId();
// Finds possible default leader
final InstanceConfigId instanceConfigId = instanceAdminClient.getInstance(multiRegionalInstanceId).getInstanceConfigId();
final InstanceConfig config = instanceAdminClient.getInstanceConfig(instanceConfigId.getInstanceConfig());
assertTrue("Expected instance config " + instanceConfigId + " to have at least one leader option", config.getLeaderOptions().size() > 0);
final String defaultLeader = config.getLeaderOptions().get(0);
// Runs sample
final String out = SampleRunner.runSample(() -> CreateDatabaseWithDefaultLeaderSample.createDatabaseWithDefaultLeader(projectId, multiRegionalInstanceId, databaseId, defaultLeader));
assertTrue("Expected created database to have default leader " + defaultLeader + "." + " Output received was " + out, out.contains("Default leader: " + defaultLeader));
}
use of com.google.cloud.spanner.InstanceConfigId in project java-spanner by googleapis.
the class QueryInformationSchemaDatabaseOptionsSampleIT method testQueryInformationSchemaDatabaseOptions.
@Test
public void testQueryInformationSchemaDatabaseOptions() throws Exception {
// Finds a possible new leader option
final InstanceConfigId instanceConfigId = instanceAdminClient.getInstance(multiRegionalInstanceId).getInstanceConfigId();
final InstanceConfig config = instanceAdminClient.getInstanceConfig(instanceConfigId.getInstanceConfig());
assertTrue("Expected instance config " + instanceConfigId + " to have at least one leader option", config.getLeaderOptions().size() > 0);
final String defaultLeader = config.getLeaderOptions().get(0);
// Creates database
final String databaseId = idGenerator.generateDatabaseId();
databaseAdminClient.createDatabase(multiRegionalInstanceId, databaseId, Arrays.asList("CREATE TABLE Singers (Id INT64 NOT NULL) PRIMARY KEY (Id)", "ALTER DATABASE `" + databaseId + "` SET OPTIONS ( default_leader = '" + defaultLeader + "')")).get(5, TimeUnit.MINUTES);
// Runs sample
final String out = SampleRunner.runSample(() -> QueryInformationSchemaDatabaseOptionsSample.queryInformationSchemaDatabaseOptions(projectId, multiRegionalInstanceId, databaseId));
assertTrue("Expected to have retrieved default_leader for " + databaseId + " as " + defaultLeader + "." + " Output received was " + out, out.contains("The default_leader for projects/" + projectId + "/instances/" + multiRegionalInstanceId + "/databases/" + databaseId + " is " + defaultLeader));
}
use of com.google.cloud.spanner.InstanceConfigId in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerClient method initializeInstance.
private void initializeInstance(InstanceId instanceId) {
InstanceConfig instanceConfig = Iterators.get(spClient.getInstanceAdminClient().listInstanceConfigs().iterateAll().iterator(), // us-east1
32, null);
checkState(instanceConfig != null, "No instance configs found");
InstanceConfigId configId = instanceConfig.getId();
logger.log(Level.INFO, "Creating instance using config {0}", configId);
InstanceInfo instance = InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("Test instance").setInstanceConfigId(configId).build();
OperationFuture<Instance, CreateInstanceMetadata> op = spClient.getInstanceAdminClient().createInstance(instance);
Instance createdInstance;
try {
createdInstance = op.get();
} catch (Exception e) {
boolean cancelled = false;
try {
// Try to cancel the createInstance operation.
spClient.getInstanceAdminClient().cancelOperation(op.getName());
com.google.longrunning.Operation createOperation = spClient.getInstanceAdminClient().getOperation(op.getName());
cancelled = createOperation.hasError() && createOperation.getError().getCode() == Status.CANCELLED.getCode().value();
if (cancelled) {
logger.info("Cancelled the createInstance operation because the operation failed");
} else {
logger.info("Tried to cancel the createInstance operation because the operation failed, but the" + " operation could not be cancelled. Current status: " + createOperation.getError().getCode());
}
} catch (Throwable t) {
logger.log(Level.WARNING, "Failed to cancel the createInstance operation", t);
}
if (!cancelled) {
try {
spClient.getInstanceAdminClient().deleteInstance(instanceId.getInstance());
logger.info("Deleted the test instance because the createInstance operation failed and" + " cancelling the operation did not succeed");
} catch (Throwable t) {
logger.log(Level.WARNING, "Failed to delete the test instance", t);
}
}
throw SpannerExceptionFactory.newSpannerException(e);
}
logger.log(Level.INFO, "Created test instance: {0}", createdInstance.getId());
}
use of com.google.cloud.spanner.InstanceConfigId in project grpc-gcp-java by GoogleCloudPlatform.
the class SpannerIntegrationTest method initializeInstance.
private static void initializeInstance(InstanceAdminClient instanceAdminClient, InstanceId instanceId) {
InstanceConfig instanceConfig = Iterators.get(instanceAdminClient.listInstanceConfigs().iterateAll().iterator(), 0, null);
checkState(instanceConfig != null, "No instance configs found");
InstanceConfigId configId = instanceConfig.getId();
InstanceInfo instance = InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("grpc-gcp test instance").setInstanceConfigId(configId).build();
OperationFuture<Instance, CreateInstanceMetadata> op = instanceAdminClient.createInstance(instance);
try {
op.get();
} catch (Exception e) {
throw SpannerExceptionFactory.newSpannerException(e);
}
}
use of com.google.cloud.spanner.InstanceConfigId in project testcontainers-java by testcontainers.
the class SpannerEmulatorContainerTest method createInstance.
// }
// createInstance {
private InstanceId createInstance(Spanner spanner) throws InterruptedException, ExecutionException {
InstanceConfigId instanceConfig = InstanceConfigId.of(PROJECT_NAME, "emulator-config");
InstanceId instanceId = InstanceId.of(PROJECT_NAME, INSTANCE_NAME);
InstanceAdminClient insAdminClient = spanner.getInstanceAdminClient();
Instance instance = insAdminClient.createInstance(InstanceInfo.newBuilder(instanceId).setNodeCount(1).setDisplayName("Test instance").setInstanceConfigId(instanceConfig).build()).get();
return instanceId;
}
Aggregations