-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcp.js
53 lines (47 loc) · 1.54 KB
/
gcp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
* gcp.js
*
* Compute Engine API wrapper
*/
"use strict";
var async = require('async');
var util = require('util');
class GCP {
constructor(project) {
var gcloud = require('google-cloud');
var current = projects.filter(function (o) { return (o.project == project || -1 != o.alias.indexOf(project)); });
this.error = false;
if (0 == current.length) {
console.log('project id or alias: \'' + project + '\' not found');
this.error = true;
return false;
}
this.gce = gcloud.compute({
projectId: current[0].project,
keyFilename: config["key_dir"] + current[0].key
});
}
getInstances(keyword, resultcallback) {
var filter = util.format('(name eq %s*) (status eq RUNNING)', keyword)
var results = [];
this.gce.getVMs({'filter': filter}, function(err, vms) {
async.eachSeries(vms, function(key, doneOfEach){
if ('RUNNING' == key.metadata.status) {
results.push({'name': key.name, 'privateIP': key.metadata.networkInterfaces[0].networkIP, 'publicIP': key.metadata.networkInterfaces[0].accessConfigs[0].natIP, 'TTL': config["default_ttl"]});
}
return doneOfEach(null);
}, function(err, rs) {
if (!err) {
resultcallback(results);
} else {
console.log(err);
}
});
});
this.gce.getVMs()
.on('error', console.error)
.on('data', function(vm) { /* intentionally empty function */ })
.on('end', function() { /* intentionally empty function */});
}
}
module.exports = GCP;