From 5227de569fa4b0dc54c0a9823ba81ad162e476ff Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Thu, 4 Oct 2012 00:51:00 +0100
Subject: [PATCH] Fix the node powered field

When initially implementing the node query, I thought the 'powered'
field is a representation of the run-time powered status, which would
make its query complex.

In reality, it's a simple config query, which we can support
easily. We also add a small helper, so that we don't hardcode the
RSUnavail case in many places.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/Query/Common.hs |  5 +++++
 htools/Ganeti/Query/Node.hs   | 13 ++++++++++---
 2 files changed, 15 insertions(+), 3 deletions(-)

diff --git a/htools/Ganeti/Query/Common.hs b/htools/Ganeti/Query/Common.hs
index 014957888..b9029bc28 100644
--- a/htools/Ganeti/Query/Common.hs
+++ b/htools/Ganeti/Query/Common.hs
@@ -25,6 +25,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
 module Ganeti.Query.Common
   ( rsNoData
+  , rsUnavail
   , rsNormal
   , rsMaybe
   , rsUnknown
@@ -66,6 +67,10 @@ vTypeToQFT VTypeInt         = QFTNumber
 rsNoData :: ResultEntry
 rsNoData = ResultEntry RSNoData Nothing
 
+-- | Helper for result for an entity which supports no such field.
+rsUnavail :: ResultEntry
+rsUnavail = ResultEntry RSUnavail Nothing
+
 -- | Helper to declare a normal result.
 rsNormal :: (JSON a) => a -> ResultEntry
 rsNormal a = ResultEntry RSNormal $ Just (showJSON a)
diff --git a/htools/Ganeti/Query/Node.hs b/htools/Ganeti/Query/Node.hs
index 7bb994b75..1fbc80eac 100644
--- a/htools/Ganeti/Query/Node.hs
+++ b/htools/Ganeti/Query/Node.hs
@@ -117,6 +117,15 @@ nodeRoleDoc =
           "\"" ++ nodeRoleToRaw role ++ "\" for " ++ roleDescription role)
    (reverse [minBound..maxBound]))
 
+-- | Get node powered status.
+getNodePower :: ConfigData -> Node -> ResultEntry
+getNodePower cfg node =
+  case getNodeNdParams cfg node of
+    Nothing -> rsNoData
+    Just ndp -> if null (ndpOobProgram ndp)
+                  then rsUnavail
+                  else rsNormal (nodePowered node)
+
 -- | List of all node fields.
 nodeFields :: FieldList Node NodeRuntime
 nodeFields =
@@ -174,11 +183,9 @@ nodeFields =
                           getNodeInstances cfg . nodeName))
   , (FieldDefinition "role" "Role" QFTText nodeRoleDoc,
      FieldConfig ((rsNormal .) . getNodeRole))
-  -- FIXME: the powered state is special (has an different context,
-  -- not runtime) in Python
   , (FieldDefinition "powered" "Powered" QFTBool
        "Whether node is thought to be powered on",
-     missingRuntime)
+     FieldConfig getNodePower)
   -- FIXME: the two fields below are incomplete in Python, part of the
   -- non-implemented node resource model; they are declared just for
   -- parity, but are not functional
-- 
GitLab