Commit f36a8028 authored by Iustin Pop's avatar Iustin Pop
Browse files

Introduce suport for optional keys in JObjects

Some keys are optional in the Ganeti opcodes (e.g. ‘node’ in the
OpReplaceDisks), and as such we need to transform them in a Maybe value,
instead of failing.

The patch reworks a bit fromObj and adds maybeFromObj which parses such
optional values. It then uses it in the opcode reading.
parent c96d44df
......@@ -31,6 +31,7 @@ module Ganeti.HTools.Utils
, readEitherString
, loadJSArray
, fromObj
, maybeFromObj
, tryFromObj
, fromJVal
, asJSObject
......@@ -41,6 +42,7 @@ module Ganeti.HTools.Utils
, annotateResult
) where
import Control.Monad (liftM)
import Data.List
import qualified Text.JSON as J
import Text.Printf (printf)
......@@ -114,13 +116,28 @@ loadJSArray :: (Monad m)
-> m [J.JSObject J.JSValue]
loadJSArray s = fromJResult s . J.decodeStrict
-- | Reads a the value of a key in a JSON object.
-- | Reads the value of a key in a JSON object.
fromObj :: (J.JSON a, Monad m) => String -> [(String, J.JSValue)] -> m a
fromObj k o =
case lookup k o of
Nothing -> fail $ printf "key '%s' not found in %s" k (show o)
Just val -> fromJResult (printf "key '%s', value '%s'" k (show val))
(J.readJSON val)
Just val -> fromKeyValue k val
-- | Reads the value of an optional key in a JSON object.
maybeFromObj :: (J.JSON a, Monad m) => String -> [(String, J.JSValue)]
-> m (Maybe a)
maybeFromObj k o =
case lookup k o of
Nothing -> return Nothing
Just val -> liftM Just (fromKeyValue k val)
-- | Reads a JValue, that originated from an object key
fromKeyValue :: (J.JSON a, Monad m)
=> String -- ^ The key name
-> J.JSValue -- ^ The value to read
-> m a
fromKeyValue k val =
fromJResult (printf "key '%s', value '%s'" k (show val)) (J.readJSON val)
-- | Annotate a Result with an ownership information
annotateResult :: String -> Result a -> Result a
......@@ -81,10 +81,10 @@ loadOpCode v = do
return $ OpTestDelay duration on_master on_nodes
inst <- fromObj "instance_name" o
node <- fromObj "remote_node" o
node <- maybeFromObj "remote_node" o
mode <- fromObj "mode" o
disks <- fromObj "disks" o
ialloc <- fromObj "iallocator" o
ialloc <- maybeFromObj "iallocator" o
return $ OpReplaceDisks inst node mode disks ialloc
inst <- fromObj "instance_name" o
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment