diff --git a/htools/Ganeti/JSON.hs b/htools/Ganeti/JSON.hs index 7c7dd3423458dce17d1d799dad4db8e55aa085be..178915a8c9012d9efac30f5f14ff77e613055b23 100644 --- a/htools/Ganeti/JSON.hs +++ b/htools/Ganeti/JSON.hs @@ -32,6 +32,7 @@ module Ganeti.JSON , fromKeyValue , fromJVal , jsonHead + , getMaybeJsonHead , asJSObject , asObjectList , tryFromObj @@ -131,6 +132,11 @@ jsonHead :: (J.JSON b) => [a] -> (a -> b) -> J.JSValue jsonHead [] _ = J.JSNull jsonHead (x:_) f = J.showJSON $ f x +-- | Helper for extracting Maybe values from a possibly empty list. +getMaybeJsonHead :: (J.JSON b) => [a] -> (a -> Maybe b) -> J.JSValue +getMaybeJsonHead [] _ = J.JSNull +getMaybeJsonHead (x:_) f = maybe J.JSNull J.showJSON (f x) + -- | Converts a JSON value into a JSON object. asJSObject :: (Monad m) => J.JSValue -> m (J.JSObject J.JSValue) asJSObject (J.JSObject a) = return a diff --git a/htools/Ganeti/Query/Node.hs b/htools/Ganeti/Query/Node.hs index 62b625b8a4397f81bb8107dc7859af4c55b45834..7bb994b75b235663df0a8385cc1c1536304e65e0 100644 --- a/htools/Ganeti/Query/Node.hs +++ b/htools/Ganeti/Query/Node.hs @@ -80,9 +80,9 @@ nodeLiveFieldExtract "csockets" res = nodeLiveFieldExtract "ctotal" res = jsonHead (rpcResNodeInfoHvInfo res) hvInfoCpuTotal nodeLiveFieldExtract "dfree" res = - jsonHead (rpcResNodeInfoVgInfo res) vgInfoVgFree + getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgFree nodeLiveFieldExtract "dtotal" res = - jsonHead (rpcResNodeInfoVgInfo res) vgInfoVgSize + getMaybeJsonHead (rpcResNodeInfoVgInfo res) vgInfoVgSize nodeLiveFieldExtract "mfree" res = jsonHead (rpcResNodeInfoHvInfo res) hvInfoMemoryFree nodeLiveFieldExtract "mnode" res =