Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: modernize the samples a bit #398

Merged
merged 1 commit into from
Jan 3, 2019
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
3 changes: 0 additions & 3 deletions samples/document-snippets/.eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
---
env:
mocha: true
rules:
node/no-unpublished-require: off
no-unused-vars: off
11 changes: 1 addition & 10 deletions samples/hello-world/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,19 @@ const TABLE_ID = 'Hello-Bigtable';
const COLUMN_FAMILY_ID = 'cf1';
const COLUMN_QUALIFIER = 'greeting';
const INSTANCE_ID = process.env.INSTANCE_ID;
const GCLOUD_PROJECT = process.env.GCLOUD_PROJECT;

if (!INSTANCE_ID) {
throw new Error('Environment variables for INSTANCE_ID must be set!');
}

if (!GCLOUD_PROJECT) {
throw new Error('Environment variables GCLOUD_PROJECT must be set!');
}

const bigtableOptions = {
projectId: GCLOUD_PROJECT,
};

const getRowGreeting = row => {
return row.data[COLUMN_FAMILY_ID][COLUMN_QUALIFIER][0].value;
};

(async () => {
try {
// [START connecting_to_bigtable]
const bigtableClient = new Bigtable(bigtableOptions);
const bigtableClient = new Bigtable();
const instance = bigtableClient.instance(INSTANCE_ID);
// [END connecting_to_bigtable]

Expand Down
115 changes: 0 additions & 115 deletions samples/hello-world/index.v6.js

This file was deleted.

12 changes: 0 additions & 12 deletions samples/hello-world/package.json

This file was deleted.

112 changes: 25 additions & 87 deletions samples/instances.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,14 @@

// Imports the Google Cloud client library
const Bigtable = require('@google-cloud/bigtable');
const GCLOUD_PROJECT = process.env.GCLOUD_PROJECT;

if (!GCLOUD_PROJECT) {
throw new Error('Environment variables GCLOUD_PROJECT must be set!');
}

const bigtableOptions = {
projectId: GCLOUD_PROJECT,
};

async function runInstanceOperations(instanceID, clusterID) {
const bigtable = Bigtable(bigtableOptions);
const bigtable = Bigtable();
const instance = bigtable.instance(instanceID);

console.log(`Check Instance Exists`);
// [START bigtable_check_instance_exists]
let instanceExists;
try {
[instanceExists] = await instance.exists();
} catch (err) {
console.error(`Error checking if Instance exists:`, err);
return;
}
const [instanceExists] = await instance.exists();
// [END bigtable_check_instance_exists]

// Create instance if does not exists
Expand All @@ -63,13 +48,8 @@ async function runInstanceOperations(instanceID, clusterID) {
};

// Create production instance with given options
try {
const [prodInstance] = await instance.create(instanceOptions);
console.log(`Created Instance: ${prodInstance.id}`);
} catch (err) {
console.error('Error creating prod-instance:', err);
return;
}
const [prodInstance] = await instance.create(instanceOptions);
console.log(`Created Instance: ${prodInstance.id}`);
// [END bigtable_create_prod_instance]
} else {
console.log(`Instance ${instance.id} exists`);
Expand All @@ -78,43 +58,28 @@ async function runInstanceOperations(instanceID, clusterID) {
console.log(); //for just a new-line
console.log(`Listing Instances:`);
// [START bigtable_list_instances]
try {
const [instances] = await bigtable.getInstances();
instances.forEach(instance => {
console.log(instance.id);
});
} catch (err) {
console.error('Error listing instances:', err);
return;
}
const [instances] = await bigtable.getInstances();
instances.forEach(instance => {
console.log(instance.id);
});
// [END bigtable_list_instances]

console.log(); //for just a new-line
console.log(`Get Instance`);
// [START bigtable_get_instance]
try {
const [instance] = await bigtable.instance(instanceID).get();
console.log(`Instance ID: ${instance.id}`);
console.log(`Instance Meta: ${JSON.stringify(instance.metadata)}`);
} catch (err) {
console.error('Error getting instance:', err);
return;
}
const [instances2] = await bigtable.instance(instanceID).get();
console.log(`Instance ID: ${instances2.id}`);
console.log(`Instance Meta: ${JSON.stringify(instances2.metadata)}`);
// [END bigtable_get_instance]

console.log(); //for just a new-line
console.log(`Listing Clusters...`);
// [START bigtable_get_clusters]
try {
const instance = bigtable.instance(instanceID);
const [clusters] = await instance.getClusters();
clusters.forEach(cluster => {
console.log(cluster.id);
});
} catch (err) {
console.error('Error creating cluster:', err);
return;
}
const instance3 = bigtable.instance(instanceID);
const [clusters] = await instance3.getClusters();
clusters.forEach(cluster => {
console.log(cluster.id);
});
// [END bigtable_get_clusters]
}

Expand All @@ -141,13 +106,8 @@ async function createDevInstance(instanceID, clusterID) {
};

// Create development instance with given options
try {
const [instance] = await bigtable.createInstance(instanceID, options);
console.log(`Created development instance: ${instance.id}`);
} catch (err) {
console.error('Error creating dev-instance:', err);
return;
}
const [instance] = await bigtable.createInstance(instanceID, options);
console.log(`Created development instance: ${instance.id}`);
// [END bigtable_create_dev_instance]
}

Expand All @@ -157,31 +117,19 @@ async function deleteInstance(instanceID) {
const bigtable = new Bigtable();
const instance = bigtable.instance(instanceID);

// [START bigtable_delete_instance]
console.log(); //for just a new-line
// [START bigtable_delete_instance]
console.log(`Deleting Instance`);
try {
await instance.delete();
console.log(`Instance deleted: ${instance.id}`);
} catch (err) {
console.error('Error deleting instance:', err);
}
await instance.delete();
console.log(`Instance deleted: ${instance.id}`);
// [END bigtable_delete_instance]
}

// Add Cluster
async function addCluster(instanceID, clusterID) {
const bigtable = new Bigtable();
const instance = bigtable.instance(instanceID);

let instanceExists;
try {
[instanceExists] = await instance.exists();
} catch (err) {
console.error(`Error checking if Instance exists:`, err);
return;
}

const [instanceExists] = await instance.exists();
if (!instanceExists) {
console.log(`Instance does not exists`);
} else {
Expand All @@ -194,13 +142,8 @@ async function addCluster(instanceID, clusterID) {
storage: 'ssd',
};

try {
const [cluster] = await instance.createCluster(clusterID, clusterOptions);
console.log(`Cluster created: ${cluster.id}`);
} catch (err) {
console.error('Error creating cluster:', err);
return;
}
const [cluster] = await instance.createCluster(clusterID, clusterOptions);
console.log(`Cluster created: ${cluster.id}`);
// [END bigtable_create_cluster]
}
}
Expand All @@ -214,12 +157,7 @@ async function deleteCluster(instanceID, clusterID) {
// [START bigtable_delete_cluster]
console.log(); //for just a new-line
console.log(`Deleting Cluster`);
try {
await cluster.delete();
} catch (err) {
console.error('Error deleting cluster:', err);
return;
}
await cluster.delete();
console.log(`Cluster deleted: ${cluster.id}`);
// [END bigtable_delete_cluster]
}
Expand Down
17 changes: 7 additions & 10 deletions samples/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
{
"name": "@google-cloud/bigtable-samples",
"version": "0.0.1",
"private": true,
"license": "Apache-2.0",
"author": "Google Inc.",
"repository": "googleapis/nodejs-bigtable",
"files": [
"*.js",
"hello-world/*.js",
"document-snippets/*.js"
],
"engines": {
"node": ">=8"
},
Expand All @@ -16,17 +20,10 @@
"devDependencies": {
"@google-cloud/nodejs-repo-tools": "^3.0.0",
"mocha": "^5.2.0",
"doctoc": "^1.3.1",
"proxyquire": "^2.0.0",
"sinon": "^7.0.0"
},
"nyc": {
"exclude": [
"**/*.test.js"
]
"doctoc": "^1.3.1"
},
"scripts": {
"doctoc": "doctoc README.md */README.md",
"test": "mocha system-test/*.test.js --timeout=600000"
"test": "mocha system-test --timeout=60000"

This comment was marked as spam.

}
}
Loading