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

Commit

Permalink
Fix output of pollStatus() and add getDownloads() function
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed Nov 28, 2019
1 parent 664de67 commit 9a71a2c
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 16 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,18 @@ The following functions are exported:
* `makeSHA256()` to generate an SHA256 string
* `makeHMAC()` to generate an SHA256 HMAC from a string and key
* `buildRequest()` to generate a request object to be sent to the `apiCall()` function
* `apiCall()` to send the actual request object to the Meta API
* `apiCall()` to send the actual request data to the Meta API
* `buildOVA()` to build an OVA through the Meta API
* `getStatus()` to obtain the status of an OVA build
* `pollStatus()` to poll the status of an OVA build (every 5 seconds)
* `getDownloads()` to obtain the list of download files for an OVA build

See the usage docs below.

# Usage

1. [CoffeeScript](#coffeescript)
2. [NodeJS](#nodejs)
2. [JavaScript](#JavaScript)

## CoffeeScript

Expand Down Expand Up @@ -168,7 +169,7 @@ meta.buildOVA "/path/to/your/app.tcz", apiParams, (err, res) ->
coffee> 1574834281.966265128
```

#### Poll the status of an OVA (returns the status object)
#### Poll the status of an OVA (returns the status)

```coffee
meta.pollStatus '1574834281.966265128', undefined, (err, res) ->
Expand All @@ -178,11 +179,20 @@ meta.pollStatus '1574834281.966265128', undefined, (err, res) ->
else
console.log res

coffee> {
status: 'success',
percentage: '100',
builddate: '1574834281.966265128'
}
coffee> success
```

#### Get the list of download URLs

```coffee
meta.getDownloads '1574834281.966265128', (err, res) ->
if err
console.error err
process.exit 1
else
console.log res

coffee> https://yourdomain.com:443/downloads/build-1574834281.966265128/your-appliance-v1.2.3-release.ova
```

#### Change a NodeJS `http.request()` option (example: `family` (for IPv6))
Expand All @@ -195,7 +205,7 @@ meta.buildRequest undefined, (error, result) =>
console.log data
```

## NodeJS
## JavaScript

#### 1. Require the library as you would any other node module:

Expand Down Expand Up @@ -306,6 +316,19 @@ meta.pollStatus('1574834281.966265128', void 0, function(err, res) {
});
```

#### Get the list of download URLs

```js
meta.getDownloads('1574834281.966265128', function(err, res) {
if (err) {
console.error(err);
return process.exit(1);
} else {
return console.log(res);
}
});
```

#### Change a NodeJS `http.request()` option (example: `family` (for IPv6))

```js
Expand Down
48 changes: 45 additions & 3 deletions lib/on-prem-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
fs = require('fs');

needle.defaults({
user_agent: 'nodeclient-on-prem-meta/1.2.0',
user_agent: 'nodeclient-on-prem-meta/1.3.0',
response_timeout: 10000 // 10 seconds
});

Expand Down Expand Up @@ -161,11 +161,14 @@
});
};

// returns a callback with and error in arg1, or the status in arg2
// the error is an Error object if non-HTTP related
// the error is the request result if 404 or other HTTP error code (4xx or 5xx)
exports.pollStatus = (build, status = {}, callback) => {
switch (status.status) {
case 'success':
case 'failed':
return status;
return status.status;
default:
return this.getStatus(build, (err, res) => {
var run;
Expand All @@ -175,7 +178,7 @@
if (res.status === 'success') {
return callback(null, this.pollStatus(build, res));
} else if (res.status === 'failed') {
return callback(this.pollStatus(build, res));
return callback(null, this.pollStatus(build, res));
} else {
run = () => {
return this.pollStatus(build, res, function(error, result) {
Expand All @@ -193,4 +196,43 @@
}
};

// returns a callback with an error in arg1, or the list of downloads in arg2
// the error is an Error object if non-HTTP related
// the error is the request result if 404 or other HTTP error code (4xx or 5xx)
exports.getDownloads = (build, callback) => {
var apiParams;
apiParams = {
method: 'GET',
endpoint: 'downloads',
query: {
builddate: build
}
};
return this.buildRequest(apiParams, (error, result) => {
if (error) {
callback(error);
}
return this.apiCall(result, function(err, res, data) {
var downloads, i, url;
if (err) {
return callback(new Error(err));
} else if (res.statusCode === 200 && res.statusMessage === 'OK') {
downloads = (function() {
var j, len, ref, results;
ref = data.downloads;
results = [];
for (url = j = 0, len = ref.length; j < len; url = ++j) {
i = ref[url];
results.push(`https://${settings.host}${i.url}`);
}
return results;
})();
return callback(null, downloads.join('\n'));
} else {
return callback(data);
}
});
});
};

}).call(this);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"description": "Official On-Prem Meta REST API client and helper library",
"author": "Alexander Williams, Unscramble <[email protected]>",
"name": "@on-prem/on-prem-meta",
"version": "1.2.0",
"version": "1.3.0",
"license": "MIT",
"homepage": "https://on-premises.com",
"main": "lib/on-prem-meta.js",
Expand Down
31 changes: 28 additions & 3 deletions src/on-prem-meta.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ formData = require 'form-data'
fs = require 'fs'

needle.defaults
user_agent: 'nodeclient-on-prem-meta/1.2.0'
user_agent: 'nodeclient-on-prem-meta/1.3.0'
response_timeout: 10000 # 10 seconds

exports.makeSHA256 = (string) ->
Expand Down Expand Up @@ -118,10 +118,13 @@ exports.getStatus = (build, callback) =>
else
callback data

# returns a callback with and error in arg1, or the status in arg2
# the error is an Error object if non-HTTP related
# the error is the request result if 404 or other HTTP error code (4xx or 5xx)
exports.pollStatus = (build, status = {}, callback) =>
switch status.status
when 'success', 'failed'
return status
return status.status
else
this.getStatus build, (err, res) =>
if err
Expand All @@ -130,7 +133,7 @@ exports.pollStatus = (build, status = {}, callback) =>
if res.status is 'success'
callback null, this.pollStatus build, res
else if res.status is 'failed'
callback this.pollStatus build, res
callback null, this.pollStatus build, res
else
run = () =>
this.pollStatus build, res, (error, result) ->
Expand All @@ -139,3 +142,25 @@ exports.pollStatus = (build, status = {}, callback) =>

# wait 5 seconds between each request
setTimeout run, 5000

# returns a callback with an error in arg1, or the list of downloads in arg2
# the error is an Error object if non-HTTP related
# the error is the request result if 404 or other HTTP error code (4xx or 5xx)
exports.getDownloads = (build, callback) =>
apiParams =
method: 'GET'
endpoint: 'downloads'
query:
builddate: build

this.buildRequest apiParams, (error, result) =>
callback error if error
this.apiCall result, (err, res, data) ->
if err
callback new Error err
else if res.statusCode is 200 and res.statusMessage is 'OK'
downloads = for i, url in data.downloads
"https://#{settings.host}#{i.url}"
callback null, downloads.join('\n')
else
callback data

0 comments on commit 9a71a2c

Please sign in to comment.