From 619e89c8313793d8770966181fd6c5eaed1ef349 Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Wed, 25 Jul 2012 16:20:56 +0200 Subject: [PATCH] htools: abstract function for parsing job ids Both the job id and submit job result parsing are abstracted into separate functions, so that later changes are more localised. Also, this makes submitManyJobs itself easier to read. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: Guido Trotter <ultrotter@google.com> --- htools/Ganeti/Luxi.hs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/htools/Ganeti/Luxi.hs b/htools/Ganeti/Luxi.hs index 03f2ef0f6..7206c9b9f 100644 --- a/htools/Ganeti/Luxi.hs +++ b/htools/Ganeti/Luxi.hs @@ -357,6 +357,19 @@ callMethod method s = do let rval = validateResult result return rval +-- | Parses a job ID. +parseJobId :: JSValue -> Result JobId +parseJobId (JSString x) = Ok $ fromJSString x +parseJobId x = Bad $ "Wrong type/value for job id: " ++ show x + +-- | Parse job submission result. +parseSubmitJobResult :: JSValue -> Result JobId +parseSubmitJobResult (JSArray [JSBool True, v]) = parseJobId v +parseSubmitJobResult (JSArray [JSBool False, JSString x]) = + Bad (fromJSString x) +parseSubmitJobResult v = Bad $ "Unknown result from the master daemon" ++ + show v + -- | Specialized submitManyJobs call. submitManyJobs :: Client -> [[OpCode]] -> IO (Result [JobId]) submitManyJobs s jobs = do @@ -364,14 +377,7 @@ submitManyJobs s jobs = do -- map each result (status, payload) pair into a nice Result ADT return $ case rval of Bad x -> Bad x - Ok (JSArray r) -> - mapM (\v -> case v of - JSArray [JSBool True, JSString x] -> - Ok (fromJSString x) - JSArray [JSBool False, JSString x] -> - Bad (fromJSString x) - _ -> Bad "Unknown result from the master daemon" - ) r + Ok (JSArray r) -> mapM parseSubmitJobResult r x -> Bad ("Cannot parse response from Ganeti: " ++ show x) -- | Custom queryJobs call. -- GitLab