WIP: create new runtime-api

This commit is contained in:
Nick O'Leary
2018-04-15 11:18:10 +01:00
parent e8a637498d
commit e8e8f70c27
22 changed files with 1422 additions and 611 deletions

19
red/runtime-api/auth.js Normal file
View File

@@ -0,0 +1,19 @@
/**
* 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.
**/
/**
* @module red/auth
*/

19
red/runtime-api/comms.js Normal file
View File

@@ -0,0 +1,19 @@
/**
* 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.
**/
/**
* @module red/comms
*/

248
red/runtime-api/flows.js Normal file
View File

@@ -0,0 +1,248 @@
/**
* 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.
**/
/**
* @namespace RED.flows
*/
/**
* @typedef Flows
* @alias Dave
* @type {object}
* @property {string} rev - the flow revision identifier
* @property {Array} flows - the flow configuration, an array of node configuration objects
*/
/**
* @typedef Flow
* @type {object}
* @property {string} id - the flow identifier
* @property {string} label - a label for the flow
* @property {Array} nodes - an array of node configuration objects
*/
var runtime;
var api = module.exports = {
init: function(_runtime) {
runtime = _runtime;
},
/**
* Gets the current flow configuration
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @return {Promise<Flows>} - the active flow configuration
* @memberof RED.flows
*/
getFlows: function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "flows.get"}/*,req*/);
var version = opts.version||"v1";
return resolve(runtime.nodes.getFlows());
});
},
/**
* Sets the current flow configuration
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @return {Promise<Flows>} - the active flow configuration
* @memberof RED.flows
*/
setFlows: function(opts) {
var err;
return new Promise(function(resolve,reject) {
var flows = opts.flows;
var deploymentType = opts.deploymentType||"full";
runtime.log.audit({event: "flows.set",type:deploymentType}/*,req*/);
var apiPromise;
if (deploymentType === 'reload') {
apiPromise = runtime.nodes.loadFlows();
} else {
if (flows.hasOwnProperty('rev')) {
var currentVersion = runtime.nodes.getFlows().rev;
if (currentVersion !== flows.rev) {
err = new Error();
err.code = "version_mismatch";
err.status = 409;
//TODO: log warning
return reject(err);
}
}
apiPromise = runtime.nodes.setFlows(flows.flows,deploymentType);
}
apiPromise.then(function(flowId) {
return resolve({rev:flowId});
}).catch(function(err) {
log.warn(log._("api.flows.error-"+(deploymentType === 'reload'?'reload':'save'),{message:err.message}));
log.warn(err.stack);
return reject(err);
});
});
},
/**
* Adds a flow configuration
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.flow - the flow to add
* @return {Promise<String>} - the id of the added flow
* @memberof RED.flows
*/
addFlow: function(opts) {
return new Promise(function(resolve,reject) {
var flow = opts.flow;
runtime.nodes.addFlow(flow).then(function(id) {
runtime.log.audit({event: "flow.add",id:id});
return resolve(id);
}).catch(function(err) {
runtime.log.audit({event: "flow.add",error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
})
})
},
/**
* Gets an individual flow configuration
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.id - the id of the flow to retrieve
* @return {Promise<Flow>} - the active flow configuration
* @memberof RED.flows
*/
getFlow: function(opts) {
return new Promise(function (resolve,reject) {
var flow = runtime.nodes.getFlow(opts.id);
if (flow) {
runtime.log.audit({event: "flow.get",id:opts.id});
return resolve(flow);
} else {
runtime.log.audit({event: "flow.get",id:opts.id,error:"not_found"});
var err = new Error();
err.status = 404;
return reject(err);
}
})
},
/**
* Updates an existing flow configuration
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.id - the id of the flow to update
* @param {Object} opts.flow - the flow configuration
* @return {Promise<String>} - the id of the updated flow
* @memberof RED.flows
*/
updateFlow: function(opts) {
return new Promise(function (resolve,reject) {
var flow = opts.flow;
var id = opts.id;
try {
runtime.nodes.updateFlow(id,flow).then(function() {
runtime.log.audit({event: "flow.update",id:id});
return resolve(id);
}).catch(function(err) {
runtime.log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
})
} catch(err) {
if (err.code === 404) {
runtime.log.audit({event: "flow.update",id:id,error:"not_found"});
// TODO: this swap around of .code and .status isn't ideal
err.status = 404;
err.code = "not_found";
return reject(err);
} else {
runtime.log.audit({event: "flow.update",error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
}
}
});
},
/**
* Deletes a flow
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.id - the id of the flow to delete
* @return {Promise} - resolves if successful
* @memberof RED.flows
*/
deleteFlow: function(opts) {
return new Promise(function (resolve,reject) {
var id = opts.id;
try {
runtime.nodes.removeFlow(id).then(function() {
log.audit({event: "flow.remove",id:id});
return resolve();
})
} catch(err) {
if (err.code === 404) {
log.audit({event: "flow.remove",id:id,error:"not_found"});
// TODO: this swap around of .code and .status isn't ideal
err.status = 404;
err.code = "not_found";
return reject(err);
} else {
log.audit({event: "flow.remove",id:id,error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
}
}
});
},
/**
* Gets the safe credentials for a node
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.type - the node type to return the credential information for
* @param {String} opts.id - the node id
* @return {Promise<Object>} - the safe credentials
* @memberof RED.flows
*/
getNodeCredentials: function(opts) {
return new Promise(function(resolve,reject) {
log.audit({event: "credentials.get",type:opts.type,id:opts.id});
var credentials = runtime.nodes.getCredentials(opts.id);
if (!credentials) {
return resolve({});
}
var definition = runtime.nodes.getCredentialDefinition(opts.type);
var sendCredentials = {};
for (var cred in definition) {
if (definition.hasOwnProperty(cred)) {
if (definition[cred].type == "password") {
var key = 'has_' + cred;
sendCredentials[key] = credentials[cred] != null && credentials[cred] !== '';
continue;
}
sendCredentials[cred] = credentials[cred] || '';
}
}
resolve(sendCredentials);
})
}
}

64
red/runtime-api/index.js Normal file
View File

@@ -0,0 +1,64 @@
/**
* 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.
**/
/**
* A user accessing the API
* @typedef User
* @type {object}
*/
/**
* @namespace RED
*/
var api = module.exports = {
init: function(runtime) {
api.flows.init(runtime);
api.nodes.init(runtime);
api.settings.init(runtime);
},
/**
* Auth module
*/
auth: require("./auth"),
/**
* Comms module
*/
comms: require("./comms"),
/**
* Flows module
*/
flows: require("./flows"),
/**
* Library module
*/
library: require("./library"),
/**
* Nodes module
*/
nodes: require("./nodes"),
/**
* Settings module
*/
settings: require("./settings")
}

View File

@@ -0,0 +1,35 @@
/**
* 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.
**/
/**
* @module red/library
*/
module.exports = {
/**
* Does something
*/
setEnty: function() {},
/**
* Does something
*/
getEntry: function() {},
/**
* Does something
*/
getEntries: function() {}
}

377
red/runtime-api/nodes.js Normal file
View File

@@ -0,0 +1,377 @@
/**
* 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.
**/
"use strict"
/**
* @namespace RED.nodes
*/
var runtime;
function putNode(node, enabled) {
var promise;
if (!node.err && node.enabled === enabled) {
promise = Promise.resolve(node);
} else {
if (enabled) {
promise = runtime.nodes.enableNode(node.id);
} else {
promise = runtime.nodes.disableNode(node.id);
}
}
return promise;
}
var api = module.exports = {
init: function(_runtime) {
runtime = _runtime;
},
/**
* Gets the info of an individual node set
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.id - the id of the node set to return
* @return {Promise<NodeInfo>} - the node information
* @memberof RED.nodes
*/
getNodeInfo: function(opts) {
return new Promise(function(resolve,reject) {
var id = opts.id;
var result = redNodes.getNodeInfo(id);
if (result) {
runtime.log.audit({event: "nodes.info.get",id:id});
delete result.loaded;
return resolve(result);
} else {
runtime.log.audit({event: "nodes.info.get",id:id,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
})
},
/**
* Gets the list of node modules installed in the runtime
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @return {Promise<NodeList>} - the list of node modules
* @memberof RED.nodes
*/
getNodeList: function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "nodes.list.get"});
return resolve(runtime.nodes.getNodeList());
})
},
/**
* Gets an individual node's html content
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.id - the id of the node set to return
* @param {String} opts.lang - the locale language to return
* @return {Promise<String>} - the node html content
* @memberof RED.nodes
*/
getNodeConfig: function(opts) {
return new Promise(function(resolve,reject) {
var id = opts.id;
var lang = opts.lang;
var result = runtime.nodes.getNodeConfig(id,lang);
if (result) {
runtime.log.audit({event: "nodes.config.get",id:id});
return resolve(result);
} else {
runtime.log.audit({event: "nodes.config.get",id:id,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
});
},
/**
* Gets all node html content
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.lang - the locale language to return
* @return {Promise<String>} - the node html content
* @memberof RED.nodes
*/
getNodeConfigs: function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "nodes.configs.get"});
return resolve(runtime.nodes.getNodeConfigs(opts.lang));
});
},
/**
* Gets the info of a node module
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.module - the id of the module to return
* @return {Promise<ModuleInfo>} - the node module info
* @memberof RED.nodes
*/
getModuleInfo: function(opts) {
return new Promise(function(resolve,reject) {
var module = opts.module;
var result = redNodes.getModuleInfo(module);
if (result) {
runtime.log.audit({event: "nodes.module.get",id:id});
return resolve(result);
} else {
runtime.log.audit({event: "nodes.module.get",id:id,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
})
},
/**
* Install a new module into the runtime
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.module - the id of the module to install
* @param {String} opts.version - (optional) the version of the module to install
* @return {Promise<ModuleInfo>} - the node module info
* @memberof RED.nodes
*/
addModule: function(opts) {
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"});
let err = new Error("Settings unavailable");
err.code = "settings_unavailable";
err.status = 400;
return reject(err);
}
if (opts.module) {
var existingModule = runtime.nodes.getModuleInfo(opts.module);
if (existingModule) {
if (!opts.version || existingModule.version === opts.version) {
runtime.log.audit({event: "nodes.install",module:opts.module, version:opts.version, error:"module_already_loaded"});
let err = new Error("Module already loaded");
err.code = "module_already_loaded";
err.status = 400;
return reject(err);
}
if (!module.local) {
runtime.log.audit({event: "nodes.install",module:opts.module, version:opts.version, error:"module_not_local"});
let err = new Error("Module not locally installed");
err.code = "module_not_local";
err.status = 400;
return reject(err);
}
}
runtime.nodes.installModule(opts.module,opts.version).then(function(info) {
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version});
return resolve(info);
}).catch(function(err) {
if (err.code === 404) {
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,error:"not_found"});
// TODO: code/status
err.status = 404;
} else if (err.code) {
err.status = 400;
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,error:err.code});
} else {
err.status = 400;
runtime.log.audit({event: "nodes.install",module:opts.module,version:opts.version,error:err.code||"unexpected_error",message:err.toString()});
}
return reject(err);
})
} else {
runtime.log.audit({event: "nodes.install",module:opts.module,error:"invalid_request"});
let err = new Error("Invalid request");
err.code = "invalid_request";
err.status = 400;
return reject(err);
}
});
},
/**
* Removes a module from the runtime
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.module - the id of the module to remove
* @return {Promise} - resolves when complete
* @memberof RED.nodes
*/
removeModule: function(opts) {
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.install",error:"settings_unavailable"});
let err = new Error("Settings unavailable");
err.code = "settings_unavailable";
err.status = 400;
return reject(err);
}
var module = runtime.nodes.getModuleInfo(opts.module);
if (!module) {
runtime.log.audit({event: "nodes.remove",module:opts.module,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
try {
runtime.nodes.uninstallModule(opts.module).then(function() {
runtime.log.audit({event: "nodes.remove",module:opts.module});
resolve();
}).catch(function(err) {
err.status = 400;
runtime.log.audit({event: "nodes.remove",module:opts.module,error:err.code||"unexpected_error",message:err.toString()});
return reject(err);
})
} catch(err) {
runtime.log.audit({event: "nodes.remove",module:opts.module,error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
}
});
},
/**
* Enables or disables a module in the runtime
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.module - the id of the module to enable or disable
* @param {String} opts.enabled - whether the module should be enabled or disabled
* @return {Promise<ModuleInfo>} - the module info object
* @memberof RED.nodes
*/
setModuleState: function(opts) {
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.module.set",error:"settings_unavailable"});
let err = new Error("Settings unavailable");
err.code = "settings_unavailable";
err.status = 400;
return reject(err);
}
try {
var mod = opts.module;
var module = runtime.nodes.getModuleInfo(mod);
if (!module) {
runtime.log.audit({event: "nodes.module.set",module:mod,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}
var nodes = module.nodes;
var promises = [];
for (var i = 0; i < nodes.length; ++i) {
promises.push(putNode(nodes[i],opts.enabled));
}
Promise.all(promises).then(function() {
return resolve(runtime.nodes.getModuleInfo(mod));
}).catch(function(err) {
err.status = 400;
return reject(err);
});
} catch(err) {
runtime.log.audit({event: "nodes.module.set",module:mod,enabled:opts.enabled,error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
}
});
},
/**
* Enables or disables a n individual node-set in the runtime
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {String} opts.id - the id of the node-set to enable or disable
* @param {String} opts.enabled - whether the module should be enabled or disabled
* @return {Promise<ModuleInfo>} - the module info object
* @memberof RED.nodes
*/
setNodeSetState: function(opts) {
return new Promise(function(resolve,reject) {
if (!runtime.settings.available()) {
runtime.log.audit({event: "nodes.info.set",error:"settings_unavailable"});
let err = new Error("Settings unavailable");
err.code = "settings_unavailable";
err.status = 400;
return reject(err);
}
var id = opts.id;
var enabled = opts.enabled;
try {
var node = runtime.nodes.getNodeInfo(id);
if (!node) {
runtime.log.audit({event: "nodes.info.set",id:id,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
} else {
delete node.loaded;
putNode(node,enabled).then(function(result) {
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled});
return resolve(result);
}).catch(function(err) {
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
});
}
} catch(err) {
runtime.log.audit({event: "nodes.info.set",id:id,enabled:enabled,error:err.code||"unexpected_error",message:err.toString()});
res.status(400).json({error:err.code||"unexpected_error", message:err.toString()});
}
});
},
/**
* TODO: getModuleCatalogs
*/
getModuleCatalogs: function() {},
/**
* TODO: getModuleCatalog
*/
getModuleCatalog: function() {},
/**
* Gets the list of all icons available in the modules installed within the runtime
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @return {Promise<IconList>} - the list of all icons
* @memberof RED.nodes
*/
getIconList: function(opts) {
return new Promise(function(resolve,reject) {
runtime.log.audit({event: "nodes.icons.get"});
return resolve(runtime.nodes.getNodeIcons());
});
},
/**
* TODO: getIcon
*/
getIcon: function() {}
}

257
red/runtime-api/settings.js Normal file
View File

@@ -0,0 +1,257 @@
/**
* 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.
**/
/**
* @namespace RED.settings
*/
var util = require("util");
var runtime;
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;
}
function getUsername(userObj) {
var username = '__default';
if ( userObj && userObj.name ) {
username = userObj.name;
}
return username;
}
var api = module.exports = {
init: function(_runtime) {
runtime = _runtime;
},
/**
* Gets the runtime settings object
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @return {Promise<Object>} - the runtime settings
* @memberof RED.settings
*/
getRuntimeSettings: function(opts) {
return new Promise(function(resolve,reject) {
try {
var safeSettings = {
httpNodeRoot: runtime.settings.httpNodeRoot||"/",
version: runtime.settings.version,
user: opts.user
}
if (util.isArray(runtime.settings.paletteCategories)) {
safeSettings.paletteCategories = runtime.settings.paletteCategories;
}
if (runtime.settings.flowFilePretty) {
safeSettings.flowFilePretty = runtime.settings.flowFilePretty;
}
if (!runtime.nodes.paletteEditorEnabled()) {
safeSettings.editorTheme = safeSettings.editorTheme || {};
safeSettings.editorTheme.palette = safeSettings.editorTheme.palette || {};
safeSettings.editorTheme.palette.editable = false;
}
if (runtime.storage.projects) {
var activeProject = runtime.storage.projects.getActiveProject();
if (activeProject) {
safeSettings.project = activeProject;
} else if (runtime.storage.projects.flowFileExists()) {
safeSettings.files = {
flow: runtime.storage.projects.getFlowFilename(),
credentials: runtime.storage.projects.getCredentialsFilename()
}
}
safeSettings.git = {
globalUser: runtime.storage.projects.getGlobalGitUser()
}
}
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();
runtime.settings.exportNodeSettings(safeSettings);
resolve(safeSettings);
}catch(err) {
console.log(err);
}
});
},
/**
* Gets an individual user's settings object
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @return {Promise<Object>} - the user settings
* @memberof RED.settings
*/
getUserSettings: function(opts) {
var username;
if (!opts.user || opts.user.anonymous) {
username = '_';
} else {
username = opts.user.username;
}
return Promise.resolve(runtime.settings.getUserSettings(username)||{});
},
/**
* Updates an individual user's settings object.
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.settings - the updates to the user settings
* @return {Promise<Object>} - the user settings
* @memberof RED.settings
*/
updateUserSettings: function(opts) {
var username;
if (!opts.user || opts.user.anonymous) {
username = '_';
} else {
username = opts.user.username;
}
return new Promise(function(resolve,reject) {
var currentSettings = runtime.settings.getUserSettings(username)||{};
currentSettings = extend(currentSettings, opts.settings);
try {
runtime.settings.setUserSettings(username, currentSettings).then(function() {
runtime.log.audit({event: "settings.update",username:username});
return resolve();
}).catch(function(err) {
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
});
} catch(err) {
log.warn(log._("settings.user-not-available",{message:log._("settings.not-available")}));
log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()});
err.status = 400;
return reject(err);
}
});
},
/**
* Gets a list of a user's ssh keys
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @return {Promise<Object>} - the user's ssh keys
* @memberof RED.settings
*/
getUserKeys: function(opts) {
return new Promise(function(resolve,reject) {
var username = getUsername(opts.user);
runtime.storage.projects.ssh.listSSHKeys(username).then(function(list) {
return resolve(list);
}).catch(function(err) {
err.status = 400;
return reject(err);
});
});
},
/**
* Gets a user's ssh public key
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.id - the id of the key to return
* @return {Promise<String>} - the user's ssh public key
* @memberof RED.settings
*/
getUserKey: function(opts) {
return new Promise(function(resolve,reject) {
var username = getUsername(opts.user);
// console.log('username:', username);
runtime.storage.projects.ssh.getSSHKey(username, opts.id).then(function(data) {
if (data) {
return resolve(data);
} else {
var err = new Error("Key not found");
err.code = "not_found";
err.status = 404;
return reject(err);
}
}).catch(function(err) {
err.status = 400;
return reject(err);
});
});
},
/**
* Generates a new ssh key pair
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.name - the id of the key to return
* @param {User} opts.password - (optional) the password for the key pair
* @param {User} opts.comment - (option) a comment to associate with the key pair
* @param {User} opts.size - (optional) the size of the key. Default: 2048
* @return {Promise<String>} - the id of the generated key
* @memberof RED.settings
*/
generateUserKey: function(opts) {
return new Promise(function(resolve,reject) {
var username = getUsername(opts.user);
runtime.storage.projects.ssh.generateSSHKey(username, opts).then(function(name) {
return resolve(name);
}).catch(function(err) {
err.status = 400;
return reject(err);
});
});
},
/**
* Deletes a user's ssh key pair
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.id - the id of the key to delete
* @return {Promise} - resolves when deleted
* @memberof RED.settings
*/
removeUserKey: function(opts) {
return new Promise(function(resolve,reject) {
var username = getUsername(req.user);
runtime.storage.projects.ssh.deleteSSHKey(username, opts.id).then(function() {
return resolve();
}).catch(function(err) {
err.status = 400;
return reject(err);
});
});
}
}