The disk free/total values are optional ones, wrapped in a Maybe, so
we shouldn't directly serialise them. In order to simplify the
embedded extraction, we add a small helper function.

Signed-off-by: Iustin Pop <[email protected]>
---
 htools/Ganeti/Query/Node.hs |    9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/htools/Ganeti/Query/Node.hs b/htools/Ganeti/Query/Node.hs
index 0630754..01d0a89 100644
--- a/htools/Ganeti/Query/Node.hs
+++ b/htools/Ganeti/Query/Node.hs
@@ -68,6 +68,11 @@ nodeLiveFieldsDefs =
      "Total amount of memory of physical machine")
   ]
 
+-- | 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)
+
 -- | Map each name to a function that extracts that value from
 -- the RPC result.
 nodeLiveFieldExtract :: String -> RpcResultNodeInfo -> J.JSValue
@@ -80,9 +85,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 =
-- 
1.7.10.4

Reply via email to