Make Query operators enforce strictness
[ganeti-local] / htools / Ganeti / HTools / Instance.hs
index 42871b3..b941df6 100644 (file)
@@ -57,10 +57,11 @@ module Ganeti.HTools.Instance
   , mirrorType
   ) where
 
+import Ganeti.BasicTypes
 import qualified Ganeti.HTools.Types as T
 import qualified Ganeti.HTools.Container as Container
 
-import Ganeti.HTools.Utils
+import Ganeti.Utils
 
 -- * Type declarations
 
@@ -78,10 +79,11 @@ data Instance = Instance
   , util         :: T.DynUtil -- ^ Dynamic resource usage
   , movable      :: Bool      -- ^ Can and should the instance be moved?
   , autoBalance  :: Bool      -- ^ Is the instance auto-balanced?
-  , tags         :: [String]  -- ^ List of instance tags
   , diskTemplate :: T.DiskTemplate -- ^ The disk template of the instance
-  , spindleUsage :: Int       -- ^ The numbers of used spindles
-  } deriving (Show, Read, Eq)
+  , spindleUse   :: Int       -- ^ The numbers of used spindles
+  , allTags      :: [String]  -- ^ List of all instance tags
+  , exclTags     :: [String]  -- ^ List of instance exclusion tags
+  } deriving (Show, Eq)
 
 instance T.Element Instance where
   nameOf   = name
@@ -98,8 +100,8 @@ isRunning _                              = False
 
 -- | Check if instance is offline.
 isOffline :: Instance -> Bool
-isOffline (Instance {runSt = T.AdminOffline}) = True
-isOffline _                                   = False
+isOffline (Instance {runSt = T.StatusOffline}) = True
+isOffline _                                    = False
 
 
 -- | Helper to check if the instance is not offline.
@@ -173,11 +175,12 @@ create name_init mem_init dsk_init vcpus_init run_init tags_init
            , sNode = sn
            , idx = -1
            , util = T.baseUtil
-           , tags = tags_init
            , movable = supportsMoves dt
            , autoBalance = auto_balance_init
            , diskTemplate = dt
-           , spindleUsage = su
+           , spindleUse = su
+           , allTags = tags_init
+           , exclTags = []
            }
 
 -- | Changes the index.
@@ -233,20 +236,20 @@ setMovable t m = t { movable = m }
 
 -- | Try to shrink the instance based on the reason why we can't
 -- allocate it.
-shrinkByType :: Instance -> T.FailMode -> T.Result Instance
+shrinkByType :: Instance -> T.FailMode -> Result Instance
 shrinkByType inst T.FailMem = let v = mem inst - T.unitMem
                               in if v < T.unitMem
-                                 then T.Bad "out of memory"
-                                 else T.Ok inst { mem = v }
+                                 then Bad "out of memory"
+                                 else Ok inst { mem = v }
 shrinkByType inst T.FailDisk = let v = dsk inst - T.unitDsk
                                in if v < T.unitDsk
-                                  then T.Bad "out of disk"
-                                  else T.Ok inst { dsk = v }
+                                  then Bad "out of disk"
+                                  else Ok inst { dsk = v }
 shrinkByType inst T.FailCPU = let v = vcpus inst - T.unitCpu
                               in if v < T.unitCpu
-                                 then T.Bad "out of vcpus"
-                                 else T.Ok inst { vcpus = v }
-shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
+                                 then Bad "out of vcpus"
+                                 else Ok inst { vcpus = v }
+shrinkByType _ f = Bad $ "Unhandled failure mode " ++ show f
 
 -- | Return the spec of an instance.
 specOf :: Instance -> T.RSpec
@@ -254,31 +257,31 @@ specOf Instance { mem = m, dsk = d, vcpus = c } =
   T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }
 
 -- | Checks if an instance is smaller than a given spec. Returns
--- OpGood for a correct spec, otherwise OpFail one of the possible
+-- OpGood for a correct spec, otherwise Bad one of the possible
 -- failure modes.
 instBelowISpec :: Instance -> T.ISpec -> T.OpResult ()
 instBelowISpec inst ispec
-  | mem inst > T.iSpecMemorySize ispec = T.OpFail T.FailMem
-  | dsk inst > T.iSpecDiskSize ispec   = T.OpFail T.FailDisk
-  | vcpus inst > T.iSpecCpuCount ispec = T.OpFail T.FailCPU
-  | otherwise = T.OpGood ()
+  | mem inst > T.iSpecMemorySize ispec = Bad T.FailMem
+  | dsk inst > T.iSpecDiskSize ispec   = Bad T.FailDisk
+  | vcpus inst > T.iSpecCpuCount ispec = Bad T.FailCPU
+  | otherwise = Ok ()
 
 -- | Checks if an instance is bigger than a given spec.
 instAboveISpec :: Instance -> T.ISpec -> T.OpResult ()
 instAboveISpec inst ispec
-  | mem inst < T.iSpecMemorySize ispec = T.OpFail T.FailMem
-  | dsk inst < T.iSpecDiskSize ispec   = T.OpFail T.FailDisk
-  | vcpus inst < T.iSpecCpuCount ispec = T.OpFail T.FailCPU
-  | otherwise = T.OpGood ()
+  | mem inst < T.iSpecMemorySize ispec = Bad T.FailMem
+  | dsk inst < T.iSpecDiskSize ispec   = Bad T.FailDisk
+  | vcpus inst < T.iSpecCpuCount ispec = Bad T.FailCPU
+  | otherwise = Ok ()
 
 -- | Checks if an instance matches a policy.
 instMatchesPolicy :: Instance -> T.IPolicy -> T.OpResult ()
 instMatchesPolicy inst ipol = do
   instAboveISpec inst (T.iPolicyMinSpec ipol)
   instBelowISpec inst (T.iPolicyMaxSpec ipol)
-  if (diskTemplate inst `elem` T.iPolicyDiskTemplates ipol)
-    then T.OpGood ()
-    else T.OpFail T.FailDisk
+  if diskTemplate inst `elem` T.iPolicyDiskTemplates ipol
+    then Ok ()
+    else Bad T.FailDisk
 
 -- | Checks whether the instance uses a secondary node.
 --