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

Added Pub/Sub samples. #55

Closed
wants to merge 6 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion appengine/datastore/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion appengine/pubsub/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion appengine/storage/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
2 changes: 1 addition & 1 deletion datastore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions datastore/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion logging/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 12 additions & 9 deletions logging/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,32 @@
'use strict';

// [START setup]
var projectId = process.env.TEST_PROJECT_ID;
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;

projectId = projectId || '<your-project-id>';
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
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.
Expand All @@ -51,7 +51,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
Expand All @@ -67,6 +69,7 @@ function updateSink(callback) {

// [START deleteSink]
function deleteSink(callback) {
// Get a reference to an existing sink
var sink = logging.sink('mySink');

// delete a sink
Expand Down
16 changes: 8 additions & 8 deletions logging/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,23 @@

// [START list]
// [START auth]
var projectId = process.env.TEST_PROJECT_ID;
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;

projectId = projectId || '<your-project-id>';
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]

// 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);
Expand All @@ -42,6 +41,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);
});
Expand Down
33 changes: 23 additions & 10 deletions logging/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,48 @@

// [START write]
// [START setup]
var projectId = process.env.TEST_PROJECT_ID;
var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS;

projectId = projectId || '<your-project-id>';
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
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
// See https://cloud.google.com/logging/docs/api/ref_v2beta1/rest \
// /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
Expand All @@ -63,9 +67,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]
Expand All @@ -74,13 +79,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!');
}
});
});
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
25 changes: 25 additions & 0 deletions pubsub/README.md
Original file line number Diff line number Diff line change
@@ -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 <sample>

Example:

npm run subscription
Loading