Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit b6febc0

Browse files
committed
Add cancelBuild() function
1 parent 9a71a2c commit b6febc0

File tree

4 files changed

+110
-24
lines changed

4 files changed

+110
-24
lines changed

README.md

+59-21
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,14 @@ The following functions are exported:
5656
* `makeHMAC()` to generate an SHA256 HMAC from a string and key
5757
* `buildRequest()` to generate a request object to be sent to the `apiCall()` function
5858
* `apiCall()` to send the actual request data to the Meta API
59+
60+
The following helper functions are exported:
61+
5962
* `buildOVA()` to build an OVA through the Meta API
6063
* `getStatus()` to obtain the status of an OVA build
6164
* `pollStatus()` to poll the status of an OVA build (every 5 seconds)
6265
* `getDownloads()` to obtain the list of download files for an OVA build
66+
* `cancelBuild()` to cancel a running OVA build
6367

6468
See the usage docs below.
6569

@@ -151,7 +155,21 @@ meta.buildRequest apiParams, (error, result) =>
151155
coffee> { Status: '200 OK' }
152156
```
153157

154-
#### Build an OVA (returns the builddate)
158+
#### Change a NodeJS `http.request()` option (example: `family` (for IPv6))
159+
160+
```coffee
161+
meta.options.agent = new https.Agent { family: 6 }
162+
meta.buildRequest undefined, (error, result) =>
163+
unless error
164+
meta.apiCall result, (err, res, data) ->
165+
console.log data
166+
```
167+
168+
### Helper examples
169+
170+
The following helper functions are designed to simplify API calls and return simple string results
171+
172+
#### buildOVA() - Build an OVA (returns the builddate)
155173

156174
```coffee
157175
apiParams =
@@ -169,7 +187,7 @@ meta.buildOVA "/path/to/your/app.tcz", apiParams, (err, res) ->
169187
coffee> 1574834281.966265128
170188
```
171189

172-
#### Poll the status of an OVA (returns the status)
190+
#### pollStatus() - Poll the status of an OVA (returns the status)
173191

174192
```coffee
175193
meta.pollStatus '1574834281.966265128', undefined, (err, res) ->
@@ -182,7 +200,7 @@ meta.pollStatus '1574834281.966265128', undefined, (err, res) ->
182200
coffee> success
183201
```
184202

185-
#### Get the list of download URLs
203+
#### getDownloads() - Get the list of download URLs (returns a list of URLs)
186204

187205
```coffee
188206
meta.getDownloads '1574834281.966265128', (err, res) ->
@@ -195,14 +213,17 @@ meta.getDownloads '1574834281.966265128', (err, res) ->
195213
coffee> https://yourdomain.com:443/downloads/build-1574834281.966265128/your-appliance-v1.2.3-release.ova
196214
```
197215

198-
#### Change a NodeJS `http.request()` option (example: `family` (for IPv6))
216+
#### cancelBuild() - Cancel an OVA build (returns OK)
199217

200218
```coffee
201-
meta.options.agent = new https.Agent { family: 6 }
202-
meta.buildRequest undefined, (error, result) =>
203-
unless error
204-
meta.apiCall result, (err, res, data) ->
205-
console.log data
219+
meta.cancelBuild '1574834281.966265128', (err, res) ->
220+
if err
221+
console.error err
222+
process.exit 1
223+
else
224+
console.log res
225+
226+
coffee> OK
206227
```
207228

208229
## JavaScript
@@ -284,7 +305,24 @@ meta.buildRequest(apiParams, (error, result) => {
284305
});
285306
```
286307

287-
#### Build an OVA (returns the builddate)
308+
#### Change a NodeJS `http.request()` option (example: `family` (for IPv6))
309+
310+
```js
311+
meta.options.agent = new https.Agent({ family: 6 });
312+
meta.buildRequest(void 0, (error, result) => {
313+
if (!error) {
314+
return meta.apiCall(result, function(err, res, data) {
315+
return console.log(data);
316+
});
317+
}
318+
});
319+
```
320+
321+
### Helper examples
322+
323+
The following helper functions are designed to simplify API calls and return simple string results
324+
325+
#### buildOVA() - Build an OVA (returns the builddate)
288326

289327
```js
290328
apiParams = {
@@ -303,7 +341,7 @@ meta.buildOVA("/path/to/your/app.tcz", apiParams, function(err, res) {
303341
});
304342
```
305343

306-
#### Poll the status of an OVA (returns the status object)
344+
#### pollStatus() - Poll the status of an OVA (returns the status)
307345

308346
```js
309347
meta.pollStatus('1574834281.966265128', void 0, function(err, res) {
@@ -316,7 +354,7 @@ meta.pollStatus('1574834281.966265128', void 0, function(err, res) {
316354
});
317355
```
318356

319-
#### Get the list of download URLs
357+
#### getDownloads() - Get the list of download URLs (returns a list of URLs)
320358

321359
```js
322360
meta.getDownloads('1574834281.966265128', function(err, res) {
@@ -329,17 +367,17 @@ meta.getDownloads('1574834281.966265128', function(err, res) {
329367
});
330368
```
331369

332-
#### Change a NodeJS `http.request()` option (example: `family` (for IPv6))
370+
#### cancelBuild() - Cancel an OVA build (returns OK)
333371

334372
```js
335-
meta.options.agent = new https.Agent({ family: 6 });
336-
meta.buildRequest(void 0, (error, result) => {
337-
if (!error) {
338-
return meta.apiCall(result, function(err, res, data) {
339-
return console.log(data);
340-
});
341-
}
342-
});
373+
meta.cancelBuild('1574941961.314614280', function(err, res) {
374+
if (err) {
375+
console.error(err);
376+
return process.exit(1);
377+
} else {
378+
return console.log(res);
379+
}
380+
});
343381
```
344382

345383
# Testing

lib/on-prem-meta.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
fs = require('fs');
1818

1919
needle.defaults({
20-
user_agent: 'nodeclient-on-prem-meta/1.3.0',
20+
user_agent: 'nodeclient-on-prem-meta/1.4.0',
2121
response_timeout: 10000 // 10 seconds
2222
});
2323

@@ -235,4 +235,32 @@
235235
});
236236
};
237237

238+
// returns a callback with an error in arg1, or 'OK' in arg2
239+
// the error is an Error object if non-HTTP related
240+
// the error is the request result if 404 or other HTTP error code (4xx or 5xx)
241+
exports.cancelBuild = (build, callback) => {
242+
var apiParams;
243+
apiParams = {
244+
method: 'POST',
245+
endpoint: 'builds/cancel',
246+
query: {
247+
builddate: build
248+
}
249+
};
250+
return this.buildRequest(apiParams, (error, result) => {
251+
if (error) {
252+
callback(error);
253+
}
254+
return this.apiCall(result, function(err, res, data) {
255+
if (err) {
256+
return callback(new Error(err));
257+
} else if (res.statusCode === 200 && res.statusMessage === 'OK') {
258+
return callback(null, res.statusMessage);
259+
} else {
260+
return callback(data);
261+
}
262+
});
263+
});
264+
};
265+
238266
}).call(this);

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"description": "Official On-Prem Meta REST API client and helper library",
33
"author": "Alexander Williams, Unscramble <[email protected]>",
44
"name": "@on-prem/on-prem-meta",
5-
"version": "1.3.0",
5+
"version": "1.4.0",
66
"license": "MIT",
77
"homepage": "https://on-premises.com",
88
"main": "lib/on-prem-meta.js",

src/on-prem-meta.coffee

+21-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ formData = require 'form-data'
1111
fs = require 'fs'
1212

1313
needle.defaults
14-
user_agent: 'nodeclient-on-prem-meta/1.3.0'
14+
user_agent: 'nodeclient-on-prem-meta/1.4.0'
1515
response_timeout: 10000 # 10 seconds
1616

1717
exports.makeSHA256 = (string) ->
@@ -164,3 +164,23 @@ exports.getDownloads = (build, callback) =>
164164
callback null, downloads.join('\n')
165165
else
166166
callback data
167+
168+
# returns a callback with an error in arg1, or 'OK' in arg2
169+
# the error is an Error object if non-HTTP related
170+
# the error is the request result if 404 or other HTTP error code (4xx or 5xx)
171+
exports.cancelBuild = (build, callback) =>
172+
apiParams =
173+
method: 'POST'
174+
endpoint: 'builds/cancel'
175+
query:
176+
builddate: build
177+
178+
this.buildRequest apiParams, (error, result) =>
179+
callback error if error
180+
this.apiCall result, (err, res, data) ->
181+
if err
182+
callback new Error err
183+
else if res.statusCode is 200 and res.statusMessage is 'OK'
184+
callback null, res.statusMessage
185+
else
186+
callback data

0 commit comments

Comments
 (0)