-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
260 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |