use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class EmulatorUtilTest method testCreateInstanceAndDatabase_propagatesOtherErrorsOnInstanceCreation.
@Test
public void testCreateInstanceAndDatabase_propagatesOtherErrorsOnInstanceCreation() throws InterruptedException, ExecutionException {
Spanner spanner = mock(Spanner.class);
SpannerOptions options = mock(SpannerOptions.class);
when(spanner.getOptions()).thenReturn(options);
when(options.getCredentials()).thenReturn(NoCredentials.getInstance());
InstanceAdminClient instanceClient = mock(InstanceAdminClient.class);
@SuppressWarnings("unchecked") OperationFuture<Instance, CreateInstanceMetadata> instanceOperationFuture = mock(OperationFuture.class);
when(spanner.getInstanceAdminClient()).thenReturn(instanceClient);
when(instanceClient.createInstance(any(InstanceInfo.class))).thenReturn(instanceOperationFuture);
when(instanceOperationFuture.get()).thenThrow(new ExecutionException(SpannerExceptionFactory.newSpannerException(ErrorCode.INVALID_ARGUMENT, "Invalid instance options")));
try {
EmulatorUtil.maybeCreateInstanceAndDatabase(spanner, DatabaseId.of("test-project", "test-instance", "test-database"));
fail("missing expected exception");
} catch (SpannerException e) {
assertEquals(ErrorCode.INVALID_ARGUMENT, e.getErrorCode());
}
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class ITInstanceAdminTest method updateInstance.
@Test
public void updateInstance() throws Exception {
assumeFalse("The emulator does not support updating instances", isUsingEmulator());
Instance instance = instanceClient.getInstance(env.getTestHelper().getInstanceId().getInstance());
String rand = new Random().nextInt() + "";
String newDisplayName = "instance test" + rand;
InstanceInfo toUpdate = InstanceInfo.newBuilder(env.getTestHelper().getInstanceId()).setDisplayName(newDisplayName).setNodeCount(instance.getNodeCount() + 1).build();
// Only update display name
OperationFuture<Instance, UpdateInstanceMetadata> op = instanceClient.updateInstance(toUpdate, InstanceInfo.InstanceField.DISPLAY_NAME);
Instance newInstance = op.get();
assertThat(newInstance.getNodeCount()).isEqualTo(instance.getNodeCount());
assertThat(newInstance.getDisplayName()).isEqualTo(newDisplayName);
Instance newInstanceFromGet = instanceClient.getInstance(env.getTestHelper().getInstanceId().getInstance());
assertThat(newInstanceFromGet).isEqualTo(newInstance);
toUpdate = InstanceInfo.newBuilder(instance.getId()).setDisplayName(instance.getDisplayName()).build();
instanceClient.updateInstance(toUpdate, InstanceInfo.InstanceField.DISPLAY_NAME).get();
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class CreateInstanceWithProcessingUnitsExample method createInstance.
static void createInstance(String projectId, String instanceId) {
Spanner spanner = SpannerOptions.newBuilder().setProjectId(projectId).build().getService();
InstanceAdminClient instanceAdminClient = spanner.getInstanceAdminClient();
// Set Instance configuration.
String configId = "regional-us-central1";
// This will create an instance with the processing power of 0.2 nodes.
int processingUnits = 500;
String displayName = "Descriptive name";
try {
// Creates a new instance
System.out.printf("Creating instance %s.%n", instanceId);
OperationFuture<Instance, CreateInstanceMetadata> operation = instanceAdminClient.createInstance(InstanceInfo.newBuilder(InstanceId.of(projectId, instanceId)).setInstanceConfigId(InstanceConfigId.of(projectId, configId)).setProcessingUnits(processingUnits).setDisplayName(displayName).build());
// Wait for the createInstance operation to finish.
System.out.printf("Waiting for operation on %s to complete...%n", instanceId);
Instance createdInstance = operation.get();
System.out.printf("Created instance %s.%n", createdInstance.getId().getInstance());
Instance instance = instanceAdminClient.getInstance(instanceId);
System.out.printf("Instance %s has %d processing units.%n", instance.getId().getInstance(), instance.getProcessingUnits());
} catch (Exception e) {
System.out.printf("Error: %s.%n", e.getMessage());
}
spanner.close();
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class InstanceAdminClientImpl method updateInstance.
@Override
public OperationFuture<Instance, UpdateInstanceMetadata> updateInstance(InstanceInfo instance, InstanceInfo.InstanceField... fieldsToUpdate) {
FieldMask fieldMask = fieldsToUpdate.length == 0 ? InstanceInfo.InstanceField.toFieldMask(InstanceInfo.InstanceField.defaultFieldsToUpdate(instance)) : InstanceInfo.InstanceField.toFieldMask(fieldsToUpdate);
OperationFuture<com.google.spanner.admin.instance.v1.Instance, UpdateInstanceMetadata> rawOperationFuture = rpc.updateInstance(instance.toProto(), fieldMask);
return new OperationFutureImpl<>(rawOperationFuture.getPollingFuture(), rawOperationFuture.getInitialFuture(), snapshot -> Instance.fromProto(ProtoOperationTransformers.ResponseTransformer.create(com.google.spanner.admin.instance.v1.Instance.class).apply(snapshot), InstanceAdminClientImpl.this, dbClient), ProtoOperationTransformers.MetadataTransformer.create(UpdateInstanceMetadata.class), e -> {
throw SpannerExceptionFactory.newSpannerException(e);
});
}
use of com.google.spanner.admin.instance.v1.Instance in project java-spanner by googleapis.
the class GapicSpannerRpc method getInstance.
@Override
public Instance getInstance(String instanceName) throws SpannerException {
GetInstanceRequest request = GetInstanceRequest.newBuilder().setName(instanceName).build();
GrpcCallContext context = newCallContext(null, instanceName, request, InstanceAdminGrpc.getGetInstanceMethod());
return get(instanceAdminStub.getInstanceCallable().futureCall(request, context));
}
Aggregations