Factor out the getInstances function
authorMichele Tartara <mtartara@google.com>
Wed, 26 Jun 2013 08:02:13 +0000 (10:02 +0200)
committerMichele Tartara <mtartara@google.com>
Thu, 4 Jul 2013 17:02:22 +0000 (19:02 +0200)
The getInstances function can be useful in general, but is defined inside
the InstStatus data collector. This commit takes it out and adds it to
a proper (newly created) library.

Signed-off-by: Michele Tartara <mtartara@google.com>
Reviewed-by: Klaus Aehlig <aehlig@google.com>

Makefile.am
src/Ganeti/Confd/ClientFunctions.hs [new file with mode: 0644]
src/Ganeti/DataCollectors/InstStatus.hs

index 7d0f87c..64109a8 100644 (file)
@@ -542,6 +542,7 @@ HS_LIB_SRCS = \
        src/Ganeti/Common.hs \
        src/Ganeti/Compat.hs \
        src/Ganeti/Confd/Client.hs \
+       src/Ganeti/Confd/ClientFunctions.hs \
        src/Ganeti/Confd/Server.hs \
        src/Ganeti/Confd/Types.hs \
        src/Ganeti/Confd/Utils.hs \
diff --git a/src/Ganeti/Confd/ClientFunctions.hs b/src/Ganeti/Confd/ClientFunctions.hs
new file mode 100644 (file)
index 0000000..03f8e43
--- /dev/null
@@ -0,0 +1,53 @@
+{-| Some utility functions, based on the Confd client, providing data
+ in a ready-to-use way.
+-}
+
+{-
+
+Copyright (C) 2013 Google Inc.
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA.
+
+-}
+
+module Ganeti.Confd.ClientFunctions
+  ( getInstances
+  ) where
+
+import qualified Text.JSON as J
+
+import Ganeti.BasicTypes as BT
+import Ganeti.Confd.Types
+import Ganeti.Confd.Client
+import Ganeti.Objects
+
+
+-- | Get the list of instances the given node is ([primary], [secondary]) for.
+-- The server address and the server port parameters are mainly intended
+-- for testing purposes. If they are Nothing, the default values will be used.
+getInstances
+  :: String
+  -> Maybe String
+  -> Maybe Int
+  -> IO (BT.Result ([Ganeti.Objects.Instance], [Ganeti.Objects.Instance]))
+getInstances node srvAddr srvPort = do
+  client <- getConfdClient srvAddr srvPort
+  reply <- query client ReqNodeInstances $ PlainQuery node
+  return $
+    case fmap (J.readJSON . confdReplyAnswer) reply of
+      Just (J.Ok instances) -> BT.Ok instances
+      Just (J.Error msg) -> BT.Bad msg
+      Nothing -> BT.Bad "No answer from the Confd server"
index ec60c0b..65fc20c 100644 (file)
@@ -43,9 +43,7 @@ import qualified Data.Map as Map
 import Network.BSD (getHostName)
 import qualified Text.JSON as J
 
-import qualified Ganeti.BasicTypes as BT
-import Ganeti.Confd.Client
-import Ganeti.Confd.Types
+import Ganeti.Confd.ClientFunctions
 import Ganeti.Common
 import Ganeti.DataCollectors.CLI
 import Ganeti.DataCollectors.InstStatusTypes
@@ -95,26 +93,6 @@ options = return
 arguments :: [ArgCompletion]
 arguments = []
 
--- | Get the list of instances ([primary], [secondary]) on the given node.
--- Implemented as a function, even if used a single time, to specify in a
--- convenient and elegant way the return data type, required in order to
--- prevent incurring in the monomorphism restriction.
--- The server address and the server port parameters are mainly intended
--- for testing purposes. If they are Nothing, the default values will be used.
-getInstances
-  :: String
-  -> Maybe String
-  -> Maybe Int
-  -> IO (BT.Result ([Ganeti.Objects.Instance], [Ganeti.Objects.Instance]))
-getInstances node srvAddr srvPort = do
-  client <- getConfdClient srvAddr srvPort
-  reply <- query client ReqNodeInstances $ PlainQuery node
-  return $
-    case fmap (J.readJSON . confdReplyAnswer) reply of
-      Just (J.Ok instances) -> BT.Ok instances
-      Just (J.Error msg) -> BT.Bad msg
-      Nothing -> BT.Bad "No answer from the Confd server"
-
 -- | Try to get the reason trail for an instance. In case it is not possible,
 -- log the failure and return an empty list instead.
 getReasonTrail :: String -> IO ReasonTrail