UNPKG

liveapicreator-admin-cli

Version:

The NodeJS command line utility for 'CA Live API Creator' DevOps Administration from CA Technologies

133 lines (118 loc) 3.46 kB
var Client = require('node-rest-client').Client; var colors = require('colors'); var _ = require('underscore'); var Table = require('easy-table'); var fs = require('fs'); var context = require('./context.js'); var login = require('../util/login.js'); var printObject = require('../util/printObject.js'); var dotfile = require('../util/dotfile.js'); module.exports = { doLicense: function(action, cmd) { if (action === 'list') { module.exports.list(cmd); } else if (action === 'import') { module.exports.import(cmd); } else { console.log('You must specify an action: list or import'); //program.help(); } }, list: function (cmd) { var client = new Client(); var loginInfo = login.login(cmd); if ( ! loginInfo) return; var url = loginInfo.url; var apiKey = loginInfo.apiKey; client.get(url + "/@license", { headers: { Authorization: "CALiveAPICreator " + apiKey + ":1", "Content-Type" : "application/json" } }, function(data) { if (data.errorMessage) { console.log(data.errorMessage.red); return; } printObject.printHeader('License'); var table = new Table(); //_.each(data, function(p) { table.cell("Company", data.company); table.cell("Organization", data.organization); table.cell("location", data.location); table.cell("license_type", data.license_type); table.cell("Expiration",data.license_expiration); table.newRow(); //}); console.log(table.toString()); }); }, import: function(cmd) { var client = new Client(); var loginInfo = login.login(cmd); if ( ! loginInfo) { return; } if ( ! cmd.file) { cmd.file = '/dev/stdin'; } var fileContent = null; var json = null; fs.readFile(cmd.file, function read(err,data){ if(err) { console.log("Unable to read file"); return; } json = data; //fileContent = JSON.parse(json); var startTime = new Date(); client.post(loginInfo.url + "/admin:server_licenses", { data: { "license_text": String(json) }, headers: { Authorization: "CALiveAPICreator " + loginInfo.apiKey + ":1", "Content-Type" : "application/json" } }, function(data) { console.log(data); var endTime = new Date(); if (data.errorMessage) { console.log(data.errorMessage.red); return; } printObject.printHeader('License created, including:'); if(data.statusCode == 200 ){ console.log("Request took: " + (endTime - startTime) + "ms"); return; } var newTopic = _.find( data.txsummary, function(p) { return p['@metadata'].resource === 'admin:server_licenses'; }); if ( ! newTopic) { console.log('ERROR: unable to find imported server license'.red); return; } if (cmd.verbose) { _.each(data.txsummary, function(obj) { printObject.printObject(obj, obj['@metadata'].entity, 0, obj['@metadata'].verb); }); } else { printObject.printObject(newTopic, newTopic['@metadata'].entity, 0, newTopic['@metadata'].verb); console.log(('and ' + (data.txsummary.length - 1) + ' other objects').grey); } var trailer = "Request took: " + (endTime - startTime) + "ms"; trailer += " - # objects touched: "; if (data.txsummary.length === 0) { console.log('No data returned'.yellow); } else { trailer += data.txsummary.length; } printObject.printHeader(trailer); }); }); } };