AllocElement: extend with the cluster score
[ganeti-local] / Ganeti / HTools / Luxi.hs
index 7eb4bfa..cc80908 100644 (file)
@@ -26,6 +26,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 module Ganeti.HTools.Luxi
     (
       loadData
+    , parseData
     ) where
 
 import qualified Control.Exception as E
@@ -53,7 +54,8 @@ toArray v =
 queryNodesMsg :: L.LuxiOp
 queryNodesMsg =
   L.QueryNodes [] ["name", "mtotal", "mnode", "mfree", "dtotal", "dfree",
-                   "ctotal", "offline", "drained", "vm_capable"] False
+                   "ctotal", "offline", "drained", "vm_capable",
+                   "group.uuid"] False
 
 -- | The input data for instance query.
 queryInstancesMsg :: L.LuxiOp
@@ -83,7 +85,7 @@ getInstances :: NameAssoc
 getInstances ktn arr = toArray arr >>= mapM (parseInstance ktn)
 
 -- | Construct an instance from a JSON object.
-parseInstance :: [(String, Ndx)]
+parseInstance :: NameAssoc
               -> JSValue
               -> Result (String, Instance.Instance)
 parseInstance ktn (JSArray [ name, disk, mem, vcpus
@@ -114,15 +116,16 @@ getNodes arr = toArray arr >>= mapM parseNode
 -- | Construct a node from a JSON object.
 parseNode :: JSValue -> Result (String, Node.Node)
 parseNode (JSArray [ name, mtotal, mnode, mfree, dtotal, dfree
-                   , ctotal, offline, drained, vm_capable ])
+                   , ctotal, offline, drained, vm_capable, g_uuid ])
     = do
   xname <- annotateResult "Parsing new node" (fromJVal name)
   let convert v = annotateResult ("Node '" ++ xname ++ "'") (fromJVal v)
   xoffline <- convert offline
   xdrained <- convert drained
   xvm_capable <- convert vm_capable
+  xguuid   <- convert g_uuid
   node <- (if xoffline || xdrained || not xvm_capable
-           then return $ Node.create xname 0 0 0 0 0 0 True
+           then return $ Node.create xname 0 0 0 0 0 0 True xguuid
            else do
              xmtotal  <- convert mtotal
              xmnode   <- convert mnode
@@ -131,7 +134,7 @@ parseNode (JSArray [ name, mtotal, mnode, mfree, dtotal, dfree
              xdfree   <- convert dfree
              xctotal  <- convert ctotal
              return $ Node.create xname xmtotal xmnode xmfree
-                    xdtotal xdfree xctotal False)
+                    xdtotal xdfree xctotal False xguuid)
   return (xname, node)
 
 parseNode v = fail ("Invalid node query result: " ++ show v)
@@ -145,9 +148,9 @@ getClusterTags v = do
 -- * Main loader functionality
 
 -- | Builds the cluster data from an URL.
-loadData :: String -- ^ Unix socket to use as source
-         -> IO (Result (Node.AssocList, Instance.AssocList, [String]))
-loadData master =
+readData :: String -- ^ Unix socket to use as source
+         -> IO (Result JSValue, Result JSValue, Result JSValue)
+readData master =
   E.bracket
        (L.getClient master)
        L.closeClient
@@ -155,11 +158,20 @@ loadData master =
           nodes <- queryNodes s
           instances <- queryInstances s
           cinfo <- queryClusterInfo s
-          return $ do -- Result monad
-            node_data <- nodes >>= getNodes
-            let (node_names, node_idx) = assignIndices node_data
-            inst_data <- instances >>= getInstances node_names
-            let (_, inst_idx) = assignIndices inst_data
-            ctags <- cinfo >>= getClusterTags
-            return (node_idx, inst_idx, ctags)
+          return (nodes, instances, cinfo)
        )
+
+parseData :: (Result JSValue, Result JSValue, Result JSValue)
+          -> Result (Node.List, Instance.List, [String])
+parseData (nodes, instances, cinfo) = do
+  node_data <- nodes >>= getNodes
+  let (node_names, node_idx) = assignIndices node_data
+  inst_data <- instances >>= getInstances node_names
+  let (_, inst_idx) = assignIndices inst_data
+  ctags <- cinfo >>= getClusterTags
+  return (node_idx, inst_idx, ctags)
+
+-- | Top level function for data loading
+loadData :: String -- ^ Unix socket to use as source
+            -> IO (Result (Node.List, Instance.List, [String]))
+loadData master = readData master >>= return . parseData