Types.hs 1.73 KB
Newer Older
Iustin Pop's avatar
Iustin Pop committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
{-| Some common types.

-}

module Ganeti.HTools.Types
    where

import qualified Ganeti.HTools.Container as Container
import qualified Ganeti.HTools.Instance as Instance
import qualified Ganeti.HTools.Node as Node

type NodeList = Container.Container Node.Node
type InstanceList = Container.Container Instance.Instance

-- | The type used to hold idx-to-name mappings
type NameList = [(Int, String)]

-- | The type used to hold name-to-idx mappings
type NameAssoc = [(String, Int)]

type IdxNode = [(Int, Node.Node)]
type IdxInstance = [(Int, Instance.Instance)]

{-

This is similar to the JSON library Result type - *very* similar, but
we want to use it in multiple places, so we abstract it into a
mini-library here

-}

data Result a
    = Bad String
    | Ok a
    deriving (Show)

instance Monad Result where
    (>>=) (Bad x) _ = Bad x
    (>>=) (Ok x) fn = fn x
    return = Ok
    fail = Bad
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

-- | A generic class for nodes and instances
class Element a where
    name    :: a -> String
    idx     :: a -> Int
    setName :: a -> String -> a
    setIdx  :: a -> Int -> a

-- Let's make nodes elements of the cluster
instance Element Node.Node where
    name = Node.name
    idx = Node.idx
    setName = Node.setName
    setIdx = Node.setIdx

-- And instances too
instance Element Instance.Instance where
    name = Instance.name
    idx = Instance.idx
    setName = Instance.setName
    setIdx = Instance.setIdx
63
64
65
66
67
68
69
70

-- | Compute the name of an element in a container
cNameOf :: (Element a) => Container.Container a -> Container.Key -> String
cNameOf c k = name $ Container.find k c

-- | Compute the maximum name length in an Element Container
cMaxNamelen :: (Element a) => Container.Container a -> Int
cMaxNamelen = maximum . map (length . name) . Container.elems