UNPKG

k2hr3-api

Version:

K2HR3 REST API is K2hdkc based Resource and Roles and policy Rules

128 lines (112 loc) 3.1 kB
/* * K2HR3 REST API * * Copyright 2017 Yahoo Japan Corporation. * * K2HR3 is K2hdkc based Resource and Roles and policy Rules, gathers * common management information for the cloud. * K2HR3 can dynamically manage information as "who", "what", "operate". * These are stored as roles, resources, policies in K2hdkc, and the * client system can dynamically read and modify these information. * * For the full copyright and license information, please view * the license file that was distributed with this source code. * * AUTHOR: Takeshi Nakatani * CREATE: Wed Jun 8 2017 * REVISION: * */ 'use strict'; var http = require('http'); var https = require('https'); var cacerts = require('../lib/cacerts'); var apiutil = require('../lib/k2hr3apiutil'); var cliutil = require('../lib/k2hr3cliutil'); // Debug logging objects var r3logger = require('../lib/dbglogging'); // // Hostname and port from env // var hostname = apiutil.getSafeString(process.env.APIHOST); var hostport = apiutil.getSafeString(process.env.APIPORT); var is_https = apiutil.compareCaseString('yes', process.env.HTTPS_ENV); // // Request API for test // function getV1Version(is_top) { var ver_path = is_top ? '/' : '/v1'; var headers = { 'Content-Type': 'application/json', 'Content-Length': 0 }; var options = { 'host': hostname, 'port': hostport, 'path': ver_path, 'method': 'GET', 'headers': headers }; r3logger.dlog('request options = ' + JSON.stringify(options)); r3logger.dlog('request headers = ' + JSON.stringify(headers)); var httpobj; if(is_https){ if(null !== cacerts.ca){ options.ca = cacerts.ca; } options.rejectUnauthorized = false; // always insecure for this manual test options.agent = new https.Agent(options); httpobj = https; }else{ options.agent = new http.Agent(options); httpobj = http; } var req = httpobj.request(options, function(res) { var response = ''; console.log('RESPONSE CODE = ' + res.statusCode); r3logger.dlog('response status = ' + res.statusCode); r3logger.dlog('response header = ' + JSON.stringify(res.headers)); res.setEncoding('utf8'); res.on('data', function (chunk) { r3logger.dlog('response chunk = ' + chunk); response += chunk; }); res.on('end', function(result) // eslint-disable-line no-unused-vars { r3logger.mlog(r3logger.dump(response)); // response is object(or not) console.log('RESPONSE BODY = ' + JSON.stringify(response)); process.exit(0); }); }); req.on('error', function(e) { r3logger.elog('problem with request: ' + e.message); }); // send request req.end(); } // // run // cliutil.getConsoleInput('path(\'/\' or \'/v1\') : ', true, false, function(isbreak, path) { if(isbreak){ process.exit(0); } if('/' === path){ getV1Version(true); }else if('/v1' === path){ getV1Version(false); }else{ console.log('Path parameter(' + path + ') must be / or /v1\n'); } }); /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noexpandtab sw=4 ts=4 fdm=marker * vim<600: noexpandtab sw=4 ts=4 */