Commits

Joe Heck  committed 1e93082

adding host API handler (GET)

  • Participants
  • Parent commits 2f87707

Comments (0)

Files changed (2)

File eyeswebapp/api/handlers.py

 # >>> rc.CODES.keys()
 # ['CREATED', 'DELETED', 'THROTTLED', 'FORBIDDEN', 'NOT_IMPLEMENTED', 'BAD_REQUEST', 'DUPLICATE_ENTRY', 'NOT_HERE', 'NOT_FOUND', 'ALL_OK']
 
-#from eyeswebapp.core.models import Host
+from eyeswebapp.core.models import Host
 from eyeswebapp.core.models import Monitor
 from util.monitor import validate_return_dictionary
 import re
 #from eyeswebapp.core.models import Datastore
 
+class HostHandler(BaseHandler):
+    """ Handler entrypoint for hosts """
+    allowed_methods = ('GET', )
+    fields = ('id', 'hostname', 'tags_string')
+    exclude = (re.compile(r'^_'), ) # exclude private/internal variables in object models
+    model = Host
 
+    def read(self, request, host_id=None):
+        """ reads all hosts, or a specific host if ID provided"""
+        if host_id is None:
+            base_api = "/api/host/%d/"
+            list_of_uri = []
+            for host in Host.objects.all():
+                list_of_uri.append(base_api % host.id)
+            return list_of_uri
+        try:
+            host = Host.objects.get(pk=host_id)
+            return host
+        except ObjectDoesNotExist:
+            return rc.NOT_FOUND
+    
 class MonitorHandler(BaseHandler):
     """
     Handler entrypoint for monitors - currently without Authentication
     """
     allowed_methods = ('GET', )
-    fields = ('id', 'lastupdate', 'plugin_name', 'name', 'json_argset', 'nextupdate', 'timeout')
+    fields = ('id', 'lastupdate', 'plugin_name', 'name', 'json_argset', 'nextupdate', 'timeout', 'host', )
     # fields = ('title', ...)
-    # exclude = ('id', re.compile(r'^private_'))
+    exclude = (re.compile(r'^_'), ) # exclude private/internal variables in object models
     model = Monitor
 
     #@throttle(5, 10*60) # allow 5 times in 10 minutes

File eyeswebapp/api/urls.py

 from django.conf.urls.defaults import *
 from piston.resource import Resource
 from piston.doc import documentation_view
-from eyeswebapp.api.handlers import MonitorHandler, MonitorResultHandler
+from eyeswebapp.api.handlers import MonitorHandler, MonitorResultHandler, HostHandler
 
 monitor_handler = Resource(MonitorHandler)
 monitor_result_handler = Resource(MonitorResultHandler)
+host_handler = Resource(HostHandler)
 #datastore_handler = Resource(DatastoreHandler)
 
 urlpatterns = patterns('',
     url(r'^monitor/(?P<monitor_id>\d+)/$', monitor_handler, {'emitter_format': 'json'}),
     url(r'^monitor/$', monitor_handler, {'emitter_format': 'json'}),
+    url(r'^host/(?P<host_id>\d+)/$', host_handler, {'emitter_format': 'json'}),
+    url(r'^host/$', host_handler, {'emitter_format': 'json'}),
 
     # for processing the monitor result JSON structure and storing into data stores
     url(r'^monitor/(?P<monitor_id>\d+)/store/$', monitor_result_handler, {'emitter_format': 'json'}),