Revision 9a2ff880 Ganeti/Luxi.hs

b/Ganeti/Luxi.hs
29 29
    , getClient
30 30
    , closeClient
31 31
    , callMethod
32
    , submitManyJobs
33
    , queryJobsStatus
32 34
    ) where
33 35

  
34 36
import Data.List
......
43 45
import Ganeti.HTools.Utils
44 46
import Ganeti.HTools.Types
45 47

  
48
import Ganeti.Jobs (JobStatus)
49

  
46 50
-- * Utility functions
47 51

  
48 52
-- | Wrapper over System.Timeout.timeout that fails in the IO monad.
......
161 165
  result <- recvMsg s
162 166
  let rval = validateResult result
163 167
  return rval
168

  
169
-- | Specialized submitManyJobs call.
170
submitManyJobs :: Client -> JSValue -> IO (Result [String])
171
submitManyJobs s jobs = do
172
  rval <- callMethod SubmitManyJobs jobs s
173
  -- map each result (status, payload) pair into a nice Result ADT
174
  return $ case rval of
175
             Bad x -> Bad x
176
             Ok (JSArray r) ->
177
                 mapM (\v -> case v of
178
                               JSArray [JSBool True, JSString x] ->
179
                                   Ok (fromJSString x)
180
                               JSArray [JSBool False, JSString x] ->
181
                                   Bad (fromJSString x)
182
                               _ -> Bad "Unknown result from the master daemon"
183
                      ) r
184
             x -> Bad ("Cannot parse response from Ganeti: " ++ show x)
185

  
186
-- | Custom queryJobs call.
187
queryJobsStatus :: Client -> [String] -> IO (Result [JobStatus])
188
queryJobsStatus s jids = do
189
  rval <- callMethod QueryJobs (J.showJSON (jids, ["status"])) s
190
  return $ case rval of
191
             Bad x -> Bad x
192
             Ok y -> case J.readJSON y::(J.Result [[JobStatus]]) of
193
                       J.Ok vals -> if any null vals
194
                                    then Bad "Missing job status field"
195
                                    else Ok (map head vals)
196
                       J.Error x -> Bad x

Also available in: Unified diff