Skip to content

Commit

Permalink
New Compute Engine samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Apr 5, 2016
1 parent 6173229 commit cff0f4b
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 17 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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

Expand Down
28 changes: 28 additions & 0 deletions computeengine/README.md
Original file line number Diff line number Diff line change
@@ -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

21 changes: 21 additions & 0 deletions computeengine/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
File renamed without changes.
14 changes: 0 additions & 14 deletions computeengine/sendgrid/package.json

This file was deleted.

61 changes: 61 additions & 0 deletions computeengine/vms.js
Original file line number Diff line number Diff line change
@@ -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);
}
84 changes: 84 additions & 0 deletions computeengine/vms_api.js
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 ../",
Expand All @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion test/computeengine/sendgrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions test/computeengine/vms.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
25 changes: 25 additions & 0 deletions test/computeengine/vms_api.test.js
Original file line number Diff line number Diff line change
@@ -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();
});
});

0 comments on commit cff0f4b

Please sign in to comment.