From a814846fbacd01a723f992584b8cb52ba8e8c887 Mon Sep 17 00:00:00 2001
From: Romain Courteaud <romain@nexedi.com>
Date: Tue, 11 Sep 2012 14:42:43 +0200
Subject: [PATCH] Add a simple status publisher for computer and instance

---
 .../portal_components/VifibRestAPIV1.py       | 74 +++++++++++++++++++
 .../bt5/vifib_slapos_rest_api_v1/bt/revision  |  2 +-
 2 files changed, 75 insertions(+), 1 deletion(-)

diff --git a/master/bt5/vifib_slapos_rest_api_v1/DocumentTemplateItem/portal_components/VifibRestAPIV1.py b/master/bt5/vifib_slapos_rest_api_v1/DocumentTemplateItem/portal_components/VifibRestAPIV1.py
index 41372e229..5d7b73be1 100644
--- a/master/bt5/vifib_slapos_rest_api_v1/DocumentTemplateItem/portal_components/VifibRestAPIV1.py
+++ b/master/bt5/vifib_slapos_rest_api_v1/DocumentTemplateItem/portal_components/VifibRestAPIV1.py
@@ -491,6 +491,74 @@ class ComputerPublisher(GenericPublisher):
     self.REQUEST.response.setStatus(204)
     return self.REQUEST.response
 
+class StatusPublisher(GenericPublisher):
+
+  @responseSupport()
+  def __call__(self):
+    """Log GET support"""
+    if self.REQUEST['REQUEST_METHOD'] == 'POST':
+      self.REQUEST.response.setStatus(404)
+      return self.REQUEST.response
+    elif self.REQUEST['REQUEST_METHOD'] == 'GET':
+      if self.REQUEST['traverse_subpath']:
+        self.__status_info()
+      else:
+        self.__status_list()
+
+  def __status_list(self):
+    kw = dict(
+      portal_type=('Computer', 'Software Instance'),
+    )
+    d = {"list": []}
+    a = d['list'].append
+    for si in self.getPortalObject().portal_catalog(**kw):
+      a('/'.join([self.getAPIRoot(), 'status', si.getRelativeUrl()]))
+    try:
+      d['list'][0]
+    except IndexError:
+      # no results, so nothing to return
+      self.REQUEST.response.setStatus(204)
+    else:
+      self.REQUEST.response.setStatus(200)
+      self.REQUEST.response.setHeader('Cache-Control', 
+                                      'max-age=300, private')
+      self.REQUEST.response.setBody(jsonify(d))
+    return self.REQUEST.response
+
+  # XXX Use a decated document to keep the history
+  @extractDocument(['Computer', 'Software Instance'])
+#   @supportModifiedSince('document_url')
+  def __status_info(self):
+    certificate = False
+    document = self.restrictedTraverse(self.document_url)
+    try:
+      memcached_dict = self.getPortalObject().portal_memcached.getMemcachedDict(
+        key_prefix='slap_tool',
+        plugin_path='portal_memcached/default_memcached_plugin')
+      try:
+        d = memcached_dict[document.getReference()]
+      except KeyError:
+        d = {
+          "user": "SlapOS Master",
+          'created_at': '%s' % rfc1123_date(DateTime()),
+          "text": "#error no data found for %s" % document.getReference()
+        }
+      else:
+        d = json.loads(d)
+    except Exception:
+      LOG('VifibRestApiV1', ERROR,
+        'Problem while trying to generate status information:', error=True)
+      self.REQUEST.response.setStatus(500)
+      self.REQUEST.response.setBody(jsonify({'error':
+        'There is system issue, please try again later.'}))
+    else:
+      d['@document'] = self.document_url
+      self.REQUEST.response.setStatus(200)
+      self.REQUEST.response.setHeader('Cache-Control', 
+                                      'max-age=300, private')
+      self.REQUEST.response.setBody(jsonify(d))
+    return self.REQUEST.response
+
 class VifibRestAPIV1(Implicit):
   security = ClassSecurityInfo()
   security.declareObjectProtected(Permissions.AccessContentsInformation)
@@ -512,6 +580,12 @@ class VifibRestAPIV1(Implicit):
     """Computer publisher"""
     return ComputerPublisher().__of__(self)
 
+  security.declarePublic('log')
+  @ComputedAttribute
+  def status(self):
+    """Status publisher"""
+    return StatusPublisher().__of__(self)
+
   @responseSupport(True)
   def OPTIONS(self, *args, **kwargs):
     """HTTP OPTIONS implementation"""
diff --git a/master/bt5/vifib_slapos_rest_api_v1/bt/revision b/master/bt5/vifib_slapos_rest_api_v1/bt/revision
index 19c7bdba7..8e2afd342 100644
--- a/master/bt5/vifib_slapos_rest_api_v1/bt/revision
+++ b/master/bt5/vifib_slapos_rest_api_v1/bt/revision
@@ -1 +1 @@
-16
\ No newline at end of file
+17
\ No newline at end of file
-- 
2.30.9