Skip to content

Commit

Permalink
Bigtable: add Table.getReplicationStates (#279)
Browse files Browse the repository at this point in the history
* add getReplicationStates in table

* fix lint
  • Loading branch information
ajaaym authored and sduskis committed Sep 7, 2018
1 parent 6d500c1 commit d9eff41
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,56 @@ Please use the format 'prezzy' or '${instance.name}/tables/prezzy'.`
});
}

/**
* Get replication states of the clusters for this table.
*
* @param {object} [gaxOptions] Request configuration options, outlined here:
* https://googleapis.github.io/gax-nodejs/CallSettings.html.
* @param {function} callback The callback function.
* @param {?error} callback.err An error returned while making this request.
* @param {Family[]} callback.clusterStates The map of clusterId and its replication state.
* @param {object} callback.apiResponse The full API response.
*
* @example
* const Bigtable = require('@google-cloud/bigtable');
* const bigtable = new Bigtable();
* const instance = bigtable.instance('my-instance');
* const table = instance.table('prezzy');
*
* table.getReplicationStates(function(err, clusterStates, apiResponse) {
* // `clusterStates` is an map of clusterId and its replication state.
* });
*
* //-
* // If the callback is omitted, we'll return a Promise.
* //-
* table.getReplicationStates().then(function(data) {
* var clusterStates = data[0];
* var apiResponse = data[1];
* });
*/
getReplicationStates(gaxOptions, callback) {
if (is.fn(gaxOptions)) {
callback = gaxOptions;
gaxOptions = {};
}
const reqOpts = {
view: 'replication',
gaxOptions: gaxOptions,
};
this.getMetadata(reqOpts, (err, metadata) => {
if (err) {
callback(err);
return;
}
const clusterStates = new Map();
Object.keys(metadata.clusterStates).map(clusterId =>
clusterStates.set(clusterId, metadata.clusterStates[clusterId])
);
callback(null, clusterStates, metadata);
});
}

/**
* Get the table's metadata.
*
Expand Down Expand Up @@ -1506,6 +1556,7 @@ Table.VIEWS = {
unspecified: 0,
name: 1,
schema: 2,
replication: 3,
full: 4,
};

Expand Down
10 changes: 10 additions & 0 deletions system-test/bigtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,16 @@ describe('Bigtable', function() {
});
});

describe('replication states', function() {
it('should get a map of clusterId and state', function(done) {
TABLE.getReplicationStates(function(err, clusterStates) {
assert(clusterStates instanceof Map);
assert(clusterStates.has(CLUSTER_ID));
done();
});
});
});

describe('column families', function() {
var FAMILY_ID = 'presidents';
var FAMILY;
Expand Down
52 changes: 52 additions & 0 deletions test/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ describe('Bigtable/Table', function() {
unspecified: 0,
name: 1,
schema: 2,
replication: 3,
full: 4,
};

Expand Down Expand Up @@ -1334,6 +1335,57 @@ describe('Bigtable/Table', function() {
});
});

describe('getReplicationStates', function() {
it('should accept gaxOptions', function(done) {
let gaxOptions = {};

table.getMetadata = function(options) {
assert.strictEqual(options.gaxOptions, gaxOptions);
done();
};

table.getReplicationStates(gaxOptions, assert.ifError);
});

it('should return an error to the callback', function(done) {
let error = new Error('err');
let response = {};

table.getMetadata = function(options, callback) {
callback(error, response);
};

table.getReplicationStates(function(err) {
assert.strictEqual(err, error);
done();
});
});

it('should return a map of cluster states', function(done) {
let response = {
clusterStates: {
cluster1: 'READY',
cluster2: 'INITIALIZING',
},
};

table.getMetadata = function(options, callback) {
callback(null, response);
};

table.getReplicationStates(function(err, clusterStates) {
assert.ifError(err);

assert(clusterStates instanceof Map);
assert.strictEqual(clusterStates.size, 2);
assert.strictEqual(clusterStates.get('cluster1'), 'READY');
assert.strictEqual(clusterStates.get('cluster2'), 'INITIALIZING');

done();
});
});
});

describe('getFamilies', function() {
it('should accept gaxOptions', function(done) {
let gaxOptions = {};
Expand Down

0 comments on commit d9eff41

Please sign in to comment.