Repository: GoogleCloudPlatform/cloud-bigtable-client
Pull request #1804 (2018-04-30 21:50:39)

Adding a simple way to create an instance admin object.

pawan-qlogic: LGTM

Patch:
diff --git a/bigtable-client-core-parent/bigtable-client-core/src/main/java/com/google/cloud/bigtable/grpc/BigtableSession.java b/bigtable-client-core-parent/bigtable-client-core/src/main/java/com/google/cloud/bigtable/grpc/BigtableSession.java
index a36bdd5da..11e1da6eb 100644
--- a/bigtable-client-core-parent/bigtable-client-core/src/main/java/com/google/cloud/bigtable/grpc/BigtableSession.java
+++ b/bigtable-client-core-parent/bigtable-client-core/src/main/java/com/google/cloud/bigtable/grpc/BigtableSession.java
@@ -34,6 +34,7 @@ import java.util.concurrent.TimeUnit;
 import javax.net.ssl.SSLException;
 
 import com.google.api.client.util.Strings;
+import com.google.bigtable.admin.v2.BigtableInstanceAdminGrpc;
 import com.google.bigtable.admin.v2.ListClustersResponse;
 import com.google.cloud.bigtable.config.BigtableOptions;
 import com.google.cloud.bigtable.config.BigtableVersionInfo;
@@ -402,6 +403,12 @@ public class BigtableSession implements Closeable {
     return channelPool;
   }
 
+  public static BigtableInstanceClient createInstanceClient()
+      throws IOException, GeneralSecurityException {
+    BigtableOptions options = new BigtableOptions.Builder().build();
+    Channel channel = createChannelPool(BigtableOptions.BIGTABLE_ADMIN_HOST_DEFAULT, options);
+    return new BigtableInstanceGrpcClient(channel);
+  }
   /**
    * Create a new {@link com.google.cloud.bigtable.grpc.io.ChannelPool}, with auth headers.
    *
@@ -426,19 +433,23 @@ public class BigtableSession implements Closeable {
    * @throws IOException if any.
    * @throws GeneralSecurityException
    */
-  public static ChannelPool createChannelPool(final String host, final BigtableOptions options, int count)
-      throws IOException, GeneralSecurityException {
-    final ClientInterceptor credentialsInterceptor = CredentialInterceptorCache.getInstance()
-        .getCredentialsInterceptor(options.getCredentialOptions(), options.getRetryOptions());
-    final ClientInterceptor prefixInterceptor =
-        new GoogleCloudResourcePrefixInterceptor(options.getInstanceName().toString());
-    return new ChannelPool(
-        new ChannelPool.ChannelFactory() {
-          @Override
-          public ManagedChannel create() throws IOException {
-            return createNettyChannel(host, options, credentialsInterceptor, prefixInterceptor);
-          }
-        }, count);
+  public static ChannelPool createChannelPool(final String host, final BigtableOptions options,
+      int count) throws IOException, GeneralSecurityException {
+    final List<ClientInterceptor> interceptorList = new ArrayList<>();
+    interceptorList.add(CredentialInterceptorCache.getInstance()
+        .getCredentialsInterceptor(options.getCredentialOptions(), options.getRetryOptions()));
+    if (options.getInstanceName() != null) {
+      interceptorList
+          .add(new GoogleCloudResourcePrefixInterceptor(options.getInstanceName().toString()));
+    }
+    final ClientInterceptor[] interceptors =
+        interceptorList.toArray(new ClientInterceptor[interceptorList.size()]);
+    ChannelPool.ChannelFactory factory = new ChannelPool.ChannelFactory() {
+      @Override public ManagedChannel create() throws IOException {
+        return createNettyChannel(host, options, interceptors);
+      }
+    };
+    return new ChannelPool(factory, count);
   }
 
   /**


Test patch:
diff --git a/bigtable-client-core-parent/bigtable-client-core/src/test/java/com/google/cloud/bigtable/grpc/TestBigtableSession.java b/bigtable-client-core-parent/bigtable-client-core/src/test/java/com/google/cloud/bigtable/grpc/TestBigtableSession.java
index 1b4facc23..fd08eaa1d 100644
--- a/bigtable-client-core-parent/bigtable-client-core/src/test/java/com/google/cloud/bigtable/grpc/TestBigtableSession.java
+++ b/bigtable-client-core-parent/bigtable-client-core/src/test/java/com/google/cloud/bigtable/grpc/TestBigtableSession.java
@@ -71,9 +71,29 @@ public class TestBigtableSession {
   }
 
   @Test
-  public void testOpenSSL() throws Throwable{
-    if(!OpenSsl.isAvailable()){
+  public void testOpenSSL() throws Throwable {
+    if (!OpenSsl.isAvailable()) {
       throw OpenSsl.unavailabilityCause();
     }
   }
+
+  /**
+   * Test to make sure that {@link BigtableSession#createInstanceClient()} can be created.
+   * @throws Throwable
+   */
+  @Test
+  public void testCreateInstanceClient() throws Throwable {
+    try {
+      BigtableSession.createInstanceClient();
+    } catch (IOException e) {
+      if (e.getMessage().toLowerCase().contains("credentials")) {
+        // ignore;  This is running on a system that doesn't have default credentails,
+        // and this test is to ensure that openssl works well.  Travis isn't sent up with
+        // credentials, so this test should pass in those conditions.
+      } else {
+        throw e;
+      }
+    }
+  }
+
 }