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

Commit

Permalink
Add new buildOVA() function
Browse files Browse the repository at this point in the history
  • Loading branch information
aw committed Nov 27, 2019
1 parent 8802358 commit 8a1232d
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 17 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Official On-Prem Meta REST API client and helper library

[![GitHub release](https://img.shields.io/github/release/on-prem/on-prem-meta-node.svg)](https://github.com/on-prem/on-prem-meta-node) ![Build status](https://github.com/on-prem/on-prem-meta-node/workflows/Node%20CI/badge.svg?branch=master) ![Downloads](http://img.shields.io/npm/dm/on-prem-meta.svg "On-Prem Meta")
[![GitHub release](https://img.shields.io/github/release/on-prem/on-prem-meta-node.svg)](https://github.com/on-prem/on-prem-meta-node) ![Build status](https://github.com/on-prem/on-prem-meta-node/workflows/Node%20CI/badge.svg?branch=master)

1. [Requirements](#requirements)
2. [Installation](#installation)
Expand All @@ -11,7 +11,7 @@

# Requirements

* NodeJS `v12.x` (tested)
* NodeJS `v8.x` to `v12.x` (tested)
* This library requires the `needle` and `form-data` node modules

# Installation
Expand Down Expand Up @@ -119,6 +119,22 @@ meta.buildRequest apiParams, (error, result) =>
console.log data
```

Build an OVA (returns the builddate)

```coffee
apiParams =
repo_name: 'your-appliance'
ova_type: 'server'
export_disks: 'raw,qcow2'

meta.buildOVA "/path/to/your/app.tcz", apiParams, (err, res) ->
if err
console.error err
process.exit 1
else
console.log res
```

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

```coffee
Expand Down Expand Up @@ -206,6 +222,25 @@ meta.buildRequest(apiParams, (error, result) => {
});
```

Build an OVA (returns the builddate)

```js
apiParams = {
repo_name: 'your-appliance',
ova_type: 'server',
export_disks: 'raw,qcow2'
};

meta.buildOVA("/path/to/your/app.tcz", apiParams, 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
46 changes: 40 additions & 6 deletions lib/on-prem-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

// Copyright (c) 2019 Alex Williams, Unscramble <[email protected]>
// MIT Licensed
var createHash, createHmac, formData, needle, options, settings;
var createHash, createHmac, formData, fs, needle, options, settings;

createHash = require('crypto').createHash;

Expand All @@ -14,6 +14,13 @@

formData = require('form-data');

fs = require('fs');

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

exports.makeSHA256 = function(string) {
return createHash('sha256').update(string).digest('hex');
};
Expand Down Expand Up @@ -42,11 +49,6 @@

exports.options = options;

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

exports.buildRequest = function(params = {
method: 'GET',
endpoint: 'version'
Expand Down Expand Up @@ -97,4 +99,36 @@
});
};

// 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);
}
});
});
};

}).call(this);
4 changes: 2 additions & 2 deletions 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.0.5",
"version": "1.1.0",
"license": "MIT",
"homepage": "https://on-premises.com",
"main": "lib/on-prem-meta.js",
Expand Down Expand Up @@ -36,7 +36,7 @@
"url": "https://github.com/on-prem/on-prem-meta-node/issues"
},
"engines": {
"node": ">=12"
"node": ">=8"
},
"directories": {
"lib": "lib",
Expand Down
38 changes: 31 additions & 7 deletions src/on-prem-meta.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ createHash = require('crypto').createHash
createHmac = require('crypto').createHmac
needle = require 'needle'
formData = require 'form-data'
fs = require 'fs'

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

exports.makeSHA256 = (string) ->
createHash('sha256')
Expand All @@ -21,9 +26,9 @@ exports.makeHMAC = (string, key) ->

# Global variables
settings =
prefix: process.env.ON_PREM_META_PREFIX || "meta" # example: admin
host: process.env.ON_PREM_META_HOST || throw "Environment variable 'ON_PREM_META_HOST' required" # example: meta.yourdomain.com:443
insecure: if process.env.ON_PREM_META_INSECURE is "true" then false else true
prefix: process.env.ON_PREM_META_PREFIX || "meta" # example: admin
host: process.env.ON_PREM_META_HOST || throw "Environment variable 'ON_PREM_META_HOST' required" # example: meta.yourdomain.com:443
insecure: if process.env.ON_PREM_META_INSECURE is "true" then false else true
tokenhash: this.makeSHA256 process.env.ON_PREM_META_APITOKEN || throw "Environment variable 'ON_PREM_META_APITOKEN' required" # example: yourtoken

options =
Expand All @@ -32,10 +37,6 @@ options =
exports.settings = settings
exports.options = options

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

exports.buildRequest = (params = {method: 'GET', endpoint: 'version'}, callback) ->
endpoint = "/api/v1/#{settings.prefix}/#{params.endpoint}"

Expand Down Expand Up @@ -73,3 +74,26 @@ exports.apiCall = (params, callback) ->
params.options
(err, res, data) ->
return callback err, res, data

# 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

0 comments on commit 8a1232d

Please sign in to comment.