From 2ebacaed5daaf7b101deb77c6d0f5f8de2232194 Mon Sep 17 00:00:00 2001 From: Alex Williams Date: Mon, 2 Dec 2019 01:11:45 +0000 Subject: [PATCH] Add getVersion() and add backward compat with older Meta versions --- README.md | 1 + lib/on-prem-meta.js | 117 +++++++++++++++++++++++----------------- package-lock.json | 4 +- package.json | 2 +- src/on-prem-meta.coffee | 87 +++++++++++++++++------------- 5 files changed, 123 insertions(+), 88 deletions(-) diff --git a/README.md b/README.md index d408643..bc755b8 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/on-prem-meta.js b/lib/on-prem-meta.js index 96b2829..c25345a 100644 --- a/lib/on-prem-meta.js +++ b/lib/on-prem-meta.js @@ -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 }); @@ -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); + } + }); }); }); }; diff --git a/package-lock.json b/package-lock.json index 72ae0e6..ddb6d0f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "on-prem-meta", - "version": "1.0.0", + "name": "@on-prem/on-prem-meta", + "version": "1.5.0", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 922e664..9da18e4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "description": "Official On-Prem Meta REST API client and helper library", "author": "Alexander Williams, Unscramble ", "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", diff --git a/src/on-prem-meta.coffee b/src/on-prem-meta.coffee index 407ab78..54135a2 100644 --- a/src/on-prem-meta.coffee +++ b/src/on-prem-meta.coffee @@ -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) -> @@ -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