Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Bigtable: instanceAdmin sample #4299

Merged
merged 5 commits into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.api.gax.rpc.AlreadyExistsException;
import com.google.api.gax.rpc.NotFoundException;
import com.google.bigtable.admin.v2.ProjectName;
import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient;
import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings;
import com.google.cloud.bigtable.admin.v2.models.Cluster;
Expand All @@ -37,19 +36,19 @@
* <p>This example demonstrates the usage of BigtableInstanceAdminClient to create, configure, and
* delete Cloud Bigtable Instances and Clusters.
*
* <pre>
* creates production instance
* lists instances
* gets instance
* lists clusters
* adds cluster
* deletes cluster
* deletes instance
* </pre>
* <ul>
* <li>creates production instance
* <li>lists instances
* <li>gets instance
* <li>lists clusters
* <li>adds cluster
* <li>deletes cluster
* <li>deletes instance
* </ul>
*/
public class InstanceAdmin {
public class InstanceAdminExample {

private static final String CLUSTER = "cluster" + System.currentTimeMillis();
private static final String CLUSTER = "cluster";
private final String clusterId;
private final String instanceId;
private final BigtableInstanceAdminClient adminClient;
Expand All @@ -62,20 +61,20 @@ public static void main(String[] args) throws IOException {
}
String projectId = args[0];

InstanceAdmin instanceAdmin = new InstanceAdmin(projectId, "ssd-instance", "ssd-cluster");
InstanceAdminExample instanceAdmin =
new InstanceAdminExample(projectId, "ssd-instance", "ssd-cluster");
instanceAdmin.run();
}

public InstanceAdmin(String projectId, String instanceId, String clusterId) throws IOException {
public InstanceAdminExample(String projectId, String instanceId, String clusterId)
throws IOException {
this.instanceId = instanceId;
this.clusterId = clusterId;

// [START connecting_to_bigtable]
// Creates the settings to configure a bigtable instance admin client.
BigtableInstanceAdminSettings instanceAdminSettings =
BigtableInstanceAdminSettings.newBuilder()
.setProjectName(ProjectName.of(projectId))
.build();
BigtableInstanceAdminSettings.newBuilder().setProjectId(projectId).build();

// Creates a bigtable instance admin client.
adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings);
Expand Down Expand Up @@ -110,9 +109,9 @@ public void createProdInstance() {
try {
Instance instance = adminClient.createInstance(createInstanceRequest);
System.out.printf("PRODUCTION type instance %s created successfully%n", instance.getId());
} catch (AlreadyExistsException e) {
System.err.println("Failed to create instance, already exists: " + e.getMessage());
System.exit(0);
} catch (Exception e) {
System.err.println("Failed to create instance: " + e.getMessage());
throw e;
}
// [END bigtable_create_prod_instance]
}
Expand All @@ -135,11 +134,7 @@ public void listInstances() {
// [END bigtable_list_instances]
}

/**
* Demonstrates how to get an instance.
*
* @return instance
*/
/** Demonstrates how to get an instance. */
public Instance getInstance() {
System.out.println("\nGet Instance");
// [START bigtable_get_instance]
Expand Down Expand Up @@ -192,7 +187,7 @@ public void deleteInstance() {

/** Demonstrates how to add a cluster to an instance. */
public void addCluster() {
System.out.printf("%nAdding cluster %s to instance: %s%n", CLUSTER, instanceId);
System.out.printf("%nAdding cluster: %s to instance: %s%n", CLUSTER, instanceId);
// [START bigtable_create_cluster]
try {
adminClient.createCluster(
Expand All @@ -209,11 +204,10 @@ public void addCluster() {

/** Demonstrates how to delete a cluster from an instance. */
public void deleteCluster() {
System.out.printf("%nDeleting cluster %s from instance: %s%n", CLUSTER, instanceId);
System.out.printf("%nDeleting cluster: %s from instance: %s%n", CLUSTER, instanceId);
// [START bigtable_delete_cluster]
try {
adminClient.deleteCluster(instanceId, CLUSTER);
adminClient.listClusters(instanceId);
System.out.printf("Cluster: %s deleted successfully%n", CLUSTER);
} catch (NotFoundException e) {
System.err.println("Failed to delete a non-existent cluster: " + e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.google.cloud.examples.bigtable;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

Expand All @@ -39,17 +40,17 @@
import org.junit.BeforeClass;
import org.junit.Test;

/** Integration tests for {@link com.google.cloud.examples.bigtable.InstanceAdmin} */
public class ITInstanceAdmin {
/** Integration tests for {@link InstanceAdminExample} */
public class ITInstanceAdminExample {

private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance";
private static final String INSTANCE_PREFIX = "instanceadmin";
private static final String CLUSTER = "cluster" + System.currentTimeMillis();
private static String clusterId;
private static String instanceId;
private static BigtableInstanceAdminClient adminClient;
private static InstanceAdmin instanceAdmin;
private static final String CLUSTER = "cluster";
private static ProjectName projectName;

This comment was marked as spam.

private static BigtableInstanceAdminClient adminClient;
private String clusterId;
private String instanceId;
private InstanceAdminExample instanceAdmin;

@BeforeClass
public static void beforeClass() throws IOException {
Expand All @@ -60,7 +61,7 @@ public static void beforeClass() throws IOException {
}
projectName = ProjectName.of(InstanceName.parse(targetProject).getProject());
BigtableInstanceAdminSettings instanceAdminSettings =
BigtableInstanceAdminSettings.newBuilder().setProjectName(projectName).build();
BigtableInstanceAdminSettings.newBuilder().setProjectId(projectName.getProject()).build();
adminClient = BigtableInstanceAdminClient.create(instanceAdminSettings);
}

Expand All @@ -78,7 +79,7 @@ public void setup() throws IOException {
}
instanceId = generateId();
clusterId = generateId();
instanceAdmin = new InstanceAdmin(projectName.getProject(), instanceId, clusterId);
instanceAdmin = new InstanceAdminExample(projectName.getProject(), instanceId, clusterId);
adminClient.createInstance(
CreateInstanceRequest.of(instanceId)
.addCluster(clusterId, "us-central1-f", 3, StorageType.SSD)
Expand All @@ -98,14 +99,14 @@ public void testCreateAndDeleteInstance() throws IOException {
// Creates an instance.
String fakeInstance = generateId();
String fakeCluster = generateId();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

InstanceAdmin testInstanceAdmin =
new InstanceAdmin(projectName.getProject(), fakeInstance, fakeCluster);
InstanceAdminExample testInstanceAdmin =
new InstanceAdminExample(projectName.getProject(), fakeInstance, fakeCluster);
testInstanceAdmin.createProdInstance();
assertTrue(adminClient.exists(fakeInstance));

// Deletes an instance.
testInstanceAdmin.deleteInstance();
assertTrue(!adminClient.exists(fakeInstance));
assertFalse(adminClient.exists(fakeInstance));
}

@Test
Expand Down