Search in sources :

Example 36 with Instance

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());
    }
}
Also used : Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) SpannerException(com.google.cloud.spanner.SpannerException) ExecutionException(java.util.concurrent.ExecutionException) InstanceInfo(com.google.cloud.spanner.InstanceInfo) SpannerOptions(com.google.cloud.spanner.SpannerOptions) Spanner(com.google.cloud.spanner.Spanner) Test(org.junit.Test)

Example 37 with Instance

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();
}
Also used : Random(java.util.Random) Instance(com.google.cloud.spanner.Instance) UpdateInstanceMetadata(com.google.spanner.admin.instance.v1.UpdateInstanceMetadata) InstanceInfo(com.google.cloud.spanner.InstanceInfo) Test(org.junit.Test) SerialIntegrationTest(com.google.cloud.spanner.SerialIntegrationTest)

Example 38 with Instance

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();
}
Also used : Instance(com.google.cloud.spanner.Instance) InstanceAdminClient(com.google.cloud.spanner.InstanceAdminClient) CreateInstanceMetadata(com.google.spanner.admin.instance.v1.CreateInstanceMetadata) Spanner(com.google.cloud.spanner.Spanner)

Example 39 with Instance

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);
    });
}
Also used : UpdateInstanceMetadata(com.google.spanner.admin.instance.v1.UpdateInstanceMetadata) OperationFutureImpl(com.google.api.gax.longrunning.OperationFutureImpl) FieldMask(com.google.protobuf.FieldMask)

Example 40 with Instance

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));
}
Also used : GetInstanceRequest(com.google.spanner.admin.instance.v1.GetInstanceRequest) GrpcCallContext(com.google.api.gax.grpc.GrpcCallContext)

Aggregations

Test (org.junit.Test)50 AbstractMessage (com.google.protobuf.AbstractMessage)19 ByteString (com.google.protobuf.ByteString)17 ExecutionException (java.util.concurrent.ExecutionException)16 Instance (com.google.cloud.spanner.Instance)15 StatusRuntimeException (io.grpc.StatusRuntimeException)14 Instance (com.google.cloud.compute.v1.Instance)11 InstancesClient (com.google.cloud.compute.v1.InstancesClient)11 CreateInstanceMetadata (com.google.spanner.admin.instance.v1.CreateInstanceMetadata)11 Instance (com.google.spanner.admin.instance.v1.Instance)11 Instance (com.google.bigtable.admin.v2.Instance)10 InstanceAdminClient (com.google.cloud.spanner.InstanceAdminClient)10 InstanceInfo (com.google.cloud.spanner.InstanceInfo)10 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)9 Instance (com.google.cloud.notebooks.v1beta1.Instance)9 Spanner (com.google.cloud.spanner.Spanner)8 ArrayList (java.util.ArrayList)8 Operation (com.google.cloud.compute.v1.Operation)7 FieldMask (com.google.protobuf.FieldMask)7 InstanceName (com.google.spanner.admin.instance.v1.InstanceName)7