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 <[email protected]>
---
 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 0149578..b9029bc 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 01d0a89..163e517 100644
--- a/htools/Ganeti/Query/Node.hs
+++ b/htools/Ganeti/Query/Node.hs
@@ -122,6 +122,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 =
@@ -179,11 +188,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
-- 
1.7.10.4

Reply via email to