Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / WConfd / Language.hs @ 62f1e053

History | View | Annotate | Download (2.4 kB)

1
{-| Function related to serialisation of WConfD requests
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2013 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Ganeti.WConfd.Language
27
  ( LockRequestType(..)
28
  , GanetiLockRequest
29
  , fromGanetiLockRequest
30
  ) where
31

    
32
import qualified Text.JSON as J
33

    
34
import Ganeti.Locking.Allocation
35
import Ganeti.Locking.Locks (GanetiLocks)
36

    
37
-- * Serialisation related to locking
38

    
39
-- | Operation to be carried out on a lock (request exclusive/shared ownership,
40
-- or release).
41
data LockRequestType = ReqExclusive | ReqShared | ReqRelease deriving (Eq, Show)
42

    
43
instance J.JSON LockRequestType where
44
  showJSON ReqExclusive = J.showJSON "exclusive"
45
  showJSON ReqShared = J.showJSON "shared"
46
  showJSON ReqRelease = J.showJSON "release"
47
  readJSON (J.JSString x) = let s = J.fromJSString x
48
                            in case s of
49
                              "exclusive" -> J.Ok ReqExclusive
50
                              "shared" -> J.Ok ReqShared
51
                              "release" -> J.Ok ReqRelease
52
                              _ -> J.Error $ "Unknown lock update request " ++ s
53
  readJSON _ = J.Error "Update requests need to be strings"
54

    
55
-- | The type describing how lock update requests are passed over the wire.
56
type GanetiLockRequest = [(GanetiLocks, LockRequestType)]
57

    
58
-- | Transform a Lock LockReqeustType pair into a LockRequest.
59
toLockRequest :: (GanetiLocks, LockRequestType) -> LockRequest GanetiLocks
60
toLockRequest (a, ReqExclusive) = requestExclusive a
61
toLockRequest (a, ReqShared) = requestShared a
62
toLockRequest (a, ReqRelease) = requestRelease a
63

    
64
-- | From a GanetiLockRequest obtain a list of
65
-- Ganeti.Lock.Allocation.LockRequest, suitable to updateLocks.
66
fromGanetiLockRequest :: GanetiLockRequest -> [LockRequest GanetiLocks]
67
fromGanetiLockRequest = map toLockRequest