diff --git a/samples/document-snippets/cluster.js b/samples/document-snippets/cluster.js new file mode 100644 index 000000000..2975390ad --- /dev/null +++ b/samples/document-snippets/cluster.js @@ -0,0 +1,135 @@ +/** + * Copyright 2018, Google, Inc. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +const snippets = { + create: (instanceId, clusterId) => { + // [START bigtable_create_cluster] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const cluster = instance.cluster(clusterId); + + cluster + .create() + .then(result => { + const cluster = result[0]; + const operation = result[1]; + const apiResponse = result[2]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_create_cluster] + }, + + delete: (instanceId, clusterId) => { + // [START bigtable_delete_cluster] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const cluster = instance.cluster(clusterId); + + cluster + .delete() + .then(result => { + const apiResponse = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_delete_cluster] + }, + + exists: (instanceId, clusterId) => { + // [START bigtable_exists_cluster] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const cluster = instance.cluster(clusterId); + + cluster + .exists() + .then(result => { + const exists = result[0]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_exists_cluster] + }, + + get: (instanceId, clusterId) => { + // [START bigtable_get_cluster] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const cluster = instance.cluster(clusterId); + + cluster + .get() + .then(result => { + const cluster = result[0]; + const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_get_cluster] + }, + + getMeta: (instanceId, clusterId) => { + // [START bigtable_cluster_get_meta] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const cluster = instance.cluster(clusterId); + + cluster + .getMetadata() + .then(result => { + const metadata = result[0]; + const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_cluster_get_meta] + }, + + setMeta: (instanceId, clusterId) => { + // [START bigtable_cluster_set_meta] + const Bigtable = require('@google-cloud/bigtable'); + const bigtable = new Bigtable(); + const instance = bigtable.instance(instanceId); + const cluster = instance.cluster(clusterId); + + const metadata = { + nodes: 4, + }; + + cluster + .setMetadata(metadata) + .then(result => { + const operation = result[0]; + const apiResponse = result[1]; + }) + .catch(err => { + // Handle the error. + }); + // [END bigtable_cluster_set_meta] + }, +}; + +module.exports = snippets; diff --git a/samples/document-snippets/tests/cluster.js b/samples/document-snippets/tests/cluster.js new file mode 100644 index 000000000..efa2f406f --- /dev/null +++ b/samples/document-snippets/tests/cluster.js @@ -0,0 +1,76 @@ +/** + * Copyright 2018 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; +const uuid = require(`uuid`); + +const Bigtable = require(`@google-cloud/bigtable`); +const bigtable = new Bigtable(); + +const INSTANCE_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules +const CLUSTER_ID = `nodejs-bigtable-samples-${uuid.v4()}`.substr(0, 30); // Bigtable naming rules + +const clusterSnippets = require('../cluster.js'); + +const instance = bigtable.instance(INSTANCE_ID); + +describe('Cluster Snippets', function() { + before(() => { + instance.create({ + clusters: [ + { + name: CLUSTER_ID, + location: 'us-central1-f', + storage: 'hdd', + }, + ], + type: 'DEVELOPMENT', + }); + }); + + after(() => { + instance.exists().then(result => { + const exists = result[0]; + if (exists) { + instance.delete(); + } + }); + }); + + it('should create a cluster', () => { + clusterSnippets.create(INSTANCE_ID, CLUSTER_ID); + }); + + it('should check cluster exists', () => { + clusterSnippets.exists(INSTANCE_ID, CLUSTER_ID); + }); + + it('should get the cluster', () => { + clusterSnippets.get(INSTANCE_ID, CLUSTER_ID); + }); + + it('should get cluster metadata', () => { + clusterSnippets.getMeta(INSTANCE_ID, CLUSTER_ID); + }); + + it('should set cluster metadata', () => { + clusterSnippets.setMeta(INSTANCE_ID, CLUSTER_ID); + }); + + it('should delete a cluster', () => { + clusterSnippets.delete(INSTANCE_ID, CLUSTER_ID); + }); +}); diff --git a/src/cluster.js b/src/cluster.js index bfc6d7d2a..18093f4d8 100644 --- a/src/cluster.js +++ b/src/cluster.js @@ -114,32 +114,8 @@ Please use the format 'my-cluster' or '${instance.name}/clusters/my-cluster'.` * request. * @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 cluster = instance.cluster('my-cluster'); - * - * cluster.create(function(err, cluster, operation, apiResponse) { - * if (err) { - * // Error handling omitted. - * } - * - * operation - * .on('error', console.error) - * .on('complete', function() { - * // The cluster was created successfully. - * }); - * }); - * - * //- - * // If the callback is omitted, we'll return a Promise. - * //- - * cluster.create().then(function(data) { - * const cluster = data[0]; - * const operation = data[1]; - * const apiResponse = data[2]; - * }); + * @example