|
18 | 18 | import static com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings.BIGTABLE_EMULATOR_HOST_ENV_VAR;
|
19 | 19 |
|
20 | 20 | import com.google.api.gax.core.CredentialsProvider;
|
| 21 | +import com.google.api.gax.core.NoCredentialsProvider; |
| 22 | +import com.google.api.gax.grpc.ChannelPoolSettings; |
| 23 | +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; |
21 | 24 | import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings;
|
22 | 25 | import com.google.common.base.MoreObjects;
|
23 | 26 | import com.google.common.base.Preconditions;
|
| 27 | +import com.google.common.base.Strings; |
24 | 28 | import com.google.common.base.Verify;
|
| 29 | +import io.grpc.ManagedChannelBuilder; |
25 | 30 | import java.io.IOException;
|
| 31 | +import java.util.logging.Logger; |
26 | 32 | import javax.annotation.Nonnull;
|
27 | 33 | import javax.annotation.Nullable;
|
28 | 34 |
|
|
47 | 53 | * }</pre>
|
48 | 54 | */
|
49 | 55 | public final class BigtableInstanceAdminSettings {
|
| 56 | + private static final Logger LOGGER = |
| 57 | + Logger.getLogger(BigtableInstanceAdminSettings.class.getName()); |
| 58 | + |
50 | 59 | private final String projectId;
|
51 | 60 | private final BigtableInstanceAdminStubSettings stubSettings;
|
52 | 61 |
|
@@ -115,12 +124,49 @@ public Builder toBuilder() {
|
115 | 124 |
|
116 | 125 | /** Returns a new builder for this class. */
|
117 | 126 | public static Builder newBuilder() {
|
118 |
| - Preconditions.checkState( |
119 |
| - System.getenv(BIGTABLE_EMULATOR_HOST_ENV_VAR) == null, |
120 |
| - "BigtableInstanceAdminSettings doesn't supported on Emulator"); |
| 127 | + String hostAndPort = System.getenv(BIGTABLE_EMULATOR_HOST_ENV_VAR); |
| 128 | + if (!Strings.isNullOrEmpty(hostAndPort)) { |
| 129 | + int port; |
| 130 | + try { |
| 131 | + port = Integer.parseInt(hostAndPort.substring(hostAndPort.lastIndexOf(":") + 1)); |
| 132 | + return newBuilderForEmulator(hostAndPort.substring(0, hostAndPort.lastIndexOf(":")), port); |
| 133 | + } catch (NumberFormatException | IndexOutOfBoundsException ex) { |
| 134 | + throw new RuntimeException( |
| 135 | + "Invalid host/port in " |
| 136 | + + BIGTABLE_EMULATOR_HOST_ENV_VAR |
| 137 | + + " environment variable: " |
| 138 | + + hostAndPort); |
| 139 | + } |
| 140 | + } |
121 | 141 | return new Builder();
|
122 | 142 | }
|
123 | 143 |
|
| 144 | + /** Create a new builder preconfigured to connect to the Bigtable emulator with port number. */ |
| 145 | + public static Builder newBuilderForEmulator(int port) { |
| 146 | + return newBuilderForEmulator("localhost", port); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Creates a new builder preconfigured to connect to the Bigtable emulator with host name and port |
| 151 | + * number. |
| 152 | + */ |
| 153 | + public static Builder newBuilderForEmulator(String hostname, int port) { |
| 154 | + Builder builder = new Builder(); |
| 155 | + |
| 156 | + builder |
| 157 | + .stubSettings() |
| 158 | + .setCredentialsProvider(NoCredentialsProvider.create()) |
| 159 | + .setEndpoint(hostname + ":" + port) |
| 160 | + .setTransportChannelProvider( |
| 161 | + InstantiatingGrpcChannelProvider.newBuilder() |
| 162 | + .setChannelPoolSettings(ChannelPoolSettings.staticallySized(1)) |
| 163 | + .setChannelConfigurator(ManagedChannelBuilder::usePlaintext) |
| 164 | + .build()); |
| 165 | + |
| 166 | + LOGGER.info("Connecting to the Bigtable emulator at " + hostname + ":" + port); |
| 167 | + return builder; |
| 168 | + } |
| 169 | + |
124 | 170 | /** Builder for BigtableInstanceAdminSettings. */
|
125 | 171 | public static final class Builder {
|
126 | 172 | @Nullable private String projectId;
|
|
0 commit comments