Skip to content

Commit 1cc8dba

Browse files
bigtable: support local emulator
1 parent cb71873 commit 1cc8dba

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

packages/bigtable/src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ var PKG = require('../package.json');
4949
* @resource [Google Cloud Bigtable Concepts Overview]{@link https://cloud.google.com/bigtable/docs/concepts}
5050
*
5151
* @param {object=} options - [Configuration object](#/docs).
52+
* @param {string=} options.apiEndpoint - Override the default API endpoint used
53+
* to reach Bigtable. This is useful for connecting to your local Bigtable
54+
* emulator.
5255
*
5356
* @example
5457
* //-
@@ -291,10 +294,20 @@ function Bigtable(options) {
291294

292295
var adminBaseUrl = 'bigtableadmin.googleapis.com';
293296

297+
var customEndpoint = is.defined(options.apiEndpoint) ||
298+
is.defined(process.env.BIGTABLE_EMULATOR_HOST);
299+
300+
var baseUrl = 'bigtable.googleapis.com';
301+
302+
if (customEndpoint) {
303+
baseUrl = options.apiEndpoint || process.env.BIGTABLE_EMULATOR_HOST;
304+
}
305+
294306
var config = {
295-
baseUrl: 'bigtable.googleapis.com',
307+
baseUrl: baseUrl,
296308
service: 'bigtable',
297309
apiVersion: 'v2',
310+
customEndpoint: customEndpoint,
298311
protoServices: {
299312
Bigtable: googleProtoFiles.bigtable.v2,
300313
BigtableTableAdmin: {

packages/bigtable/test/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('Bigtable', function() {
7373
});
7474

7575
beforeEach(function() {
76+
delete process.env.BIGTABLE_EMULATOR_HOST;
7677
bigtable = new Bigtable({ projectId: PROJECT_ID });
7778
});
7879

@@ -113,6 +114,7 @@ describe('Bigtable', function() {
113114
assert.strictEqual(calledWith.baseUrl, 'bigtable.googleapis.com');
114115
assert.strictEqual(calledWith.service, 'bigtable');
115116
assert.strictEqual(calledWith.apiVersion, 'v2');
117+
assert.strictEqual(calledWith.customEndpoint, false);
116118

117119
assert.deepEqual(calledWith.protoServices, {
118120
Bigtable: googleProtoFiles('bigtable/v2/bigtable.proto'),
@@ -146,6 +148,30 @@ describe('Bigtable', function() {
146148
assert.strictEqual(calledWith.userAgent, PKG.name + '/' + PKG.version);
147149
});
148150

151+
it('should work with the emulator', function() {
152+
var endpoint = 'http://emulator:8288';
153+
process.env.BIGTABLE_EMULATOR_HOST = endpoint;
154+
155+
var bigtable = new Bigtable({ projectId: PROJECT_ID });
156+
157+
var calledWith = bigtable.calledWith_[0];
158+
assert.strictEqual(calledWith.baseUrl, endpoint);
159+
assert.strictEqual(calledWith.customEndpoint, true);
160+
});
161+
162+
it('should work with a custom apiEndpoint', function() {
163+
var options = {
164+
projectId: PROJECT_ID,
165+
apiEndpoint: 'http://local:3888'
166+
};
167+
168+
var bigtable = new Bigtable(options);
169+
170+
var calledWith = bigtable.calledWith_[0];
171+
assert.strictEqual(calledWith.baseUrl, options.apiEndpoint);
172+
assert.strictEqual(calledWith.customEndpoint, true);
173+
});
174+
149175
it('should set the projectName', function() {
150176
assert.strictEqual(bigtable.projectName, 'projects/' + PROJECT_ID);
151177
});

0 commit comments

Comments
 (0)