5 module Ganeti.HTools.Types
8 import qualified Ganeti.HTools.Container as Container
9 import qualified Ganeti.HTools.Instance as Instance
10 import qualified Ganeti.HTools.Node as Node
12 type NodeList = Container.Container Node.Node
13 type InstanceList = Container.Container Instance.Instance
15 -- | The type used to hold idx-to-name mappings
16 type NameList = [(Int, String)]
18 -- | The type used to hold name-to-idx mappings
19 type NameAssoc = [(String, Int)]
21 type IdxNode = [(Int, Node.Node)]
22 type IdxInstance = [(Int, Instance.Instance)]
26 This is similar to the JSON library Result type - *very* similar, but
27 we want to use it in multiple places, so we abstract it into a
37 instance Monad Result where
38 (>>=) (Bad x) _ = Bad x
39 (>>=) (Ok x) fn = fn x
43 -- | A generic class for nodes and instances
47 setName :: a -> String -> a
48 setIdx :: a -> Int -> a
50 -- Let's make nodes elements of the cluster
51 instance Element Node.Node where
54 setName = Node.setName
58 instance Element Instance.Instance where
61 setName = Instance.setName
62 setIdx = Instance.setIdx
64 -- | Compute the name of an element in a container
65 cNameOf :: (Element a) => Container.Container a -> Container.Key -> String
66 cNameOf c k = name $ Container.find k c
68 -- | Compute the maximum name length in an Element Container
69 cMaxNamelen :: (Element a) => Container.Container a -> Int
70 cMaxNamelen = maximum . map (length . name) . Container.elems
72 -- | Find an element by name in a Container; this is a very slow function
73 findByName :: (Element a, Monad m) =>
74 Container.Container a -> String -> m Container.Key
76 let all_elems = Container.elems c
77 result = filter ((== n) . name) all_elems
81 fail $ "Wrong number of elems (" ++ (show nems) ++
82 ") found with name " ++ n
84 return $ idx $ head result