From 7dd142110abb750abda75c0042bfb5caec71a6f8 Mon Sep 17 00:00:00 2001
From: Agata Murawska <agatamurawska@google.com>
Date: Thu, 10 Nov 2011 11:21:32 +0100
Subject: [PATCH] htools: change runSt type to InstanceStatus

Signed-off-by: Agata Murawska <agatamurawska@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 htools/Ganeti/HTools/IAlloc.hs         |  3 +--
 htools/Ganeti/HTools/Instance.hs       |  8 ++++----
 htools/Ganeti/HTools/Program/Hspace.hs |  2 +-
 htools/Ganeti/HTools/QC.hs             | 28 +++++++++++++++-----------
 htools/Ganeti/HTools/Text.hs           |  5 +++--
 5 files changed, 25 insertions(+), 21 deletions(-)

diff --git a/htools/Ganeti/HTools/IAlloc.hs b/htools/Ganeti/HTools/IAlloc.hs
index edaf4132b..8bb9df7f0 100644
--- a/htools/Ganeti/HTools/IAlloc.hs
+++ b/htools/Ganeti/HTools/IAlloc.hs
@@ -68,8 +68,7 @@ parseBaseInstance n a = do
   vcpus <- extract "vcpus"
   tags  <- extract "tags"
   dt    <- extract "disk_template"
-  let running = "running"
-  return (n, Instance.create n mem disk vcpus running tags True 0 0 dt)
+  return (n, Instance.create n mem disk vcpus Running tags True 0 0 dt)
 
 -- | Parses an instance as found in the cluster instance list.
 parseInstance :: NameAssoc -- ^ The node name-to-index association list
diff --git a/htools/Ganeti/HTools/Instance.hs b/htools/Ganeti/HTools/Instance.hs
index a6debe8e2..519e3851c 100644
--- a/htools/Ganeti/HTools/Instance.hs
+++ b/htools/Ganeti/HTools/Instance.hs
@@ -62,7 +62,7 @@ data Instance = Instance
     , dsk          :: Int       -- ^ Disk size of instance
     , vcpus        :: Int       -- ^ Number of VCPUs
     , running      :: Bool      -- ^ Is the instance running?
-    , runSt        :: String    -- ^ Original (text) run status
+    , runSt        :: T.InstanceStatus -- ^ Original run status
     , pNode        :: T.Ndx     -- ^ Original primary node
     , sNode        :: T.Ndx     -- ^ Original secondary node
     , idx          :: T.Idx     -- ^ Internal index
@@ -81,8 +81,8 @@ instance T.Element Instance where
     allNames n = [name n, alias n]
 
 -- | Constant holding the running instance states.
-runningStates :: [String]
-runningStates = [C.inststRunning, C.inststErrorup]
+runningStates :: [T.InstanceStatus]
+runningStates = [T.Running, T.ErrorUp]
 
 -- | Constant holding the local storage templates.
 --
@@ -115,7 +115,7 @@ type List = Container.Container Instance
 --
 -- Some parameters are not initialized by function, and must be set
 -- later (via 'setIdx' for example).
-create :: String -> Int -> Int -> Int -> String
+create :: String -> Int -> Int -> Int -> T.InstanceStatus
        -> [String] -> Bool -> T.Ndx -> T.Ndx -> T.DiskTemplate -> Instance
 create name_init mem_init dsk_init vcpus_init run_init tags_init
        auto_balance_init pn sn dt =
diff --git a/htools/Ganeti/HTools/Program/Hspace.hs b/htools/Ganeti/HTools/Program/Hspace.hs
index 206224122..02076532c 100644
--- a/htools/Ganeti/HTools/Program/Hspace.hs
+++ b/htools/Ganeti/HTools/Program/Hspace.hs
@@ -424,7 +424,7 @@ main = do
 
   -- utility functions
   let iofspec spx = Instance.create "new" (rspecMem spx) (rspecDsk spx)
-                    (rspecCpu spx) "running" [] True (-1) (-1) disk_template
+                    (rspecCpu spx) Running [] True (-1) (-1) disk_template
 
   allocnodes <- exitIfBad $ Cluster.genAllocNodes gl nl req_nodes True
 
diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs
index e7f6bc5cf..40fa0fe68 100644
--- a/htools/Ganeti/HTools/QC.hs
+++ b/htools/Ganeti/HTools/QC.hs
@@ -123,7 +123,7 @@ setInstanceSmallerThanNode node inst =
 
 -- | Create an instance given its spec.
 createInstance mem dsk vcpus =
-    Instance.create "inst-unnamed" mem dsk vcpus "running" [] True (-1) (-1)
+    Instance.create "inst-unnamed" mem dsk vcpus Types.Running [] True (-1) (-1)
                     Types.DTDrbd8
 
 -- | Create a small cluster by repeating a node spec.
@@ -187,20 +187,23 @@ getFQDN = do
   let frest' = map (map dnsGetChar) frest
   return (felem ++ "." ++ intercalate "." frest')
 
+instance Arbitrary Types.InstanceStatus where
+    arbitrary = elements [ Types.AdminDown
+                         , Types.AdminOffline
+                         , Types.ErrorDown
+                         , Types.ErrorUp
+                         , Types.NodeDown
+                         , Types.NodeOffline
+                         , Types.Running
+                         , Types.WrongNode]
+
 -- let's generate a random instance
 instance Arbitrary Instance.Instance where
     arbitrary = do
       name <- getFQDN
       mem <- choose (0, maxMem)
       dsk <- choose (0, maxDsk)
-      run_st <- elements [ C.inststErrorup
-                         , C.inststErrordown
-                         , C.inststAdmindown
-                         , C.inststNodedown
-                         , C.inststNodeoffline
-                         , C.inststRunning
-                         , "no_such_status1"
-                         , "no_such_status2"]
+      run_st <- arbitrary
       pn <- arbitrary
       sn <- arbitrary
       vcpus <- choose (0, maxCpu)
@@ -591,6 +594,7 @@ prop_Text_Load_Instance name mem dsk vcpus status
     let vcpus_s = show vcpus
         dsk_s = show dsk
         mem_s = show mem
+        status_s = Types.instanceStatusToRaw status
         ndx = if null snode
               then [(pnode, pdx)]
               else [(pnode, pdx), (snode, sdx)]
@@ -599,13 +603,13 @@ prop_Text_Load_Instance name mem dsk vcpus status
         sbal = if autobal then "Y" else "N"
         sdt = Types.diskTemplateToRaw dt
         inst = Text.loadInst nl
-               [name, mem_s, dsk_s, vcpus_s, status,
+               [name, mem_s, dsk_s, vcpus_s, status_s,
                 sbal, pnode, snode, sdt, tags]
         fail1 = Text.loadInst nl
-               [name, mem_s, dsk_s, vcpus_s, status,
+               [name, mem_s, dsk_s, vcpus_s, status_s,
                 sbal, pnode, pnode, tags]
         _types = ( name::String, mem::Int, dsk::Int
-                 , vcpus::Int, status::String
+                 , vcpus::Int, status::Types.InstanceStatus
                  , snode::String
                  , autobal::Bool)
     in
diff --git a/htools/Ganeti/HTools/Text.hs b/htools/Ganeti/HTools/Text.hs
index 7ac1d5c08..8958f9f58 100644
--- a/htools/Ganeti/HTools/Text.hs
+++ b/htools/Ganeti/HTools/Text.hs
@@ -95,7 +95,7 @@ serializeInstance nl inst =
     in
       printf "%s|%d|%d|%d|%s|%s|%s|%s|%s|%s"
              iname (Instance.mem inst) (Instance.dsk inst)
-             (Instance.vcpus inst) (Instance.runSt inst)
+             (Instance.vcpus inst) (instanceStatusToRaw (Instance.runSt inst))
              (if Instance.autoBalance inst then "Y" else "N")
              pnode snode (diskTemplateToRaw (Instance.diskTemplate inst))
              (intercalate "," (Instance.tags inst))
@@ -162,6 +162,7 @@ loadInst ktn [ name, mem, dsk, vcpus, status, auto_bal, pnode, snode
   vmem <- tryRead name mem
   vdsk <- tryRead name dsk
   vvcpus <- tryRead name vcpus
+  vstatus <- instanceStatusFromRaw status
   auto_balance <- case auto_bal of
                     "Y" -> return True
                     "N" -> return False
@@ -172,7 +173,7 @@ loadInst ktn [ name, mem, dsk, vcpus, status, auto_bal, pnode, snode
   when (sidx == pidx) $ fail $ "Instance " ++ name ++
            " has same primary and secondary node - " ++ pnode
   let vtags = sepSplit ',' tags
-      newinst = Instance.create name vmem vdsk vvcpus status vtags
+      newinst = Instance.create name vmem vdsk vvcpus vstatus vtags
                 auto_balance pidx sidx disk_template
   return (name, newinst)
 loadInst _ s = fail $ "Invalid/incomplete instance data: '" ++ show s ++ "'"
-- 
GitLab