[icinga-checkins] icinga.org: icingaweb2-module-director/master: ObjectController: allow all REST api methods

git at icinga.org git at icinga.org
Wed Feb 3 00:58:47 CET 2016


Module: icingaweb2-module-director
Branch: master
Commit: 0ebf269ebd4b97788cce650c315f1bf9cd93b89d
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=0ebf269ebd4b97788cce650c315f1bf9cd93b89d

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Feb  3 00:54:00 2016 +0100

ObjectController: allow all REST api methods

---

 .../Director/Web/Controller/ObjectController.php   |   68 +++++++++++++++++++-
 1 file changed, 65 insertions(+), 3 deletions(-)

diff --git a/library/Director/Web/Controller/ObjectController.php b/library/Director/Web/Controller/ObjectController.php
index 507c24b..3135491 100644
--- a/library/Director/Web/Controller/ObjectController.php
+++ b/library/Director/Web/Controller/ObjectController.php
@@ -2,6 +2,7 @@
 
 namespace Icinga\Module\Director\Web\Controller;
 
+use Exception;
 use Icinga\Web\Url;
 use Icinga\Module\Director\Objects\IcingaObject;
 
@@ -59,9 +60,11 @@ abstract class ObjectController extends ActionController
     public function indexAction()
     {
         if ($this->getRequest()->isApiRequest()) {
-            return $this->sendJson(
-                $this->object->toPlainObject($this->params->shift('resolved'))
-            );
+            try {
+                return $this->handleApiRequest();
+            } catch (Exception $e) {
+                return $this->sendJson((object) array('error' => $e->getMessage()));
+            }
         }
 
         return $this->editAction();
@@ -254,4 +257,63 @@ abstract class ObjectController extends ActionController
 
         return $this->object;
     }
+
+    protected function handleApiRequest()
+    {
+        $request = $this->getRequest();
+        $db = $this->db();
+
+        switch ($request->getMethod()) {
+            case 'DELETE':
+                $name = $this->object->object_name;
+                $obj = $this->object->toPlainObject(false, true);
+                $form = $this->loadForm(
+                    'icingaDeleteObject'
+                )->setObject($this->object)->setRequest($request)->onSuccess();
+
+                return $this->sendJson($obj);
+
+            case 'POST':
+            case 'PUT':
+                $type = $this->getType();
+                $data = (array) json_decode($request->getRawBody());
+                if ($object = $this->object) {
+                    if ($request->getMethod() === 'POST') {
+                        $object->setProperties($data);
+                    } else {
+                        $object->replaceWith(
+                            IcingaObject::createByType($type, $data)
+                        );
+                    }
+                } else {
+                    $object = IcingaObject::createByType($type, $data, $db);
+                }
+
+                $response = $this->getResponse();
+                if ($object->hasBeenModified()) {
+                    $object->store();
+                    if ($object->hasBeenLoadedFromDb()) {
+                        $response->setHttpResponseCode(200);
+                    } else {
+                        $response->setHttpResponseCode(201);
+                    }
+                } else {
+                    $response->setHttpResponseCode(304);
+                }
+
+                return $this->sendJson($object->toPlainObject(false, true));
+
+            case 'GET':
+                return $this->sendJson(
+                    $this->object->toPlainObject(
+                        $this->params->shift('resolved'),
+                        ! $this->params->shift('withNull'),
+                        $this->params->shift('properties')
+                    )
+                );
+
+            default:
+                throw new Exception('Unsupported method ' . $request->getMethod());
+        }
+    }
 }



More information about the icinga-checkins mailing list