Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Locking / Locks.hs @ 15a53b1e

History | View | Annotate | Download (1.9 kB)

1
{-| Ganeti lock structure
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2014 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.Locking.Locks
27
  ( GanetiLocks(..)
28
  , GanetiLockAllocation
29
  ) where
30

    
31
import Control.Monad (liftM)
32
import qualified Text.JSON as J
33

    
34
import Ganeti.JSON (asJSObject, fromObj)
35
import Ganeti.Locking.Allocation
36
import Ganeti.Locking.Types
37
import Ganeti.Types
38

    
39
-- | The type of Locks available in Ganeti. The order of this type
40
-- is the lock oder.
41
data GanetiLocks = BGL deriving (Ord, Eq, Show)
42
-- TODO: add the remaining locks
43

    
44
-- | Describe the parts the pieces of information that are needed to
45
-- describe the lock.
46
lockData :: GanetiLocks -> [(String, J.JSValue)]
47
lockData BGL = [("type", J.showJSON "BGL")]
48

    
49
-- | Read a lock form its JSON representation.
50
readLock :: J.JSValue -> J.Result GanetiLocks
51
readLock v =  do
52
  fields <- liftM J.fromJSObject $ asJSObject v
53
  tp <- fromObj fields "type"
54
  case tp of
55
    "BGL" -> return BGL
56
    _ -> fail $ "Unknown lock type " ++ tp
57

    
58
instance J.JSON GanetiLocks where
59
  showJSON = J.JSObject . J.toJSObject . lockData
60
  readJSON = readLock
61

    
62

    
63
instance Lock GanetiLocks where
64
  lockImplications BGL = []
65

    
66
-- | The type of lock Allocations in Ganeti. In Ganeti, the owner of
67
-- locks are jobs.
68
type GanetiLockAllocation = LockAllocation GanetiLocks JobId