Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion packages/bigtable/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
* //-
* // <h3>The Bigtable Emulator</h3>
* //
* // Make sure you have the
* // [gcloud SDK installed](https://cloud.google.com/sdk/downloads), then run:
* //
* // <pre>
* // $ gcloud beta emulators bigtable start
* // </pre>
* //
* // Before running your Node.js app, set the environment variables that this
* // library will look for to connect to the emulator:
* //
* // <pre>
* // $ $(gcloud beta emulators bigtable env-init)
* // </pre>
* //-
*
* //-
* // <h3>Creating a Compute Instance</h3>
* //
* // Before you create your table, you first need to create a Compute Instance
Expand Down Expand Up @@ -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: {
Expand Down
45 changes: 45 additions & 0 deletions packages/bigtable/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -72,6 +73,7 @@ describe('Bigtable', function() {
});

beforeEach(function() {
delete process.env.BIGTABLE_EMULATOR_HOST;
bigtable = new Bigtable({ projectId: PROJECT_ID });
});

Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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);
});
Expand Down