From 318853ab71bda7615f3abe5e9a9aaf5445ae44a4 Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 4 Oct 2012 03:44:23 +0100
Subject: [PATCH] Fixup node disk free/total queries

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 <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/JSON.hs       | 6 ++++++
 htools/Ganeti/Query/Node.hs | 4 ++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/htools/Ganeti/JSON.hs b/htools/Ganeti/JSON.hs
index 7c7dd3423..178915a8c 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 62b625b8a..7bb994b75 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 =
-- 
GitLab