diff --git a/packages/bigtable/src/index.js b/packages/bigtable/src/index.js
index 8bde933bbaf..f922ef3726b 100644
--- a/packages/bigtable/src/index.js
+++ b/packages/bigtable/src/index.js
@@ -47,9 +47,30 @@ var Instance = require('./instance.js');
* @resource [Google Cloud Bigtable Concepts Overview]{@link https://cloud.google.com/bigtable/docs/concepts}
*
* @param {object=} options - [Configuration object](#/docs).
+ * @param {string=} options.apiEndpoint - Override the default API endpoint used
+ * to reach Bigtable. This is useful for connecting to your local Bigtable
+ * emulator.
*
* @example
* //-
+ * //
The Bigtable Emulator
+ * //
+ * // Make sure you have the
+ * // [gcloud SDK installed](https://cloud.google.com/sdk/downloads), then run:
+ * //
+ * //
+ * // $ gcloud beta emulators bigtable start
+ * //
+ * //
+ * // Before running your Node.js app, set the environment variables that this
+ * // library will look for to connect to the emulator:
+ * //
+ * //
+ * // $ $(gcloud beta emulators bigtable env-init)
+ * //
+ * //-
+ *
+ * //-
* // Creating a Compute Instance
* //
* // Before you create your table, you first need to create a Compute Instance
@@ -287,12 +308,22 @@ function Bigtable(options) {
return new Bigtable(options);
}
+ var baseUrl = 'bigtable.googleapis.com';
var adminBaseUrl = 'bigtableadmin.googleapis.com';
+ var customEndpoint = options.apiEndpoint ||
+ process.env.BIGTABLE_EMULATOR_HOST;
+
+ if (customEndpoint) {
+ baseUrl = customEndpoint;
+ adminBaseUrl = baseUrl;
+ }
+
var config = {
- baseUrl: 'bigtable.googleapis.com',
+ baseUrl: baseUrl,
service: 'bigtable',
apiVersion: 'v2',
+ customEndpoint: !!customEndpoint,
protoServices: {
Bigtable: googleProtoFiles.bigtable.v2,
BigtableTableAdmin: {
diff --git a/packages/bigtable/test/index.js b/packages/bigtable/test/index.js
index b548e7ce9f3..8b66c51b658 100644
--- a/packages/bigtable/test/index.js
+++ b/packages/bigtable/test/index.js
@@ -19,6 +19,7 @@
var assert = require('assert');
var extend = require('extend');
var googleProtoFiles = require('google-proto-files');
+var is = require('is');
var nodeutil = require('util');
var proxyquire = require('proxyquire');
var sinon = require('sinon').sandbox.create();
@@ -72,6 +73,7 @@ describe('Bigtable', function() {
});
beforeEach(function() {
+ delete process.env.BIGTABLE_EMULATOR_HOST;
bigtable = new Bigtable({ projectId: PROJECT_ID });
});
@@ -112,6 +114,7 @@ describe('Bigtable', function() {
assert.strictEqual(calledWith.baseUrl, 'bigtable.googleapis.com');
assert.strictEqual(calledWith.service, 'bigtable');
assert.strictEqual(calledWith.apiVersion, 'v2');
+ assert.strictEqual(calledWith.customEndpoint, false);
assert.deepEqual(calledWith.protoServices, {
Bigtable: googleProtoFiles('bigtable/v2/bigtable.proto'),
@@ -145,6 +148,48 @@ describe('Bigtable', function() {
assert.deepEqual(calledWith.packageJson, require('../package.json'));
});
+ it('should work with the emulator', function() {
+ var endpoint = 'http://emulator:8288';
+ process.env.BIGTABLE_EMULATOR_HOST = endpoint;
+
+ var bigtable = new Bigtable({ projectId: PROJECT_ID });
+
+ var calledWith = bigtable.calledWith_[0];
+ assert.strictEqual(calledWith.baseUrl, endpoint);
+ assert.strictEqual(calledWith.customEndpoint, true);
+
+ Object.keys(calledWith.protoServices).forEach(function(service) {
+ service = calledWith.protoServices[service];
+
+ if (is.object(service)) {
+ assert.strictEqual(service.baseUrl, endpoint);
+ }
+ });
+
+ delete process.env.BIGTABLE_EMULATOR_HOST;
+ });
+
+ it('should work with a custom apiEndpoint', function() {
+ var options = {
+ projectId: PROJECT_ID,
+ apiEndpoint: 'http://local:3888'
+ };
+
+ var bigtable = new Bigtable(options);
+
+ var calledWith = bigtable.calledWith_[0];
+ assert.strictEqual(calledWith.baseUrl, options.apiEndpoint);
+ assert.strictEqual(calledWith.customEndpoint, true);
+
+ Object.keys(calledWith.protoServices).forEach(function(service) {
+ service = calledWith.protoServices[service];
+
+ if (is.object(service)) {
+ assert.strictEqual(service.baseUrl, options.apiEndpoint);
+ }
+ });
+ });
+
it('should set the projectName', function() {
assert.strictEqual(bigtable.projectName, 'projects/' + PROJECT_ID);
});