Revision d575c755

b/htools/Ganeti/HTools/Rapi.hs
30 30
  , parseData
31 31
  ) where
32 32

  
33
import Data.List (isPrefixOf)
33 34
import Data.Maybe (fromMaybe)
34 35
#ifndef NO_CURL
35 36
import Network.Curl
......
39 40
import Text.JSON (JSObject, fromJSObject, decodeStrict)
40 41
import Text.JSON.Types (JSValue(..))
41 42
import Text.Printf (printf)
43
import System.FilePath
42 44

  
43 45
import Ganeti.HTools.Loader
44 46
import Ganeti.HTools.Types
......
50 52

  
51 53
{-# ANN module "HLint: ignore Eta reduce" #-}
52 54

  
55
-- | File method prefix.
56
filePrefix :: String
57
filePrefix = "file://"
58

  
53 59
-- | Read an URL via curl and return the body if successful.
54 60
getUrl :: (Monad m) => String -> IO (m String)
55 61

  
......
74 80
                 url (show code))
75 81
#endif
76 82

  
83
-- | Helper to convert I/O errors in 'Bad' values.
84
ioErrToResult :: IO a -> IO (Result a)
85
ioErrToResult ioaction =
86
  catch (ioaction >>= return . Ok) (return . Bad . show)
87

  
77 88
-- | Append the default port if not passed in.
78 89
formatHost :: String -> String
79 90
formatHost master =
......
175 186
  return (tags, ipolicy)
176 187

  
177 188
-- | Loads the raw cluster data from an URL.
178
readData :: String -- ^ Cluster or URL to use as source
179
         -> IO (Result String, Result String, Result String, Result String)
180
readData master = do
189
readDataHttp :: String -- ^ Cluster or URL to use as source
190
             -> IO (Result String, Result String, Result String, Result String)
191
readDataHttp master = do
181 192
  let url = formatHost master
182 193
  group_body <- getUrl $ printf "%s/2/groups?bulk=1" url
183 194
  node_body <- getUrl $ printf "%s/2/nodes?bulk=1" url
......
185 196
  info_body <- getUrl $ printf "%s/2/info" url
186 197
  return (group_body, node_body, inst_body, info_body)
187 198

  
199
-- | Loads the raw cluster data from the filesystem.
200
readDataFile:: String -- ^ Path to the directory containing the files
201
             -> IO (Result String, Result String, Result String, Result String)
202
readDataFile path = do
203
  group_body <- ioErrToResult $ readFile $ path </> "groups.json"
204
  node_body <- ioErrToResult $ readFile $ path </> "nodes.json"
205
  inst_body <- ioErrToResult $ readFile $ path </> "instances.json"
206
  info_body <- ioErrToResult $ readFile $ path </> "info.json"
207
  return (group_body, node_body, inst_body, info_body)
208

  
209
-- | Loads data via either 'readDataFile' or 'readDataHttp'.
210
readData :: String -- ^ URL to use as source
211
         -> IO (Result String, Result String, Result String, Result String)
212
readData url = do
213
  if filePrefix `isPrefixOf` url
214
    then readDataFile (drop (length filePrefix) url)
215
    else readDataHttp url
216

  
188 217
-- | Builds the cluster data from the raw Rapi content.
189 218
parseData :: (Result String, Result String, Result String, Result String)
190 219
          -> Result ClusterData

Also available in: Unified diff