Skip to content

Commit cff0f4b

Browse files
committed
New Compute Engine samples.
1 parent 6173229 commit cff0f4b

File tree

11 files changed

+260
-17
lines changed

11 files changed

+260
-17
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This repository holds Node.js samples used throughout [cloud.google.com]().
99

1010
* [Google App Engine](#google-app-engine)
1111
* [Google BigQuery](#google-bigquery)
12+
* [Google Compute Engine](#google-compute-engine)
1213
* [Google Cloud Functions](#google-cloud-functions)
1314
* [Google Cloud Logging](#google-cloud-logging)
1415
* [Google Cloud Pub/Sub](#google-cloud-pubsub)
@@ -77,6 +78,11 @@ __Other Examples__
7778
- Load from file sample - [Source code][bigquery_file_1] | [Documentation][bigquery_file_2]
7879
- Load from GCS sample - [Source code][bigquery_gcs_1] | [Documentation][bigquery_gcs_2]
7980

81+
## Google Compute Engine
82+
83+
- Sendgrid sample - [Source code][compute_sendgrid_1] | [Documentation][compute_sendgrid_2]
84+
- VMs sample - [Source code][compute_vms_1] | [Documentation][compute_vms_2]
85+
8086
## Google Cloud Datastore
8187

8288
- Tasks sample - [Source code][datastore_1] | [Documentation][datastore_2]
@@ -274,6 +280,12 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
274280
[bigquery_gcs_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/bigquery/load_data_from_gcs.js
275281
[bigquery_gcs_2]: https://cloud.google.com/bigquery/loading-data-into-bigquery#loaddatagcs
276282

283+
[compute_sendgrid_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/computeengine/sendgrid.js
284+
[compute_sendgrid_2]: https://cloud.google.com/compute/docs/tutorials/sending-mail/using-sendgrid
285+
286+
[compute_vms_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/computeengine/vms.js
287+
[compute_vms_2]: https://cloud.google.com/compute/docs/tutorials/nodejs-guide
288+
277289
[datastore_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/datastore/tasks.js
278290
[datastore_2]: https://cloud.google.com/datastore/docs/concepts/overview
279291

computeengine/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Compute Engine Samples
2+
3+
These samples require two environment variables to be set:
4+
5+
- `GOOGLE_APPLICATION_CREDENTIALS` - Path to a service account file. You can
6+
download one from your Google project's "permissions" page.
7+
- `GCLOUD_PROJECT` - Id of your Google project.
8+
9+
## Run the samples
10+
11+
Install dependencies:
12+
13+
npm install
14+
15+
### sendgrid.js
16+
17+
Also required a `SENDGRID_API_KEY` environment variable to be set.
18+
19+
npm run sendgrid
20+
21+
### vms.js
22+
23+
npm run vms
24+
25+
### vms_api.js
26+
27+
npm run vms_api
28+

computeengine/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "computeengine",
3+
"description": "Collection of Node.js samples on Google Compute Engine.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"author": "Google Inc.",
8+
"engines": {
9+
"node": ">=0.10.x"
10+
},
11+
"scripts": {
12+
"sendgrid": "node sendgrid.js",
13+
"vms": "node vms.js",
14+
"vms_api": "node vms_api.js"
15+
},
16+
"dependencies": {
17+
"gcloud": "^0.30.2",
18+
"googleapis": "^4.0.0",
19+
"sendgrid": "^2.0.0"
20+
}
21+
}

computeengine/sendgrid/package.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

computeengine/vms.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
// [START complete]
15+
'use strict';
16+
17+
// [START auth]
18+
// You must set the GOOGLE_APPLICATION_CREDENTIALS and GCLOUD_PROJECT
19+
// environment variables to run this sample
20+
var projectId = process.env.GCLOUD_PROJECT;
21+
22+
// Initialize gcloud
23+
var gcloud = require('gcloud')({
24+
projectId: projectId
25+
});
26+
// [END auth]
27+
28+
// [START initialize]
29+
// Get a reference to the compute component
30+
var compute = gcloud.compute();
31+
// [END initialize]
32+
33+
// [START list]
34+
/**
35+
* @param {Function} callback Callback function.
36+
*/
37+
function getVmsExample(callback) {
38+
// In this example we only want one VM per page
39+
var options = {
40+
maxResults: 1
41+
};
42+
compute.getVMs(options, function (err, vms) {
43+
if (err) {
44+
return callback(err);
45+
}
46+
47+
console.log('VMs:', vms);
48+
callback(null, vms);
49+
});
50+
}
51+
// [END list]
52+
// [END complete]
53+
54+
// Run the examples
55+
exports.main = function (cb) {
56+
getVmsExample(cb);
57+
};
58+
59+
if (module === require.main) {
60+
exports.main(console.log);
61+
}

computeengine/vms_api.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START complete]
17+
// [START initialize]
18+
var google = require('googleapis');
19+
var compute = google.compute('v1');
20+
// [END initialize]
21+
22+
// [START auth]
23+
function auth(callback) {
24+
google.auth.getApplicationDefault(function (err, authClient) {
25+
if (err) {
26+
return callback(err);
27+
}
28+
29+
// The createScopedRequired method returns true when running on GAE or a
30+
// local developer machine. In that case, the desired scopes must be passed
31+
// in manually. When the code is running in GCE or a Managed VM, the scopes
32+
// are pulled from the GCE metadata server.
33+
// See https://cloud.google.com/compute/docs/authentication for more
34+
// information.
35+
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
36+
// Scopes can be specified either as an array or as a single,
37+
// space-delimited string.
38+
authClient = authClient.createScoped([
39+
'https://www.googleapis.com/auth/cloud-platform',
40+
'https://www.googleapis.com/auth/compute',
41+
'https://www.googleapis.com/auth/compute.readonly'
42+
]);
43+
}
44+
callback(null, authClient);
45+
});
46+
}
47+
// [END auth]
48+
49+
// [START list]
50+
/**
51+
* @param {Function} callback Callback function.
52+
*/
53+
function getVmsExample(callback) {
54+
auth(function (err, authClient) {
55+
if (err) {
56+
return callback(err);
57+
}
58+
// Retrieve the vms
59+
compute.instances.aggregatedList({
60+
auth: authClient,
61+
project: process.env.GCLOUD_PROJECT,
62+
// In this example we only want one VM per page
63+
maxResults: 1
64+
}, function (err, vms) {
65+
if (err) {
66+
return callback(err);
67+
}
68+
69+
console.log('VMs:', vms);
70+
callback(null, vms);
71+
});
72+
});
73+
}
74+
// [END list]
75+
// [END complete]
76+
77+
// Run the examples
78+
exports.main = function (cb) {
79+
getVmsExample(cb);
80+
};
81+
82+
if (module === require.main) {
83+
exports.main(console.log);
84+
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"cover": "npm run deps_appengine && nyc ava --match='!*: dependencies should install*'",
3232
"report": "nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls",
3333
"report-html": "nyc report --reporter=html",
34+
"deps_gce": "cd computeengine; npm i; cd ../",
3435
"deps_bigquery": "cd bigquery; npm i; cd ../",
3536
"deps_datastore": "cd datastore; npm i; cd ../",
3637
"deps_pubsub": "cd pubsub; npm i; cd ../",
@@ -39,9 +40,8 @@
3940
"deps_prediction": "cd prediction; npm i; cd ../",
4041
"deps_logging": "cd logging; npm i; cd ../",
4142
"deps_functions": "cd functions/uuid; npm i; cd ../..",
42-
"deps_sendgrid": "cd computeengine/sendgrid; npm i; cd ../..",
4343
"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 ../..;",
44-
"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",
44+
"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",
4545
"test": "npm run jshint && npm run cover"
4646
},
4747
"ava": {

test/computeengine/sendgrid.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var proxyquire = require('proxyquire').noPreserveCache();
1818
process.env.SENDGRID_API_KEY = 'foo';
1919

2020
test.cb('should send an email', function (t) {
21-
proxyquire('../../computeengine/sendgrid/sendmail.js', {
21+
proxyquire('../../computeengine/sendgrid.js', {
2222
sendgrid: function (key) {
2323
t.is(key, 'foo');
2424
return {

test/computeengine/vms.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2016, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var test = require('ava');
17+
var vmsExample = require('../../computeengine/vms');
18+
19+
test.cb('should retrieve vms', function (t) {
20+
vmsExample.main(function (err, result) {
21+
t.ifError(err);
22+
t.ok(result);
23+
t.is(Array.isArray(result), true);
24+
t.end();
25+
});
26+
});

0 commit comments

Comments
 (0)