Skip to content

Commit 0bce471

Browse files
committed
start/stop GCE instances Cloud Function linting updates
1 parent 1c3740e commit 0bce471

File tree

2 files changed

+85
-87
lines changed

2 files changed

+85
-87
lines changed

functions/scheduleinstance/index.js

+41-43
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,28 @@ exports.startInstance = (req, res) => {
2020
try {
2121
const reqBody = _validateReqBody(_parseReqBody(_validateReq(req)));
2222
compute.zone(reqBody.zone)
23-
.vm(reqBody.instance)
24-
.start()
25-
.then(data => {
26-
// Operation pending.
27-
const operation = data[0];
28-
return operation.promise();
29-
})
30-
.then(() => {
31-
// Operation complete. Instance successfully started.
32-
const message = 'Successfully started instance ' + reqBody.instance;
33-
console.log(message);
34-
res.status(200).send(message);
35-
})
36-
.catch(err => {
37-
console.log(err);
38-
res.status(500).send({error: err.message});
39-
});
23+
.vm(reqBody.instance)
24+
.start()
25+
.then(data => {
26+
// Operation pending.
27+
const operation = data[0];
28+
return operation.promise();
29+
})
30+
.then(() => {
31+
// Operation complete. Instance successfully started.
32+
const message = 'Successfully started instance ' + reqBody.instance;
33+
console.log(message);
34+
res.status(200).send(message);
35+
})
36+
.catch(err => {
37+
console.log(err);
38+
res.status(500).send({error: err.message});
39+
});
4040
} catch (err) {
4141
console.log(err);
4242
res.status(400).send({error: err.message});
43-
} finally {
44-
return res;
4543
}
44+
return res;
4645
};
4746
// [END functions_start_instance_http]
4847

@@ -63,29 +62,28 @@ exports.stopInstance = (req, res) => {
6362
try {
6463
const reqBody = _validateReqBody(_parseReqBody(_validateReq(req)));
6564
compute.zone(reqBody.zone)
66-
.vm(reqBody.instance)
67-
.stop()
68-
.then(data => {
69-
// Operation pending.
70-
const operation = data[0];
71-
return operation.promise();
72-
})
73-
.then(() => {
74-
// Operation complete. Instance successfully stopped.
75-
const message = 'Successfully stopped instance ' + reqBody.instance;
76-
console.log(message);
77-
res.status(200).send(message);
78-
})
79-
.catch(err => {
80-
console.log(err);
81-
res.status(500).send({error: err.message});
82-
});
65+
.vm(reqBody.instance)
66+
.stop()
67+
.then(data => {
68+
// Operation pending.
69+
const operation = data[0];
70+
return operation.promise();
71+
})
72+
.then(() => {
73+
// Operation complete. Instance successfully stopped.
74+
const message = 'Successfully stopped instance ' + reqBody.instance;
75+
console.log(message);
76+
res.status(200).send(message);
77+
})
78+
.catch(err => {
79+
console.log(err);
80+
res.status(500).send({error: err.message});
81+
});
8382
} catch (err) {
8483
console.log(err);
8584
res.status(400).send({error: err.message});
86-
} finally {
87-
return res;
8885
}
86+
return res;
8987
};
9088
// [START functions_start_instance_http]
9189

@@ -95,7 +93,7 @@ exports.stopInstance = (req, res) => {
9593
* @param {!object} req a Cloud Functions HTTP request object.
9694
* @returns {!object} an object with attributes matching the HTTP request body.
9795
*/
98-
function _parseReqBody(req) {
96+
function _parseReqBody (req) {
9997
const contentType = req.get('content-type');
10098
if (contentType === 'application/json') {
10199
// Request.body automatically parsed as an object.
@@ -115,7 +113,7 @@ function _parseReqBody(req) {
115113
* @param {!object} reqBody the request body to validate.
116114
* @returns {!object} the request body object.
117115
*/
118-
function _validateReqBody(reqBody) {
116+
function _validateReqBody (reqBody) {
119117
if (!reqBody.zone) {
120118
throw new Error(`Attribute 'zone' missing from POST request`);
121119
} else if (!reqBody.instance) {
@@ -130,10 +128,10 @@ function _validateReqBody(reqBody) {
130128
* @param {!object} req the request to validate.
131129
* @returns {!object} the request object.
132130
*/
133-
function _validateReq(req) {
131+
function _validateReq (req) {
134132
if (req.method !== 'POST') {
135-
throw new Error(
136-
'Unsupported HTTP method ' + req.method + '; use method POST');
133+
throw new Error('Unsupported HTTP method ' + req.method +
134+
'; use method POST');
137135
} else if (typeof req.get('content-type') === 'undefined') {
138136
throw new Error('HTTP content-type missing');
139137
}

functions/scheduleinstance/test/index.test.js

+44-44
Original file line numberDiff line numberDiff line change
@@ -59,221 +59,221 @@ test(`startInstance: should accept application/json`, async (t) => {
5959
const sample = getSample();
6060
mocks.req.method = `POST`;
6161
mocks.req.headers[`content-type`] = `application/json`;
62-
mocks.req.body = {zone:`test-zone`, instance:`test-instance`};
62+
mocks.req.body = {zone: `test-zone`, instance: `test-instance`};
6363
sample.program.startInstance(mocks.req, mocks.res);
6464

6565
sample.mocks.requestPromise()
66-
.then((data) => {
67-
// The request was successfully sent.
68-
t.deepEqual(data, 'request sent');
69-
});
66+
.then((data) => {
67+
// The request was successfully sent.
68+
t.deepEqual(data, 'request sent');
69+
});
7070
});
7171

7272
test(`startInstance: should accept application/octect-stream`, async (t) => {
7373
const mocks = getMocks();
7474
const sample = getSample();
7575
mocks.req.method = `POST`;
7676
mocks.req.headers[`content-type`] = `application/octet-stream`;
77-
mocks.req.body = Buffer.from(`{'zone':'test-zone', 'instance':'test-instance'}`);
77+
mocks.req.body = Buffer.from(`{'zone': 'test-zone', 'instance': 'test-instance'}`);
7878
sample.program.startInstance(mocks.req, mocks.res);
7979

8080
sample.mocks.requestPromise()
81-
.then((data) => {
82-
// The request was successfully sent.
83-
t.deepEqual(data, 'request sent');
84-
});
81+
.then((data) => {
82+
// The request was successfully sent.
83+
t.deepEqual(data, 'request sent');
84+
});
8585
});
8686

8787
test(`startInstance: should fail missing HTTP request method`, async (t) => {
8888
const mocks = getMocks();
8989
const sample = getSample();
9090
mocks.req.headers[`content-type`] = `application/json`;
91-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
91+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
9292
sample.program.startInstance(mocks.req, mocks.res);
9393

9494
t.true(mocks.res.status.calledOnce);
9595
t.is(mocks.res.status.firstCall.args[0], 400);
9696
t.true(mocks.res.send.calledOnce);
97-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'Unsupported HTTP method undefined; use method POST'});
97+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'Unsupported HTTP method undefined; use method POST'});
9898
});
9999

100100
test(`startInstance: should reject HTTP GET request`, async (t) => {
101101
const mocks = getMocks();
102102
const sample = getSample();
103103
mocks.req.method = `GET`;
104104
mocks.req.headers[`content-type`] = `application/json`;
105-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
105+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
106106
sample.program.startInstance(mocks.req, mocks.res);
107107

108108
t.true(mocks.res.status.calledOnce);
109109
t.is(mocks.res.status.firstCall.args[0], 400);
110110
t.true(mocks.res.send.calledOnce);
111-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'Unsupported HTTP method GET; use method POST'});
111+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'Unsupported HTTP method GET; use method POST'});
112112
});
113113

114114
test(`startInstance: should fail missing content-type header`, async (t) => {
115115
const mocks = getMocks();
116116
const sample = getSample();
117117
mocks.req.method = `POST`;
118-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
118+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
119119
sample.program.startInstance(mocks.req, mocks.res);
120120

121121
t.true(mocks.res.status.calledOnce);
122122
t.is(mocks.res.status.firstCall.args[0], 400);
123123
t.true(mocks.res.send.calledOnce);
124-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'HTTP content-type missing'});
124+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'HTTP content-type missing'});
125125
});
126126

127127
test(`startInstance: should reject unsupported HTTP content-type`, async (t) => {
128128
const mocks = getMocks();
129129
const sample = getSample();
130130
mocks.req.method = `POST`;
131131
mocks.req.headers[`content-type`] = `text/plain`;
132-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
132+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
133133
sample.program.startInstance(mocks.req, mocks.res);
134134

135135
t.true(mocks.res.status.calledOnce);
136136
t.is(mocks.res.status.firstCall.args[0], 400);
137137
t.true(mocks.res.send.calledOnce);
138-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'Unsupported HTTP content-type text/plain; use application/json or application/octet-stream'});
138+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'Unsupported HTTP content-type text/plain; use application/json or application/octet-stream'});
139139
});
140140

141141
test(`startInstance: should fail with missing 'zone' attribute`, async (t) => {
142142
const mocks = getMocks();
143143
const sample = getSample();
144144
mocks.req.method = `POST`;
145145
mocks.req.headers[`content-type`] = `application/json`;
146-
mocks.req.body = {"instance":"test-instance"};
146+
mocks.req.body = {'instance': 'test-instance'};
147147
sample.program.startInstance(mocks.req, mocks.res);
148148

149149
t.true(mocks.res.status.calledOnce);
150150
t.is(mocks.res.status.firstCall.args[0], 400);
151151
t.true(mocks.res.send.calledOnce);
152-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:`Attribute 'zone' missing from POST request`});
152+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: `Attribute 'zone' missing from POST request`});
153153
});
154154

155155
test(`startInstance: should fail with missing 'instance' attribute`, async (t) => {
156156
const mocks = getMocks();
157157
const sample = getSample();
158158
mocks.req.method = `POST`;
159159
mocks.req.headers[`content-type`] = `application/json`;
160-
mocks.req.body = {"zone":"test-zone"};
160+
mocks.req.body = {'zone': 'test-zone'};
161161
sample.program.startInstance(mocks.req, mocks.res);
162162

163163
t.true(mocks.res.status.calledOnce);
164164
t.is(mocks.res.status.firstCall.args[0], 400);
165165
t.true(mocks.res.send.calledOnce);
166-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:`Attribute 'instance' missing from POST request`});
166+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: `Attribute 'instance' missing from POST request`});
167167
});
168168

169169
test(`stopInstance: should accept application/json`, async (t) => {
170170
const mocks = getMocks();
171171
const sample = getSample();
172172
mocks.req.method = `POST`;
173173
mocks.req.headers[`content-type`] = `application/json`;
174-
mocks.req.body = {zone:`test-zone`, instance:`test-instance`};
174+
mocks.req.body = {zone: `test-zone`, instance: `test-instance`};
175175
sample.program.stopInstance(mocks.req, mocks.res);
176176

177177
sample.mocks.requestPromise()
178-
.then((data) => {
179-
// The request was successfully sent.
180-
t.deepEqual(data, 'request sent');
181-
});
178+
.then((data) => {
179+
// The request was successfully sent.
180+
t.deepEqual(data, 'request sent');
181+
});
182182
});
183183

184184
test(`stopInstance: should accept application/octect-stream`, async (t) => {
185185
const mocks = getMocks();
186186
const sample = getSample();
187187
mocks.req.method = `POST`;
188188
mocks.req.headers[`content-type`] = `application/octet-stream`;
189-
mocks.req.body = Buffer.from(`{'zone':'test-zone', 'instance':'test-instance'}`);
189+
mocks.req.body = Buffer.from(`{'zone': 'test-zone', 'instance': 'test-instance'}`);
190190
sample.program.stopInstance(mocks.req, mocks.res);
191191

192192
sample.mocks.requestPromise()
193-
.then((data) => {
194-
// The request was successfully sent.
195-
t.deepEqual(data, 'request sent');
196-
});
193+
.then((data) => {
194+
// The request was successfully sent.
195+
t.deepEqual(data, 'request sent');
196+
});
197197
});
198198

199199
test(`stopInstance: should fail missing HTTP request method`, async (t) => {
200200
const mocks = getMocks();
201201
const sample = getSample();
202202
mocks.req.headers[`content-type`] = `application/json`;
203-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
203+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
204204
sample.program.stopInstance(mocks.req, mocks.res);
205205

206206
t.true(mocks.res.status.calledOnce);
207207
t.is(mocks.res.status.firstCall.args[0], 400);
208208
t.true(mocks.res.send.calledOnce);
209-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'Unsupported HTTP method undefined; use method POST'});
209+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'Unsupported HTTP method undefined; use method POST'});
210210
});
211211

212212
test(`stopInstance: should reject HTTP GET request`, async (t) => {
213213
const mocks = getMocks();
214214
const sample = getSample();
215215
mocks.req.method = `GET`;
216216
mocks.req.headers[`content-type`] = `application/json`;
217-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
217+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
218218
sample.program.stopInstance(mocks.req, mocks.res);
219219

220220
t.true(mocks.res.status.calledOnce);
221221
t.is(mocks.res.status.firstCall.args[0], 400);
222222
t.true(mocks.res.send.calledOnce);
223-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'Unsupported HTTP method GET; use method POST'});
223+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'Unsupported HTTP method GET; use method POST'});
224224
});
225225

226226
test(`stopInstance: should fail missing content-type header`, async (t) => {
227227
const mocks = getMocks();
228228
const sample = getSample();
229229
mocks.req.method = `POST`;
230-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
230+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
231231
sample.program.stopInstance(mocks.req, mocks.res);
232232

233233
t.true(mocks.res.status.calledOnce);
234234
t.is(mocks.res.status.firstCall.args[0], 400);
235235
t.true(mocks.res.send.calledOnce);
236-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'HTTP content-type missing'});
236+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'HTTP content-type missing'});
237237
});
238238

239239
test(`stopInstance: should reject unsupported HTTP content-type`, async (t) => {
240240
const mocks = getMocks();
241241
const sample = getSample();
242242
mocks.req.method = `POST`;
243243
mocks.req.headers[`content-type`] = `text/plain`;
244-
mocks.req.body = {"zone":"test-zone", "instance":"test-instance"};
244+
mocks.req.body = {'zone': 'test-zone', 'instance': 'test-instance'};
245245
sample.program.stopInstance(mocks.req, mocks.res);
246246

247247
t.true(mocks.res.status.calledOnce);
248248
t.is(mocks.res.status.firstCall.args[0], 400);
249249
t.true(mocks.res.send.calledOnce);
250-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:'Unsupported HTTP content-type text/plain; use application/json or application/octet-stream'});
250+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: 'Unsupported HTTP content-type text/plain; use application/json or application/octet-stream'});
251251
});
252252

253253
test(`stopInstance: should fail with missing 'zone' attribute`, async (t) => {
254254
const mocks = getMocks();
255255
const sample = getSample();
256256
mocks.req.method = `POST`;
257257
mocks.req.headers[`content-type`] = `application/json`;
258-
mocks.req.body = {"instance":"test-instance"};
258+
mocks.req.body = {'instance': 'test-instance'};
259259
sample.program.stopInstance(mocks.req, mocks.res);
260260

261261
t.true(mocks.res.status.calledOnce);
262262
t.is(mocks.res.status.firstCall.args[0], 400);
263263
t.true(mocks.res.send.calledOnce);
264-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:`Attribute 'zone' missing from POST request`});
264+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: `Attribute 'zone' missing from POST request`});
265265
});
266266

267267
test(`stopInstance: should fail with missing 'instance' attribute`, async (t) => {
268268
const mocks = getMocks();
269269
const sample = getSample();
270270
mocks.req.method = `POST`;
271271
mocks.req.headers[`content-type`] = `application/json`;
272-
mocks.req.body = {"zone":"test-zone"};
272+
mocks.req.body = {'zone': 'test-zone'};
273273
sample.program.stopInstance(mocks.req, mocks.res);
274274

275275
t.true(mocks.res.status.calledOnce);
276276
t.is(mocks.res.status.firstCall.args[0], 400);
277277
t.true(mocks.res.send.calledOnce);
278-
t.deepEqual(mocks.res.send.firstCall.args[0], {error:`Attribute 'instance' missing from POST request`});
278+
t.deepEqual(mocks.res.send.firstCall.args[0], {error: `Attribute 'instance' missing from POST request`});
279279
});

0 commit comments

Comments
 (0)