htools: simplify select tests
[ganeti-local] / htools / Ganeti / HTools / ExtLoader.hs
index 0412c1b..435d368 100644 (file)
@@ -1,6 +1,4 @@
-{-# LANGUAGE CPP #-}
-
-{-| External data loader
+{-| External data loader.
 
 This module holds the external data loading, and thus is the only one
 depending (via the specialized Text\/Rapi\/Luxi modules) on the actual
@@ -10,7 +8,7 @@ libraries implementing the low-level protocols.
 
 {-
 
-Copyright (C) 2009, 2010 Google Inc.
+Copyright (C) 2009, 2010, 2011 Google Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -35,17 +33,15 @@ module Ganeti.HTools.ExtLoader
     , maybeSaveData
     ) where
 
+import Control.Monad
 import Data.Maybe (isJust, fromJust)
-import Monad
 import System.FilePath
 import System.IO
 import System
 import Text.Printf (printf, hPrintf)
 
 import qualified Ganeti.HTools.Luxi as Luxi
-#ifndef NO_CURL
 import qualified Ganeti.HTools.Rapi as Rapi
-#endif
 import qualified Ganeti.HTools.Simu as Simu
 import qualified Ganeti.HTools.Text as Text
 import Ganeti.HTools.Loader (mergeData, checkData, ClusterData(..)
@@ -55,23 +51,23 @@ import Ganeti.HTools.Types
 import Ganeti.HTools.CLI
 import Ganeti.HTools.Utils (sepSplit, tryRead)
 
--- | Error beautifier
+-- | Error beautifier.
 wrapIO :: IO (Result a) -> IO (Result a)
 wrapIO = flip catch (return . Bad . show)
 
 parseUtilisation :: String -> Result (String, DynUtil)
 parseUtilisation line =
-    let columns = sepSplit ' ' line
-    in case columns of
-         [name, cpu, mem, dsk, net] -> do
-                      rcpu <- tryRead name cpu
-                      rmem <- tryRead name mem
-                      rdsk <- tryRead name dsk
-                      rnet <- tryRead name net
-                      let du = DynUtil { cpuWeight = rcpu, memWeight = rmem
-                                       , dskWeight = rdsk, netWeight = rnet }
-                      return (name, du)
-         _ -> Bad $ "Cannot parse line " ++ line
+    case sepSplit ' ' line of
+      [name, cpu, mem, dsk, net] ->
+          do
+            rcpu <- tryRead name cpu
+            rmem <- tryRead name mem
+            rdsk <- tryRead name dsk
+            rnet <- tryRead name net
+            let du = DynUtil { cpuWeight = rcpu, memWeight = rmem
+                             , dskWeight = rdsk, netWeight = rnet }
+            return (name, du)
+      _ -> Bad $ "Cannot parse line " ++ line
 
 -- | External tool data loader from a variety of sources.
 loadExternalData :: Options
@@ -89,6 +85,7 @@ loadExternalData opts = do
       exTags = case optExTags opts of
                  Nothing -> []
                  Just etl -> map (++ ":") etl
+      selInsts = optSelInst opts
       exInsts = optExInst opts
 
   when (length allSet > 1) $
@@ -109,24 +106,19 @@ loadExternalData opts = do
                      exitWith $ ExitFailure 1)
   input_data <-
       case () of
-        _ | setRapi ->
-#ifdef NO_CURL
-              return $ Bad "RAPI/curl backend disabled at compile time"
-#else
-              wrapIO $ Rapi.loadData mhost
-#endif
+        _ | setRapi -> wrapIO $ Rapi.loadData mhost
           | setLuxi -> wrapIO $ Luxi.loadData $ fromJust lsock
           | setSim -> Simu.loadData simdata
           | setFile -> wrapIO $ Text.loadData $ fromJust tfile
           | otherwise -> return $ Bad "No backend selected! Exiting."
 
-  let ldresult = input_data >>= mergeData util_data' exTags exInsts
+  let ldresult = input_data >>= mergeData util_data' exTags selInsts exInsts
   cdata <-
       (case ldresult of
          Ok x -> return x
          Bad s -> do
-           hPrintf stderr "Error: failed to load data. Details:\n%s\n" s
-               :: IO ()
+           hPrintf stderr
+             "Error: failed to load data, aborting. Details:\n%s\n" s:: IO ()
            exitWith $ ExitFailure 1
       )
   let (fix_msgs, nl) = checkData (cdNodes cdata) (cdInstances cdata)