use of com.google.cloud.bigtable.admin.v2.models.Cluster in project java-bigtable by googleapis.
the class BigtableInstanceAdminClientIT method appProfileTestMultiClusterWithIds.
@Test
public void appProfileTestMultiClusterWithIds() {
String newInstanceId = prefixGenerator.newPrefix();
String newClusterId = newInstanceId + "-c1";
String newClusterId2 = newInstanceId + "-c2";
client.createInstance(CreateInstanceRequest.of(newInstanceId).addCluster(newClusterId, testEnvRule.env().getPrimaryZone(), 1, StorageType.SSD).addCluster(newClusterId2, testEnvRule.env().getSecondaryZone(), 1, StorageType.SSD).setDisplayName("Multi-Cluster-Instance-Test").addLabel("state", "readytodelete").setType(Type.PRODUCTION));
try {
assertThat(client.exists(newInstanceId)).isTrue();
String testAppProfile = "test-app-profile";
CreateAppProfileRequest request = CreateAppProfileRequest.of(newInstanceId, testAppProfile).setRoutingPolicy(AppProfile.MultiClusterRoutingPolicy.of(newClusterId)).setDescription("This is to test app profile");
AppProfile newlyCreatedAppProfile = client.createAppProfile(request);
AppProfile updated = client.updateAppProfile(UpdateAppProfileRequest.of(newlyCreatedAppProfile).setDescription("new description"));
AppProfile freshAppProfile = client.getAppProfile(newInstanceId, testAppProfile);
assertThat(freshAppProfile.getDescription()).isEqualTo(updated.getDescription());
AppProfile.MultiClusterRoutingPolicy freshAppProfilePolicy = (AppProfile.MultiClusterRoutingPolicy) freshAppProfile.getPolicy();
AppProfile.MultiClusterRoutingPolicy updatedAppProfilePolicy = (AppProfile.MultiClusterRoutingPolicy) updated.getPolicy();
assertThat(freshAppProfilePolicy.getClusterIds()).containsExactly(newClusterId);
assertThat(freshAppProfilePolicy.getClusterIds()).isEqualTo(updatedAppProfilePolicy.getClusterIds());
assertThat(freshAppProfilePolicy).isEqualTo(updatedAppProfilePolicy);
assertThat(client.listAppProfiles(newInstanceId)).contains(freshAppProfile);
// update again with routing policy
AppProfile updated2 = client.updateAppProfile(UpdateAppProfileRequest.of(updated).setRoutingPolicy(AppProfile.MultiClusterRoutingPolicy.of(newClusterId2)));
AppProfile freshAppProfile2 = client.getAppProfile(newInstanceId, testAppProfile);
assertThat(freshAppProfile2.getDescription()).isEqualTo(updated2.getDescription());
AppProfile.MultiClusterRoutingPolicy freshAppProfilePolicy2 = (AppProfile.MultiClusterRoutingPolicy) freshAppProfile2.getPolicy();
AppProfile.MultiClusterRoutingPolicy updatedAppProfilePolicy2 = (AppProfile.MultiClusterRoutingPolicy) updated2.getPolicy();
assertThat(freshAppProfilePolicy2.getClusterIds()).containsExactly(newClusterId2);
assertThat(freshAppProfilePolicy2.getClusterIds()).isEqualTo(updatedAppProfilePolicy2.getClusterIds());
assertThat(freshAppProfilePolicy2).isEqualTo(updatedAppProfilePolicy2);
assertThat(client.listAppProfiles(newInstanceId)).contains(freshAppProfile2);
// update to single routing policy
AppProfile updated3 = client.updateAppProfile(UpdateAppProfileRequest.of(updated).setRoutingPolicy(AppProfile.SingleClusterRoutingPolicy.of(newClusterId)));
AppProfile freshAppProfile3 = client.getAppProfile(newInstanceId, testAppProfile);
assertThat(freshAppProfile3.getDescription()).isEqualTo(updated3.getDescription());
AppProfile.SingleClusterRoutingPolicy freshAppProfilePolicy3 = (AppProfile.SingleClusterRoutingPolicy) freshAppProfile3.getPolicy();
AppProfile.SingleClusterRoutingPolicy updatedAppProfilePolicy3 = (AppProfile.SingleClusterRoutingPolicy) updated3.getPolicy();
assertThat(freshAppProfilePolicy3.getClusterId()).isEqualTo(newClusterId);
assertThat(freshAppProfilePolicy3.getClusterId()).isEqualTo(updatedAppProfilePolicy3.getClusterId());
assertThat(freshAppProfilePolicy3).isEqualTo(updatedAppProfilePolicy3);
assertThat(client.listAppProfiles(newInstanceId)).contains(freshAppProfile3);
} finally {
if (client.exists(newInstanceId)) {
client.deleteInstance(newInstanceId);
}
}
}
use of com.google.cloud.bigtable.admin.v2.models.Cluster in project java-bigtable by googleapis.
the class BigtableInstanceAdminClientIT method basicInstanceOperationTest.
/* As cluster creation is very expensive operation, so reusing existing clusters to verify rest
of the operation.*/
@Test
public void basicInstanceOperationTest() {
assertThat(client.exists(instanceId)).isTrue();
client.updateInstance(UpdateInstanceRequest.of(instanceId).setDisplayName("Updated-Instance-Name"));
Instance instance = client.getInstance(instanceId);
assertThat(instance.getDisplayName()).isEqualTo("Updated-Instance-Name");
assertThat(client.listInstances()).contains(instance);
}
use of com.google.cloud.bigtable.admin.v2.models.Cluster in project java-bigtable by googleapis.
the class BigtableCmekIT method instanceAndClusterTest.
@Test
public void instanceAndClusterTest() {
// Keys are specified per-cluster with each cluster requesting the same key and the cluster's
// zone must be within the region of the key
Cluster cluster = instanceAdmin.getCluster(instanceId, clusterId1);
assertThat(cluster.getKmsKeyName()).isEqualTo(kmsKeyName);
LOGGER.info("Creating cluster in zone: " + zones.get(1));
instanceAdmin.createCluster(CreateClusterRequest.of(instanceId, clusterId2).setZone(zones.get(1)).setServeNodes(1).setStorageType(StorageType.SSD).setKmsKeyName(kmsKeyName));
Cluster secondCluster = instanceAdmin.getCluster(instanceId, clusterId2);
assertThat(secondCluster.getKmsKeyName()).isEqualTo(kmsKeyName);
LOGGER.info("Trying to create cluster in zone: " + otherZone);
try {
instanceAdmin.createCluster(CreateClusterRequest.of(instanceId, clusterId3).setZone(otherZone).setServeNodes(1).setStorageType(StorageType.SSD).setKmsKeyName(kmsKeyName));
Assert.fail("should have thrown an error");
} catch (com.google.api.gax.rpc.FailedPreconditionException e) {
assertThat(e.getMessage()).contains("FAILED_PRECONDITION: Error in field 'cluster' : " + "Error in field 'encryption_config.kms_key_name' : CMEK key " + kmsKeyName + " cannot be used to protect a cluster in zone " + NameUtil.formatLocationName(testEnvRule.env().getProjectId(), otherZone));
}
}
use of com.google.cloud.bigtable.admin.v2.models.Cluster in project java-bigtable by googleapis.
the class BigtableInstanceAdminClient method listClustersAsync.
/**
* Asynchronously lists all clusters in the specified instance.
*
* <p>This method will throw a {@link PartialListClustersException} when any zone is unavailable.
* If a partial list is OK, the exception can be caught and inspected.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Cluster> clustersFuture = client.listClustersAsync("my-instance");
*
* ApiFutures.addCallback(clustersFuture, new ApiFutureCallback<List<Cluster>>() {
* public void onFailure(Throwable t) {
* if (t instanceof PartialListClustersException) {
* PartialListClustersException partialError = (PartialListClustersException)t;
* System.out.println("The following zones are unavailable: " + partialError.getUnavailableZones());
* System.out.println("But the following clusters are reachable: " + partialError.getClusters());
* } else {
* t.printStackTrace();
* }
* }
*
* public void onSuccess(List<Cluster> result) {
* System.out.println("Found a complete set of instances: " + result);
* }
* }, MoreExecutors.directExecutor());
* }</pre>
*/
@SuppressWarnings("WeakerAccess")
public ApiFuture<List<Cluster>> listClustersAsync(String instanceId) {
String name = NameUtil.formatInstanceName(projectId, instanceId);
com.google.bigtable.admin.v2.ListClustersRequest request = com.google.bigtable.admin.v2.ListClustersRequest.newBuilder().setParent(name).build();
return ApiFutures.transform(stub.listClustersCallable().futureCall(request), new ApiFunction<com.google.bigtable.admin.v2.ListClustersResponse, List<Cluster>>() {
@Override
public List<Cluster> apply(com.google.bigtable.admin.v2.ListClustersResponse proto) {
// NOTE: Server-side pagination is not and will not be implemented, so remaining pages
// are not fetched. However, if that assumption turns out to be wrong, fail fast to
// avoid returning partial data.
Verify.verify(proto.getNextPageToken().isEmpty(), "Server returned an unexpected paginated response");
ImmutableList.Builder<Cluster> clusters = ImmutableList.builder();
for (com.google.bigtable.admin.v2.Cluster cluster : proto.getClustersList()) {
clusters.add(Cluster.fromProto(cluster));
}
ImmutableList.Builder<String> failedZones = ImmutableList.builder();
for (String locationStr : proto.getFailedLocationsList()) {
failedZones.add(NameUtil.extractZoneIdFromLocationName(locationStr));
}
if (!failedZones.build().isEmpty()) {
throw new PartialListClustersException(failedZones.build(), clusters.build());
}
return clusters.build();
}
}, MoreExecutors.directExecutor());
}
use of com.google.cloud.bigtable.admin.v2.models.Cluster in project java-bigtable by googleapis.
the class BigtableInstanceAdminClient method disableClusterAutoscalingAsync.
/**
* Asynchronously disables autoscaling and enables manual scaling by setting a static node count
* for the cluster. Please note that only clusters that belong to a production instance can be
* resized.
*
* <p>Sample code:
*
* <pre>{@code
* ApiFuture<Cluster> clusterApiFuture = client.disableClusterAutoscalingAsync("my-instance", "my-cluster", 3);
* Cluster cluster = clusterApiFuture.get();
* }</pre>
*/
public ApiFuture<Cluster> disableClusterAutoscalingAsync(String instanceId, String clusterId, int staticSize) {
String name = NameUtil.formatClusterName(projectId, instanceId, clusterId);
com.google.bigtable.admin.v2.Cluster request = com.google.bigtable.admin.v2.Cluster.newBuilder().setName(name).setServeNodes(staticSize).setClusterConfig(com.google.bigtable.admin.v2.Cluster.ClusterConfig.getDefaultInstance()).build();
PartialUpdateClusterRequest partialUpdateClusterRequest = PartialUpdateClusterRequest.newBuilder().setUpdateMask(FieldMaskUtil.fromStringList(com.google.bigtable.admin.v2.Cluster.class, Lists.newArrayList("cluster_config.cluster_autoscaling_config", "serve_nodes"))).setCluster(request).build();
return ApiFutures.transform(stub.partialUpdateClusterOperationCallable().futureCall(partialUpdateClusterRequest), Cluster::fromProto, MoreExecutors.directExecutor());
}
Aggregations