diff --git a/README.md b/README.md index 468b6c1961..2019545dee 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This repository holds Node.js samples used throughout [cloud.google.com](). * [Google App Engine](#google-app-engine) * [Google BigQuery](#google-bigquery) +* [Google Compute Engine](#google-compute-engine) * [Google Cloud Functions](#google-cloud-functions) * [Google Cloud Logging](#google-cloud-logging) * [Google Cloud Pub/Sub](#google-cloud-pubsub) @@ -77,6 +78,11 @@ __Other Examples__ - Load from file sample - [Source code][bigquery_file_1] | [Documentation][bigquery_file_2] - Load from GCS sample - [Source code][bigquery_gcs_1] | [Documentation][bigquery_gcs_2] +## Google Compute Engine + +- Sendgrid sample - [Source code][compute_sendgrid_1] | [Documentation][compute_sendgrid_2] +- VMs sample - [Source code][compute_vms_1] | [Documentation][compute_vms_2] + ## Google Cloud Datastore - Tasks sample - [Source code][datastore_1] | [Documentation][datastore_2] @@ -274,6 +280,12 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma [bigquery_gcs_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/bigquery/load_data_from_gcs.js [bigquery_gcs_2]: https://cloud.google.com/bigquery/loading-data-into-bigquery#loaddatagcs +[compute_sendgrid_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/computeengine/sendgrid.js +[compute_sendgrid_2]: https://cloud.google.com/compute/docs/tutorials/sending-mail/using-sendgrid + +[compute_vms_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/computeengine/vms.js +[compute_vms_2]: https://cloud.google.com/compute/docs/tutorials/nodejs-guide + [datastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/datastore/tasks.js [datastore_2]: https://cloud.google.com/datastore/docs/concepts/overview diff --git a/computeengine/README.md b/computeengine/README.md new file mode 100644 index 0000000000..bed0d54b3f --- /dev/null +++ b/computeengine/README.md @@ -0,0 +1,28 @@ +## Compute Engine 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 the samples + +Install dependencies: + + npm install + +### sendgrid.js + +Also required a `SENDGRID_API_KEY` environment variable to be set. + + npm run sendgrid + +### vms.js + + npm run vms + +### vms_api.js + + npm run vms_api + diff --git a/computeengine/package.json b/computeengine/package.json new file mode 100644 index 0000000000..b080b4b275 --- /dev/null +++ b/computeengine/package.json @@ -0,0 +1,21 @@ +{ + "name": "computeengine", + "description": "Collection of Node.js samples on Google Compute Engine.", + "version": "0.0.1", + "private": true, + "license": "Apache Version 2.0", + "author": "Google Inc.", + "engines": { + "node": ">=0.10.x" + }, + "scripts": { + "sendgrid": "node sendgrid.js", + "vms": "node vms.js", + "vms_api": "node vms_api.js" + }, + "dependencies": { + "gcloud": "^0.30.2", + "googleapis": "^4.0.0", + "sendgrid": "^2.0.0" + } +} diff --git a/computeengine/sendgrid/sendmail.js b/computeengine/sendgrid.js similarity index 100% rename from computeengine/sendgrid/sendmail.js rename to computeengine/sendgrid.js diff --git a/computeengine/sendgrid/package.json b/computeengine/sendgrid/package.json deleted file mode 100644 index 0b8e86679f..0000000000 --- a/computeengine/sendgrid/package.json +++ /dev/null @@ -1,14 +0,0 @@ -{ - "name": "computeengine-sendgrid", - "description": "An example of sending an email with Sendgrid in Node.js on Google Compute Engine.", - "version": "0.0.1", - "private": true, - "license": "Apache Version 2.0", - "author": "Google Inc.", - "engines": { - "node": ">=0.10.x" - }, - "dependencies": { - "sendgrid": "^2.0.0" - } -} diff --git a/computeengine/vms.js b/computeengine/vms.js new file mode 100644 index 0000000000..e891e0861e --- /dev/null +++ b/computeengine/vms.js @@ -0,0 +1,61 @@ +// 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. + +// [START complete] +'use strict'; + +// [START auth] +// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT +// environment variables to run this sample +var projectId = process.env.GCLOUD_PROJECT; + +// Initialize gcloud +var gcloud = require('gcloud')({ + projectId: projectId +}); +// [END auth] + +// [START initialize] +// Get a reference to the compute component +var compute = gcloud.compute(); +// [END initialize] + +// [START list] +/** + * @param {Function} callback Callback function. + */ +function getVmsExample(callback) { + // In this example we only want one VM per page + var options = { + maxResults: 1 + }; + compute.getVMs(options, function (err, vms) { + if (err) { + return callback(err); + } + + console.log('VMs:', vms); + callback(null, vms); + }); +} +// [END list] +// [END complete] + +// Run the examples +exports.main = function (cb) { + getVmsExample(cb); +}; + +if (module === require.main) { + exports.main(console.log); +} diff --git a/computeengine/vms_api.js b/computeengine/vms_api.js new file mode 100644 index 0000000000..c39bbeb209 --- /dev/null +++ b/computeengine/vms_api.js @@ -0,0 +1,84 @@ +// 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 complete] +// [START initialize] +var google = require('googleapis'); +var compute = google.compute('v1'); +// [END initialize] + +// [START auth] +function auth(callback) { + google.auth.getApplicationDefault(function (err, authClient) { + if (err) { + return callback(err); + } + + // The createScopedRequired method returns true when running on GAE or a + // local developer machine. In that case, the desired scopes must be passed + // in manually. When the code is running in GCE or a Managed VM, the scopes + // are pulled from the GCE metadata server. + // See https://cloud.google.com/compute/docs/authentication for more + // information. + if (authClient.createScopedRequired && authClient.createScopedRequired()) { + // Scopes can be specified either as an array or as a single, + // space-delimited string. + authClient = authClient.createScoped([ + 'https://www.googleapis.com/auth/cloud-platform', + 'https://www.googleapis.com/auth/compute', + 'https://www.googleapis.com/auth/compute.readonly' + ]); + } + callback(null, authClient); + }); +} +// [END auth] + +// [START list] +/** + * @param {Function} callback Callback function. + */ +function getVmsExample(callback) { + auth(function (err, authClient) { + if (err) { + return callback(err); + } + // Retrieve the vms + compute.instances.aggregatedList({ + auth: authClient, + project: process.env.GCLOUD_PROJECT, + // In this example we only want one VM per page + maxResults: 1 + }, function (err, vms) { + if (err) { + return callback(err); + } + + console.log('VMs:', vms); + callback(null, vms); + }); + }); +} +// [END list] +// [END complete] + +// Run the examples +exports.main = function (cb) { + getVmsExample(cb); +}; + +if (module === require.main) { + exports.main(console.log); +} diff --git a/package.json b/package.json index f49f56d768..d6f11e363a 100644 --- a/package.json +++ b/package.json @@ -31,6 +31,7 @@ "cover": "npm run deps_appengine && nyc ava --match='!*: dependencies should install*'", "report": "nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls", "report-html": "nyc report --reporter=html", + "deps_gce": "cd computeengine; npm i; cd ../", "deps_bigquery": "cd bigquery; npm i; cd ../", "deps_datastore": "cd datastore; npm i; cd ../", "deps_pubsub": "cd pubsub; npm i; cd ../", @@ -39,9 +40,8 @@ "deps_prediction": "cd prediction; npm i; cd ../", "deps_logging": "cd logging; npm i; cd ../", "deps_functions": "cd functions/uuid; npm i; cd ../..", - "deps_sendgrid": "cd computeengine/sendgrid; 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_bigquery; npm run deps_datastore; npm run deps_monitoring; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run deps_sendgrid; npm run pretest_geddy", + "pretest": "npm run deps_gce; npm run deps_bigquery; npm run deps_datastore; npm run deps_monitoring; npm run deps_storage; npm run deps_pubsub; npm run deps_prediction; npm run deps_logging; npm run deps_functions; npm run pretest_geddy", "test": "npm run jshint && npm run cover" }, "ava": { diff --git a/test/computeengine/sendgrid.test.js b/test/computeengine/sendgrid.test.js index 8c5820164c..a5b065587f 100644 --- a/test/computeengine/sendgrid.test.js +++ b/test/computeengine/sendgrid.test.js @@ -18,7 +18,7 @@ var proxyquire = require('proxyquire').noPreserveCache(); process.env.SENDGRID_API_KEY = 'foo'; test.cb('should send an email', function (t) { - proxyquire('../../computeengine/sendgrid/sendmail.js', { + proxyquire('../../computeengine/sendgrid.js', { sendgrid: function (key) { t.is(key, 'foo'); return { diff --git a/test/computeengine/vms.test.js b/test/computeengine/vms.test.js new file mode 100644 index 0000000000..bbb129d536 --- /dev/null +++ b/test/computeengine/vms.test.js @@ -0,0 +1,26 @@ +// 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 test = require('ava'); +var vmsExample = require('../../computeengine/vms'); + +test.cb('should retrieve vms', function (t) { + vmsExample.main(function (err, result) { + t.ifError(err); + t.ok(result); + t.is(Array.isArray(result), true); + t.end(); + }); +}); diff --git a/test/computeengine/vms_api.test.js b/test/computeengine/vms_api.test.js new file mode 100644 index 0000000000..d058e5114a --- /dev/null +++ b/test/computeengine/vms_api.test.js @@ -0,0 +1,25 @@ +// 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 test = require('ava'); +var vmsExample = require('../../computeengine/vms_api'); + +test.cb('should retrieve vms', function (t) { + vmsExample.main(function (err, result) { + t.ifError(err); + t.ok(result); + t.end(); + }); +});