Take the foldl out of Loader.fixNodes
[ganeti-local] / Ganeti / HTools / Rapi.hs
index 4f3b89b..fc6a46c 100644 (file)
@@ -2,6 +2,27 @@
 
 -}
 
 
 -}
 
+{-
+
+Copyright (C) 2009 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
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.
+
+-}
+
 module Ganeti.HTools.Rapi
     (
       loadData
 module Ganeti.HTools.Rapi
     (
       loadData
@@ -21,7 +42,7 @@ import Ganeti.HTools.Types
 import qualified Ganeti.HTools.Node as Node
 import qualified Ganeti.HTools.Instance as Instance
 
 import qualified Ganeti.HTools.Node as Node
 import qualified Ganeti.HTools.Instance as Instance
 
--- | Read an URL via curl and return the body if successful
+-- | Read an URL via curl and return the body if successful.
 getUrl :: (Monad m) => String -> IO (m String)
 getUrl url = do
   (code, body) <- curlGetString url [CurlSSLVerifyPeer False,
 getUrl :: (Monad m) => String -> IO (m String)
 getUrl url = do
   (code, body) <- curlGetString url [CurlSSLVerifyPeer False,
@@ -31,59 +52,60 @@ getUrl url = do
             _ -> fail $ printf "Curl error for '%s', error %s"
                  url (show code))
 
             _ -> fail $ printf "Curl error for '%s', error %s"
                  url (show code))
 
--- | Append the default port if not passed in
+-- | Append the default port if not passed in.
 formatHost :: String -> String
 formatHost master =
     if elem ':' master then  master
     else "https://" ++ master ++ ":5080"
 
 formatHost :: String -> String
 formatHost master =
     if elem ':' master then  master
     else "https://" ++ master ++ ":5080"
 
+-- | Parse a instance list in JSON format.
 getInstances :: NameAssoc
              -> String
              -> Result [(String, Instance.Instance)]
 getInstances :: NameAssoc
              -> String
              -> Result [(String, Instance.Instance)]
-getInstances ktn body = do
-  arr <- loadJSArray body
-  ilist <- mapM (parseInstance ktn) arr
-  return ilist
+getInstances ktn body = loadJSArray body >>= mapM (parseInstance ktn)
 
 
+-- | Parse a node list in JSON format.
 getNodes :: String -> Result [(String, Node.Node)]
 getNodes :: String -> Result [(String, Node.Node)]
-getNodes body = do
-  arr <- loadJSArray body
-  nlist <- mapM parseNode arr
-  return nlist
+getNodes body = loadJSArray body >>= mapM parseNode
 
 
-parseInstance :: [(String, Int)]
+-- | Construct an instance from a JSON object.
+parseInstance :: [(String, Ndx)]
               -> JSObject JSValue
               -> Result (String, Instance.Instance)
 parseInstance ktn a = do
   name <- fromObj "name" a
   disk <- fromObj "disk_usage" a
   mem <- fromObj "beparams" a >>= fromObj "memory"
               -> JSObject JSValue
               -> Result (String, Instance.Instance)
 parseInstance ktn a = do
   name <- fromObj "name" a
   disk <- fromObj "disk_usage" a
   mem <- fromObj "beparams" a >>= fromObj "memory"
+  vcpus <- fromObj "beparams" a >>= fromObj "vcpus"
   pnode <- fromObj "pnode" a >>= lookupNode ktn name
   snodes <- fromObj "snodes" a
   snode <- (if null snodes then return Node.noSecondary
             else readEitherString (head snodes) >>= lookupNode ktn name)
   running <- fromObj "status" a
   pnode <- fromObj "pnode" a >>= lookupNode ktn name
   snodes <- fromObj "snodes" a
   snode <- (if null snodes then return Node.noSecondary
             else readEitherString (head snodes) >>= lookupNode ktn name)
   running <- fromObj "status" a
-  let inst = Instance.create name mem disk running pnode snode
+  let inst = Instance.create name mem disk vcpus running pnode snode
   return (name, inst)
 
   return (name, inst)
 
+-- | Construct a node from a JSON object.
 parseNode :: JSObject JSValue -> Result (String, Node.Node)
 parseNode a = do
     name <- fromObj "name" a
     offline <- fromObj "offline" a
 parseNode :: JSObject JSValue -> Result (String, Node.Node)
 parseNode a = do
     name <- fromObj "name" a
     offline <- fromObj "offline" a
-    node <- (case offline of
-               True -> return $ Node.create name 0 0 0 0 0 True
-               _ -> do
-                 drained <- fromObj "drained" a
-                 mtotal <- fromObj "mtotal" a
-                 mnode <- fromObj "mnode" a
-                 mfree <- fromObj "mfree" a
-                 dtotal <- fromObj "dtotal" a
-                 dfree <- fromObj "dfree" a
-                 return $ Node.create name mtotal mnode mfree
-                        dtotal dfree (offline || drained))
+    node <- (if offline
+             then return $ Node.create name 0 0 0 0 0 0 True
+             else do
+               drained <- fromObj "drained" a
+               mtotal  <- fromObj "mtotal"  a
+               mnode   <- fromObj "mnode"   a
+               mfree   <- fromObj "mfree"   a
+               dtotal  <- fromObj "dtotal"  a
+               dfree   <- fromObj "dfree"   a
+               ctotal  <- fromObj "ctotal"  a
+               return $ Node.create name mtotal mnode mfree
+                      dtotal dfree ctotal (offline || drained))
     return (name, node)
 
     return (name, node)
 
-loadData :: String -- ^ Cluster/URL to use as source
+-- | Builds the cluster data from an URL.
+loadData :: String -- ^ Cluster or URL to use as source
          -> IO (Result (Node.AssocList, Instance.AssocList))
 loadData master = do -- IO monad
   let url = formatHost master
          -> IO (Result (Node.AssocList, Instance.AssocList))
 loadData master = do -- IO monad
   let url = formatHost master