From 507a416f809216a986fdae73b623774ea13adb5a Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Tue, 6 Sep 2016 09:52:10 -0400 Subject: [PATCH 1/3] bigtable: support local emulator --- packages/bigtable/src/index.js | 15 ++++++++++++++- packages/bigtable/test/index.js | 26 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/packages/bigtable/src/index.js b/packages/bigtable/src/index.js index 8bde933bbaf..c152d78f76d 100644 --- a/packages/bigtable/src/index.js +++ b/packages/bigtable/src/index.js @@ -47,6 +47,9 @@ 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 * //- @@ -289,10 +292,20 @@ function Bigtable(options) { var adminBaseUrl = 'bigtableadmin.googleapis.com'; + var customEndpoint = is.defined(options.apiEndpoint) || + is.defined(process.env.BIGTABLE_EMULATOR_HOST); + + var baseUrl = 'bigtable.googleapis.com'; + + if (customEndpoint) { + baseUrl = options.apiEndpoint || process.env.BIGTABLE_EMULATOR_HOST; + } + 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..cbf058d5ce3 100644 --- a/packages/bigtable/test/index.js +++ b/packages/bigtable/test/index.js @@ -72,6 +72,7 @@ describe('Bigtable', function() { }); beforeEach(function() { + delete process.env.BIGTABLE_EMULATOR_HOST; bigtable = new Bigtable({ projectId: PROJECT_ID }); }); @@ -112,6 +113,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 +147,30 @@ 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); + }); + + 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); + }); + it('should set the projectName', function() { assert.strictEqual(bigtable.projectName, 'projects/' + PROJECT_ID); }); From 3e9f70d0fe4ed5609cc04b4ec8432d8b5a0cdcd9 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Mon, 19 Sep 2016 13:49:24 -0400 Subject: [PATCH 2/3] updated admin base url with custom endpoint for emulator use --- packages/bigtable/src/index.js | 12 ++++++------ packages/bigtable/test/index.js | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/packages/bigtable/src/index.js b/packages/bigtable/src/index.js index c152d78f76d..108878307b2 100644 --- a/packages/bigtable/src/index.js +++ b/packages/bigtable/src/index.js @@ -290,22 +290,22 @@ function Bigtable(options) { return new Bigtable(options); } + var baseUrl = 'bigtable.googleapis.com'; var adminBaseUrl = 'bigtableadmin.googleapis.com'; - var customEndpoint = is.defined(options.apiEndpoint) || - is.defined(process.env.BIGTABLE_EMULATOR_HOST); - - var baseUrl = 'bigtable.googleapis.com'; + var customEndpoint = options.apiEndpoint || + process.env.BIGTABLE_EMULATOR_HOST; if (customEndpoint) { - baseUrl = options.apiEndpoint || process.env.BIGTABLE_EMULATOR_HOST; + baseUrl = customEndpoint; + adminBaseUrl = baseUrl; } var config = { baseUrl: baseUrl, service: 'bigtable', apiVersion: 'v2', - customEndpoint: customEndpoint, + customEndpoint: !!customEndpoint, protoServices: { Bigtable: googleProtoFiles.bigtable.v2, BigtableTableAdmin: { diff --git a/packages/bigtable/test/index.js b/packages/bigtable/test/index.js index cbf058d5ce3..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(); @@ -156,6 +157,16 @@ describe('Bigtable', function() { 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() { @@ -169,6 +180,14 @@ describe('Bigtable', function() { 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() { From d4f3d58ef9a3827e44db57074402b07806f4dacc Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Tue, 4 Oct 2016 10:21:47 -0400 Subject: [PATCH 3/3] add docs --- packages/bigtable/src/index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/bigtable/src/index.js b/packages/bigtable/src/index.js index 108878307b2..f922ef3726b 100644 --- a/packages/bigtable/src/index.js +++ b/packages/bigtable/src/index.js @@ -53,6 +53,24 @@ var Instance = require('./instance.js'); * * @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