Revision 28f19313

b/htools/Ganeti/HTools/IAlloc.hs
49 49
-- 'Allocate' request share some common properties, which are read by
50 50
-- this function.
51 51
parseBaseInstance :: String
52
                  -> [(String, JSValue)]
52
                  -> JSRecord
53 53
                  -> Result (String, Instance.Instance)
54 54
parseBaseInstance n a = do
55 55
  let extract x = tryFromObj ("invalid data for instance '" ++ n ++ "'") a x
......
61 61
  return (n, Instance.create n mem disk vcpus running tags True 0 0)
62 62

  
63 63
-- | Parses an instance as found in the cluster instance listg.
64
parseInstance :: NameAssoc        -- ^ The node name-to-index association list
65
              -> String           -- ^ The name of the instance
66
              -> [(String, JSValue)] -- ^ The JSON object
64
parseInstance :: NameAssoc -- ^ The node name-to-index association list
65
              -> String    -- ^ The name of the instance
66
              -> JSRecord  -- ^ The JSON object
67 67
              -> Result (String, Instance.Instance)
68 68
parseInstance ktn n a = do
69 69
  base <- parseBaseInstance n a
......
78 78
  return (n, Instance.setBoth (snd base) pidx sidx)
79 79

  
80 80
-- | Parses a node as found in the cluster node list.
81
parseNode :: NameAssoc           -- ^ The group association
82
          -> String              -- ^ The node's name
83
          -> [(String, JSValue)] -- ^ The JSON object
81
parseNode :: NameAssoc   -- ^ The group association
82
          -> String      -- ^ The node's name
83
          -> JSRecord    -- ^ The JSON object
84 84
          -> Result (String, Node.Node)
85 85
parseNode ktg n a = do
86 86
  let desc = "invalid data for node '" ++ n ++ "'"
......
105 105
  return (n, node)
106 106

  
107 107
-- | Parses a group as found in the cluster group list.
108
parseGroup :: String              -- ^ The group UUID
109
           -> [(String, JSValue)] -- ^ The JSON object
108
parseGroup :: String     -- ^ The group UUID
109
           -> JSRecord   -- ^ The JSON object
110 110
           -> Result (String, Group.Group)
111 111
parseGroup u a = do
112 112
  let extract x = tryFromObj ("invalid data for group '" ++ u ++ "'") a x
......
114 114
  apol <- extract "alloc_policy"
115 115
  return (u, Group.create name u apol)
116 116

  
117
parseTargetGroups :: [(String, JSValue)] -- ^ The JSON object (request dict)
118
                  -> Group.List          -- ^ The existing groups
117
parseTargetGroups :: JSRecord      -- ^ The JSON object (request dict)
118
                  -> Group.List    -- ^ The existing groups
119 119
                  -> Result [Gdx]
120 120
parseTargetGroups req map_g = do
121 121
  group_uuids <- fromObjWithDefault req "target_groups" []
b/htools/Ganeti/HTools/Rapi.hs
37 37
import Network.Curl.Types ()
38 38
#endif
39 39
import Control.Monad
40
import Text.JSON (JSObject, JSValue, fromJSObject, decodeStrict)
40
import Text.JSON (JSObject, fromJSObject, decodeStrict)
41 41
import Text.JSON.Types (JSValue(..))
42 42
import Text.Printf (printf)
43 43

  
......
104 104

  
105 105
-- | Construct an instance from a JSON object.
106 106
parseInstance :: NameAssoc
107
              -> [(String, JSValue)]
107
              -> JSRecord
108 108
              -> Result (String, Instance.Instance)
109 109
parseInstance ktn a = do
110 110
  name <- tryFromObj "Parsing new instance" a "name"
......
129 129
  return (name, inst)
130 130

  
131 131
-- | Construct a node from a JSON object.
132
parseNode :: NameAssoc -> [(String, JSValue)] -> Result (String, Node.Node)
132
parseNode :: NameAssoc -> JSRecord -> Result (String, Node.Node)
133 133
parseNode ktg a = do
134 134
  name <- tryFromObj "Parsing new node" a "name"
135 135
  let desc = "Node '" ++ name ++ "', error while parsing data"
......
154 154
  return (name, node)
155 155

  
156 156
-- | Construct a group from a JSON object.
157
parseGroup :: [(String, JSValue)] -> Result (String, Group.Group)
157
parseGroup :: JSRecord -> Result (String, Group.Group)
158 158
parseGroup a = do
159 159
  name <- tryFromObj "Parsing new group" a "name"
160 160
  let extract s = tryFromObj ("Group '" ++ name ++ "'") a s
b/htools/Ganeti/HTools/Utils.hs
30 30
    , stdDev
31 31
    , commaJoin
32 32
    , readEitherString
33
    , JSRecord
33 34
    , loadJSArray
34 35
    , fromObj
35 36
    , fromObjWithDefault
......
106 107

  
107 108
-- * JSON-related functions
108 109

  
110
-- | A type alias for the list-based representation of J.JSObject
111
type JSRecord = [(String, J.JSValue)]
112

  
109 113
-- | Converts a JSON Result into a monadic value.
110 114
fromJResult :: Monad m => String -> J.Result a -> m a
111 115
fromJResult s (J.Error x) = fail (s ++ ": " ++ x)
......
129 133
loadJSArray s = fromJResult s . J.decodeStrict
130 134

  
131 135
-- | Reads the value of a key in a JSON object.
132
fromObj :: (J.JSON a, Monad m) => [(String, J.JSValue)] -> String -> m a
136
fromObj :: (J.JSON a, Monad m) => JSRecord -> String -> m a
133 137
fromObj o k =
134 138
    case lookup k o of
135 139
      Nothing -> fail $ printf "key '%s' not found, object contains only %s"
......
138 142

  
139 143
-- | Reads the value of an optional key in a JSON object.
140 144
maybeFromObj :: (J.JSON a, Monad m) =>
141
             [(String, J.JSValue)] -> String -> m (Maybe a)
145
                JSRecord -> String -> m (Maybe a)
142 146
maybeFromObj o k =
143 147
    case lookup k o of
144 148
      Nothing -> return Nothing
......
146 150

  
147 151
-- | Reads the value of a key in a JSON object with a default if missing.
148 152
fromObjWithDefault :: (J.JSON a, Monad m) =>
149
                      [(String, J.JSValue)] -> String -> a -> m a
153
                      JSRecord -> String -> a -> m a
150 154
fromObjWithDefault o k d = liftM (fromMaybe d) $ maybeFromObj o k
151 155

  
152 156
-- | Reads a JValue, that originated from an object key
......
165 169
-- | Try to extract a key from a object with better error reporting
166 170
-- than fromObj
167 171
tryFromObj :: (J.JSON a) =>
168
              String                -- ^ Textual "owner" in error messages
169
           -> [(String, J.JSValue)] -- ^ The object array
170
           -> String                -- ^ The desired key from the object
172
              String     -- ^ Textual "owner" in error messages
173
           -> JSRecord   -- ^ The object array
174
           -> String     -- ^ The desired key from the object
171 175
           -> Result a
172 176
tryFromObj t o = annotateResult t . fromObj o
173 177

  

Also available in: Unified diff