From f3f76ccc3617d44a49b4e66ca9ced9396a9b34bc Mon Sep 17 00:00:00 2001 From: Iustin Pop <iustin@google.com> Date: Thu, 22 Dec 2011 15:50:23 +0100 Subject: [PATCH] More reshuffling of code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Following the split Types/BasicTypes, we can remove the last JSON-related stuff from Utils.hs, and do some more cleanup. Signed-off-by: Iustin Pop <iustin@google.com> Reviewed-by: RenΓ© Nussbaumer <rn@google.com> --- htools/Ganeti/BasicTypes.hs | 6 ++++++ htools/Ganeti/HTools/JSON.hs | 12 ++++++++++++ htools/Ganeti/HTools/Luxi.hs | 2 +- htools/Ganeti/HTools/QC.hs | 2 +- htools/Ganeti/HTools/Types.hs | 6 ++++++ htools/Ganeti/HTools/Utils.hs | 22 ---------------------- 6 files changed, 26 insertions(+), 24 deletions(-) diff --git a/htools/Ganeti/BasicTypes.hs b/htools/Ganeti/BasicTypes.hs index 031e3c298..b7b437fa9 100644 --- a/htools/Ganeti/BasicTypes.hs +++ b/htools/Ganeti/BasicTypes.hs @@ -24,6 +24,7 @@ module Ganeti.BasicTypes , isOk , isBad , eitherToResult + , annotateResult ) where import Control.Monad @@ -65,3 +66,8 @@ isBad = not . isOk eitherToResult :: Either String a -> Result a eitherToResult (Left s) = Bad s eitherToResult (Right v) = Ok v + +-- | Annotate a Result with an ownership information. +annotateResult :: String -> Result a -> Result a +annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s +annotateResult _ v = v diff --git a/htools/Ganeti/HTools/JSON.hs b/htools/Ganeti/HTools/JSON.hs index 185201fa5..31a6d1947 100644 --- a/htools/Ganeti/HTools/JSON.hs +++ b/htools/Ganeti/HTools/JSON.hs @@ -33,6 +33,7 @@ module Ganeti.HTools.JSON , fromJVal , asJSObject , asObjectList + , tryFromObj ) where @@ -42,6 +43,8 @@ import Text.Printf (printf) import qualified Text.JSON as J +import Ganeti.BasicTypes + -- * JSON-related functions -- | A type alias for the list-based representation of J.JSObject. @@ -114,3 +117,12 @@ asJSObject _ = fail "not an object" -- | Coneverts a list of JSON values into a list of JSON objects. asObjectList :: (Monad m) => [J.JSValue] -> m [J.JSObject J.JSValue] asObjectList = mapM asJSObject + +-- | Try to extract a key from a object with better error reporting +-- than fromObj. +tryFromObj :: (J.JSON a) => + String -- ^ Textual "owner" in error messages + -> JSRecord -- ^ The object array + -> String -- ^ The desired key from the object + -> Result a +tryFromObj t o = annotateResult t . fromObj o diff --git a/htools/Ganeti/HTools/Luxi.hs b/htools/Ganeti/HTools/Luxi.hs index f7c6dee1d..38bb266fc 100644 --- a/htools/Ganeti/HTools/Luxi.hs +++ b/htools/Ganeti/HTools/Luxi.hs @@ -38,7 +38,7 @@ import Ganeti.HTools.Types import qualified Ganeti.HTools.Group as Group import qualified Ganeti.HTools.Node as Node import qualified Ganeti.HTools.Instance as Instance -import Ganeti.HTools.Utils (fromJVal, annotateResult, tryFromObj, asJSObject, +import Ganeti.HTools.Utils (fromJVal, tryFromObj, asJSObject, fromObj) {-# ANN module "HLint: ignore Eta reduce" #-} diff --git a/htools/Ganeti/HTools/QC.hs b/htools/Ganeti/HTools/QC.hs index df1da6b98..435d37823 100644 --- a/htools/Ganeti/HTools/QC.hs +++ b/htools/Ganeti/HTools/QC.hs @@ -91,7 +91,7 @@ maxCpu = 1024 defGroup :: Group.Group defGroup = flip Group.setIdx 0 $ - Group.create "default" Utils.defaultGroupID Types.AllocPreferred + Group.create "default" Types.defaultGroupID Types.AllocPreferred defGroupList :: Group.List defGroupList = Container.fromList [(Group.idx defGroup, defGroup)] diff --git a/htools/Ganeti/HTools/Types.hs b/htools/Ganeti/HTools/Types.hs index 644d4d979..3209ce7a7 100644 --- a/htools/Ganeti/HTools/Types.hs +++ b/htools/Ganeti/HTools/Types.hs @@ -33,6 +33,7 @@ module Ganeti.HTools.Types , Score , Weight , GroupID + , defaultGroupID , AllocPolicy(..) , allocPolicyFromRaw , allocPolicyToRaw @@ -62,6 +63,7 @@ module Ganeti.HTools.Types , isOk , isBad , eitherToResult + , annotateResult , Element(..) , FailMode(..) , FailStats @@ -100,6 +102,10 @@ type Weight = Double -- | The Group UUID type. type GroupID = String +-- | Default group UUID (just a string, not a real UUID). +defaultGroupID :: GroupID +defaultGroupID = "00000000-0000-0000-0000-000000000000" + -- | The Group allocation policy type. -- -- Note that the order of constructors is important as the automatic diff --git a/htools/Ganeti/HTools/Utils.hs b/htools/Ganeti/HTools/Utils.hs index c3b3caad9..b1898ebe7 100644 --- a/htools/Ganeti/HTools/Utils.hs +++ b/htools/Ganeti/HTools/Utils.hs @@ -44,18 +44,14 @@ module Ganeti.HTools.Utils , fromJResult , tryRead , formatTable - , annotateResult - , defaultGroupID , parseUnit ) where import Data.Char (toUpper) import Data.List -import qualified Text.JSON as J import Debug.Trace -import Ganeti.HTools.Types -- we will re-export these for our existing users import Ganeti.HTools.JSON @@ -134,20 +130,6 @@ select :: a -- ^ default result -> a -- ^ first result which has a True condition, or default select def = maybe def snd . find fst --- | Annotate a Result with an ownership information. -annotateResult :: String -> Result a -> Result a -annotateResult owner (Bad s) = Bad $ owner ++ ": " ++ s -annotateResult _ v = v - --- | Try to extract a key from a object with better error reporting --- than fromObj. -tryFromObj :: (J.JSON a) => - String -- ^ Textual "owner" in error messages - -> JSRecord -- ^ The object array - -> String -- ^ The desired key from the object - -> Result a -tryFromObj t o = annotateResult t . fromObj o - -- * Parsing utility functions @@ -182,10 +164,6 @@ formatTable vals numpos = ) (zip3 vtrans numpos mlens) in transpose expnd --- | Default group UUID (just a string, not a real UUID). -defaultGroupID :: GroupID -defaultGroupID = "00000000-0000-0000-0000-000000000000" - -- | Tries to extract number and scale from the given string. -- -- Input must be in the format NUMBER+ SPACE* [UNIT]. If no unit is -- GitLab