Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / IAlloc.hs @ 9ba5c28f

History | View | Annotate | Download (2.2 kB)

1
{-| Implementation of the iallocator interface.
2

    
3
-}
4

    
5
module Ganeti.HTools.IAlloc
6
    (
7
      parseData
8
    , formatResponse
9
    ) where
10

    
11
import Data.Either ()
12
import Data.Maybe
13
import Control.Monad
14
import Text.JSON
15
import Text.Printf (printf)
16
import Ganeti.HTools.Utils ()
17

    
18

    
19
parseInstance :: JSObject JSValue -> Either String String
20
parseInstance a =
21
    let name = getStringElement "name" a
22
        disk = case getIntElement "disk_usage" a of
23
                 Left _ -> let log_sz = apply2 (+)
24
                                        (getIntElement "sda_size" a)
25
                                        (getIntElement "sdb_size" a)
26
                           in apply2 (+) log_sz (Right $ 128 * 2)
27
                 Right x -> Right x
28
        bep = fromObj "beparams" a
29
        pnode = getStringElement "pnode" a
30
        snode = (listHead $ getListElement "snodes" a) `combine` readString
31
        mem = case bep of
32
                Left _ -> getIntElement "admin_ram" a
33
                Right o -> getIntElement "memory" o
34
        running = getStringElement "status" a
35
    in
36
      concatElems name $
37
                  concatElems (show `apply1` mem) $
38
                  concatElems (show `apply1` disk) $
39
                  concatElems running $
40
                  concatElems pnode snode
41

    
42
parseNode :: JSObject JSValue -> Either String String
43
parseNode a =
44
    let name = getStringElement "name" a
45
        mtotal = getIntElement "mtotal" a
46
        mnode = getIntElement "mnode" a
47
        mfree = getIntElement "mfree" a
48
        dtotal = getIntElement "dtotal" a
49
        dfree = getIntElement "dfree" a
50
    in concatElems name $
51
       concatElems (show `apply1` mtotal) $
52
       concatElems (show `apply1` mnode) $
53
       concatElems (show `apply1` mfree) $
54
       concatElems (show `apply1` dtotal) (show `apply1` dfree)
55

    
56
parseData :: String -> Maybe String
57

    
58
parseData x = Just x
59

    
60
formatResponse :: Bool -> String -> [String] -> String
61
formatResponse success info nodes =
62
    let
63
        e_success = ("success", JSBool success)
64
        e_info = ("info", JSString . toJSString $ info)
65
        e_nodes = ("nodes", JSArray $ map (JSString . toJSString) nodes)
66
    in encodeStrict $ makeObj [e_success, e_info, e_nodes]