Revision ebf38064 htools/Ganeti/HTools/Luxi.hs
b/htools/Ganeti/HTools/Luxi.hs | ||
---|---|---|
24 | 24 |
-} |
25 | 25 |
|
26 | 26 |
module Ganeti.HTools.Luxi |
27 |
( |
|
28 |
loadData |
|
29 |
, parseData |
|
30 |
) where |
|
27 |
( loadData |
|
28 |
, parseData |
|
29 |
) where |
|
31 | 30 |
|
32 | 31 |
import qualified Control.Exception as E |
33 | 32 |
import Text.JSON.Types |
... | ... | |
53 | 52 |
parseQueryField :: (Monad m) => JSValue -> m (JSValue, JSValue) |
54 | 53 |
parseQueryField (JSArray [status, result]) = return (status, result) |
55 | 54 |
parseQueryField o = |
56 |
fail $ "Invalid query field, expected (status, value) but got " ++ show o
|
|
55 |
fail $ "Invalid query field, expected (status, value) but got " ++ show o |
|
57 | 56 |
|
58 | 57 |
-- | Parse a result row. |
59 | 58 |
parseQueryRow :: (Monad m) => JSValue -> m [(JSValue, JSValue)] |
60 | 59 |
parseQueryRow (JSArray arr) = mapM parseQueryField arr |
61 | 60 |
parseQueryRow o = |
62 |
fail $ "Invalid query row result, expected array but got " ++ show o
|
|
61 |
fail $ "Invalid query row result, expected array but got " ++ show o |
|
63 | 62 |
|
64 | 63 |
-- | Parse an overall query result and get the [(status, value)] list |
65 | 64 |
-- for each element queried. |
66 | 65 |
parseQueryResult :: (Monad m) => JSValue -> m [[(JSValue, JSValue)]] |
67 | 66 |
parseQueryResult (JSArray arr) = mapM parseQueryRow arr |
68 | 67 |
parseQueryResult o = |
69 |
fail $ "Invalid query result, expected array but got " ++ show o
|
|
68 |
fail $ "Invalid query result, expected array but got " ++ show o |
|
70 | 69 |
|
71 | 70 |
-- | Prepare resulting output as parsers expect it. |
72 | 71 |
extractArray :: (Monad m) => JSValue -> m [[(JSValue, JSValue)]] |
... | ... | |
76 | 75 |
-- | Testing result status for more verbose error message. |
77 | 76 |
fromJValWithStatus :: (Text.JSON.JSON a, Monad m) => (JSValue, JSValue) -> m a |
78 | 77 |
fromJValWithStatus (st, v) = do |
79 |
st' <- fromJVal st
|
|
80 |
L.checkRS st' v >>= fromJVal
|
|
78 |
st' <- fromJVal st |
|
79 |
L.checkRS st' v >>= fromJVal |
|
81 | 80 |
|
82 | 81 |
-- | Annotate errors when converting values with owner/attribute for |
83 | 82 |
-- better debugging. |
... | ... | |
88 | 87 |
-> (JSValue, JSValue) -- ^ The value we're trying to convert |
89 | 88 |
-> Result a -- ^ The annotated result |
90 | 89 |
genericConvert otype oname oattr = |
91 |
annotateResult (otype ++ " '" ++ oname ++
|
|
92 |
"', error while reading attribute '" ++
|
|
93 |
oattr ++ "'") . fromJValWithStatus
|
|
90 |
annotateResult (otype ++ " '" ++ oname ++ |
|
91 |
"', error while reading attribute '" ++ |
|
92 |
oattr ++ "'") . fromJValWithStatus |
|
94 | 93 |
|
95 | 94 |
-- * Data querying functionality |
96 | 95 |
|
... | ... | |
104 | 103 |
-- | The input data for instance query. |
105 | 104 |
queryInstancesMsg :: L.LuxiOp |
106 | 105 |
queryInstancesMsg = |
107 |
L.Query L.QRInstance ["name", "disk_usage", "be/memory", "be/vcpus",
|
|
108 |
"status", "pnode", "snodes", "tags", "oper_ram",
|
|
109 |
"be/auto_balance", "disk_template"] ()
|
|
106 |
L.Query L.QRInstance ["name", "disk_usage", "be/memory", "be/vcpus", |
|
107 |
"status", "pnode", "snodes", "tags", "oper_ram", |
|
108 |
"be/auto_balance", "disk_template"] () |
|
110 | 109 |
|
111 | 110 |
-- | The input data for cluster query. |
112 | 111 |
queryClusterInfoMsg :: L.LuxiOp |
Also available in: Unified diff