-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgce-instance.js
186 lines (170 loc) · 7.27 KB
/
gce-instance.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/**
* Copyright 2022 Google Inc.
* Created by Ari Victor
*
* 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.
*
* This node GCE instance operations. We will assume that msg.payload
* contains the the configuration required if not present in `config`.
*/
/* jshint esversion: 8 */
module.exports = function (RED) {
"use strict";
const compute = require("@google-cloud/compute");
const NODE_TYPE = "google-cloud-compute-engine-instance";
function GCEInstanceNode(config) {
RED.nodes.createNode(this, config);
const node = this;
const Input = async (msg, send, done) => {
// Handle inputs
const account = config.account ? config.account : msg.payload.account;
const instance = config.instance ? config.instance : msg.payload.instance;
const keyFilename = config.keyFilename ? config.keyFilename : msg.payload.keyFilename
const operation = config.operation ? config.operation : msg.payload.operation
const projectId = config.projectId ? config.projectId : msg.payload.projectId
const template = config.template ? config.template : msg.payload.template
const zone = config.zone ? config.zone : msg.payload.zone
let credentials;
if (account) credentials = JSON.parse(RED.nodes.getCredentials(node).account);
const scopes = [
'https://www.googleapis.com/auth/cloud-platform',
'https://www.googleapis.com/auth/compute',
]
let computeClient;
if (credentials) {
computeClient = new compute.InstancesClient({
"projectId": projectId,
"credentials": credentials,
"scopes": scopes
});
} else if (keyFilename) {
computeClient = new compute.InstancesClient({
"projectId": projectId,
"keyFilename": keyFilename,
"scopes": scopes
});
} else {
computeClient = new compute.InstancesClient({
"projectId": projectId,
"scopes": scopes
});
}
try {
/* Handle Get Instance */
if (operation === "get") {
node.status({fill:"blue",shape:"dot",text:"getting instance"});
await computeClient
.get({
instance: instance,
project: projectId,
zone: zone
})
.then(response => {
msg.payload = response.length ? response[0] : {};
});
}
/* Handle List Instances */
else if (operation === "list") {
node.status({fill:"blue",shape:"dot",text:"listing instances"});
await computeClient
.list({
project: projectId,
zone: zone
})
.then(response => {
msg.payload = response.length ? response[0] : [];
});
}
/* Handle Stop Instance */
else if (operation === "stop") {
node.status({fill:"blue",shape:"dot",text:"stopping instance"});
await computeClient
.stop({
instance: instance,
project: projectId,
zone: zone
})
.then(response => {
msg.payload = response.length ? response[0] : {};
});
}
/* Handle Start Instance */
else if (operation === "start") {
node.status({fill:"blue",shape:"dot",text:"starting instance"});
await computeClient
.start({
instance: instance,
project: projectId,
zone: zone
})
.then(response => {
msg.payload = response.length ? response[0] : {};
});
}
/* Handle Reset Instance */
else if (operation === "reset") {
node.status({fill:"blue",shape:"dot",text:"resetting instance"});
await computeClient
.reset({
instance: instance,
project: projectId,
zone: zone
})
.then(response => {
msg.payload = response.length ? response[0] : {};
});
}
/* Handle Delete Instance */
else if (operation === "delete") {
node.status({fill:"blue",shape:"dot",text:"deleting instance"});
await computeClient
.delete({
instance: instance,
project: projectId,
zone: zone
})
.then(response => {
msg.payload = response.length ? response[0] : {};
});
}
/* Handle Create Instance */
else if (operation === "create") {
node.status({fill:"blue",shape:"dot",text:"creating instance"});
await computeClient
.insert({
project: projectId,
zone: zone,
instanceResource: typeof template === "string" ? JSON.parse(template) : template
})
.then(response => {
msg.payload = response.length ? response[0] : {};
});
}
node.status({fill:"green",shape:"dot",text:"complete"});
node.send(msg);
}
catch (error) {
node.status({fill:"red",shape:"dot",text:"error"});
if (done) {
done(error);
} else {
node.err(error, msg);
}
}
}
const Close = () => { };
node.on("input", Input);
node.on("close", Close);
}
RED.nodes.registerType(NODE_TYPE, GCEInstanceNode);
}