From add9bf66d33b04046dbd83cdbae1cfc53f7e6285 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Wed, 20 Jan 2016 11:18:29 -0800 Subject: [PATCH 1/4] Added some comments. --- logging/export.js | 13 +++++++++++-- logging/list.js | 8 +++++++- logging/write.js | 25 ++++++++++++++++++++++--- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/logging/export.js b/logging/export.js index 142823e049..f12149b181 100644 --- a/logging/export.js +++ b/logging/export.js @@ -14,32 +14,38 @@ 'use strict'; // [START setup] +// You must set these environment variables to run this sample var projectId = process.env.TEST_PROJECT_ID; var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; +// If you don't set the environment variables, then you can modify this file +// to set the values projectId = projectId || ''; keyFilename = keyFilename || '/path/to/keyfile.json'; +// Provide projectId and authentication to gcloud var gcloud = require('gcloud')({ projectId: projectId, keyFilename: keyFilename }); +// Get a reference to the logging component var logging = gcloud.logging(); // [END setup] // [START listSinks] function listSinks(callback) { - // list all sinks in the project + // list all sinks in the authenticated project logging.getSinks(callback); } // [END listSinks] // [START createSink] function createSink(callback) { + // Get a reference to the Cloud Storage component var gcs = gcloud.storage(); - // create a new sink + // create a new sink in the authenticated project // // This method only works if you are authenticated as yourself, e.g. using the // gcloud SDK. @@ -51,7 +57,9 @@ function createSink(callback) { // [START updateSink] function updateSink(callback) { + // Get a reference to the Cloud Storage component var gcs = gcloud.storage(); + // Get a reference to an existing sink var sink = logging.sink('mySink'); // update a sink @@ -67,6 +75,7 @@ function updateSink(callback) { // [START deleteSink] function deleteSink(callback) { + // Get a reference to an existing sink var sink = logging.sink('mySink'); // delete a sink diff --git a/logging/list.js b/logging/list.js index 2f4d25cd59..2360b0b6fe 100644 --- a/logging/list.js +++ b/logging/list.js @@ -15,13 +15,17 @@ // [START list] // [START auth] +// You must set these environment variables to run this sample var projectId = process.env.TEST_PROJECT_ID; var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; +// If you don't set the environment variables, then you can modify this file +// to set the values projectId = projectId || ''; keyFilename = keyFilename || '/path/to/keyfile.json'; // [START require] +// Provide projectId and authentication to gcloud var gcloud = require('gcloud')({ projectId: projectId, keyFilename: keyFilename @@ -29,10 +33,11 @@ var gcloud = require('gcloud')({ // [END require] // [END auth] +// Get a reference to the logging component var logging = gcloud.logging(); function list(callback) { - // Retrieve 3 log entries. + // Retrieve the latest 3 log entries from the authenticated project. logging.getEntries({ pageSize: 3 }, callback); @@ -42,6 +47,7 @@ function list(callback) { exports.list = list; if (module === require.main) { + console.log('retrieving latest 3 log entries...'); list(function (err, apiResponse) { console.log(err, 'apiResponse:', apiResponse); }); diff --git a/logging/write.js b/logging/write.js index 3c36693f42..f6a87fcd0d 100644 --- a/logging/write.js +++ b/logging/write.js @@ -16,21 +16,27 @@ // [START write] // [START setup] +// You must set these environment variables to run this sample var projectId = process.env.TEST_PROJECT_ID; var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; +// If you don't set the environment variables, then you can modify this file +// to set the values projectId = projectId || ''; keyFilename = keyFilename || '/path/to/keyfile.json'; +// Provide projectId and authentication to gcloud var gcloud = require('gcloud')({ projectId: projectId, keyFilename: keyFilename }); +// Get a reference to the logging component var logging = gcloud.logging(); // [END setup] function write(callback) { + // Get a reference to an existing log var log = logging.log('myLog'); // Modify this resource type to match a resource in your project @@ -38,22 +44,26 @@ function write(callback) { // /v2beta1/monitoredResourceDescriptors/list var resource = { type: 'gae_app', + // This example targets a "App Engine" resource in the default module with + // a version_id of "express" labels: { module_id: 'default', version_id: 'express' } }; + // Create a log entry attached to the specified resource var entry = log.entry(resource, { foo: 'bar' }); + // Create a second log entry attached to the specified resource var secondEntry = log.entry(resource, { beep: 'boop' }); - // You can log multiple entries one at a a time, but it is best to write - // multiple entires together in a batch. + // Save the two log entries. You can log multiple entries one at a a time, but + // it is best to write multiple entires together in a batch. log.write([ entry, secondEntry @@ -63,9 +73,10 @@ function write(callback) { // [START deleteLog] function deleteLog(callback) { + // Get a reference to an existing log var log = logging.log('myLog'); - // Delete the logs + // Delete the log log.delete(callback); } // [END deleteLog] @@ -74,13 +85,21 @@ exports.write = write; exports.deleteLog = deleteLog; if (module === require.main) { + console.log('writing 2 log entries...'); write(function (err, apiResponse) { console.log(err, 'apiResponse:', apiResponse); if (err) { return; } + console.log('success!'); + console.log('deleting the log entries...'); + // If you remove this code, then you can find the two log entries that + // were written in the log view in the cloud console. deleteLog(function (err, apiResponse) { console.log(err, 'apiResponse:', apiResponse); + if (!err) { + console.log('success!'); + } }); }); } From 1c67404955d766accba1e5f00ca8cdc7106630e0 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Fri, 22 Jan 2016 12:27:01 -0800 Subject: [PATCH 2/4] Added Pub/Sub samples. --- pubsub/README.md | 25 ++++ pubsub/iam.js | 183 +++++++++++++++++++++++ pubsub/package.json | 17 +++ pubsub/subscription.js | 246 +++++++++++++++++++++++++++++++ test/pubsub/iam.test.js | 67 +++++++++ test/pubsub/subscription.test.js | 56 +++++++ 6 files changed, 594 insertions(+) create mode 100644 pubsub/README.md create mode 100644 pubsub/iam.js create mode 100644 pubsub/package.json create mode 100644 pubsub/subscription.js create mode 100644 test/pubsub/iam.test.js create mode 100644 test/pubsub/subscription.test.js diff --git a/pubsub/README.md b/pubsub/README.md new file mode 100644 index 0000000000..193d9bbcfa --- /dev/null +++ b/pubsub/README.md @@ -0,0 +1,25 @@ +## Pub/Sub Samples + +These samples require two environment variables to be set: + +- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can +download one from your Google project's "permissions" page. +- `TEST_PROJECT_ID` - Id of your Google project. + +## Run a sample + +Install dependencies: + + npm install + +To print available commands: + + npm run + +Execute a sample: + + npm run + +Example: + + npm run subscription diff --git a/pubsub/iam.js b/pubsub/iam.js new file mode 100644 index 0000000000..2ab999f3a1 --- /dev/null +++ b/pubsub/iam.js @@ -0,0 +1,183 @@ +// Copyright 2016, 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. + +'use strict'; + +var async = require('async'); +var utils = require('./subscription'); +var createTopic = utils.createTopic; +var subscribe = utils.subscribe; + +// [START get_topic_policy] +function getTopicPolicy(topic, callback) { + // Retrieve the IAM policy for the provided topic + topic.iam.getPolicy(callback); +} +// [END get_topic_policy] + +// [START get_subscription_policy] +function getSubscriptionPolicy(subscription, callback) { + // Retrieve the IAM policy for the provided subscription + subscription.iam.getPolicy(callback); +} +// [END get_subscription_policy] + +// [START set_topic_policy] +function setTopicPolicy(topic, callback) { + // Policy update + var myPolicy = { + bindings: [ + { + role: 'roles/pubsub.subscriber', + members: ['serviceAccount:myotherproject@appspot.gserviceaccount.com'] + } + ] + }; + + // Retrieve the IAM policy for the provided topic + topic.iam.setPolicy(myPolicy, callback); +} +// [END set_topic_policy] + +// [START set_subscription_policy] +function setSubscriptionPolicy(subscription, callback) { + // Policy update + var myPolicy = { + bindings: [ + { + role: 'roles/pubsub.subscriber', + members: ['serviceAccount:myotherproject@appspot.gserviceaccount.com'] + } + ] + }; + + // Retrieve the IAM policy for the provided subscription + subscription.iam.setPolicy(myPolicy, callback); +} +// [END set_subscription_policy] + +// [START test_topic_permissions] +function testTopicPermissions(topic, callback) { + var tests = [ + 'pubsub.topics.attachSubscription', + 'pubsub.topics.publish', + 'pubsub.topics.update' + ]; + + // Retrieve the IAM policy for the provided topic + topic.iam.testPermissions(tests, callback); +} +// [END test_topic_permissions] + +// [START test_subscription_permissions] +function testSubscriptionPermissions(subscription, callback) { + var tests = [ + 'pubsub.subscriptions.consume', + 'pubsub.subscriptions.update' + ]; + + // Retrieve the IAM policy for the provided subscription + subscription.iam.testPermissions(tests, callback); +} +// [END test_subscription_permissions] + +exports.getTopicPolicy = getTopicPolicy; +exports.getSubscriptionPolicy = getSubscriptionPolicy; +exports.setTopicPolicy = setTopicPolicy; +exports.setSubscriptionPolicy = setSubscriptionPolicy; +exports.testTopicPermissions = testTopicPermissions; +exports.testSubscriptionPermissions = testSubscriptionPermissions; +exports.runSample = runSample; + +function runSample(callback) { + var _subscription; + var _topic; + // Gather responses + var responses = []; + async.waterfall([ + function (cb) { + console.log('create topic...'); + createTopic(cb); + }, + function (topic, apiResponse, cb) { + _topic = topic; + responses.push([topic, apiResponse]); + console.log('created topic'); + console.log('get topic IAM policy...'); + getTopicPolicy(topic, cb); + }, + function (policy, apiResponse, cb) { + responses.push([policy, apiResponse]); + console.log('got topic policy', policy); + // IAM server is down? + // console.log('updating topic policy...'); + // setTopicPolicy(_topic, cb); + // }, + // function (policy, apiResponse, cb) { + // responses.push([policy, apiResponse]); + // console.log('updated topic policy', policy); + console.log('testing topic permissions...'); + testTopicPermissions(_topic, cb); + }, + function (permissions, apiResponse, cb) { + responses.push([permissions, apiResponse]); + console.log('tested topic permissions', permissions); + console.log('create subscription...'); + subscribe(cb); + }, + function (subscription, apiResponse, cb) { + _subscription = subscription; + responses.push([subscription, apiResponse]); + console.log('created subscription'); + console.log('get subscription IAM policy...'); + getSubscriptionPolicy(subscription, cb); + }, + function (policy, apiResponse, cb) { + responses.push([policy, apiResponse]); + console.log('got subscription policy', policy); + // IAM server is down? + // console.log('updating subscription policy...'); + // setSubscriptionPolicy(_subscription, cb); + // }, + // function (policy, apiResponse, cb) { + // responses.push([policy, apiResponse]); + // console.log('updated subscription policy', policy); + console.log('testing subscription permissions...'); + testSubscriptionPermissions(_subscription, cb); + }, + function (permissions, apiResponse, cb) { + responses.push([permissions, apiResponse]); + console.log('tested subscription permissions', permissions); + console.log('deleting subscription...'); + _subscription.delete(cb); + }, + function (apiResponse, cb) { + console.log('deleted subscription'); + console.log('deleting topic...'); + _topic.delete(cb); + } + ], function (err) { + if (err) { + console.error(err); + } else { + console.log('deleted topic'); + } + if (typeof callback === 'function') { + callback(err, responses); + } + }); +} + +if (module === require.main) { + runSample(); +} diff --git a/pubsub/package.json b/pubsub/package.json new file mode 100644 index 0000000000..564b9a5d29 --- /dev/null +++ b/pubsub/package.json @@ -0,0 +1,17 @@ +{ + "name": "nodejs-docs-samples-pubsub", + "description": "Node.js samples for Google Cloud Pub/Sub.", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "engines": { + "node": ">=0.10.x" + }, + "scripts": { + "iam": "node iam.js", + "subscription": "node subscription.js" + }, + "dependencies": { + "gcloud": "^0.27.0" + } +} diff --git a/pubsub/subscription.js b/pubsub/subscription.js new file mode 100644 index 0000000000..83e77853d3 --- /dev/null +++ b/pubsub/subscription.js @@ -0,0 +1,246 @@ +// Copyright 2016, 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. + +'use strict'; + +var async = require('async'); + +// [START auth] +// You must set these environment variables to run this sample +var projectId = process.env.TEST_PROJECT_ID; +var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; + +// If you don't set the environment variables, then you can modify this file +// to set the values +projectId = projectId || ''; +keyFilename = keyFilename || '/path/to/keyfile.json'; + +// [START require] +// Provide projectId and authentication to gcloud +var gcloud = require('gcloud')({ + projectId: projectId, + keyFilename: keyFilename +}); + +// Get a reference to the pubsub component +var pubsub = gcloud.pubsub(); +// [END auth] + +// [START create_topic] +function createTopic(callback) { + var topicName = 'messageCenter'; + + var topic = pubsub.topic(topicName); + + // Get the topic if it exists. Create it if it does not exist. + topic.get({ + autoCreate: true + }, callback); +} +// [END create_topic] + +// [START publish] +function publish(callback) { + var topicName = 'messageCenter'; + + // Grab a reference to an existing topic + var topic = pubsub.topic(topicName); + + // Publish a message to the topic + topic.publish({ + data: 'Hello, world!' + }, callback); +} +// [END publish] + +// [START list_topics] +function getAllTopics(callback) { + // Grab paginated topics + pubsub.getTopics(function (err, topics, nextQuery) { + // Quit on error + if (err) { + return callback(err); + } + // There is another page of topics + if (nextQuery) { + // Grab the remaining pages of topics recursively + return getAllTopics(function (err, _topics) { + if (_topics) { + topics = topics.concat(_topics); + } + callback(err, topics); + }); + } + // Last page of topics + return callback(err, topics); + }); +} +// [END list_topics] + +// [START get_all_subscriptions] +function getAllSubscriptions(callback) { + // Grab paginated subscriptions + pubsub.getSubscriptions(function (err, subscriptions, nextQuery) { + // Quit on error + if (err) { + return callback(err); + } + // There is another page of subscriptions + if (nextQuery) { + // Grab the remaining pages of subscriptions recursively + return getAllSubscriptions(function (err, _subscriptions) { + if (_subscriptions) { + subscriptions = subscriptions.concat(_subscriptions); + } + callback(err, subscriptions); + }); + } + // Last page of subscriptions + return callback(err, subscriptions); + }); +} +// [END get_all_subscriptions] + +// [START create_subscription] +function subscribe(callback) { + var topicName = 'messageCenter'; + var subscriptionName = 'newMessages'; + + var options = { + reuseExisting: true + }; + pubsub.subscribe(topicName, subscriptionName, options, callback); +} +// [END create_subscription] + +// [START pull_messages] +function pullMessages(callback) { + // Create a topic + createTopic(function (err) { + if (err) { + return callback(err); + } + // Create a subscription to the topic + subscribe(function (err, subscription) { + if (err) { + return callback(err); + } + var options = { + // Limit the amount of messages pulled. + maxResults: 100, + // If set, the system will respond immediately. Otherwise, wait until + // new messages are available. Returns if timeout is reached. + returnImmediately: false + }; + // Pull any messages on the subscription + subscription.pull(options, function (err, messages) { + if (err) { + return callback(err); + } + + // Do something with messages here? + + // Acknowledge messages + subscription.ack(messages.map(function (message) { + return message.ackId; + }), function (err) { + if (err) { + return callback(err); + } + callback(null, messages); + }); + }); + }); + }); +} +// [END pull_messages] + +exports.createTopic = createTopic; +exports.publish = publish; +exports.getAllTopics = getAllTopics; +exports.getAllSubscriptions = getAllSubscriptions; +exports.pullMessages = pullMessages; +exports.subscribe = subscribe; +exports.pubsub = pubsub; +exports.runSample = runSample; + +function runSample(callback) { + var _subscription; + var _topic; + // Gather responses + var responses = []; + async.waterfall([ + function (cb) { + console.log('create topic...'); + createTopic(cb); + }, + function (topic, apiResponse, cb) { + _topic = topic; + responses.push([topic, apiResponse]); + console.log('created topic'); + console.log('create subscription...'); + subscribe(cb); + }, + function (subscription, apiResponse, cb) { + _subscription = subscription; + responses.push([subscription, apiResponse]); + console.log('created subscription'); + console.log('list all topics...'); + getAllTopics(cb); + }, + function (topics, cb) { + responses.push([topics]); + console.log('got all topics'); + console.log('list all subscriptions...'); + getAllSubscriptions(cb); + }, + function (subscriptions, cb) { + responses.push([subscriptions]); + console.log('got all subscriptions'); + console.log('publishing a message...'); + publish(cb); + }, + function (messageIds, apiResponse, cb) { + responses.push([messageIds, apiResponse]); + console.log('published message'); + console.log('pulling messages...'); + pullMessages(cb); + }, + function (messages, cb) { + responses.push([messages]); + console.log('got messages', messages.map(function (message) { + return message.data; + })); + console.log('deleting subscription...'); + _subscription.delete(cb); + }, + function (apiResponse, cb) { + console.log('deleted subscription'); + console.log('deleting topic...'); + _topic.delete(cb); + } + ], function (err) { + if (err) { + console.error(err); + } else { + console.log('deleted topic'); + } + if (typeof callback === 'function') { + callback(err, responses); + } + }); +} + +if (module === require.main) { + runSample(); +} diff --git a/test/pubsub/iam.test.js b/test/pubsub/iam.test.js new file mode 100644 index 0000000000..89467d2615 --- /dev/null +++ b/test/pubsub/iam.test.js @@ -0,0 +1,67 @@ +// Copyright 2016, 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. + +'use strict'; + +var assert = require('assert'); +var projectId = process.env.TEST_PROJECT_ID; + +var iamSample = require('../../pubsub/iam'); + +describe('pubsub/iam', function () { + it('should run the sample', function (done) { + this.timeout(30000); + iamSample.runSample(function (err, responses) { + try { + assert.ok(err === null); + // topic + var expectedTopic = 'projects/' + projectId + '/topics/messageCenter'; + assert.equal(responses[0][0].name, expectedTopic); + assert.ok(responses[0][0].iam); + // apiResponse + assert.ok(responses[0][1]); + // policy + assert.deepEqual(responses[1][0], { etag: 'ACAB' }); + // apiResponse + assert.ok(responses[1][1]); + // permissions + assert.deepEqual(responses[2][0], { + 'pubsub.topics.attachSubscription': true, + 'pubsub.topics.publish': true, + 'pubsub.topics.update': true + }) + // apiResponse + assert.ok(responses[2][1]); + // subscription + assert.ok(responses[3][0].on); + assert.ok(responses[3][0].iam); + // apiResponse + assert.ok(responses[3][1]); + // policy + assert.deepEqual(responses[4][0], { etag: 'ACAB' }); + // apiResponse + assert.ok(responses[4][1]); + // permissions + assert.deepEqual(responses[5][0], { + 'pubsub.subscriptions.consume': true, + 'pubsub.subscriptions.update': true + }) + // apiResponse + assert.ok(responses[5][1]); + done(); + } catch (err) { + done(err); + } + }); + }); +}); diff --git a/test/pubsub/subscription.test.js b/test/pubsub/subscription.test.js new file mode 100644 index 0000000000..311490c096 --- /dev/null +++ b/test/pubsub/subscription.test.js @@ -0,0 +1,56 @@ +// Copyright 2016, 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. + +'use strict'; + +var assert = require('assert'); +var projectId = process.env.TEST_PROJECT_ID; + +var subscriptionSample = require('../../pubsub/subscription'); + +describe('pubsub/subscription', function () { + it('should run the sample', function (done) { + this.timeout(30000); + subscriptionSample.runSample(function (err, responses) { + try { + assert.ok(err === null); + // topic + var expectedTopic = 'projects/' + projectId + '/topics/messageCenter'; + assert.equal(responses[0][0].name, expectedTopic); + assert.ok(responses[0][0].iam); + // apiResponse + assert.ok(responses[0][1]); + // subscription + assert.ok(responses[1][0].on); + assert.ok(responses[1][0].iam); + // apiResponse + assert.ok(responses[1][1]); + // topics + assert.equal(responses[2][0][0].name, expectedTopic); + assert.ok(responses[2][0][0].iam); + // subscriptions + assert.ok(responses[3][0][0].on); + assert.ok(responses[3][0][0].iam); + // messageIds + assert.ok(typeof responses[4][0][0] === 'string'); + // apiResponse + assert.ok(responses[4][1]); + // messages + assert.equal(responses[5][0][0].data, 'Hello, world!'); + done(); + } catch (err) { + done(err); + } + }); + }); +}); From ce1c178be886e8ffff05907fb6610793342b73e2 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Fri, 22 Jan 2016 12:27:01 -0800 Subject: [PATCH 3/4] Added Pub/Sub samples. --- README.md | 15 +- package.json | 3 +- pubsub/README.md | 25 ++++ pubsub/iam.js | 183 +++++++++++++++++++++++ pubsub/package.json | 17 +++ pubsub/subscription.js | 246 +++++++++++++++++++++++++++++++ test/pubsub/iam.test.js | 67 +++++++++ test/pubsub/subscription.test.js | 56 +++++++ 8 files changed, 609 insertions(+), 3 deletions(-) create mode 100644 pubsub/README.md create mode 100644 pubsub/iam.js create mode 100644 pubsub/package.json create mode 100644 pubsub/subscription.js create mode 100644 test/pubsub/iam.test.js create mode 100644 test/pubsub/subscription.test.js diff --git a/README.md b/README.md index bc1bb71758..16c5bfd150 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ This repository holds Node.js samples used throughout [cloud.google.com](). * [Google App Engine](#google-app-engine) * [Google Cloud Logging](#google-cloud-logging) +* [Google Cloud Pub/Sub](#google-cloud-pubsub) * [Google Cloud Storage](#google-cloud-storage) * [Google Prediction API](#google-prediction-api) * [Other Example Apps](#other-example-apps) @@ -75,6 +76,11 @@ __Other Examples__ - Writing logs sample - [Source code][logging_write_1] | [Documentation][logging_write_2] - Exporting logs sample - [Source code][logging_export_1] | [Documentation][logging_export_2] +## Google Cloud Pub/Sub + +- Subscriber/Publisher sample - [Source code][pubsub_subscriber_1] | [Documentation][pubsub_subscriber_2] +- IAM sample - [Source code][pubsub_iam_1] | [Documentation][pubsub_iam_2] + ## Google Cloud Storage - Auth sample - [Source code][storage_1] | [Documentation][storage_2] @@ -240,15 +246,20 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma [aestaticfiles_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/static-files [datastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/datastore/tasks.js -[datastore_2]: https://cloud-dot-devsite.googleplex.com/datastore/docs/concepts/overview +[datastore_2]: https://cloud.google.com/datastore/docs/concepts/overview [logging_read_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/list.js -[logging_read_2]: https://cloud-dot-devsite.googleplex.com/logging/docs/api/tasks/authorization +[logging_read_2]: https://cloud.google.com/logging/docs/api/tasks/authorization [logging_write_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/write.js [logging_write_2]: https://cloud.google.com/logging/docs/api/tasks/creating-logs [logging_export_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/logging/export.js [logging_export_2]: https://cloud.google.com/logging/docs/api/tasks/exporting-logs +[pubsub_subscriber_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/pubsub/subscription.js +[pubsub_subscriber_2]: https://cloud.google.com/pubsub/subscriber +[pubsub_iam_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/pubsub/iam.js +[pubsub_iam_2]: https://cloud.google.com/pubsub/access_control + [storage_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/storage/authSample.js [storage_2]: https://cloud.google.com/storage/docs/authentication#acd-examples diff --git a/package.json b/package.json index 5e5c9bccf9..f99ea0027f 100644 --- a/package.json +++ b/package.json @@ -32,11 +32,12 @@ "deps_storage": "cd storage; npm i; cd ../..", "deps_prediction": "cd prediction; npm i; cd ../..", "deps_logging": "cd logging; npm i; cd ../..", + "deps_pubsub": "cd pubsub; npm i; cd ../..", "deps_express": "cd appengine/express; npm i; cd ../..", "deps_sendgrid": "cd appengine/sendgrid; npm i; cd ../..; cd computeengine/sendgrid; npm i; cd ../..", "deps_memcached": "cd appengine/express-memcached-session && npm i && cd ../..", "pretest_geddy": "cd appengine/geddy; npm i geddy; GEDDY_SECRET=config/secrets.json; [[ -f $GEDDY_SECRET ]] || echo '{}' > $GEDDY_SECRET && node node_modules/.bin/geddy gen secret; cd ../..;", - "pretest": "npm run deps_datastore; npm run deps_storage; npm run deps_prediction; npm run deps_logging; npm run deps_memcached; npm run deps_express; npm run deps_sendgrid; npm run pretest_geddy", + "pretest": "npm run deps_datastore; npm run deps_storage; npm run deps_prediction; npm run deps_logging; npm run deps_pubsub; npm run deps_memcached; npm run deps_express; npm run deps_sendgrid; npm run pretest_geddy", "test": "npm run jshint && npm run cover" }, "devDependencies": { diff --git a/pubsub/README.md b/pubsub/README.md new file mode 100644 index 0000000000..193d9bbcfa --- /dev/null +++ b/pubsub/README.md @@ -0,0 +1,25 @@ +## Pub/Sub Samples + +These samples require two environment variables to be set: + +- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can +download one from your Google project's "permissions" page. +- `TEST_PROJECT_ID` - Id of your Google project. + +## Run a sample + +Install dependencies: + + npm install + +To print available commands: + + npm run + +Execute a sample: + + npm run + +Example: + + npm run subscription diff --git a/pubsub/iam.js b/pubsub/iam.js new file mode 100644 index 0000000000..2ab999f3a1 --- /dev/null +++ b/pubsub/iam.js @@ -0,0 +1,183 @@ +// Copyright 2016, 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. + +'use strict'; + +var async = require('async'); +var utils = require('./subscription'); +var createTopic = utils.createTopic; +var subscribe = utils.subscribe; + +// [START get_topic_policy] +function getTopicPolicy(topic, callback) { + // Retrieve the IAM policy for the provided topic + topic.iam.getPolicy(callback); +} +// [END get_topic_policy] + +// [START get_subscription_policy] +function getSubscriptionPolicy(subscription, callback) { + // Retrieve the IAM policy for the provided subscription + subscription.iam.getPolicy(callback); +} +// [END get_subscription_policy] + +// [START set_topic_policy] +function setTopicPolicy(topic, callback) { + // Policy update + var myPolicy = { + bindings: [ + { + role: 'roles/pubsub.subscriber', + members: ['serviceAccount:myotherproject@appspot.gserviceaccount.com'] + } + ] + }; + + // Retrieve the IAM policy for the provided topic + topic.iam.setPolicy(myPolicy, callback); +} +// [END set_topic_policy] + +// [START set_subscription_policy] +function setSubscriptionPolicy(subscription, callback) { + // Policy update + var myPolicy = { + bindings: [ + { + role: 'roles/pubsub.subscriber', + members: ['serviceAccount:myotherproject@appspot.gserviceaccount.com'] + } + ] + }; + + // Retrieve the IAM policy for the provided subscription + subscription.iam.setPolicy(myPolicy, callback); +} +// [END set_subscription_policy] + +// [START test_topic_permissions] +function testTopicPermissions(topic, callback) { + var tests = [ + 'pubsub.topics.attachSubscription', + 'pubsub.topics.publish', + 'pubsub.topics.update' + ]; + + // Retrieve the IAM policy for the provided topic + topic.iam.testPermissions(tests, callback); +} +// [END test_topic_permissions] + +// [START test_subscription_permissions] +function testSubscriptionPermissions(subscription, callback) { + var tests = [ + 'pubsub.subscriptions.consume', + 'pubsub.subscriptions.update' + ]; + + // Retrieve the IAM policy for the provided subscription + subscription.iam.testPermissions(tests, callback); +} +// [END test_subscription_permissions] + +exports.getTopicPolicy = getTopicPolicy; +exports.getSubscriptionPolicy = getSubscriptionPolicy; +exports.setTopicPolicy = setTopicPolicy; +exports.setSubscriptionPolicy = setSubscriptionPolicy; +exports.testTopicPermissions = testTopicPermissions; +exports.testSubscriptionPermissions = testSubscriptionPermissions; +exports.runSample = runSample; + +function runSample(callback) { + var _subscription; + var _topic; + // Gather responses + var responses = []; + async.waterfall([ + function (cb) { + console.log('create topic...'); + createTopic(cb); + }, + function (topic, apiResponse, cb) { + _topic = topic; + responses.push([topic, apiResponse]); + console.log('created topic'); + console.log('get topic IAM policy...'); + getTopicPolicy(topic, cb); + }, + function (policy, apiResponse, cb) { + responses.push([policy, apiResponse]); + console.log('got topic policy', policy); + // IAM server is down? + // console.log('updating topic policy...'); + // setTopicPolicy(_topic, cb); + // }, + // function (policy, apiResponse, cb) { + // responses.push([policy, apiResponse]); + // console.log('updated topic policy', policy); + console.log('testing topic permissions...'); + testTopicPermissions(_topic, cb); + }, + function (permissions, apiResponse, cb) { + responses.push([permissions, apiResponse]); + console.log('tested topic permissions', permissions); + console.log('create subscription...'); + subscribe(cb); + }, + function (subscription, apiResponse, cb) { + _subscription = subscription; + responses.push([subscription, apiResponse]); + console.log('created subscription'); + console.log('get subscription IAM policy...'); + getSubscriptionPolicy(subscription, cb); + }, + function (policy, apiResponse, cb) { + responses.push([policy, apiResponse]); + console.log('got subscription policy', policy); + // IAM server is down? + // console.log('updating subscription policy...'); + // setSubscriptionPolicy(_subscription, cb); + // }, + // function (policy, apiResponse, cb) { + // responses.push([policy, apiResponse]); + // console.log('updated subscription policy', policy); + console.log('testing subscription permissions...'); + testSubscriptionPermissions(_subscription, cb); + }, + function (permissions, apiResponse, cb) { + responses.push([permissions, apiResponse]); + console.log('tested subscription permissions', permissions); + console.log('deleting subscription...'); + _subscription.delete(cb); + }, + function (apiResponse, cb) { + console.log('deleted subscription'); + console.log('deleting topic...'); + _topic.delete(cb); + } + ], function (err) { + if (err) { + console.error(err); + } else { + console.log('deleted topic'); + } + if (typeof callback === 'function') { + callback(err, responses); + } + }); +} + +if (module === require.main) { + runSample(); +} diff --git a/pubsub/package.json b/pubsub/package.json new file mode 100644 index 0000000000..564b9a5d29 --- /dev/null +++ b/pubsub/package.json @@ -0,0 +1,17 @@ +{ + "name": "nodejs-docs-samples-pubsub", + "description": "Node.js samples for Google Cloud Pub/Sub.", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "engines": { + "node": ">=0.10.x" + }, + "scripts": { + "iam": "node iam.js", + "subscription": "node subscription.js" + }, + "dependencies": { + "gcloud": "^0.27.0" + } +} diff --git a/pubsub/subscription.js b/pubsub/subscription.js new file mode 100644 index 0000000000..83e77853d3 --- /dev/null +++ b/pubsub/subscription.js @@ -0,0 +1,246 @@ +// Copyright 2016, 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. + +'use strict'; + +var async = require('async'); + +// [START auth] +// You must set these environment variables to run this sample +var projectId = process.env.TEST_PROJECT_ID; +var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; + +// If you don't set the environment variables, then you can modify this file +// to set the values +projectId = projectId || ''; +keyFilename = keyFilename || '/path/to/keyfile.json'; + +// [START require] +// Provide projectId and authentication to gcloud +var gcloud = require('gcloud')({ + projectId: projectId, + keyFilename: keyFilename +}); + +// Get a reference to the pubsub component +var pubsub = gcloud.pubsub(); +// [END auth] + +// [START create_topic] +function createTopic(callback) { + var topicName = 'messageCenter'; + + var topic = pubsub.topic(topicName); + + // Get the topic if it exists. Create it if it does not exist. + topic.get({ + autoCreate: true + }, callback); +} +// [END create_topic] + +// [START publish] +function publish(callback) { + var topicName = 'messageCenter'; + + // Grab a reference to an existing topic + var topic = pubsub.topic(topicName); + + // Publish a message to the topic + topic.publish({ + data: 'Hello, world!' + }, callback); +} +// [END publish] + +// [START list_topics] +function getAllTopics(callback) { + // Grab paginated topics + pubsub.getTopics(function (err, topics, nextQuery) { + // Quit on error + if (err) { + return callback(err); + } + // There is another page of topics + if (nextQuery) { + // Grab the remaining pages of topics recursively + return getAllTopics(function (err, _topics) { + if (_topics) { + topics = topics.concat(_topics); + } + callback(err, topics); + }); + } + // Last page of topics + return callback(err, topics); + }); +} +// [END list_topics] + +// [START get_all_subscriptions] +function getAllSubscriptions(callback) { + // Grab paginated subscriptions + pubsub.getSubscriptions(function (err, subscriptions, nextQuery) { + // Quit on error + if (err) { + return callback(err); + } + // There is another page of subscriptions + if (nextQuery) { + // Grab the remaining pages of subscriptions recursively + return getAllSubscriptions(function (err, _subscriptions) { + if (_subscriptions) { + subscriptions = subscriptions.concat(_subscriptions); + } + callback(err, subscriptions); + }); + } + // Last page of subscriptions + return callback(err, subscriptions); + }); +} +// [END get_all_subscriptions] + +// [START create_subscription] +function subscribe(callback) { + var topicName = 'messageCenter'; + var subscriptionName = 'newMessages'; + + var options = { + reuseExisting: true + }; + pubsub.subscribe(topicName, subscriptionName, options, callback); +} +// [END create_subscription] + +// [START pull_messages] +function pullMessages(callback) { + // Create a topic + createTopic(function (err) { + if (err) { + return callback(err); + } + // Create a subscription to the topic + subscribe(function (err, subscription) { + if (err) { + return callback(err); + } + var options = { + // Limit the amount of messages pulled. + maxResults: 100, + // If set, the system will respond immediately. Otherwise, wait until + // new messages are available. Returns if timeout is reached. + returnImmediately: false + }; + // Pull any messages on the subscription + subscription.pull(options, function (err, messages) { + if (err) { + return callback(err); + } + + // Do something with messages here? + + // Acknowledge messages + subscription.ack(messages.map(function (message) { + return message.ackId; + }), function (err) { + if (err) { + return callback(err); + } + callback(null, messages); + }); + }); + }); + }); +} +// [END pull_messages] + +exports.createTopic = createTopic; +exports.publish = publish; +exports.getAllTopics = getAllTopics; +exports.getAllSubscriptions = getAllSubscriptions; +exports.pullMessages = pullMessages; +exports.subscribe = subscribe; +exports.pubsub = pubsub; +exports.runSample = runSample; + +function runSample(callback) { + var _subscription; + var _topic; + // Gather responses + var responses = []; + async.waterfall([ + function (cb) { + console.log('create topic...'); + createTopic(cb); + }, + function (topic, apiResponse, cb) { + _topic = topic; + responses.push([topic, apiResponse]); + console.log('created topic'); + console.log('create subscription...'); + subscribe(cb); + }, + function (subscription, apiResponse, cb) { + _subscription = subscription; + responses.push([subscription, apiResponse]); + console.log('created subscription'); + console.log('list all topics...'); + getAllTopics(cb); + }, + function (topics, cb) { + responses.push([topics]); + console.log('got all topics'); + console.log('list all subscriptions...'); + getAllSubscriptions(cb); + }, + function (subscriptions, cb) { + responses.push([subscriptions]); + console.log('got all subscriptions'); + console.log('publishing a message...'); + publish(cb); + }, + function (messageIds, apiResponse, cb) { + responses.push([messageIds, apiResponse]); + console.log('published message'); + console.log('pulling messages...'); + pullMessages(cb); + }, + function (messages, cb) { + responses.push([messages]); + console.log('got messages', messages.map(function (message) { + return message.data; + })); + console.log('deleting subscription...'); + _subscription.delete(cb); + }, + function (apiResponse, cb) { + console.log('deleted subscription'); + console.log('deleting topic...'); + _topic.delete(cb); + } + ], function (err) { + if (err) { + console.error(err); + } else { + console.log('deleted topic'); + } + if (typeof callback === 'function') { + callback(err, responses); + } + }); +} + +if (module === require.main) { + runSample(); +} diff --git a/test/pubsub/iam.test.js b/test/pubsub/iam.test.js new file mode 100644 index 0000000000..e0f3ef060a --- /dev/null +++ b/test/pubsub/iam.test.js @@ -0,0 +1,67 @@ +// Copyright 2016, 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. + +'use strict'; + +var assert = require('assert'); +var projectId = process.env.TEST_PROJECT_ID; + +var iamSample = require('../../pubsub/iam'); + +describe('pubsub/iam', function () { + it('should run the sample', function (done) { + this.timeout(30000); + iamSample.runSample(function (err, responses) { + try { + assert.ok(err === null); + // topic + var expectedTopic = 'projects/' + projectId + '/topics/messageCenter'; + assert.equal(responses[0][0].name, expectedTopic); + assert.ok(responses[0][0].iam); + // apiResponse + assert.ok(responses[0][1]); + // policy + assert.deepEqual(responses[1][0], { etag: 'ACAB' }); + // apiResponse + assert.ok(responses[1][1]); + // permissions + assert.deepEqual(responses[2][0], { + 'pubsub.topics.attachSubscription': true, + 'pubsub.topics.publish': true, + 'pubsub.topics.update': true + }); + // apiResponse + assert.ok(responses[2][1]); + // subscription + assert.ok(responses[3][0].on); + assert.ok(responses[3][0].iam); + // apiResponse + assert.ok(responses[3][1]); + // policy + assert.deepEqual(responses[4][0], { etag: 'ACAB' }); + // apiResponse + assert.ok(responses[4][1]); + // permissions + assert.deepEqual(responses[5][0], { + 'pubsub.subscriptions.consume': true, + 'pubsub.subscriptions.update': true + }); + // apiResponse + assert.ok(responses[5][1]); + done(); + } catch (err) { + done(err); + } + }); + }); +}); diff --git a/test/pubsub/subscription.test.js b/test/pubsub/subscription.test.js new file mode 100644 index 0000000000..311490c096 --- /dev/null +++ b/test/pubsub/subscription.test.js @@ -0,0 +1,56 @@ +// Copyright 2016, 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. + +'use strict'; + +var assert = require('assert'); +var projectId = process.env.TEST_PROJECT_ID; + +var subscriptionSample = require('../../pubsub/subscription'); + +describe('pubsub/subscription', function () { + it('should run the sample', function (done) { + this.timeout(30000); + subscriptionSample.runSample(function (err, responses) { + try { + assert.ok(err === null); + // topic + var expectedTopic = 'projects/' + projectId + '/topics/messageCenter'; + assert.equal(responses[0][0].name, expectedTopic); + assert.ok(responses[0][0].iam); + // apiResponse + assert.ok(responses[0][1]); + // subscription + assert.ok(responses[1][0].on); + assert.ok(responses[1][0].iam); + // apiResponse + assert.ok(responses[1][1]); + // topics + assert.equal(responses[2][0][0].name, expectedTopic); + assert.ok(responses[2][0][0].iam); + // subscriptions + assert.ok(responses[3][0][0].on); + assert.ok(responses[3][0][0].iam); + // messageIds + assert.ok(typeof responses[4][0][0] === 'string'); + // apiResponse + assert.ok(responses[4][1]); + // messages + assert.equal(responses[5][0][0].data, 'Hello, world!'); + done(); + } catch (err) { + done(err); + } + }); + }); +}); From d60b52a823cac012f0d7425184263cd92603191a Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 26 Jan 2016 11:00:38 -0800 Subject: [PATCH 4/4] s --- .travis.yml | 2 +- README.md | 2 +- appengine/datastore/app.js | 2 +- appengine/pubsub/app.js | 4 +++- appengine/storage/app.js | 2 +- datastore/README.md | 2 +- datastore/tasks.js | 4 ++-- logging/README.md | 2 +- logging/export.js | 14 ++++---------- logging/list.js | 14 ++++---------- logging/write.js | 14 ++++---------- pubsub/README.md | 2 +- pubsub/iam.js | 24 +++--------------------- pubsub/subscription.js | 19 ++++--------------- test/appengine/all.test.js | 2 +- test/pubsub/iam.test.js | 2 +- test/pubsub/subscription.test.js | 2 +- test/storage/authSample.test.js | 2 +- 18 files changed, 35 insertions(+), 80 deletions(-) diff --git a/.travis.yml b/.travis.yml index fa18bcdb85..ba11c18edb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,7 +41,7 @@ services: - docker env: - - PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/test/encrypted/nodejs-docs-samples.json TEST_BUCKET_NAME=nodejs-docs-samples TEST_PROJECT_ID=nodejs-docs-samples #Other environment variables on same line + - PATH=$PATH:$HOME/gcloud/google-cloud-sdk/bin GOOGLE_APPLICATION_CREDENTIALS=$TRAVIS_BUILD_DIR/test/encrypted/nodejs-docs-samples.json TEST_BUCKET_NAME=nodejs-docs-samples GCLOUD_PROJECT=nodejs-docs-samples #Other environment variables on same line before_install: - if [ ! -d $HOME/gcloud/google-cloud-sdk ]; then diff --git a/README.md b/README.md index 16c5bfd150..25e43c3fab 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ See [CONTRIBUTING.md](https://github.com/GoogleCloudPlatform/nodejs-docs-samples 1. `npm install` 1. Start Redis 1. Start Memcached -1. Set the `TEST_PROJECT_ID` environment variable to id of your project +1. Set the `GCLOUD_PROJECT` environment variable to id of your project 1. Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to the path to a service account file. You can download one from your Google project's "permissions" page. diff --git a/appengine/datastore/app.js b/appengine/datastore/app.js index 1ccb0f9baf..9e0574ee46 100644 --- a/appengine/datastore/app.js +++ b/appengine/datastore/app.js @@ -25,7 +25,7 @@ app.enable('trust proxy'); var dataset = gcloud.datastore.dataset({ // This environment variable is set by app.yaml when running on GAE, but will // need to be manually set when running locally. - projectId: process.env.GCLOUD_PROJECT || process.env.TEST_PROJECT_ID + projectId: process.env.GCLOUD_PROJECT }); app.get('/', function(req, res, next) { diff --git a/appengine/pubsub/app.js b/appengine/pubsub/app.js index a7efe2443d..acdc5175d8 100644 --- a/appengine/pubsub/app.js +++ b/appengine/pubsub/app.js @@ -32,8 +32,10 @@ var messages = []; // but will need to be manually set when running locally. var PUBSUB_VERIFICATION_TOKEN = process.env.PUBSUB_VERIFICATION_TOKEN; +// You must set the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS +// environment variables to run this sample var pubsub = gcloud.pubsub({ - projectId: process.env.GCLOUD_PROJECT || process.env.TEST_PROJECT_ID + projectId: process.env.GCLOUD_PROJECT }); var topic = pubsub.topic(process.env.PUBSUB_TOPIC); diff --git a/appengine/storage/app.js b/appengine/storage/app.js index 4b88b1ed49..70ee32f0b7 100644 --- a/appengine/storage/app.js +++ b/appengine/storage/app.js @@ -34,7 +34,7 @@ var multer = require('multer')({ // but will need to be manually set when running locally. // The storage client is used to communicate with Google Cloud Storage var storage = gcloud.storage({ - projectId: process.env.GCLOUD_PROJECT || process.env.TEST_PROJECT_ID + projectId: process.env.GCLOUD_PROJECT }); // A bucket is a container for objects (files). diff --git a/datastore/README.md b/datastore/README.md index 48370161be..fa9124b702 100644 --- a/datastore/README.md +++ b/datastore/README.md @@ -4,7 +4,7 @@ These samples require two environment variables to be set: - `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can download one from your Google project's "permissions" page. -- `TEST_PROJECT_ID` - Id of your Google project. +- `GCLOUD_PROJECT` - Id of your Google project. ## Run a sample diff --git a/datastore/tasks.js b/datastore/tasks.js index 6981c0f678..fb21b9c575 100755 --- a/datastore/tasks.js +++ b/datastore/tasks.js @@ -16,9 +16,9 @@ var input = process.argv.splice(2); var command = input.shift(); -var projectId = process.env.DATASTORE_PROJECT_ID || process.env.TEST_PROJECT_ID; +var projectId = process.env.DATASTORE_PROJECT_ID || process.env.GCLOUD_PROJECT; if (!projectId) { - throw new Error('TEST_PROJECT_ID environment variable required.'); + throw new Error('GCLOUD_PROJECT environment variable required.'); } var keyFile = process.env.DATASTORE_KEYFILE || process.env.GOOGLE_APPLICATION_CREDENTIALS; diff --git a/logging/README.md b/logging/README.md index 80600b3ea1..005f4893a1 100644 --- a/logging/README.md +++ b/logging/README.md @@ -4,7 +4,7 @@ These samples require two environment variables to be set: - `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can download one from your Google project's "permissions" page. -- `TEST_PROJECT_ID` - Id of your Google project. +- `GCLOUD_PROJECT` - Id of your Google project. ## Run a sample diff --git a/logging/export.js b/logging/export.js index e683d74b17..dd130a6ab8 100644 --- a/logging/export.js +++ b/logging/export.js @@ -14,19 +14,13 @@ 'use strict'; // [START setup] -// You must set these environment variables to run this sample -var projectId = process.env.TEST_PROJECT_ID; -var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; - -// If you don't set the environment variables, then you can modify this file -// to set the values -projectId = projectId || ''; -keyFilename = keyFilename || '/path/to/keyfile.json'; +// You must set the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; // Provide projectId and authentication to gcloud var gcloud = require('gcloud')({ - projectId: projectId, - keyFilename: keyFilename + projectId: projectId }); // Get a reference to the logging component diff --git a/logging/list.js b/logging/list.js index 816a6d3c25..00b504161b 100644 --- a/logging/list.js +++ b/logging/list.js @@ -15,20 +15,14 @@ // [START list] // [START auth] -// You must set these environment variables to run this sample -var projectId = process.env.TEST_PROJECT_ID; -var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; - -// If you don't set the environment variables, then you can modify this file -// to set the values -projectId = projectId || ''; -keyFilename = keyFilename || '/path/to/keyfile.json'; +// You must set the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; // [START require] // Provide projectId and authentication to gcloud var gcloud = require('gcloud')({ - projectId: projectId, - keyFilename: keyFilename + projectId: projectId }); // [END require] // [END auth] diff --git a/logging/write.js b/logging/write.js index 57d22c566f..3f001c2939 100644 --- a/logging/write.js +++ b/logging/write.js @@ -16,19 +16,13 @@ // [START write] // [START setup] -// You must set these environment variables to run this sample -var projectId = process.env.TEST_PROJECT_ID; -var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; - -// If you don't set the environment variables, then you can modify this file -// to set the values -projectId = projectId || ''; -keyFilename = keyFilename || '/path/to/keyfile.json'; +// You must set the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; // Provide projectId and authentication to gcloud var gcloud = require('gcloud')({ - projectId: projectId, - keyFilename: keyFilename + projectId: projectId }); // Get a reference to the logging component diff --git a/pubsub/README.md b/pubsub/README.md index 193d9bbcfa..6bb6339fa4 100644 --- a/pubsub/README.md +++ b/pubsub/README.md @@ -4,7 +4,7 @@ These samples require two environment variables to be set: - `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can download one from your Google project's "permissions" page. -- `TEST_PROJECT_ID` - Id of your Google project. +- `GCLOUD_PROJECT` - Id of your Google project. ## Run a sample diff --git a/pubsub/iam.js b/pubsub/iam.js index 2ab999f3a1..a1614c35fe 100644 --- a/pubsub/iam.js +++ b/pubsub/iam.js @@ -14,9 +14,9 @@ 'use strict'; var async = require('async'); -var utils = require('./subscription'); -var createTopic = utils.createTopic; -var subscribe = utils.subscribe; +var subscriptionSampleMethods = require('./subscription'); +var createTopic = subscriptionSampleMethods.createTopic; +var subscribe = subscriptionSampleMethods.subscribe; // [START get_topic_policy] function getTopicPolicy(topic, callback) { @@ -91,12 +91,8 @@ function testSubscriptionPermissions(subscription, callback) { } // [END test_subscription_permissions] -exports.getTopicPolicy = getTopicPolicy; -exports.getSubscriptionPolicy = getSubscriptionPolicy; exports.setTopicPolicy = setTopicPolicy; exports.setSubscriptionPolicy = setSubscriptionPolicy; -exports.testTopicPermissions = testTopicPermissions; -exports.testSubscriptionPermissions = testSubscriptionPermissions; exports.runSample = runSample; function runSample(callback) { @@ -119,13 +115,6 @@ function runSample(callback) { function (policy, apiResponse, cb) { responses.push([policy, apiResponse]); console.log('got topic policy', policy); - // IAM server is down? - // console.log('updating topic policy...'); - // setTopicPolicy(_topic, cb); - // }, - // function (policy, apiResponse, cb) { - // responses.push([policy, apiResponse]); - // console.log('updated topic policy', policy); console.log('testing topic permissions...'); testTopicPermissions(_topic, cb); }, @@ -145,13 +134,6 @@ function runSample(callback) { function (policy, apiResponse, cb) { responses.push([policy, apiResponse]); console.log('got subscription policy', policy); - // IAM server is down? - // console.log('updating subscription policy...'); - // setSubscriptionPolicy(_subscription, cb); - // }, - // function (policy, apiResponse, cb) { - // responses.push([policy, apiResponse]); - // console.log('updated subscription policy', policy); console.log('testing subscription permissions...'); testSubscriptionPermissions(_subscription, cb); }, diff --git a/pubsub/subscription.js b/pubsub/subscription.js index 83e77853d3..da8df5455b 100644 --- a/pubsub/subscription.js +++ b/pubsub/subscription.js @@ -16,20 +16,14 @@ var async = require('async'); // [START auth] -// You must set these environment variables to run this sample -var projectId = process.env.TEST_PROJECT_ID; -var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; - -// If you don't set the environment variables, then you can modify this file -// to set the values -projectId = projectId || ''; -keyFilename = keyFilename || '/path/to/keyfile.json'; +// You must set the GCLOUD_PROJECT and GOOGLE_APPLICATION_CREDENTIALS +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; // [START require] // Provide projectId and authentication to gcloud var gcloud = require('gcloud')({ - projectId: projectId, - keyFilename: keyFilename + projectId: projectId }); // Get a reference to the pubsub component @@ -166,12 +160,7 @@ function pullMessages(callback) { // [END pull_messages] exports.createTopic = createTopic; -exports.publish = publish; -exports.getAllTopics = getAllTopics; -exports.getAllSubscriptions = getAllSubscriptions; -exports.pullMessages = pullMessages; exports.subscribe = subscribe; -exports.pubsub = pubsub; exports.runSample = runSample; function runSample(callback) { diff --git a/test/appengine/all.test.js b/test/appengine/all.test.js index b3c96d5d8d..548446b514 100644 --- a/test/appengine/all.test.js +++ b/test/appengine/all.test.js @@ -18,7 +18,7 @@ var request = require('request'); var fs = require('fs'); var async = require('async'); var cwd = process.cwd(); -var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; +var projectId = process.env.GCLOUD_PROJECT || 'nodejs-docs-samples'; function getPath(dir) { return cwd + '/appengine/' + dir; diff --git a/test/pubsub/iam.test.js b/test/pubsub/iam.test.js index e0f3ef060a..b992a5bd52 100644 --- a/test/pubsub/iam.test.js +++ b/test/pubsub/iam.test.js @@ -14,7 +14,7 @@ 'use strict'; var assert = require('assert'); -var projectId = process.env.TEST_PROJECT_ID; +var projectId = process.env.GCLOUD_PROJECT; var iamSample = require('../../pubsub/iam'); diff --git a/test/pubsub/subscription.test.js b/test/pubsub/subscription.test.js index 311490c096..317a1a87ff 100644 --- a/test/pubsub/subscription.test.js +++ b/test/pubsub/subscription.test.js @@ -14,7 +14,7 @@ 'use strict'; var assert = require('assert'); -var projectId = process.env.TEST_PROJECT_ID; +var projectId = process.env.GCLOUD_PROJECT; var subscriptionSample = require('../../pubsub/subscription'); diff --git a/test/storage/authSample.test.js b/test/storage/authSample.test.js index ee32ce5e8b..ca801818ca 100644 --- a/test/storage/authSample.test.js +++ b/test/storage/authSample.test.js @@ -18,7 +18,7 @@ var assert = require('assert'); var authSample = require('../../storage/authSample'); -var projectId = process.env.TEST_PROJECT_ID || 'nodejs-docs-samples'; +var projectId = process.env.GCLOUD_PROJECT; describe('listBuckets', function () {