From da45c3520bcb6d9485e57d8b6d47308c23071b0b Mon Sep 17 00:00:00 2001
From: Iustin Pop <iustin@google.com>
Date: Fri, 24 Aug 2012 14:48:12 +0200
Subject: [PATCH] Add more node-related data types and functions

This is a simple type declaration for NodeRole, a NdParamObject type
class, and a few related helper functions for nodes and node groups.

Signed-off-by: Iustin Pop <iustin@google.com>
Reviewed-by: Agata Murawska <agatamurawska@google.com>
---
 htools/Ganeti/Config.hs  | 46 ++++++++++++++++++++++++++++++++++++++++
 htools/Ganeti/Objects.hs | 22 +++++++++++++++++++
 2 files changed, 68 insertions(+)

diff --git a/htools/Ganeti/Config.hs b/htools/Ganeti/Config.hs
index 226351731..4c75a2255 100644
--- a/htools/Ganeti/Config.hs
+++ b/htools/Ganeti/Config.hs
@@ -25,13 +25,18 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
 module Ganeti.Config
     ( LinkIpMap
+    , NdParamObject(..)
     , loadConfig
     , getNodeInstances
+    , getNodeRole
+    , getNodeNdParams
     , getDefaultNicLink
     , getInstancesIpByLink
     , getNode
     , getInstance
     , getGroup
+    , getGroupNdParams
+    , getGroupOfNode
     , getInstPrimaryNode
     , getInstMinorsForNode
     , buildLinkIpInstnameMap
@@ -53,6 +58,10 @@ import Ganeti.Objects
 -- | Type alias for the link and ip map.
 type LinkIpMap = M.Map String (M.Map String String)
 
+-- | Type class denoting objects which have node parameters.
+class NdParamObject a where
+  getNdParamsOf :: ConfigData -> a -> Maybe FilledNDParams
+
 -- | Reads the config file.
 readConfig :: FilePath -> IO String
 readConfig = readFile
@@ -99,6 +108,15 @@ getNodeInstances cfg nname =
         sec_inst = filter ((nname `S.member`) . instSecondaryNodes) all_inst
     in (pri_inst, sec_inst)
 
+-- | Computes the role of a node.
+getNodeRole :: ConfigData -> Node -> NodeRole
+getNodeRole cfg node
+  | nodeName node == (clusterMasterNode $ configCluster cfg) = NRMaster
+  | nodeMasterCandidate node = NRCandidate
+  | nodeDrained node = NRDrained
+  | nodeOffline node = NROffline
+  | otherwise = NRRegular
+
 -- | Returns the default cluster link.
 getDefaultNicLink :: ConfigData -> String
 getDefaultNicLink =
@@ -145,6 +163,11 @@ getGroup cfg name =
                               (\k -> groupName ((M.!) groups k )) groups
                 in getItem "NodeGroup" name by_name
 
+-- | Computes a node group's node params.
+getGroupNdParams :: ConfigData -> NodeGroup -> FilledNDParams
+getGroupNdParams cfg ng =
+  fillNDParams (clusterNdparams $ configCluster cfg) (groupNdparams ng)
+
 -- | Looks up an instance's primary node.
 getInstPrimaryNode :: ConfigData -> String -> Result Node
 getInstPrimaryNode cfg name =
@@ -214,3 +237,26 @@ buildLinkIpInstnameMap cfg =
                                    newipmap = M.insert ip iname oldipmap
                                in M.insert link newipmap accum
             ) M.empty nics
+
+
+-- | Returns a node's group, with optional failure if we can't find it
+-- (configuration corrupt).
+getGroupOfNode :: ConfigData -> Node -> Maybe NodeGroup
+getGroupOfNode cfg node =
+  M.lookup (nodeGroup node) (fromContainer . configNodegroups $ cfg)
+
+-- | Returns a node's ndparams, filled.
+getNodeNdParams :: ConfigData -> Node -> Maybe FilledNDParams
+getNodeNdParams cfg node = do
+  group <- getGroupOfNode cfg node
+  let gparams = getGroupNdParams cfg group
+  return $ fillNDParams gparams (nodeNdparams node)
+
+instance NdParamObject Node where
+  getNdParamsOf = getNodeNdParams
+
+instance NdParamObject NodeGroup where
+  getNdParamsOf cfg = Just . getGroupNdParams cfg
+
+instance NdParamObject Cluster where
+  getNdParamsOf _ = Just . clusterNdparams
diff --git a/htools/Ganeti/Objects.hs b/htools/Ganeti/Objects.hs
index d382b5218..423547eb2 100644
--- a/htools/Ganeti/Objects.hs
+++ b/htools/Ganeti/Objects.hs
@@ -53,6 +53,9 @@ module Ganeti.Objects
   , FilledNDParams(..)
   , fillNDParams
   , Node(..)
+  , NodeRole(..)
+  , nodeRoleToRaw
+  , roleDescription
   , AllocPolicy(..)
   , FilledISpecParams(..)
   , PartialISpecParams(..)
@@ -125,6 +128,25 @@ class SerialNoObject a where
 class TagsObject a where
   tagsOf :: a -> Set.Set String
 
+-- * Node role object
+
+$(declareSADT "NodeRole"
+  [ ("NROffline",   'C.nrOffline)
+  , ("NRDrained",   'C.nrDrained)
+  , ("NRRegular",   'C.nrRegular)
+  , ("NRCandidate", 'C.nrMcandidate)
+  , ("NRMaster",    'C.nrMaster)
+  ])
+$(makeJSONInstance ''NodeRole)
+
+-- | The description of the node role.
+roleDescription :: NodeRole -> String
+roleDescription NROffline   = "offline"
+roleDescription NRDrained   = "drained"
+roleDescription NRRegular   = "regular"
+roleDescription NRCandidate = "master candidate"
+roleDescription NRMaster    = "master"
+
 -- * NIC definitions
 
 $(declareSADT "NICMode"
-- 
GitLab