diff --git a/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/Api/SettingsController.php b/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/Api/SettingsController.php index b62613b82..6ac701921 100644 --- a/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/Api/SettingsController.php +++ b/net/haproxy/src/opnsense/mvc/app/controllers/OPNsense/HAProxy/Api/SettingsController.php @@ -29,1468 +29,347 @@ */ namespace OPNsense\HAProxy\Api; -use \OPNsense\Base\ApiControllerBase; -use \OPNsense\HAProxy\HAProxy; -use \OPNsense\Core\Config; +use OPNsense\Base\ApiMutableModelControllerBase; use \OPNsense\Base\UIModelGrid; +use \OPNsense\Core\Config; +use \OPNsense\HAProxy\HAProxy; /** * Class SettingsController * @package OPNsense\HAProxy */ -class SettingsController extends ApiControllerBase +class SettingsController extends ApiMutableModelControllerBase { - /** - * Validate and save model after update or insertion. - * Use the reference node and tag to rename validation output for a specific - * node to a new offset, which makes it easier to reference specific uuids - * without having to use them in the frontend descriptions. - * @param $mdl model reference - * @param $node reference node, to use as relative offset - * @param $reference reference for validation output, used to rename the validation output keys - * @return array result / validation output - */ - private function save($mdl, $node = null, $reference = null) - { - $result = array("result"=>"failed","validations" => array()); - // perform validation - $valMsgs = $mdl->performValidation(); - foreach ($valMsgs as $field => $msg) { - // replace absolute path to attribute for relative one at uuid. - if ($node != null) { - $fieldnm = str_replace($node->__reference, $reference, $msg->getField()); - $result["validations"][$fieldnm] = $msg->getMessage(); - } else { - $result["validations"][$msg->getField()] = $msg->getMessage(); - } - } + protected static $internalModelName = 'haproxy'; + protected static $internalModelClass = '\OPNsense\HAProxy\HAProxy'; - // serialize model to config and save when there are no validation errors - if (count($result['validations']) == 0) { - // save config if validated correctly - $mdl->serializeToConfig(); - - Config::getInstance()->save(); - $result = array("result" => "saved"); - } - - return $result; - } - - /** - * retrieve haproxy settings - * @return array - */ - public function getAction() - { - $result = array(); - if ($this->request->isGet()) { - $mdlProxy = new HAProxy(); - $result['haproxy'] = $mdlProxy->getNodes(); - } - - return $result; - } - - /** - * update haproxy configuration fields - * @return array - * @throws \Phalcon\Validation\Exception - */ - public function setAction() - { - $result = array("result"=>"failed"); - if ($this->request->hasPost("haproxy")) { - // load model and update with provided data - $mdlProxy = new HAProxy(); - $mdlProxy->setNodes($this->request->getPost("haproxy")); - - // perform validation - $valMsgs = $mdlProxy->performValidation(); - foreach ($valMsgs as $field => $msg) { - if (!array_key_exists("validations", $result)) { - $result["validations"] = array(); - } - $result["validations"]["haproxy.".$msg->getField()] = $msg->getMessage(); - } - - // serialize model to config and save - if ($valMsgs->count() == 0) { - $mdlProxy->serializeToConfig(); - $cnf = Config::getInstance(); - $cnf->save(); - $result["result"] = "saved"; - } - } - - return $result; - } - - /** - * retrieve frontend settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getFrontendAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('frontends.frontend.'.$uuid); - if ($node != null) { - // return node - return array("frontend" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->frontends->frontend->add(); - return array("frontend" => $node->getNodes()); - } - return array(); + return $this->getBase('frontend', 'frontends.frontend', $uuid); } - /** - * update frontend with given properties - * @param $uuid item unique id - * @return array - */ public function setFrontendAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("frontend")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('frontends.frontend.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("frontend")); - return $this->save($mdlCP, $node, "frontend"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('frontend', 'frontends.frontend', $uuid); } - /** - * add new frontend and set with attributes from post - * @return array - */ public function addFrontendAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("frontend")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->frontends->frontend->Add(); - $node->setNodes($this->request->getPost("frontend")); - return $this->save($mdlCP, $node, "frontend"); - } - return $result; + return $this->addBase('frontend', 'frontends.frontend'); } - /** - * delete frontend by uuid - * @param $uuid item unique id - * @return array status - */ public function delFrontendAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->frontends->frontend->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('frontends.frontend', $uuid); } - /** - * toggle frontend by uuid (enable/disable) - * @param $uuid item unique id - * @param $enabled desired state enabled(1)/disabled(0), leave empty for toggle - * @return array status - */ - public function toggleFrontendAction($uuid, $enabled = null) + public function toggleFrontendAction($uuid) { - - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('frontends.frontend.' . $uuid); - if ($node != null) { - if ($enabled == "0" || $enabled == "1") { - $node->enabled = (string)$enabled; - } elseif ((string)$node->enabled == "1") { - $node->enabled = "0"; - } else { - $node->enabled = "1"; - } - $result['result'] = $node->enabled; - // if item has toggled, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->toggleBase('frontends.frontend', $uuid); } - /** - * search haproxy frontends - * @return array - */ public function searchFrontendsAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->frontends->frontend); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "name", "description"), - "name" - ); + return $this->searchBase('frontends.frontend', array('enabled', 'name', 'description'), 'name'); } - /** - * retrieve backend settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getBackendAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('backends.backend.'.$uuid); - if ($node != null) { - // return node - return array("backend" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->backends->backend->add(); - return array("backend" => $node->getNodes()); - } - return array(); + return $this->getBase('backend', 'backends.backend', $uuid); } - /** - * update backend with given properties - * @param $uuid item unique id - * @return array - */ public function setBackendAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("backend")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('backends.backend.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("backend")); - return $this->save($mdlCP, $node, "backend"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('backend', 'backends.backend', $uuid); } - /** - * add new backend and set with attributes from post - * @return array - */ public function addBackendAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("backend")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->backends->backend->Add(); - $node->setNodes($this->request->getPost("backend")); - return $this->save($mdlCP, $node, "backend"); - } - return $result; + return $this->addBase('backend', 'backends.backend'); } - /** - * delete backend by uuid - * @param $uuid item unique id - * @return array status - */ public function delBackendAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->backends->backend->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('backends.backend', $uuid); } - /** - * toggle backend by uuid (enable/disable) - * @param $uuid item unique id - * @param $enabled desired state enabled(1)/disabled(0), leave empty for toggle - * @return array status - */ public function toggleBackendAction($uuid, $enabled = null) { - - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('backends.backend.' . $uuid); - if ($node != null) { - if ($enabled == "0" || $enabled == "1") { - $node->enabled = (string)$enabled; - } elseif ((string)$node->enabled == "1") { - $node->enabled = "0"; - } else { - $node->enabled = "1"; - } - $result['result'] = $node->enabled; - // if item has toggled, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->toggleBase('backends.backend', $uuid); } - /** - * search haproxy backends - * @return array - */ public function searchBackendsAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->backends->backend); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "name", "description"), - "name" - ); + return $this->searchBase('backends.backend', array('enabled', 'name', 'description'), 'name'); } - /** - * retrieve server settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getServerAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('servers.server.'.$uuid); - if ($node != null) { - // return node - return array("server" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->servers->server->add(); - return array("server" => $node->getNodes()); - } - return array(); + return $this->getBase('server', 'servers.server', $uuid); } - /** - * update server with given properties - * @param $uuid item unique id - * @return array - */ public function setServerAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("server")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('servers.server.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("server")); - return $this->save($mdlCP, $node, "server"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('server', 'servers.server', $uuid); } - /** - * add new server and set with attributes from post - * @return array - */ public function addServerAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("server")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->servers->server->Add(); - $node->setNodes($this->request->getPost("server")); - return $this->save($mdlCP, $node, "server"); - } - return $result; + return $this->addBase('server', 'servers.server'); } - /** - * delete server by uuid - * @param $uuid item unique id - * @return array status - */ public function delServerAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->servers->server->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('servers.server', $uuid); } - /** - * search servers - * @return array - */ public function searchServersAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->servers->server); - return $grid->fetchBindRequest( - $this->request, - array("name", "address", "port", "description"), - "name" - ); + return $this->searchBase('servers.server', array('name', 'address', 'port', 'description'), 'name'); } - /** - * retrieve healthcheck settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getHealthcheckAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('healthchecks.healthcheck.'.$uuid); - if ($node != null) { - // return node - return array("healthcheck" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->healthchecks->healthcheck->add(); - return array("healthcheck" => $node->getNodes()); - } - return array(); + return $this->getBase('healthcheck', 'healthchecks.healthcheck', $uuid); } - /** - * update healthcheck with given properties - * @param $uuid item unique id - * @return array - */ public function setHealthcheckAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("healthcheck")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('healthchecks.healthcheck.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("healthcheck")); - return $this->save($mdlCP, $node, "healthcheck"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('healthcheck', 'healthchecks.healthcheck', $uuid); } - /** - * add new healthcheck and set with attributes from post - * @return array - */ public function addHealthcheckAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("healthcheck")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->healthchecks->healthcheck->Add(); - $node->setNodes($this->request->getPost("healthcheck")); - return $this->save($mdlCP, $node, "healthcheck"); - } - return $result; + return $this->addBase('healthcheck', 'healthchecks.healthcheck'); } - /** - * delete healthcheck by uuid - * @param $uuid item unique id - * @return array status - */ public function delHealthcheckAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->healthchecks->healthcheck->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('healthchecks.healthcheck', $uuid); } - /** - * search healthchecks - * @return array - */ public function searchHealthchecksAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->healthchecks->healthcheck); - return $grid->fetchBindRequest( - $this->request, - array("name", "description"), - "name" - ); + return $this->searchBase('healthchecks.healthcheck', array('name', 'description'), 'name'); } - /** - * retrieve acl settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getAclAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('acls.acl.'.$uuid); - if ($node != null) { - // return node - return array("acl" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->acls->acl->add(); - return array("acl" => $node->getNodes()); - } - return array(); + return $this->getBase('acl', 'acls.acl', $uuid); } - /** - * update acl with given properties - * @param $uuid item unique id - * @return array - */ public function setAclAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("acl")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('acls.acl.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("acl")); - return $this->save($mdlCP, $node, "acl"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('acl', 'acls.acl', $uuid); } - /** - * add new acl and set with attributes from post - * @return array - */ public function addAclAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("acl")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->acls->acl->Add(); - $node->setNodes($this->request->getPost("acl")); - return $this->save($mdlCP, $node, "acl"); - } - return $result; + return $this->addBase('acl', 'acls.acl'); } - /** - * delete acl by uuid - * @param $uuid item unique id - * @return array status - */ public function delAclAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->acls->acl->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('acls.acl', $uuid); } - /** - * search acls - * @return array - */ public function searchAclsAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->acls->acl); - return $grid->fetchBindRequest( - $this->request, - array("name", "description"), - "name" - ); + return $this->searchBase('acls.acl', array('name', 'description'), 'name'); } - /** - * retrieve action settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getActionAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('actions.action.'.$uuid); - if ($node != null) { - // return node - return array("action" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->actions->action->add(); - return array("action" => $node->getNodes()); - } - return array(); + return $this->getBase('action', 'actions.action', $uuid); } - /** - * update action with given properties - * @param $uuid item unique id - * @return array - */ public function setActionAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("action")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('actions.action.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("action")); - return $this->save($mdlCP, $node, "action"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('action', 'actions.action', $uuid); } - /** - * add new action and set with attributes from post - * @return array - */ public function addActionAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("action")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->actions->action->Add(); - $node->setNodes($this->request->getPost("action")); - return $this->save($mdlCP, $node, "action"); - } - return $result; + return $this->addBase('action', 'actions.action'); } - /** - * delete action by uuid - * @param $uuid item unique id - * @return array status - */ public function delActionAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->actions->action->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('actions.action', $uuid); } - /** - * search actions - * @return array - */ public function searchActionsAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->actions->action); - return $grid->fetchBindRequest( - $this->request, - array("name", "description"), - "name" - ); + return $this->searchBase('actions.action', array('name', 'description'), 'name'); } - /** - * retrieve lua settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getLuaAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('luas.lua.'.$uuid); - if ($node != null) { - // return node - return array("lua" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->luas->lua->add(); - return array("lua" => $node->getNodes()); - } - return array(); + return $this->getBase('lua', 'luas.lua', $uuid); } - /** - * update lua with given properties - * @param $uuid item unique id - * @return array - */ public function setLuaAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("lua")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('luas.lua.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("lua")); - return $this->save($mdlCP, $node, "lua"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('lua', 'luas.lua', $uuid); } - /** - * add new lua and set with attributes from post - * @return array - */ public function addLuaAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("lua")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->luas->lua->Add(); - $node->setNodes($this->request->getPost("lua")); - return $this->save($mdlCP, $node, "lua"); - } - return $result; + return $this->addBase('lua', 'luas.lua'); } - /** - * delete lua by uuid - * @param $uuid item unique id - * @return array status - */ public function delLuaAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->luas->lua->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('luas.lua', $uuid); } - /** - * toggle lua by uuid (enable/disable) - * @param $uuid item unique id - * @param $enabled desired state enabled(1)/disabled(0), leave empty for toggle - * @return array status - */ public function toggleLuaAction($uuid, $enabled = null) { - - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('luas.lua.' . $uuid); - if ($node != null) { - if ($enabled == "0" || $enabled == "1") { - $node->enabled = (string)$enabled; - } elseif ((string)$node->enabled == "1") { - $node->enabled = "0"; - } else { - $node->enabled = "1"; - } - $result['result'] = $node->enabled; - // if item has toggled, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->toggleBase('luas.lua', $uuid); } - /** - * search luas - * @return array - */ public function searchLuasAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->luas->lua); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "name", "description"), - "name" - ); + return $this->searchBase('luas.lua', array('enabled', 'name', 'description'), 'name'); } - /** - * retrieve errorfile settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getErrorfileAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('errorfiles.errorfile.'.$uuid); - if ($node != null) { - // return node - return array("errorfile" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->errorfiles->errorfile->add(); - return array("errorfile" => $node->getNodes()); - } - return array(); + return $this->getBase('errorfile', 'errorfiles.errorfile', $uuid); } - /** - * update errorfile with given properties - * @param $uuid item unique id - * @return array - */ public function setErrorfileAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("errorfile")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('errorfiles.errorfile.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("errorfile")); - return $this->save($mdlCP, $node, "errorfile"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('errorfile', 'errorfiles.errorfile', $uuid); } - /** - * add new errorfile and set with attributes from post - * @return array - */ public function addErrorfileAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("errorfile")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->errorfiles->errorfile->Add(); - $node->setNodes($this->request->getPost("errorfile")); - return $this->save($mdlCP, $node, "errorfile"); - } - return $result; + return $this->addBase('errorfile', 'errorfiles.errorfile'); } - /** - * delete errorfile by uuid - * @param $uuid item unique id - * @return array status - */ public function delErrorfileAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->errorfiles->errorfile->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('errorfiles.errorfile', $uuid); } - /** - * search errorfiles - * @return array - */ public function searchErrorfilesAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->errorfiles->errorfile); - return $grid->fetchBindRequest( - $this->request, - array("name", "description"), - "name" - ); + return $this->searchBase('errorfiles.errorfile', array('name', 'description'), 'name'); } - /** - * retrieve mapfile settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getMapfileAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('mapfiles.mapfile.'.$uuid); - if ($node != null) { - // return node - return array("mapfile" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->mapfiles->mapfile->add(); - return array("mapfile" => $node->getNodes()); - } - return array(); + return $this->getBase('mapfile', 'mapfiles.mapfile', $uuid); } - /** - * update mapfile with given properties - * @param $uuid item unique id - * @return array - */ public function setMapfileAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("mapfile")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('mapfiles.mapfile.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("mapfile")); - return $this->save($mdlCP, $node, "mapfile"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('mapfile', 'mapfiles.mapfile', $uuid); } - /** - * add new mapfile and set with attributes from post - * @return array - */ public function addMapfileAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("mapfile")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->mapfiles->mapfile->Add(); - $node->setNodes($this->request->getPost("mapfile")); - return $this->save($mdlCP, $node, "mapfile"); - } - return $result; + return $this->addBase('mapfile', 'mapfiles.mapfile'); } - /** - * delete mapfile by uuid - * @param $uuid item unique id - * @return array status - */ public function delMapfileAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->mapfiles->mapfile->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('mapfiles.mapfile', $uuid); } - /** - * search mapfiles - * @return array - */ public function searchMapfilesAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->mapfiles->mapfile); - return $grid->fetchBindRequest( - $this->request, - array("name", "description"), - "name" - ); + return $this->searchBase('mapfiles.mapfile', array('name', 'description'), 'name'); } - /** - * retrieve cpu settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getCpuAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('cpus.cpu.'.$uuid); - if ($node != null) { - // return node - return array("cpu" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->cpus->cpu->add(); - return array("cpu" => $node->getNodes()); - } - return array(); + return $this->getBase('cpu', 'cpus.cpu', $uuid); } - /** - * update cpu with given properties - * @param $uuid item unique id - * @return array - */ public function setCpuAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("cpu")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('cpus.cpu.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("cpu")); - return $this->save($mdlCP, $node, "cpu"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('cpu', 'cpus.cpu', $uuid); } - /** - * add new cpu and set with attributes from post - * @return array - */ public function addCpuAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("cpu")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->cpus->cpu->Add(); - $node->setNodes($this->request->getPost("cpu")); - return $this->save($mdlCP, $node, "cpu"); - } - return $result; + return $this->addBase('cpu', 'cpus.cpu'); } - /** - * delete cpu by uuid - * @param $uuid item unique id - * @return array status - */ public function delCpuAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->cpus->cpu->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('cpus.cpu', $uuid); } - /** - * toggle cpu by uuid (enable/disable) - * @param $uuid item unique id - * @param $enabled desired state enabled(1)/disabled(0), leave empty for toggle - * @return array status - */ public function toggleCpuAction($uuid, $enabled = null) { - - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('cpus.cpu.' . $uuid); - if ($node != null) { - if ($enabled == "0" || $enabled == "1") { - $node->enabled = (string)$enabled; - } elseif ((string)$node->enabled == "1") { - $node->enabled = "0"; - } else { - $node->enabled = "1"; - } - $result['result'] = $node->enabled; - // if item has toggled, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->toggleBase('cpus.cpu', $uuid); } - /** - * search cpus - * @return array - */ public function searchCpusAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->cpus->cpu); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "name", "process_id", "thread_id", "cpu_id"), - "name" - ); + return $this->searchBase('cpus.cpu', array('enabled', 'name', 'process_id', 'thread_id', 'cpu_id'), 'name'); } - /** - * retrieve group settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getGroupAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('groups.group.'.$uuid); - if ($node != null) { - // return node - return array("group" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->groups->group->add(); - return array("group" => $node->getNodes()); - } - return array(); + return $this->getBase('group', 'groups.group', $uuid); } - /** - * update group with given properties - * @param $uuid item unique id - * @return array - */ public function setGroupAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("group")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('groups.group.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("group")); - return $this->save($mdlCP, $node, "group"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('group', 'groups.group', $uuid); } - /** - * add new group and set with attributes from post - * @return array - */ public function addGroupAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("group")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->groups->group->Add(); - $node->setNodes($this->request->getPost("group")); - return $this->save($mdlCP, $node, "group"); - } - return $result; + return $this->addBase('group', 'groups.group'); } - /** - * delete group by uuid - * @param $uuid item unique id - * @return array status - */ public function delGroupAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->groups->group->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('groups.group', $uuid); } - /** - * toggle group by uuid (enable/disable) - * @param $uuid item unique id - * @param $enabled desired state enabled(1)/disabled(0), leave empty for toggle - * @return array status - */ public function toggleGroupAction($uuid, $enabled = null) { - - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('groups.group.' . $uuid); - if ($node != null) { - if ($enabled == "0" || $enabled == "1") { - $node->enabled = (string)$enabled; - } elseif ((string)$node->enabled == "1") { - $node->enabled = "0"; - } else { - $node->enabled = "1"; - } - $result['result'] = $node->enabled; - // if item has toggled, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->toggleBase('groups.group', $uuid); } - /** - * search groups - * @return array - */ public function searchGroupsAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->groups->group); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "name", "description"), - "name" - ); + return $this->searchBase('groups.group', array('enabled', 'name', 'description'), 'name'); } - /** - * retrieve user settings or return defaults - * @param $uuid item unique id - * @return array - */ public function getUserAction($uuid = null) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('users.user.'.$uuid); - if ($node != null) { - // return node - return array("user" => $node->getNodes()); - } - } else { - // generate new node, but don't save to disc - $node = $mdlCP->users->user->add(); - return array("user" => $node->getNodes()); - } - return array(); + return $this->getBase('user', 'users.user', $uuid); } - /** - * update user with given properties - * @param $uuid item unique id - * @return array - */ public function setUserAction($uuid) { - if ($this->request->isPost() && $this->request->hasPost("user")) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('users.user.'.$uuid); - if ($node != null) { - $node->setNodes($this->request->getPost("user")); - return $this->save($mdlCP, $node, "user"); - } - } - } - return array("result"=>"failed"); + return $this->setBase('user', 'users.user', $uuid); } - /** - * add new user and set with attributes from post - * @return array - */ public function addUserAction() { - $result = array("result"=>"failed"); - if ($this->request->isPost() && $this->request->hasPost("user")) { - $mdlCP = new HAProxy(); - $node = $mdlCP->users->user->Add(); - $node->setNodes($this->request->getPost("user")); - return $this->save($mdlCP, $node, "user"); - } - return $result; + return $this->addBase('user', 'users.user'); } - /** - * delete user by uuid - * @param $uuid item unique id - * @return array status - */ public function delUserAction($uuid) { - $result = array("result"=>"failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - if ($mdlCP->users->user->del($uuid)) { - // if item is removed, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - $result['result'] = 'deleted'; - } else { - $result['result'] = 'not found'; - } - } - } - return $result; + return $this->delBase('users.user', $uuid); } - /** - * toggle user by uuid (enable/disable) - * @param $uuid item unique id - * @param $enabled desired state enabled(1)/disabled(0), leave empty for toggle - * @return array status - */ public function toggleUserAction($uuid, $enabled = null) { - - $result = array("result" => "failed"); - if ($this->request->isPost()) { - $mdlCP = new HAProxy(); - if ($uuid != null) { - $node = $mdlCP->getNodeByReference('users.user.' . $uuid); - if ($node != null) { - if ($enabled == "0" || $enabled == "1") { - $node->enabled = (string)$enabled; - } elseif ((string)$node->enabled == "1") { - $node->enabled = "0"; - } else { - $node->enabled = "1"; - } - $result['result'] = $node->enabled; - // if item has toggled, serialize to config and save - $mdlCP->serializeToConfig(); - Config::getInstance()->save(); - } - } - } - return $result; + return $this->toggleBase('users.user', $uuid); } - /** - * search users - * @return array - */ public function searchUsersAction() { - $this->sessionClose(); - $mdlCP = new HAProxy(); - $grid = new UIModelGrid($mdlCP->users->user); - return $grid->fetchBindRequest( - $this->request, - array("enabled", "name", "description"), - "name" - ); + return $this->searchBase('users.user', array('enabled', 'name', 'description'), 'name'); } }