UNPKG

cloud-red

Version:

Harnessing Serverless for your cloud integration needs

133 lines (130 loc) 4.83 kB
/** * Copyright JS Foundation and other contributors, http://js.foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. **/ var apiUtils = require("../util"); var runtimeAPI; var sshkeys = require("./sshkeys"); var theme = require("./theme"); var clone = require("clone"); //Below requires are added for cloudred var fs = require('fs') var path = require('path') function extend(target, source) { var keys = Object.keys(source); var i = keys.length; while(i--) { var value = source[keys[i]] var type = typeof value; if (type === 'string' || type === 'number' || type === 'boolean' || Array.isArray(value)) { target[keys[i]] = value; } else if (value === null) { if (target.hasOwnProperty(keys[i])) { delete target[keys[i]]; } } else { // Object if (target.hasOwnProperty(keys[i])) { target[keys[i]] = extend(target[keys[i]],value); } else { target[keys[i]] = value; } } } return target; } module.exports = { init: function(_runtimeAPI) { runtimeAPI = _runtimeAPI; sshkeys.init(runtimeAPI); }, runtimeSettings: function(req,res) { var opts = { user: req.user } runtimeAPI.settings.getRuntimeSettings(opts).then(function(result) { var themeSettings = theme.settings(); if (themeSettings) { // result.editorTheme may already exist with the palette // disabled. Need to merge that into the receive settings result.editorTheme = extend(clone(themeSettings),result.editorTheme||{}); } res.json(result); }); }, userSettings: function(req, res) { var opts = { user: req.user } runtimeAPI.settings.getUserSettings(opts).then(function(result) { res.json(result); }); }, updateUserSettings: function(req,res) { var opts = { user: req.user, settings: req.body } runtimeAPI.settings.updateUserSettings(opts).then(function(result) { res.status(204).end(); }).catch(function(err) { apiUtils.rejectHandler(req,res,err); }); }, sshkeys: function() { return sshkeys.app() }, /** * This function is used for listing the * existing profiles ffrom ~/.aws/credentials * @param {} req * @param {*} res */ getAwsCredProfiles: function(req,res) { var textByLine = fs.readFileSync(path.join(process.env.HOME, '.aws', 'credentials')).toString().split("\n"); var testByline_str = JSON.stringify(textByLine) var jsondata = JSON.parse(testByline_str) var lucky = jsondata.filter(function(data) { return (data.startsWith("[") && data.endsWith("]")); }); let profiles = new Array(); for(i=0;i<lucky.length;i++){ profiles[i]=lucky[i].toString().replace(/[\[\]']+/g,'') } var responsedata = JSON.stringify({'envprofile' : profiles}) res.contentType("application/json"); res.json(JSON.parse(responsedata)) }, /** * This function creates a profile entry in * ~/.aws/credentials file * Note : not used currently * @param {*} req * @param {*} res */ // setAwsCredProfiles: function(req,res) { // console.log(req.body.profilename) // var profile_entry = '['+req.body.profilename+']\naws_access_key_id = '+req.body.awskey+'\naws_secret_access_key = '+req.body.awssecret+'\nregion = '+req.body.region+'\n' // console.log(profile_entry) // console.log(process.env.HOME) // console.log(path.join(process.env.HOME, '.aws', 'credentials')) // var textByLine = fs.readFileSync(path.join(process.env.HOME, '.aws', 'credentials')).toString(); // fs.appendFile(path.join(process.env.HOME, '.aws', 'credentials'), profile_entry, function (err) { // if (err) throw err; // console.log('Profile Saved!'); // }); // res.contentType("application/json"); // res.json(JSON.parse("{}")); // } }