@Test public void testListClusters() { // Setup com.google.bigtable.admin.v2.ListClustersRequest expectedRequest = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder() .setParent(INSTANCE_NAME) .build(); com.google.bigtable.admin.v2.ListClustersResponse expectedResponse = com.google.bigtable.admin.v2.ListClustersResponse.newBuilder() .addClusters( com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME + "1")) .addClusters( com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME + "2")) .build(); Mockito.when(mockListClustersCallable.futureCall(expectedRequest)) .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute List<Cluster> actualResult = adminClient.listClusters(INSTANCE_ID); // Verify assertThat(actualResult) .containsExactly( Cluster.fromProto(expectedResponse.getClusters(0)), Cluster.fromProto(expectedResponse.getClusters(1))); }
/** * @return a Single Cluster for the project and instance. * @throws IllegalStateException for any project / instance combination that does not return * exactly 1 cluster. */ public Cluster getSingleCluster() { ListClustersResponse response = getClusters(); Preconditions.checkState(response.getClustersCount() != 0, "The instance does not exist."); Preconditions.checkState(response.getClustersCount() == 1, "There can only be one cluster for this method to work."); return response.getClusters(0); }
/** * Snapshot operations need various aspects of a {@link BigtableClusterName}. This method gets a * clusterId from either a lookup (projectId and instanceId translate to a single clusterId when * an instance has only one cluster). */ public synchronized BigtableClusterName getClusterName() throws IOException { if (this.clusterName == null) { try (BigtableClusterUtilities util = new BigtableClusterUtilities(options)) { ListClustersResponse clusters = util.getClusters(); Preconditions.checkState(clusters.getClustersCount() == 1, String.format( "Project '%s' / Instance '%s' has %d clusters. There must be exactly 1 for this operation to work.", options.getProjectId(), options.getInstanceId(), clusters.getClustersCount())); clusterName = new BigtableClusterName(clusters.getClusters(0).getName()); } catch (GeneralSecurityException e) { throw new IOException("Could not get cluster Id.", e); } } return clusterName; }
@Test public void testListClustersFailedZone() { // Setup com.google.bigtable.admin.v2.ListClustersRequest expectedRequest = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder() .setParent(INSTANCE_NAME) .build(); com.google.bigtable.admin.v2.ListClustersResponse expectedResponse = com.google.bigtable.admin.v2.ListClustersResponse.newBuilder() .addClusters(com.google.bigtable.admin.v2.Cluster.newBuilder().setName(CLUSTER_NAME)) .addFailedLocations(NameUtil.formatLocationName(PROJECT_ID, "us-east1-c")) .build(); Mockito.when(mockListClustersCallable.futureCall(expectedRequest)) .thenReturn(ApiFutures.immediateFuture(expectedResponse)); // Execute Exception actualError = null; try { adminClient.listClusters(INSTANCE_ID); } catch (Exception e) { actualError = e; } // Verify assertThat(actualError).isInstanceOf(PartialListClustersException.class); assert actualError != null; PartialListClustersException partialListError = (PartialListClustersException) actualError; assertThat(partialListError.getClusters()) .containsExactly(Cluster.fromProto(expectedResponse.getClusters(0))); assertThat(partialListError.getUnavailableZones()).containsExactly("us-east1-c"); }