From b3c668adbf0ea47ebc023a28e3d84816ca052200 Mon Sep 17 00:00:00 2001 From: William Mantly Date: Fri, 20 Dec 2019 20:25:00 -0500 Subject: [PATCH] host model uses the object validation and passed better errors --- nodejs/models/hosts.js | 104 ++++++++++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 21 deletions(-) diff --git a/nodejs/models/hosts.js b/nodejs/models/hosts.js index 7876107..365003e 100755 --- a/nodejs/models/hosts.js +++ b/nodejs/models/hosts.js @@ -2,25 +2,56 @@ const {promisify} = require('util'); const client = require('../redis'); +const {processKeys, ObjectValidateError} = require('../utils/object_validate'); -async function getInfo(data){ - let info = await client.HGETALL('host_' + data.host); - info['host'] = data.host; - - return info +const hostKeysMap = { + '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', 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'}, } +async function getInfo(data){ + try{ + let info = await client.HGETALL('host_' + data.host); + + if(info){ + info['host'] = data.host; + + return info + } + + }catch(error){ + throw error + } + + let error = new Error('HostNotFound'); + error.message = 'Host does not exists'; + error.status = 404; + throw error; +} + +async function exists(host){ + try{ + await getInfo({host}) + return true + }catch(error){ + return false; + } +} async function listAll(){ try{ let hosts = await client.SMEMBERS('hosts'); return hosts; }catch(error){ - return new Error(error); + throw error; } } - async function listAllDetail(){ let out = []; @@ -31,34 +62,65 @@ async function listAllDetail(){ return out } +async function add(data, edit){ + try{ + data = processKeys(hostKeysMap, data, edit); -async function add(data){ + if(data.host && await exists(data.host)){ + let error = new Error('HostNameUsed'); + error.message = 'Host already exists'; + error.status = 409; + throw error; + }else if(data.host){ + await client.SADD('hosts', data.host); + } + for(let key of Object.keys(data)){ + await client.HSET('host_' + data.host, key, data[key]); + } - await client.SADD('hosts', data.host); - await client.HSET('host_' + data.host, 'ip', data.ip); - await client.HSET('host_' + data.host, 'updated', (new Date).getTime()); - await client.HSET('host_' + data.host, 'username', data.username); - await client.HSET('host_' + data.host, 'targetPort', data.targetPort); - if(data.forceSSL !== undefined){ - await client.HSET('host_' + data.host, 'forcessl', !!data.forceSSL); + return true; + } catch(error){ + throw error } - if(data.targetSSL !== undefined){ - await client.HSET('host_' + data.host, 'targetssl', !!data.targetSSL); - } - } +async function edit(data, host){ + try{ + let hostData = await getInfo({host}); + + if(data.host && data.host !== host){ + data = Object.assign({}, hostData, data); + + if(await add('hosts', hostData)) await remove({host}); + + }else{ + data = processKeys(hostKeysMap, data, true); + // console.log('host edit data', data); + for(let key of Object.keys(data)){ + await client.HSET('host_' + host, key, data[key]); + } + } + } catch(error){ + throw error; + } +} async function remove(data){ try{ + await getInfo(data); + await client.SREM('hosts', data.host); let count = await client.DEL('host_' + data.host); + + for(let key of Object.keys(hostKeysMap)){ + await client.HDEL('host_' + data.host, key); + } return count; } catch(error) { - return new Error(error); + throw error; } } -module.exports = {getInfo, listAll, listAllDetail, add, remove}; +module.exports = {getInfo, listAll, listAllDetail, add, edit, remove};