Prefer the UuidObject type class over specific functions
[ganeti-github.git] / src / Ganeti / Confd / ClientFunctions.hs
1 {-| Some utility functions, based on the Confd client, providing data
2 in a ready-to-use way.
3 -}
4
5 {-
6
7 Copyright (C) 2013 Google Inc.
8 All rights reserved.
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are
12 met:
13
14 1. Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16
17 2. Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
25 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 -}
34
35 module Ganeti.Confd.ClientFunctions
36 ( getInstances
37 , getInstanceDisks
38 ) where
39
40 import Control.Monad (liftM)
41 import qualified Text.JSON as J
42
43 import Ganeti.BasicTypes as BT
44 import Ganeti.Confd.Types
45 import Ganeti.Confd.Client
46 import Ganeti.Objects
47
48
49 -- | Get the list of instances the given node is ([primary], [secondary]) for.
50 -- The server address and the server port parameters are mainly intended
51 -- for testing purposes. If they are Nothing, the default values will be used.
52 getInstances
53 :: String
54 -> Maybe String
55 -> Maybe Int
56 -> BT.ResultT String IO ([Ganeti.Objects.Instance], [Ganeti.Objects.Instance])
57 getInstances node srvAddr srvPort = do
58 client <- liftIO $ getConfdClient srvAddr srvPort
59 reply <- liftIO . query client ReqNodeInstances $ PlainQuery node
60 case fmap (J.readJSON . confdReplyAnswer) reply of
61 Just (J.Ok instances) -> return instances
62 Just (J.Error msg) -> fail msg
63 Nothing -> fail "No answer from the Confd server"
64
65 -- | Get the list of disks that belong to a given instance
66 -- The server address and the server port parameters are mainly intended
67 -- for testing purposes. If they are Nothing, the default values will be used.
68 getDisks
69 :: Ganeti.Objects.Instance
70 -> Maybe String
71 -> Maybe Int
72 -> BT.ResultT String IO [Ganeti.Objects.Disk]
73 getDisks inst srvAddr srvPort = do
74 client <- liftIO $ getConfdClient srvAddr srvPort
75 reply <- liftIO . query client ReqInstanceDisks . PlainQuery . uuidOf $ inst
76 case fmap (J.readJSON . confdReplyAnswer) reply of
77 Just (J.Ok disks) -> return disks
78 Just (J.Error msg) -> fail msg
79 Nothing -> fail "No answer from the Confd server"
80
81 -- | Get the list of instances on the given node along with their disks
82 -- The server address and the server port parameters are mainly intended
83 -- for testing purposes. If they are Nothing, the default values will be used.
84 getInstanceDisks
85 :: String
86 -> Maybe String
87 -> Maybe Int
88 -> BT.ResultT String IO [(Ganeti.Objects.Instance, [Ganeti.Objects.Disk])]
89 getInstanceDisks node srvAddr srvPort =
90 liftM (uncurry (++)) (getInstances node srvAddr srvPort) >>=
91 mapM (\i -> liftM ((,) i) (getDisks i srvAddr srvPort))