Add a separate function for looking up instances
[ganeti-local] / Ganeti / HTools / IAlloc.hs
index 07b114d..bafde49 100644 (file)
@@ -17,6 +17,7 @@ import Text.JSON (JSObject, JSValue(JSBool, JSString, JSArray),
                   makeObj, encodeStrict, decodeStrict,
                   fromJSObject, toJSString)
 --import Text.Printf (printf)
+import qualified Ganeti.HTools.Container as Container
 import qualified Ganeti.HTools.Node as Node
 import qualified Ganeti.HTools.Instance as Instance
 import Ganeti.HTools.Loader
@@ -25,10 +26,10 @@ import Ganeti.HTools.Types
 
 data RqType
     = Allocate Instance.Instance Int
-    | Relocate Int Int [Int]
+    | Relocate Idx Int [Ndx]
     deriving (Show)
 
-data Request = Request RqType NodeList InstanceList String
+data Request = Request RqType Node.List Instance.List String
     deriving (Show)
 
 parseBaseInstance :: String
@@ -64,15 +65,19 @@ parseInstance ktn n a = do
 parseNode :: String -> JSObject JSValue -> Result (String, Node.Node)
 parseNode n a = do
     let name = n
-    mtotal <- fromObj "total_memory" a
-    mnode <- fromObj "reserved_memory" a
-    mfree <- fromObj "free_memory" a
-    dtotal <- fromObj "total_disk" a
-    dfree <- fromObj "free_disk" a
     offline <- fromObj "offline" a
     drained <- fromObj "drained" a
-    return $ (name, Node.create n mtotal mnode mfree dtotal dfree
-                      (offline || drained))
+    node <- (case offline of
+               True -> return $ Node.create name 0 0 0 0 0 True
+               _ -> do
+                 mtotal <- fromObj "total_memory" a
+                 mnode <- fromObj "reserved_memory" a
+                 mfree <- fromObj "free_memory" a
+                 dtotal <- fromObj "total_disk" a
+                 dfree <- fromObj "free_disk" a
+                 return $ Node.create n mtotal mnode mfree
+                        dtotal dfree (offline || drained))
+    return (name, node)
 
 parseData :: String -> Result Request
 parseData body = do
@@ -103,10 +108,10 @@ parseData body = do
               return $ Allocate io req_nodes
         "relocate" ->
             do
-              ridx <- lookupNode kti rname rname
+              ridx <- lookupInstance kti rname
               ex_nodes <- fromObj "relocate_from" request
               let ex_nodes' = map (stripSuffix $ length csf) ex_nodes
-              ex_idex <- mapM (findByName map_n) ex_nodes'
+              ex_idex <- mapM (Container.findByName map_n) ex_nodes'
               return $ Relocate ridx req_nodes ex_idex
         other -> fail $ ("Invalid request type '" ++ other ++ "'")
   return $ Request rqtype map_n map_i csf