forked from edwardchuang/gcp-dns-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcp.js
46 lines (41 loc) · 1.28 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
/*
* gcp.js
*
* Compute Engine API wrapper
*/
"use strict";
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(resultcallback) {
var results = [];
this.gce.getVMs(function(err, vms) {
if (null == vms || 0 == vms.length) {
return;
}
for(var i = 0 ; i < vms.length ; i++) {
var key = vms[i];
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"]});
}
}
});
this.gce.getVMs()
.on('error', console.error)
.on('data', function(vm) { /* intentionally empty function */ })
.on('end', function() { resultcallback(results); });
}
}
module.exports = GCP;