From 5a6ecef197e6bf887cac180f57d2615f358693ab Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Tue, 26 Jan 2016 11:51:53 -0800 Subject: [PATCH] Added Pub/Sub samples. --- .travis.yml | 2 +- README.md | 17 ++- appengine/datastore/app.js | 2 +- appengine/pubsub/app.js | 2 +- appengine/storage/app.js | 2 +- datastore/README.md | 2 +- datastore/tasks.js | 4 +- logging/README.md | 2 +- logging/export.js | 16 +- logging/list.js | 16 +- logging/write.js | 16 +- package.json | 3 +- pubsub/README.md | 25 ++++ pubsub/iam.js | 177 ++++++++++++++++++++++ pubsub/package.json | 17 +++ pubsub/subscription.js | 242 +++++++++++++++++++++++++++++++ test/appengine/all.test.js | 2 +- test/pubsub/iam.test.js | 63 ++++++++ test/pubsub/subscription.test.js | 56 +++++++ test/storage/authSample.test.js | 2 +- 20 files changed, 621 insertions(+), 47 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/.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 bc1bb71758..25e43c3fab 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] @@ -110,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. @@ -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/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..98fab1ca02 100644 --- a/appengine/pubsub/app.js +++ b/appengine/pubsub/app.js @@ -33,7 +33,7 @@ var messages = []; var PUBSUB_VERIFICATION_TOKEN = process.env.PUBSUB_VERIFICATION_TOKEN; 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..4c2555c3f0 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; +// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; -// 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 +// Initialize 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..f571fc3085 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 GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; // [START require] -// Provide projectId and authentication to gcloud +// Initialize 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..ae99602171 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; +// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; -// 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 +// Initialize gcloud var gcloud = require('gcloud')({ - projectId: projectId, - keyFilename: keyFilename + projectId: projectId }); // Get a reference to the logging component diff --git a/package.json b/package.json index 5e5c9bccf9..19a47fb584 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "cover": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- --timeout 10000 --recursive", "coveralls": "cat ./coverage/lcov.info | node_modules/.bin/coveralls", "deps_datastore": "cd datastore; npm i; cd ../..", + "deps_pubsub": "cd pubsub; npm i; cd ../..", "deps_storage": "cd storage; npm i; cd ../..", "deps_prediction": "cd prediction; npm i; cd ../..", "deps_logging": "cd logging; npm i; cd ../..", @@ -36,7 +37,7 @@ "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_pubsub; npm run deps_prediction; npm run deps_logging; 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..6bb6339fa4 --- /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. +- `GCLOUD_PROJECT` - 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..eb7aef555d --- /dev/null +++ b/pubsub/iam.js @@ -0,0 +1,177 @@ +// 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(function (err, policy) { + if (err) { + return callback(err); + } + console.log(policy); // { etag: 'ACAB' } + return callback(err, policy); + }); +} +// [END get_topic_policy] + +// [START get_subscription_policy] +function getSubscriptionPolicy(subscription, callback) { + // Retrieve the IAM policy for the provided subscription + subscription.iam.getPolicy(function (err, policy) { + if (err) { + return callback(err); + } + console.log(policy); // { etag: 'ACAB' } + return callback(err, policy); + }); +} +// [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.setTopicPolicy = setTopicPolicy; +exports.setSubscriptionPolicy = setSubscriptionPolicy; +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, cb) { + responses.push([policy]); + console.log('got 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, cb) { + responses.push([policy]); + console.log('got 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..6250376b60 --- /dev/null +++ b/pubsub/subscription.js @@ -0,0 +1,242 @@ +// 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 the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; + +// [START require] +// Initialize gcloud +var gcloud = require('gcloud')({ + projectId: projectId +}); + +// 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 handle_message] +function handleMessage(message) { + console.log('received message: ' + message.data); +} +// [END handle_message] + +// [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 for each message + messages.forEach(handleMessage); + + // 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.subscribe = subscribe; +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/appengine/all.test.js b/test/appengine/all.test.js index b3c96d5d8d..b06cc1827b 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; function getPath(dir) { return cwd + '/appengine/' + dir; diff --git a/test/pubsub/iam.test.js b/test/pubsub/iam.test.js new file mode 100644 index 0000000000..cdc81b6ff6 --- /dev/null +++ b/test/pubsub/iam.test.js @@ -0,0 +1,63 @@ +// 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.GCLOUD_PROJECT; + +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' }); + // 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' }); + // 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..317a1a87ff --- /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.GCLOUD_PROJECT; + +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); + } + }); + }); +}); 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 () {