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 5 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
15 changes: 13 additions & 2 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 @@ -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
13 changes: 11 additions & 2 deletions logging/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '<your-project-id>';
keyFilename = keyFilename || '/path/to/keyfile.json';

// Provide projectId and authentication to gcloud
var gcloud = require('gcloud')({
projectId: projectId,
keyFilename: keyFilename

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keyfilename is optional, as it can automatically use application default credentials.

});

// 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 +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
Expand All @@ -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
Expand Down
8 changes: 7 additions & 1 deletion logging/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,29 @@

// [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 || '<your-project-id>';
keyFilename = keyFilename || '/path/to/keyfile.json';

// [START require]
// Provide projectId and authentication to gcloud
var gcloud = require('gcloud')({
projectId: projectId,
keyFilename: keyFilename
});
// [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 +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);
});
Expand Down
25 changes: 22 additions & 3 deletions logging/write.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,54 @@

// [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 || '<your-project-id>';
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
// 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 +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]
Expand All @@ -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!');
}
});
});
}
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.
- `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 <sample>

Example:

npm run subscription
Loading