fixed issue when renaming host
This commit is contained in:
parent
fb71dc1dea
commit
eb41fce6df
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
const {promisify} = require('util');
|
const {promisify} = require('util');
|
||||||
const client = require('../utils/redis');
|
const client = require('../utils/redis');
|
||||||
const {processKeys, ObjectValidateError} = require('../utils/object_validate');
|
const objValidate = require('../utils/object_validate');
|
||||||
|
|
||||||
const hostKeysMap = {
|
const hostKeysMap = {
|
||||||
'host': {isRequired: true, type: 'string', min: 3, max: 500},
|
'host': {isRequired: true, type: 'string', min: 3, max: 500},
|
||||||
@ -21,7 +21,7 @@ async function getInfo(data){
|
|||||||
if(info){
|
if(info){
|
||||||
info['host'] = data.host;
|
info['host'] = data.host;
|
||||||
|
|
||||||
return info
|
return objValidate.parseFromString(hostKeysMap, info);
|
||||||
}
|
}
|
||||||
|
|
||||||
}catch(error){
|
}catch(error){
|
||||||
@ -63,9 +63,11 @@ async function listAllDetail(){
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
async function add(data, edit){
|
async function add(data){
|
||||||
try{
|
try{
|
||||||
data = processKeys(hostKeysMap, data, edit);
|
console.log('host', data)
|
||||||
|
data = objValidate.processKeys(hostKeysMap, data);
|
||||||
|
|
||||||
|
|
||||||
if(data.host && await exists(data.host)){
|
if(data.host && await exists(data.host)){
|
||||||
let error = new Error('HostNameUsed');
|
let error = new Error('HostNameUsed');
|
||||||
@ -96,18 +98,23 @@ async function edit(data, host){
|
|||||||
// Check to see if host name changed
|
// Check to see if host name changed
|
||||||
if(data.host && data.host !== host){
|
if(data.host && data.host !== host){
|
||||||
|
|
||||||
|
console.log('pre assign', hostData, data)
|
||||||
|
|
||||||
// Merge the current data into with the updated 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,
|
// Create a new record for the updated host. If that succeeds,
|
||||||
// delete the old recored
|
// delete the old recored
|
||||||
if(await add(hostData)) await remove({host});
|
if(await add(newData)) await remove({host});
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
// Update what ever fields that where passed.
|
// Update what ever fields that where passed.
|
||||||
|
|
||||||
// Validate the passed data, ignoring required fields.
|
// 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
|
// Loop over the data fields and apply them to redis
|
||||||
for(let key of Object.keys(data)){
|
for(let key of Object.keys(data)){
|
||||||
|
@ -32,6 +32,8 @@ function processKeys(map, data, partial){
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out[key] = data.hasOwnProperty(key) ? data[key] : returnOrCall(map[key].default);
|
||||||
|
|
||||||
if(data.hasOwnProperty(key) && process_type[map[key].type]){
|
if(data.hasOwnProperty(key) && process_type[map[key].type]){
|
||||||
let typeError = process_type[map[key].type](map[key], data[key]);
|
let typeError = process_type[map[key].type](map[key], data[key]);
|
||||||
if(typeError){
|
if(typeError){
|
||||||
@ -40,8 +42,6 @@ function processKeys(map, data, partial){
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
out[key] = data.hasOwnProperty(key) ? data[key] : returnOrCall(map[key].default);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(errors.length !== 0){
|
if(errors.length !== 0){
|
||||||
@ -52,23 +52,41 @@ function processKeys(map, data, partial){
|
|||||||
return out;
|
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) {
|
function ObjectValidateError(message) {
|
||||||
this.name = 'ObjectValidateError';
|
this.name = 'ObjectValidateError';
|
||||||
this.message = (message || {});
|
this.message = (message || {});
|
||||||
this.status = 422;
|
this.status = 422;
|
||||||
}
|
}
|
||||||
ObjectValidateError.prototype = Error.prototype;
|
ObjectValidateError.prototype = Error.prototype;
|
||||||
|
|
||||||
|
|
||||||
module.exports = {processKeys, ObjectValidateError};
|
module.exports = {processKeys, parseFromString, ObjectValidateError};
|
||||||
|
|
||||||
if (require.main === module) {
|
if (require.main === module) {
|
||||||
const keys_map = {
|
const keys_map = {
|
||||||
'host': {isRequired: true, type: 'string', min: 3, max: 500},
|
'host': {isRequired: true, type: 'string', min: 3, max: 500},
|
||||||
'ip': {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},
|
'updated': {default: function(){return (new Date).getTime()}, always:true},
|
||||||
'username': {isRequired: true, type: 'string'},
|
'username': {isRequired: true, type: 'string', always: true},
|
||||||
'targetPort': {isRequired: true, type: 'number', min:0, max:65535},
|
'targetport': {isRequired: true, type: 'number', min:0, max:65535},
|
||||||
'forcessl': {isRequired: false, default: true, type: 'boolean'},
|
'forcessl': {isRequired: false, default: true, type: 'boolean'},
|
||||||
'targetssl': {isRequired: false, default: false, type: 'boolean'},
|
'targetssl': {isRequired: false, default: false, type: 'boolean'},
|
||||||
}
|
}
|
||||||
@ -77,10 +95,21 @@ if (require.main === module) {
|
|||||||
host:'asdqwwd',
|
host:'asdqwwd',
|
||||||
ip: 'sdfwef',
|
ip: 'sdfwef',
|
||||||
username: '',
|
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' }
|
||||||
|
))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user