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

Commit

Permalink
Add getVersion() and add backward compat with older Meta versions
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed Dec 2, 2019
1 parent b6febc0 commit 2ebacae
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 88 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The following functions are exported:

The following helper functions are exported:

* `getVersion()` to get the version of the Meta or Admin appliance
* `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)
Expand Down
117 changes: 69 additions & 48 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.4.0',
user_agent: 'nodeclient-on-prem-meta/1.5.0',
response_timeout: 10000 // 10 seconds
});

Expand Down Expand Up @@ -99,64 +99,85 @@
});
};

// returns a callback with the version of the API
exports.getVersion = (callback) => {
return this.buildRequest(void 0, (error, result) => {
if (!error) {
return this.apiCall(result, (err, res, data) => {
if (!err) {
return callback(data.version);
}
});
}
});
};

// returns a callback with an error in arg1, or the builddate 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.buildOVA = (application, parameters, callback) => {
var apiParams;
apiParams = {
method: 'POST',
endpoint: 'builds',
files: {
app: {
filename: 'app.tcz',
data: fs.readFileSync(application)
}
},
query: parameters
};
return this.buildRequest(apiParams, (error, result) => {
if (error) {
callback(error);
}
return this.apiCall(result, function(err, res, data) {
if (err) {
return callback(new Error(err));
} else if (res.statusCode === 202 && res.statusMessage === 'Accepted') {
return callback(null, data.builddate);
} else {
return callback(data);
return this.getVersion((version) => {
var apiParams;
apiParams = {
method: 'POST',
endpoint: 'builds',
files: {
app: {
filename: 'app.tcz',
data: fs.readFileSync(application)
}
},
query: parameters
};
return this.buildRequest(apiParams, (error, result) => {
if (error) {
callback(error);
}
return this.apiCall(result, function(err, res, data) {
if (err) {
return callback(new Error(err));
} else if (res.statusCode === 202 && res.statusMessage === 'Accepted') {
if (version >= '1.13.1') {
return callback(null, data.builddate); // builddate added in v1.13.1
} else {
return callback(null, data.Location.split("=")[1]);
}
} else {
return callback(data);
}
});
});
});
};

exports.getStatus = (build, callback) => {
var apiParams;
apiParams = {
method: 'GET',
endpoint: 'builds/status',
query: {
builddate: build,
log: 0
}
};
return this.buildRequest(apiParams, (error, result) => {
if (error) {
callback(error);
}
return this.apiCall(result, function(err, res, data) {
if (err) {
return callback(new Error(err));
} else if (res.statusCode === 202 && res.statusMessage === 'Accepted') {
return callback(null, {
status: 'queued'
});
} else if (res.statusCode === 200 && data) {
return callback(null, data);
} else {
return callback(data);
return this.getVersion((version) => {
var apiParams;
apiParams = {
method: 'GET',
endpoint: 'builds/status',
query: {
builddate: build,
log: version >= '1.13.1' ? 0 : void 0 // log parameter added in v1.13.1
}
};
return this.buildRequest(apiParams, (error, result) => {
if (error) {
callback(error);
}
return this.apiCall(result, function(err, res, data) {
if (err) {
return callback(new Error(err));
} else if (res.statusCode === 202 && res.statusMessage === 'Accepted') {
return callback(null, {
status: 'queued'
});
} else if (res.statusCode === 200 && data) {
return callback(null, data);
} else {
return callback(data);
}
});
});
});
};
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.4.0",
"version": "1.5.0",
"license": "MIT",
"homepage": "https://on-premises.com",
"main": "lib/on-prem-meta.js",
Expand Down
87 changes: 50 additions & 37 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.4.0'
user_agent: 'nodeclient-on-prem-meta/1.5.0'
response_timeout: 10000 # 10 seconds

exports.makeSHA256 = (string) ->
Expand Down Expand Up @@ -75,48 +75,61 @@ exports.apiCall = (params, callback) ->
(err, res, data) ->
return callback err, res, data

# returns a callback with the version of the API
exports.getVersion = (callback) =>
this.buildRequest undefined, (error, result) =>
unless error
this.apiCall result, (err, res, data) =>
unless err
callback data.version

# returns a callback with an error in arg1, or the builddate 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.buildOVA = (application, parameters, callback) =>
apiParams =
method: 'POST'
endpoint: 'builds'
files:
app:
filename: 'app.tcz'
data: fs.readFileSync(application)
query: parameters

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 202 and res.statusMessage is 'Accepted'
callback null, data.builddate
else
callback data
this.getVersion (version) =>
apiParams =
method: 'POST'
endpoint: 'builds'
files:
app:
filename: 'app.tcz'
data: fs.readFileSync(application)
query: parameters

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 202 and res.statusMessage is 'Accepted'
if version >= '1.13.1'
callback null, data.builddate # builddate added in v1.13.1
else
callback null, data.Location.split("=")[1] # parse the Location to get the builddate
else
callback data

exports.getStatus = (build, callback) =>
apiParams =
method: 'GET'
endpoint: 'builds/status'
query:
builddate: build
log: 0

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 202 and res.statusMessage is 'Accepted'
callback null, { status: 'queued' }
else if res.statusCode is 200 and data
callback null, data
else
callback data
this.getVersion (version) =>
apiParams =
method: 'GET'
endpoint: 'builds/status'
query:
builddate: build
log: 0 if version >= '1.13.1' # log parameter added in v1.13.1

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 202 and res.statusMessage is 'Accepted'
callback null, { status: 'queued' }
else if res.statusCode is 200 and data
callback null, data
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
Expand Down

0 comments on commit 2ebacae

Please sign in to comment.