Skip to content
Snippets Groups Projects
Commit b8b9a53c authored by Iustin Pop's avatar Iustin Pop
Browse files

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.
parent 01f6a5d2
No related merge requests found
...@@ -6,14 +6,14 @@ module Rapi ...@@ -6,14 +6,14 @@ module Rapi
where where
import Network.Curl import Network.Curl
import Network.Curl.Types import Network.Curl.Types ()
import Network.Curl.Code import Network.Curl.Code
import Data.Either (either) import Data.Either ()
import Data.Maybe import Data.Maybe ()
import Control.Monad import Control.Monad
import Text.JSON import Text.JSON
import Text.Printf (printf) import Text.Printf (printf)
import Utils import Utils ()
{-- Our cheap monad-like stuff. {-- Our cheap monad-like stuff.
...@@ -37,6 +37,13 @@ ensureList lst = ...@@ -37,6 +37,13 @@ ensureList lst =
) )
(Right []) 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 :: String -> Either String [JSObject JSValue]
loadJSArray s = resultToEither $ decodeStrict s loadJSArray s = resultToEither $ decodeStrict s
...@@ -51,6 +58,18 @@ getIntElement key o = ...@@ -51,6 +58,18 @@ getIntElement key o =
Left x -> Left x Left x -> Left x
Right x -> Right $ show 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 = concatElems a b =
case a of case a of
Left _ -> a Left _ -> a
...@@ -98,11 +117,14 @@ parseInstance a = ...@@ -98,11 +117,14 @@ parseInstance a =
Left _ -> getIntElement "sda_size" a Left _ -> getIntElement "sda_size" a
Right x -> Right x Right x -> Right x
bep = (resultToEither $ valFromObj "beparams" a) bep = (resultToEither $ valFromObj "beparams" a)
pnode = getStringElement "pnode" a
snode = (listHead $ getListElement "snodes" a) `combine` readString
in in
case bep of case bep of
Left x -> Left x Left x -> Left x
Right x -> let mem = getIntElement "memory" 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 :: JSObject JSValue -> Either String String
parseNode a = parseNode a =
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment