fixed issue when renaming host

This commit is contained in:
William Mantly 2019-12-22 18:53:00 -05:00
parent fb71dc1dea
commit eb41fce6df
Signed by: wmantly
GPG Key ID: E1EEC7650BA97160
2 changed files with 53 additions and 17 deletions

View File

@ -2,7 +2,7 @@
const {promisify} = require('util');
const client = require('../utils/redis');
const {processKeys, ObjectValidateError} = require('../utils/object_validate');
const objValidate = require('../utils/object_validate');
const hostKeysMap = {
'host': {isRequired: true, type: 'string', min: 3, max: 500},
@ -21,7 +21,7 @@ async function getInfo(data){
if(info){
info['host'] = data.host;
return info
return objValidate.parseFromString(hostKeysMap, info);
}
}catch(error){
@ -63,9 +63,11 @@ async function listAllDetail(){
return out
}
async function add(data, edit){
async function add(data){
try{
data = processKeys(hostKeysMap, data, edit);
console.log('host', data)
data = objValidate.processKeys(hostKeysMap, data);
if(data.host && await exists(data.host)){
let error = new Error('HostNameUsed');
@ -96,18 +98,23 @@ async function edit(data, host){
// Check to see if host name changed
if(data.host && data.host !== host){
console.log('pre assign', hostData, data)
// Merge the current data into with the updated data
data = Object.assign({}, hostData, data);
let newData = Object.assign({}, hostData, data);
// Remove the updated failed so it doesnt keep it
delete newData.updated;
// Create a new record for the updated host. If that succeeds,
// delete the old recored
if(await add(hostData)) await remove({host});
if(await add(newData)) await remove({host});
}else{
// Update what ever fields that where passed.
// Validate the passed data, ignoring required fields.
data = processKeys(hostKeysMap, data, true);
data = objValidate.processKeys(hostKeysMap, data, true);
// Loop over the data fields and apply them to redis
for(let key of Object.keys(data)){

View File

@ -32,6 +32,8 @@ function processKeys(map, data, partial){
continue;
}
out[key] = data.hasOwnProperty(key) ? data[key] : returnOrCall(map[key].default);
if(data.hasOwnProperty(key) && process_type[map[key].type]){
let typeError = process_type[map[key].type](map[key], data[key]);
if(typeError){
@ -40,8 +42,6 @@ function processKeys(map, data, partial){
continue;
}
}
out[key] = data.hasOwnProperty(key) ? data[key] : returnOrCall(map[key].default);
}
if(errors.length !== 0){
@ -52,23 +52,41 @@ function processKeys(map, data, partial){
return out;
}
function parseFromString(map, data){
let types = {
boolean: function(value){ return value === 'true' ? true : false },
number: Number,
string: String,
};
for(let key of Object.keys(data)){
console.log('looking at', key)
if(map[key] && map[key].type){
console.log('converting', data[key], 'to', map[key].type)
data[key] = types[map[key].type](data[key]);
}
}
return data;
}
function ObjectValidateError(message) {
this.name = 'ObjectValidateError';
this.message = (message || {});
this.status = 422;
this.name = 'ObjectValidateError';
this.message = (message || {});
this.status = 422;
}
ObjectValidateError.prototype = Error.prototype;
module.exports = {processKeys, ObjectValidateError};
module.exports = {processKeys, parseFromString, ObjectValidateError};
if (require.main === module) {
const keys_map = {
'host': {isRequired: true, type: 'string', min: 3, max: 500},
'ip': {isRequired: true, type: 'string', min: 3, max: 500},
'updated': {default: function(){return (new Date).getTime()}, always:true},
'username': {isRequired: true, type: 'string'},
'targetPort': {isRequired: true, type: 'number', min:0, max:65535},
'username': {isRequired: true, type: 'string', always: true},
'targetport': {isRequired: true, type: 'number', min:0, max:65535},
'forcessl': {isRequired: false, default: true, type: 'boolean'},
'targetssl': {isRequired: false, default: false, type: 'boolean'},
}
@ -77,10 +95,21 @@ if (require.main === module) {
host:'asdqwwd',
ip: 'sdfwef',
username: '',
targetPort: 8000
targetport: 8000,
updated: 'dqwqwdq'
}));
console.log(parseFromString(keys_map, {
host: 'stest.theta42.com',
ip: 'googs',
updated: '1577054966047',
username: 'william',
targetport: '8080',
forcessl: 'true',
targetssl: 'false' }
))
}