/** * @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); }
/** * Gets the current configuration of the cluster as encapsulated by a {@link Cluster} object. * * @param clusterId * @param zoneId * @return the {@link Cluster} if it was set. If the cluster is not found, throw a {@link * NullPointerException}. */ public Cluster getCluster(String clusterId, String zoneId) { Cluster response = null; for (Cluster cluster : getClusters().getClustersList()) { if (cluster.getName().endsWith("/clusters/" + clusterId) && cluster.getLocation().endsWith("/locations/" + zoneId)) { if (response == null) { response = cluster; } else { throw new IllegalStateException( String.format("Got multiple clusters named %s in zone %z.", clusterId, zoneId)); } } } return Preconditions.checkNotNull(response, String.format("Cluster %s in zone %s was not found.", clusterId, zoneId)); }
/** * 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; }