Text.hs: change to use sepSplit
[ganeti-local] / Ganeti / HTools / Loader.hs
index e1f35a0..b7c74a0 100644 (file)
@@ -32,20 +32,20 @@ module Ganeti.HTools.Loader
     , assignIndices
     , lookupNode
     , lookupInstance
+    , lookupGroup
     , commonSuffix
     , RqType(..)
     , Request(..)
     ) where
 
-import Data.Function (on)
 import Data.List
-import Data.Maybe (fromJust)
 import qualified Data.Map as M
 import Text.Printf (printf)
 
 import qualified Ganeti.HTools.Container as Container
 import qualified Ganeti.HTools.Instance as Instance
 import qualified Ganeti.HTools.Node as Node
+import qualified Ganeti.HTools.Group as Group
 
 import Ganeti.HTools.Types
 
@@ -71,7 +71,7 @@ data RqType
     deriving (Show)
 
 -- | A complete request, as received from Ganeti.
-data Request = Request RqType Node.List Instance.List [String]
+data Request = Request RqType Group.List Node.List Instance.List [String]
     deriving (Show)
 
 -- * Functions
@@ -90,38 +90,39 @@ lookupInstance kti inst =
       Nothing -> fail $ "Unknown instance '" ++ inst ++ "'"
       Just idx -> return idx
 
+-- | Lookups a group into an assoc list.
+lookupGroup :: (Monad m) => NameAssoc -> String -> String -> m Gdx
+lookupGroup ktg nname gname =
+    case M.lookup gname ktg of
+      Nothing -> fail $ "Unknown group '" ++ gname ++ "' for node " ++ nname
+      Just idx -> return idx
+
 -- | Given a list of elements (and their names), assign indices to them.
 assignIndices :: (Element a) =>
                  [(String, a)]
-              -> (NameAssoc, [(Int, a)])
+              -> (NameAssoc, Container.Container a)
 assignIndices nodes =
   let (na, idx_node) =
           unzip . map (\ (idx, (k, v)) -> ((k, idx), (idx, setIdx v idx)))
           . zip [0..] $ nodes
-  in (M.fromList na, idx_node)
-
--- | Assoc element comparator
-assocEqual :: (Eq a) => (a, b) -> (a, b) -> Bool
-assocEqual = (==) `on` fst
+  in (M.fromList na, Container.fromAssocList idx_node)
 
 -- | For each instance, add its index to its primary and secondary nodes.
-fixNodes :: [(Ndx, Node.Node)]
+fixNodes :: Node.List
          -> Instance.Instance
-         -> [(Ndx, Node.Node)]
+         -> Node.List
 fixNodes accu inst =
     let
         pdx = Instance.pNode inst
         sdx = Instance.sNode inst
-        pold = fromJust $ lookup pdx accu
+        pold = Container.find pdx accu
         pnew = Node.setPri pold inst
-        ac1 = deleteBy assocEqual (pdx, pold) accu
-        ac2 = (pdx, pnew):ac1
+        ac2 = Container.add pdx pnew accu
     in
       if sdx /= Node.noSecondary
-      then let sold = fromJust $ lookup sdx accu
+      then let sold = Container.find sdx accu
                snew = Node.setSec sold inst
-               ac3 = deleteBy assocEqual (sdx, sold) ac2
-           in (sdx, snew):ac3
+           in Container.add sdx snew ac2
       else ac2
 
 -- | Remove non-selected tags from the exclusion list
@@ -168,11 +169,11 @@ commonSuffix nl il =
 mergeData :: [(String, DynUtil)]  -- ^ Instance utilisation data
           -> [String]             -- ^ Exclusion tags
           -> [String]             -- ^ Untouchable instances
-          -> (Node.AssocList, Instance.AssocList, [String])
+          -> (Group.List, Node.List, Instance.List, [String])
           -- ^ Data from backends
-          -> Result (Node.List, Instance.List, [String])
-mergeData um extags exinsts (nl, il, tags) =
-  let il2 = Container.fromAssocList il
+          -> Result (Group.List, Node.List, Instance.List, [String])
+mergeData um extags exinsts (gl, nl, il2, tags) =
+  let il = Container.elems il2
       il3 = foldl' (\im (name, n_util) ->
                         case Container.findByName im name of
                           Nothing -> im -- skipping unknown instance
@@ -184,10 +185,9 @@ mergeData um extags exinsts (nl, il, tags) =
       il4 = Container.map (filterExTags allextags .
                            updateMovable exinsts) il3
       nl2 = foldl' fixNodes nl (Container.elems il4)
-      nl3 = Container.fromAssocList
-            (map (\ (k, v) -> (k, Node.buildPeers v il4)) nl2)
-      node_names = map (Node.name . snd) nl
-      inst_names = map (Instance.name . snd) il
+      nl3 = Container.map (\node -> Node.buildPeers node il4) nl2
+      node_names = map Node.name (Container.elems nl)
+      inst_names = map Instance.name il
       common_suffix = longestDomain (node_names ++ inst_names)
       snl = Container.map (computeAlias common_suffix) nl3
       sil = Container.map (computeAlias common_suffix) il4
@@ -195,7 +195,7 @@ mergeData um extags exinsts (nl, il, tags) =
   in if not $ all (`elem` all_inst_names) exinsts
      then Bad $ "Some of the excluded instances are unknown: " ++
           show (exinsts \\ all_inst_names)
-     else Ok (snl, sil, tags)
+     else Ok (gl, snl, sil, tags)
 
 -- | Checks the cluster data for consistency.
 checkData :: Node.List -> Instance.List