Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / WConfd / Language.hs @ f3010b75

History | View | Annotate | Download (2.4 kB)

1 62f1e053 Klaus Aehlig
{-| Function related to serialisation of WConfD requests
2 62f1e053 Klaus Aehlig
3 62f1e053 Klaus Aehlig
-}
4 62f1e053 Klaus Aehlig
5 62f1e053 Klaus Aehlig
{-
6 62f1e053 Klaus Aehlig
7 62f1e053 Klaus Aehlig
Copyright (C) 2013 Google Inc.
8 62f1e053 Klaus Aehlig
9 62f1e053 Klaus Aehlig
This program is free software; you can redistribute it and/or modify
10 62f1e053 Klaus Aehlig
it under the terms of the GNU General Public License as published by
11 62f1e053 Klaus Aehlig
the Free Software Foundation; either version 2 of the License, or
12 62f1e053 Klaus Aehlig
(at your option) any later version.
13 62f1e053 Klaus Aehlig
14 62f1e053 Klaus Aehlig
This program is distributed in the hope that it will be useful, but
15 62f1e053 Klaus Aehlig
WITHOUT ANY WARRANTY; without even the implied warranty of
16 62f1e053 Klaus Aehlig
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 62f1e053 Klaus Aehlig
General Public License for more details.
18 62f1e053 Klaus Aehlig
19 62f1e053 Klaus Aehlig
You should have received a copy of the GNU General Public License
20 62f1e053 Klaus Aehlig
along with this program; if not, write to the Free Software
21 62f1e053 Klaus Aehlig
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 62f1e053 Klaus Aehlig
02110-1301, USA.
23 62f1e053 Klaus Aehlig
24 62f1e053 Klaus Aehlig
-}
25 62f1e053 Klaus Aehlig
26 62f1e053 Klaus Aehlig
module Ganeti.WConfd.Language
27 62f1e053 Klaus Aehlig
  ( LockRequestType(..)
28 62f1e053 Klaus Aehlig
  , GanetiLockRequest
29 62f1e053 Klaus Aehlig
  , fromGanetiLockRequest
30 62f1e053 Klaus Aehlig
  ) where
31 62f1e053 Klaus Aehlig
32 62f1e053 Klaus Aehlig
import qualified Text.JSON as J
33 62f1e053 Klaus Aehlig
34 62f1e053 Klaus Aehlig
import Ganeti.Locking.Allocation
35 62f1e053 Klaus Aehlig
import Ganeti.Locking.Locks (GanetiLocks)
36 62f1e053 Klaus Aehlig
37 62f1e053 Klaus Aehlig
-- * Serialisation related to locking
38 62f1e053 Klaus Aehlig
39 62f1e053 Klaus Aehlig
-- | Operation to be carried out on a lock (request exclusive/shared ownership,
40 62f1e053 Klaus Aehlig
-- or release).
41 62f1e053 Klaus Aehlig
data LockRequestType = ReqExclusive | ReqShared | ReqRelease deriving (Eq, Show)
42 62f1e053 Klaus Aehlig
43 62f1e053 Klaus Aehlig
instance J.JSON LockRequestType where
44 62f1e053 Klaus Aehlig
  showJSON ReqExclusive = J.showJSON "exclusive"
45 62f1e053 Klaus Aehlig
  showJSON ReqShared = J.showJSON "shared"
46 62f1e053 Klaus Aehlig
  showJSON ReqRelease = J.showJSON "release"
47 62f1e053 Klaus Aehlig
  readJSON (J.JSString x) = let s = J.fromJSString x
48 62f1e053 Klaus Aehlig
                            in case s of
49 62f1e053 Klaus Aehlig
                              "exclusive" -> J.Ok ReqExclusive
50 62f1e053 Klaus Aehlig
                              "shared" -> J.Ok ReqShared
51 62f1e053 Klaus Aehlig
                              "release" -> J.Ok ReqRelease
52 62f1e053 Klaus Aehlig
                              _ -> J.Error $ "Unknown lock update request " ++ s
53 62f1e053 Klaus Aehlig
  readJSON _ = J.Error "Update requests need to be strings"
54 62f1e053 Klaus Aehlig
55 62f1e053 Klaus Aehlig
-- | The type describing how lock update requests are passed over the wire.
56 62f1e053 Klaus Aehlig
type GanetiLockRequest = [(GanetiLocks, LockRequestType)]
57 62f1e053 Klaus Aehlig
58 62f1e053 Klaus Aehlig
-- | Transform a Lock LockReqeustType pair into a LockRequest.
59 62f1e053 Klaus Aehlig
toLockRequest :: (GanetiLocks, LockRequestType) -> LockRequest GanetiLocks
60 62f1e053 Klaus Aehlig
toLockRequest (a, ReqExclusive) = requestExclusive a
61 62f1e053 Klaus Aehlig
toLockRequest (a, ReqShared) = requestShared a
62 62f1e053 Klaus Aehlig
toLockRequest (a, ReqRelease) = requestRelease a
63 62f1e053 Klaus Aehlig
64 62f1e053 Klaus Aehlig
-- | From a GanetiLockRequest obtain a list of
65 62f1e053 Klaus Aehlig
-- Ganeti.Lock.Allocation.LockRequest, suitable to updateLocks.
66 62f1e053 Klaus Aehlig
fromGanetiLockRequest :: GanetiLockRequest -> [LockRequest GanetiLocks]
67 62f1e053 Klaus Aehlig
fromGanetiLockRequest = map toLockRequest