Rework the types used during data loading
[ganeti-local] / Ganeti / HTools / Rapi.hs
index 261b290..58df9bd 100644 (file)
@@ -4,7 +4,7 @@
 
 {-
 
-Copyright (C) 2009 Google Inc.
+Copyright (C) 2009, 2010 Google Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -26,14 +26,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 module Ganeti.HTools.Rapi
     (
       loadData
+    , parseData
     ) where
 
 import Network.Curl
 import Network.Curl.Types ()
-import Network.Curl.Code
-import Data.List
 import Control.Monad
-import Text.JSON (JSObject, JSValue, fromJSObject)
+import Text.JSON (JSObject, JSValue, fromJSObject, decodeStrict)
+import Text.JSON.Types (JSValue(..))
 import Text.Printf (printf)
 
 import Ganeti.HTools.Utils
@@ -58,7 +58,7 @@ getUrl url = do
 -- | Append the default port if not passed in.
 formatHost :: String -> String
 formatHost master =
-    if elem ':' master then  master
+    if ':' `elem` master then  master
     else "https://" ++ master ++ ":5080"
 
 -- | Parse a instance list in JSON format.
@@ -66,22 +66,28 @@ getInstances :: NameAssoc
              -> String
              -> Result [(String, Instance.Instance)]
 getInstances ktn body =
-    loadJSArray body >>= mapM (parseInstance ktn . fromJSObject)
+    loadJSArray "Parsing instance data" body >>=
+    mapM (parseInstance ktn . fromJSObject)
 
 -- | Parse a node list in JSON format.
 getNodes :: String -> Result [(String, Node.Node)]
-getNodes body = loadJSArray body >>= mapM (parseNode . fromJSObject)
+getNodes body = loadJSArray "Parsing node data" body >>=
+                mapM (parseNode . fromJSObject)
 
 -- | Construct an instance from a JSON object.
-parseInstance :: [(String, Ndx)]
+parseInstance :: NameAssoc
               -> [(String, JSValue)]
               -> Result (String, Instance.Instance)
 parseInstance ktn a = do
   name <- tryFromObj "Parsing new instance" a "name"
-  let extract s x = tryFromObj ("Instance '" ++ name ++ "'") x s
+  let owner_name = "Instance '" ++ name ++ "'"
+  let extract s x = tryFromObj owner_name x s
   disk <- extract "disk_usage" a
   beparams <- liftM fromJSObject (extract "beparams" a)
-  mem <- extract "memory" beparams
+  omem <- extract "oper_ram" a
+  mem <- (case omem of
+            JSRational _ _ -> annotateResult owner_name (fromJVal omem)
+            _ -> extract "memory" beparams)
   vcpus <- extract "vcpus" beparams
   pnode <- extract "pnode" a >>= lookupNode ktn name
   snodes <- extract "snodes" a
@@ -98,10 +104,11 @@ parseNode a = do
   name <- tryFromObj "Parsing new node" a "name"
   let extract s = tryFromObj ("Node '" ++ name ++ "'") a s
   offline <- extract "offline"
-  node <- (if offline
-           then return $ Node.create name 0 0 0 0 0 0 True
+  drained <- extract "drained"
+  guuid   <- extract "group.uuid"
+  node <- (if offline || drained
+           then return $ Node.create name 0 0 0 0 0 0 True guuid
            else do
-             drained <- extract "drained"
              mtotal  <- extract "mtotal"
              mnode   <- extract "mnode"
              mfree   <- extract "mfree"
@@ -109,19 +116,31 @@ parseNode a = do
              dfree   <- extract "dfree"
              ctotal  <- extract "ctotal"
              return $ Node.create name mtotal mnode mfree
-                    dtotal dfree ctotal (offline || drained))
+                    dtotal dfree ctotal False guuid)
   return (name, node)
 
--- | Builds the cluster data from an URL.
-loadData :: String -- ^ Cluster or URL to use as source
-         -> IO (Result (Node.AssocList, Instance.AssocList, [String]))
-loadData master = do -- IO monad
+-- | Loads the raw cluster data from an URL.
+readData :: String -- ^ Cluster or URL to use as source
+         -> IO (Result String, Result String, Result String)
+readData master = do
   let url = formatHost master
   node_body <- getUrl $ printf "%s/2/nodes?bulk=1" url
   inst_body <- getUrl $ printf "%s/2/instances?bulk=1" url
-  return $ do -- Result monad
-    node_data <- node_body >>= getNodes
-    let (node_names, node_idx) = assignIndices node_data
-    inst_data <- inst_body >>= getInstances node_names
-    let (_, inst_idx) = assignIndices inst_data
-    return (node_idx, inst_idx, [])
+  tags_body <- getUrl $ printf "%s/2/tags" url
+  return (node_body, inst_body, tags_body)
+
+-- | Builds the cluster data from the raw Rapi content
+parseData :: (Result String, Result String, Result String)
+          -> Result (Node.List, Instance.List, [String])
+parseData (node_body, inst_body, tags_body) = do
+  node_data <- node_body >>= getNodes
+  let (node_names, node_idx) = assignIndices node_data
+  inst_data <- inst_body >>= getInstances node_names
+  let (_, inst_idx) = assignIndices inst_data
+  tags_data <- tags_body >>= (fromJResult "Parsing tags data" . decodeStrict)
+  return (node_idx, inst_idx, tags_data)
+
+-- | Top level function for data loading
+loadData :: String -- ^ Cluster or URL to use as source
+            -> IO (Result (Node.List, Instance.List, [String]))
+loadData master = readData master >>= return . parseData