From a488a21718694f6b8150eb43a7a8467fcec20dfe Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 16 Oct 2009 13:09:55 +0200
Subject: [PATCH] Merge the Node.setPri and Node.addCpus functions

The latter is only used right after the former in the Loader module, and
we'll need more of this 'update not with the data of this instance'
functionality (which is different than addPri where all information must
be updated).

The patch also changes the signature of Node.setSec (to remain
consistent with setPri).
---
 Ganeti/HTools/Loader.hs |  9 ++++-----
 Ganeti/HTools/Node.hs   | 21 +++++++++------------
 2 files changed, 13 insertions(+), 17 deletions(-)

diff --git a/Ganeti/HTools/Loader.hs b/Ganeti/HTools/Loader.hs
index fd39461bb..8756f1a41 100644
--- a/Ganeti/HTools/Loader.hs
+++ b/Ganeti/HTools/Loader.hs
@@ -98,19 +98,18 @@ assocEqual = (==) `on` fst
 fixNodes :: [(Ndx, Node.Node)]
          -> (Idx, Instance.Instance)
          -> [(Ndx, Node.Node)]
-fixNodes accu (idx, inst) =
+fixNodes accu (_, inst) =
     let
         pdx = Instance.pNode inst
         sdx = Instance.sNode inst
         pold = fromJust $ lookup pdx accu
-        pnew = Node.setPri pold idx
-        pnew' = Node.addCpus pnew (Instance.vcpus inst)
+        pnew = Node.setPri pold inst
         ac1 = deleteBy assocEqual (pdx, pold) accu
-        ac2 = (pdx, pnew'):ac1
+        ac2 = (pdx, pnew):ac1
     in
       if sdx /= Node.noSecondary
       then let sold = fromJust $ lookup sdx accu
-               snew = Node.setSec sold idx
+               snew = Node.setSec sold inst
                ac3 = deleteBy assocEqual (sdx, sold) ac2
            in (sdx, snew):ac3
       else ac2
diff --git a/Ganeti/HTools/Node.hs b/Ganeti/HTools/Node.hs
index 355db0be6..bc3210824 100644
--- a/Ganeti/HTools/Node.hs
+++ b/Ganeti/HTools/Node.hs
@@ -47,7 +47,6 @@ module Ganeti.HTools.Node
     , setSec
     , setMdsk
     , setMcpu
-    , addCpus
     -- * Instance (re)location
     , removePri
     , removeSec
@@ -217,19 +216,17 @@ buildPeers t il =
         new_prem = fromIntegral new_rmem / tMem t
     in t {peers=pmap, failN1 = new_failN1, rMem = new_rmem, pRem = new_prem}
 
--- | Assigns an instance to a node as primary without other updates.
-setPri :: Node -> T.Idx -> Node
-setPri t ix = t { pList = ix:pList t }
+-- | Assigns an instance to a node as primary and update the used VCPU
+-- count.
+setPri :: Node -> Instance.Instance -> Node
+setPri t inst = t { pList = (Instance.idx inst):pList t
+                  , uCpu = new_count
+                  , pCpu = fromIntegral new_count / tCpu t }
+    where new_count = uCpu t + Instance.vcpus inst
 
 -- | Assigns an instance to a node as secondary without other updates.
-setSec :: Node -> T.Idx -> Node
-setSec t ix = t { sList = ix:sList t }
-
--- | Add primary cpus to a node
-addCpus :: Node -> Int -> Node
-addCpus t count =
-    let new_count = uCpu t + count
-    in t { uCpu = new_count, pCpu = fromIntegral new_count / tCpu t }
+setSec :: Node -> Instance.Instance -> Node
+setSec t inst = t { sList = (Instance.idx inst):sList t }
 
 -- * Update functions
 
-- 
GitLab