Commit 89a072b6 authored by Klaus Aehlig's avatar Klaus Aehlig

Abstract out a method to plainly return JSON

A common operation, also for our HTTP-speaking daemons, is to
return a piece of JSON. So factor out a method for this purpose,
so that it can be reused later.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarPetr Pudlak <pudlak@google.com>
parent a93d53ac
......@@ -46,7 +46,7 @@ import Control.DeepSeq (force)
import Control.Exception.Base (evaluate)
import Control.Monad
import Control.Monad.IO.Class
import Data.ByteString.Char8 (pack, unpack)
import Data.ByteString.Char8 (unpack)
import Data.Maybe (fromMaybe)
import Data.List (find)
import Data.Monoid (mempty)
......@@ -70,7 +70,7 @@ import qualified Ganeti.Constants as C
import qualified Ganeti.ConstantUtils as CU
import Ganeti.Runtime
import Ganeti.Utils (getCurrentTimeUSec)
import Ganeti.Utils.Http (httpConfFromOpts, error404)
import Ganeti.Utils.Http (httpConfFromOpts, error404, plainJSON)
-- * Types and constants definitions
......@@ -100,12 +100,12 @@ prepMain opts _ = httpConfFromOpts GanetiMond opts
-- | Reply to the supported API version numbers query.
versionQ :: Snap ()
versionQ = writeBS . pack $ J.encode [latestAPIVersion]
versionQ = plainJSON [latestAPIVersion]
-- | Version 1 of the monitoring HTTP API.
version1Api :: MVar CollectorMap -> MVar ConfigAccess -> Snap ()
version1Api mvar mvarConfig =
let returnNull = writeBS . pack $ J.encode J.JSNull :: Snap ()
let returnNull = plainJSON J.JSNull
in ifTop returnNull <|>
route
[ ("list", listHandler mvarConfig)
......@@ -148,7 +148,7 @@ dcListItem dc =
listHandler :: MVar ConfigAccess -> Snap ()
listHandler mvarConfig = dir "collectors" $ do
collectors' <- liftIO $ activeCollectors mvarConfig
writeBS . pack . J.encode $ map dcListItem collectors'
plainJSON $ map dcListItem collectors'
-- | Handler for returning data collector reports.
reportHandler :: MVar CollectorMap -> MVar ConfigAccess -> Snap ()
......@@ -164,7 +164,7 @@ allReports :: MVar CollectorMap -> MVar ConfigAccess -> Snap ()
allReports mvar mvarConfig = do
collectors' <- liftIO $ activeCollectors mvarConfig
reports <- mapM (liftIO . getReport mvar) collectors'
writeBS . pack . J.encode $ reports
plainJSON reports
-- | Takes the CollectorMap and a DataCollector and returns the report for this
-- collector.
......@@ -215,7 +215,7 @@ oneReport mvar mvarConfig = do
Just col -> return col
Nothing -> fail "Unable to find the requested collector"
dcr <- liftIO $ getReport mvar collector
writeBS . pack . J.encode $ dcr
plainJSON dcr
-- | The function implementing the HTTP API of the monitoring agent.
monitoringApi :: MVar CollectorMap -> MVar ConfigAccess -> Snap ()
......
......@@ -37,6 +37,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
module Ganeti.Utils.Http
( httpConfFromOpts
, error404
, plainJSON
) where
import Control.Monad (liftM)
......@@ -47,6 +48,7 @@ import Snap.Core (Snap, writeBS, modifyResponse, setResponseStatus)
import Snap.Http.Server.Config ( Config, ConfigLog(ConfigFileLog), emptyConfig
, setAccessLog, setErrorLog, setCompression
, setVerbose, setPort, setBind )
import qualified Text.JSON as J
import qualified Ganeti.Constants as C
import Ganeti.Daemon (DaemonOptions(..))
......@@ -90,3 +92,6 @@ error404 = do
modifyResponse $ setResponseStatus 404 "Not found"
writeBS "Resource not found"
-- | Return the JSON encoding of an object
plainJSON :: J.JSON a => a -> Snap ()
plainJSON = writeBS . pack . J.encode
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment