Add monitoring HTTP API structure
[ganeti-local] / src / Ganeti / Monitoring / Server.hs
index 3b141e1..fc9174e 100644 (file)
@@ -31,9 +31,11 @@ module Ganeti.Monitoring.Server
   , prepMain
   ) where
 
+import Control.Applicative
+import Control.Monad
 import Snap.Core
 import Snap.Http.Server
-import Data.Text
+import Data.ByteString.Char8
 import qualified Text.JSON as J
 
 import Ganeti.Daemon
@@ -79,16 +81,53 @@ prepMain opts _ =
 
 -- | Reply to the supported API version numbers query.
 versionQ :: Snap ()
-versionQ = writeText . pack $ J.encode [latestAPIVersion]
+versionQ = writeBS . pack $ J.encode [latestAPIVersion]
+
+-- | Version 1 of the monitoring HTTP API.
+version1Api :: Snap ()
+version1Api =
+  let returnNull = writeBS . pack $ J.encode J.JSNull :: Snap ()
+  in ifTop returnNull <|>
+     route
+       [ ("list", listHandler)
+       , ("report", reportHandler)
+       ]
+
+-- | Handler for returning lists.
+listHandler :: Snap ()
+listHandler =
+  dir "collectors" $ writeText "TODO: return the list of collectors"
+
+-- | Handler for returning data collector reports.
+reportHandler :: Snap ()
+reportHandler =
+  route
+    [ ("all", allReports)
+    , (":category/:collector", oneReport)
+    ]
+
+-- | Return the report of all the available collectors
+allReports :: Snap ()
+allReports = writeText "TODO: return the reports of all the collectors"
+
+-- | Return the report of one collector
+oneReport :: Snap ()
+oneReport = do
+  category <- fmap (maybe mzero unpack) $ getParam "category"
+  collector <- fmap (maybe mzero unpack) $ getParam "collector"
+  writeBS . pack $
+    "TODO: return the report for collector " ++ category
+      ++ "/" ++ collector
 
 -- | The function implementing the HTTP API of the monitoring agent.
 -- TODO: Currently it only replies to the API version query: implement all the
 -- missing features.
 monitoringApi :: Snap ()
 monitoringApi =
-  ifTop versionQ
+  ifTop versionQ <|>
+  dir "1" version1Api
 
 -- | Main function.
 main :: MainFn CheckResult PrepResult
 main _ _ httpConf =
-  httpServe httpConf monitoringApi
+  httpServe httpConf $ method GET monitoringApi