Search in sources :

Example 1 with PartialListInstancesException

use of com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException in project java-bigtable by googleapis.

the class BigtableInstanceAdminClientTest method testListInstancesFailedZone.

@Test
public void testListInstancesFailedZone() {
    // Setup
    Mockito.when(mockStub.listInstancesCallable()).thenReturn(mockListInstancesCallable);
    com.google.bigtable.admin.v2.ListInstancesRequest expectedRequest = com.google.bigtable.admin.v2.ListInstancesRequest.newBuilder().setParent(PROJECT_NAME).build();
    com.google.bigtable.admin.v2.ListInstancesResponse expectedResponse = com.google.bigtable.admin.v2.ListInstancesResponse.newBuilder().addInstances(com.google.bigtable.admin.v2.Instance.newBuilder().setName(INSTANCE_NAME + "1").build()).addFailedLocations(NameUtil.formatLocationName(PROJECT_ID, "us-east1-d")).build();
    Mockito.when(mockListInstancesCallable.futureCall(expectedRequest)).thenReturn(ApiFutures.immediateFuture(expectedResponse));
    // Execute
    Exception actualError = null;
    try {
        adminClient.listInstances();
    } catch (Exception e) {
        actualError = e;
    }
    // Verify
    assertThat(actualError).isInstanceOf(PartialListInstancesException.class);
    assert actualError != null;
    PartialListInstancesException partialListError = (PartialListInstancesException) actualError;
    assertThat(partialListError.getInstances()).containsExactly(Instance.fromProto(expectedResponse.getInstances(0)));
    assertThat(partialListError.getUnavailableZones()).containsExactly("us-east1-d");
}
Also used : PartialListInstancesException(com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException) NotFoundException(com.google.api.gax.rpc.NotFoundException) PartialListClustersException(com.google.cloud.bigtable.admin.v2.models.PartialListClustersException) PartialListInstancesException(com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException) Test(org.junit.Test)

Example 2 with PartialListInstancesException

use of com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException in project java-bigtable by googleapis.

the class BigtableInstanceAdminClient method listInstancesAsync.

/**
 * Asynchronously lists all of the instances in the current project.
 *
 * <p>This method will throw a {@link PartialListInstancesException} when any zone is unavailable.
 * If a partial list is OK, the exception can be caught and inspected.
 *
 * <p>Sample code:
 *
 * <pre>{@code
 * ApiFuture<Instance> instancesFuture = client.listInstancesAsync();
 *
 * ApiFutures.addCallback(instancesFuture, new ApiFutureCallback<List<Instance>>() {
 *   public void onFailure(Throwable t) {
 *     if (t instanceof PartialListInstancesException) {
 *       PartialListInstancesException partialError = (PartialListInstancesException)t;
 *       System.out.println("The following zones are unavailable: " + partialError.getUnavailableZones());
 *       System.out.println("But the following instances are reachable: " + partialError.getInstances());
 *     } else {
 *       t.printStackTrace();
 *     }
 *   }
 *
 *   public void onSuccess(List<Instance> result) {
 *     System.out.println("Found a complete set of instances: " + result);
 *   }
 * }, MoreExecutors.directExecutor());
 * }</pre>
 */
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<Instance>> listInstancesAsync() {
    com.google.bigtable.admin.v2.ListInstancesRequest request = com.google.bigtable.admin.v2.ListInstancesRequest.newBuilder().setParent(NameUtil.formatProjectName(projectId)).build();
    ApiFuture<com.google.bigtable.admin.v2.ListInstancesResponse> responseFuture = stub.listInstancesCallable().futureCall(request);
    return ApiFutures.transform(responseFuture, new ApiFunction<com.google.bigtable.admin.v2.ListInstancesResponse, List<Instance>>() {

        @Override
        public List<Instance> apply(com.google.bigtable.admin.v2.ListInstancesResponse proto) {
            // NOTE: Pagination is intentionally ignored. The server does not implement it and never
            // will.
            Verify.verify(proto.getNextPageToken().isEmpty(), "Server returned an unexpected paginated response");
            ImmutableList.Builder<Instance> instances = ImmutableList.builder();
            for (com.google.bigtable.admin.v2.Instance protoInstance : proto.getInstancesList()) {
                instances.add(Instance.fromProto(protoInstance));
            }
            ImmutableList.Builder<String> failedZones = ImmutableList.builder();
            for (String locationStr : proto.getFailedLocationsList()) {
                failedZones.add(NameUtil.extractZoneIdFromLocationName(locationStr));
            }
            if (!failedZones.build().isEmpty()) {
                throw new PartialListInstancesException(failedZones.build(), instances.build());
            }
            return instances.build();
        }
    }, MoreExecutors.directExecutor());
}
Also used : Instance(com.google.cloud.bigtable.admin.v2.models.Instance) PartialListInstancesException(com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Aggregations

PartialListInstancesException (com.google.cloud.bigtable.admin.v2.models.PartialListInstancesException)2 NotFoundException (com.google.api.gax.rpc.NotFoundException)1 Instance (com.google.cloud.bigtable.admin.v2.models.Instance)1 PartialListClustersException (com.google.cloud.bigtable.admin.v2.models.PartialListClustersException)1 ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 Test (org.junit.Test)1