From b8b9a53c2b490b12ce9750dbeb483d27c349723f Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Thu, 12 Feb 2009 21:14:17 +0100 Subject: [PATCH] Make RAPI return the same data format as gnt-* This patch changes the RAPI endpoint return the same data format as the input files. This will allow using it instead of the files. --- src/Rapi.hs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Rapi.hs b/src/Rapi.hs index 9e715902d..20c30910e 100644 --- a/src/Rapi.hs +++ b/src/Rapi.hs @@ -6,14 +6,14 @@ module Rapi where import Network.Curl -import Network.Curl.Types +import Network.Curl.Types () import Network.Curl.Code -import Data.Either (either) -import Data.Maybe +import Data.Either () +import Data.Maybe () import Control.Monad import Text.JSON import Text.Printf (printf) -import Utils +import Utils () {-- Our cheap monad-like stuff. @@ -37,6 +37,13 @@ ensureList lst = ) (Right []) lst +listHead :: Either String [a] -> Either String a +listHead lst = + case lst of + Left x -> Left x + Right (x:_) -> Right x + Right [] -> Left "List empty" + loadJSArray :: String -> Either String [JSObject JSValue] loadJSArray s = resultToEither $ decodeStrict s @@ -51,6 +58,18 @@ getIntElement key o = Left x -> Left x Right x -> Right $ show x +getListElement :: String -> JSObject JSValue + -> Either String [JSValue] +getListElement key o = + let tmp = resultToEither $ ((valFromObj key o)::Result [JSValue]) + in tmp + +readString :: JSValue -> Either String String +readString v = + case v of + JSString s -> Right $ fromJSString s + _ -> Left "Wrong JSON type" + concatElems a b = case a of Left _ -> a @@ -98,11 +117,14 @@ parseInstance a = Left _ -> getIntElement "sda_size" a Right x -> Right x bep = (resultToEither $ valFromObj "beparams" a) + pnode = getStringElement "pnode" a + snode = (listHead $ getListElement "snodes" a) `combine` readString in case bep of Left x -> Left x Right x -> let mem = getIntElement "memory" x - in concatElems name $ concatElems mem disk + in concatElems name $ concatElems mem $ + concatElems disk $ concatElems pnode snode parseNode :: JSObject JSValue -> Either String String parseNode a = -- GitLab