From 503fe2f3dadeec21a2ef0f7c0a3a6a1148290e37 Mon Sep 17 00:00:00 2001 From: Jason Dobry Date: Thu, 14 Jan 2016 13:42:31 -0800 Subject: [PATCH] Add Logging examples. --- computeengine/sendgrid/package.json | 2 +- datastore/README.md | 6 ++ datastore/package.json | 7 ++- logging/README.md | 25 ++++++++ logging/export.js | 92 +++++++++++++++++++++++++++++ logging/list.js | 48 +++++++++++++++ logging/package.json | 18 ++++++ logging/write.js | 86 +++++++++++++++++++++++++++ package.json | 11 ++-- prediction/README.md | 24 ++++++++ prediction/package.json | 3 + storage/README.md | 21 +++---- storage/package.json | 5 +- test/logging/export.test.js | 30 ++++++++++ test/logging/list.test.js | 31 ++++++++++ test/logging/write.test.js | 39 ++++++++++++ 16 files changed, 427 insertions(+), 21 deletions(-) create mode 100644 logging/README.md create mode 100644 logging/export.js create mode 100644 logging/list.js create mode 100644 logging/package.json create mode 100644 logging/write.js create mode 100644 prediction/README.md create mode 100644 test/logging/export.test.js create mode 100644 test/logging/list.test.js create mode 100644 test/logging/write.test.js diff --git a/computeengine/sendgrid/package.json b/computeengine/sendgrid/package.json index 04c3dd6209..e22e34f062 100644 --- a/computeengine/sendgrid/package.json +++ b/computeengine/sendgrid/package.json @@ -5,7 +5,7 @@ "private": true, "license": "Apache Version 2.0", "engines": { - "node": "~4.2" + "node": ">=0.10.x" }, "dependencies": { "sendgrid": "^2.0.0" diff --git a/datastore/README.md b/datastore/README.md index eca0dabc71..48370161be 100644 --- a/datastore/README.md +++ b/datastore/README.md @@ -1,5 +1,11 @@ ## Datastore 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: diff --git a/datastore/package.json b/datastore/package.json index 07b39f758e..05a3dc762a 100644 --- a/datastore/package.json +++ b/datastore/package.json @@ -4,11 +4,14 @@ "version": "0.0.1", "private": true, "license": "Apache Version 2.0", + "engines": { + "node": ">=0.10.x" + }, "scripts": { "tasks": "node tasks.js" }, "dependencies": { - "async": "^1.5.0", - "gcloud": "^0.25.0" + "async": "^1.5.2", + "gcloud": "^0.27.0" } } diff --git a/logging/README.md b/logging/README.md new file mode 100644 index 0000000000..80600b3ea1 --- /dev/null +++ b/logging/README.md @@ -0,0 +1,25 @@ +## Logging 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 write diff --git a/logging/export.js b/logging/export.js new file mode 100644 index 0000000000..142823e049 --- /dev/null +++ b/logging/export.js @@ -0,0 +1,92 @@ +// 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'; + +// [START setup] +var projectId = process.env.TEST_PROJECT_ID; +var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; + +projectId = projectId || ''; +keyFilename = keyFilename || '/path/to/keyfile.json'; + +var gcloud = require('gcloud')({ + projectId: projectId, + keyFilename: keyFilename +}); + +var logging = gcloud.logging(); +// [END setup] + +// [START listSinks] +function listSinks(callback) { + // list all sinks in the project + logging.getSinks(callback); +} +// [END listSinks] + +// [START createSink] +function createSink(callback) { + var gcs = gcloud.storage(); + + // create a new sink + // + // This method only works if you are authenticated as yourself, e.g. using the + // gcloud SDK. + logging.createSink('mySink', { + destination: gcs.bucket('logging-bucket') + }, callback); +} +// [END createSink] + +// [START updateSink] +function updateSink(callback) { + var gcs = gcloud.storage(); + var sink = logging.sink('mySink'); + + // update a sink + // + // This method only works if you are authenticated as yourself, e.g. using the + // gcloud SDK. + sink.setMetadata({ + // change destination to something else + destination: gcs.bucket('other-logging-bucket') + }, callback); +} +// [END updateSink] + +// [START deleteSink] +function deleteSink(callback) { + var sink = logging.sink('mySink'); + + // delete a sink + // + // This method only works if you are authenticated as yourself, e.g. using the + // gcloud SDK. + sink.delete(callback); +} +// [END deleteSink] + +exports.listSinks = listSinks; +exports.createSink = createSink; +exports.updateSink = updateSink; +exports.deleteSink = deleteSink; + +if (module === require.main) { + listSinks(function (err, sinks, apiResponse) { + console.log(err, 'sinks:', sinks, 'apiResponse:', apiResponse); + if (err) { + return; + } + }); +} diff --git a/logging/list.js b/logging/list.js new file mode 100644 index 0000000000..2f4d25cd59 --- /dev/null +++ b/logging/list.js @@ -0,0 +1,48 @@ +// 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'; + +// [START list] +// [START auth] +var projectId = process.env.TEST_PROJECT_ID; +var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; + +projectId = projectId || ''; +keyFilename = keyFilename || '/path/to/keyfile.json'; + +// [START require] +var gcloud = require('gcloud')({ + projectId: projectId, + keyFilename: keyFilename +}); +// [END require] +// [END auth] + +var logging = gcloud.logging(); + +function list(callback) { + // Retrieve 3 log entries. + logging.getEntries({ + pageSize: 3 + }, callback); +} +// [END list] + +exports.list = list; + +if (module === require.main) { + list(function (err, apiResponse) { + console.log(err, 'apiResponse:', apiResponse); + }); +} diff --git a/logging/package.json b/logging/package.json new file mode 100644 index 0000000000..340a02a931 --- /dev/null +++ b/logging/package.json @@ -0,0 +1,18 @@ +{ + "name": "nodejs-docs-samples-logging", + "description": "Node.js samples for Google Cloud Logging.", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "engines": { + "node": ">=0.10.x" + }, + "scripts": { + "list": "node list.js", + "write": "node write.js", + "export": "node export.js" + }, + "dependencies": { + "gcloud": "^0.27.0" + } +} diff --git a/logging/write.js b/logging/write.js new file mode 100644 index 0000000000..3c36693f42 --- /dev/null +++ b/logging/write.js @@ -0,0 +1,86 @@ +// 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. + +/* jshint camelcase:false */ +'use strict'; + +// [START write] +// [START setup] +var projectId = process.env.TEST_PROJECT_ID; +var keyFilename = process.env.GOOGLE_APPLICATION_CREDENTIALS; + +projectId = projectId || ''; +keyFilename = keyFilename || '/path/to/keyfile.json'; + +var gcloud = require('gcloud')({ + projectId: projectId, + keyFilename: keyFilename +}); + +var logging = gcloud.logging(); +// [END setup] + +function write(callback) { + 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', + labels: { + module_id: 'default', + version_id: 'express' + } + }; + + var entry = log.entry(resource, { + foo: 'bar' + }); + + 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. + log.write([ + entry, + secondEntry + ], callback); +} +// [END write] + +// [START deleteLog] +function deleteLog(callback) { + var log = logging.log('myLog'); + + // Delete the logs + log.delete(callback); +} +// [END deleteLog] + +exports.write = write; +exports.deleteLog = deleteLog; + +if (module === require.main) { + write(function (err, apiResponse) { + console.log(err, 'apiResponse:', apiResponse); + if (err) { + return; + } + deleteLog(function (err, apiResponse) { + console.log(err, 'apiResponse:', apiResponse); + }); + }); +} diff --git a/package.json b/package.json index 5013ceab9d..01b768454c 100644 --- a/package.json +++ b/package.json @@ -27,18 +27,19 @@ "deps_datastore": "cd datastore && npm i && cd ../..", "deps_storage": "cd storage && npm i && cd ../..", "deps_prediction": "cd prediction && npm i && cd ../..", + "deps_logging": "cd logging && 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_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_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy", "test": "npm run jshint && npm run cover" }, "devDependencies": { - "async": "^1.5.0", - "coveralls": "^2.11.4", - "googleapis": "^2.1.6", - "istanbul": "^0.4.0", + "async": "^1.5.2", + "coveralls": "^2.11.6", + "googleapis": "^2.1.7", + "istanbul": "^0.4.2", "jshint": "~2.8.0", "mocha": "^2.2.5", "proxyquire": "^1.7.3", diff --git a/prediction/README.md b/prediction/README.md new file mode 100644 index 0000000000..ed88404357 --- /dev/null +++ b/prediction/README.md @@ -0,0 +1,24 @@ +## Prediction API Samples + +These samples require an environment variable to be set: + +- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can +download one from your Google project's "permissions" page. + +## Run a sample + +Install dependencies: + + npm install + +To print available commands: + + npm run + +Execute a sample: + + npm run + +Example: + + npm run hostedmodels diff --git a/prediction/package.json b/prediction/package.json index 34a6cd213e..c9d1df34d7 100644 --- a/prediction/package.json +++ b/prediction/package.json @@ -4,6 +4,9 @@ "version": "0.0.1", "private": true, "license": "Apache Version 2.0", + "engines": { + "node": ">=0.10.x" + }, "scripts": { "hostedmodels": "node hostedmodels.js" }, diff --git a/storage/README.md b/storage/README.md index 40a98a38e9..e27f20ba10 100644 --- a/storage/README.md +++ b/storage/README.md @@ -1,27 +1,24 @@ ## Storage Samples +These samples require an environment variable to be set: + +- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can +download one from your Google project's "permissions" page. + ## Run a sample Install dependencies: -``` -$ npm install -``` + npm install To print available commands: -``` -$ npm run -``` + npm run Execute a sample: -``` -$ npm run -- [arg_1] [arg_2] [arg_n] -``` + npm run -- [arg_1] [arg_2] [arg_n] Example: -``` -$ npm run authSample -- my-cool-project -``` \ No newline at end of file + npm run authSample -- my-cool-project diff --git a/storage/package.json b/storage/package.json index ea474984df..073802d7ee 100644 --- a/storage/package.json +++ b/storage/package.json @@ -4,10 +4,13 @@ "version": "0.0.1", "private": true, "license": "Apache Version 2.0", + "engines": { + "node": ">=0.10.x" + }, "scripts": { "authSample": "node authSample.js" }, "dependencies": { - "googleapis": "^2.1.6" + "googleapis": "^2.1.7" } } diff --git a/test/logging/export.test.js b/test/logging/export.test.js new file mode 100644 index 0000000000..5853a2844c --- /dev/null +++ b/test/logging/export.test.js @@ -0,0 +1,30 @@ +// 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 logging = require('../../logging/export'); + +describe('logging/export', function () { + it('should list sinks', function (done) { + logging.listSinks(function (err, sinks) { + if (err) { + return done(err); + } + assert.ok(Array.isArray(sinks), 'should be an array'); + done(); + }); + }); +}); diff --git a/test/logging/list.test.js b/test/logging/list.test.js new file mode 100644 index 0000000000..737a3ac928 --- /dev/null +++ b/test/logging/list.test.js @@ -0,0 +1,31 @@ +// 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 logging = require('../../logging/list'); + +describe('logging/list', function () { + it('should list entries', function (done) { + logging.list(function (err, entries) { + if (err) { + return done(err); + } + assert.ok(Array.isArray(entries), 'should have got an array'); + assert.equal(entries.length, 3, 'should have three entries'); + done(); + }); + }); +}); diff --git a/test/logging/write.test.js b/test/logging/write.test.js new file mode 100644 index 0000000000..f2bf628465 --- /dev/null +++ b/test/logging/write.test.js @@ -0,0 +1,39 @@ +// 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 logging = require('../../logging/write'); + +describe('logging/write', function () { + it('should write entries', function (done) { + logging.write(function (err, apiResponse) { + if (err) { + return done(err); + } + assert.deepEqual(apiResponse, {}, 'should have correct response'); + done(); + }); + }); + it('should delete entries', function (done) { + logging.deleteLog(function (err, apiResponse) { + if (err) { + return done(err); + } + assert.deepEqual(apiResponse, {}, 'should have correct response'); + done(); + }); + }); +});