Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / OpCodes.hs @ 3bebda52

History | View | Annotate | Download (4 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Implementation of the opcodes.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
10

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

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

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

    
26
-}
27

    
28
module Ganeti.OpCodes
29
  ( OpCode(..)
30
  , TagObject(..)
31
  , ReplaceDisksMode(..)
32
  , DiskIndex
33
  , mkDiskIndex
34
  , unDiskIndex
35
  , opID
36
  , allOpIDs
37
  ) where
38

    
39
import Text.JSON (readJSON, showJSON, makeObj, JSON)
40

    
41
import qualified Ganeti.Constants as C
42
import Ganeti.THH
43

    
44
import Ganeti.JSON
45

    
46
-- | Data type representing what items do the tag operations apply to.
47
$(declareSADT "TagObject"
48
  [ ("TagInstance", 'C.tagInstance)
49
  , ("TagNode",     'C.tagNode)
50
  , ("TagGroup",    'C.tagNodegroup)
51
  , ("TagCluster",  'C.tagCluster)
52
  ])
53
$(makeJSONInstance ''TagObject)
54

    
55
-- | Replace disks type.
56
$(declareSADT "ReplaceDisksMode"
57
  [ ("ReplaceOnPrimary",    'C.replaceDiskPri)
58
  , ("ReplaceOnSecondary",  'C.replaceDiskSec)
59
  , ("ReplaceNewSecondary", 'C.replaceDiskChg)
60
  , ("ReplaceAuto",         'C.replaceDiskAuto)
61
  ])
62
$(makeJSONInstance ''ReplaceDisksMode)
63

    
64
-- | Disk index type (embedding constraints on the index value via a
65
-- smart constructor).
66
newtype DiskIndex = DiskIndex { unDiskIndex :: Int }
67
  deriving (Show, Read, Eq, Ord)
68

    
69
-- | Smart constructor for 'DiskIndex'.
70
mkDiskIndex :: (Monad m) => Int -> m DiskIndex
71
mkDiskIndex i | i >= 0 && i < C.maxDisks = return (DiskIndex i)
72
              | otherwise = fail $ "Invalid value for disk index '" ++
73
                            show i ++ "', required between 0 and " ++
74
                            show C.maxDisks
75

    
76
instance JSON DiskIndex where
77
  readJSON v = readJSON v >>= mkDiskIndex
78
  showJSON = showJSON . unDiskIndex
79

    
80
-- | OpCode representation.
81
--
82
-- We only implement a subset of Ganeti opcodes: those which are actually used
83
-- in the htools codebase.
84
$(genOpCode "OpCode"
85
  [ ("OpTestDelay",
86
     [ simpleField "duration"  [t| Double   |]
87
     , simpleField "on_master" [t| Bool     |]
88
     , simpleField "on_nodes"  [t| [String] |]
89
     ])
90
  , ("OpInstanceReplaceDisks",
91
     [ simpleField "instance_name" [t| String |]
92
     , optionalField $ simpleField "remote_node" [t| String |]
93
     , simpleField "mode"  [t| ReplaceDisksMode |]
94
     , simpleField "disks" [t| [DiskIndex] |]
95
     , optionalField $ simpleField "iallocator" [t| String |]
96
     ])
97
  , ("OpInstanceFailover",
98
     [ simpleField "instance_name"      [t| String |]
99
     , simpleField "ignore_consistency" [t| Bool   |]
100
     , optionalField $ simpleField "target_node" [t| String |]
101
     ])
102
  , ("OpInstanceMigrate",
103
     [ simpleField "instance_name"  [t| String |]
104
     , simpleField "live"           [t| Bool   |]
105
     , simpleField "cleanup"        [t| Bool   |]
106
     , defaultField [| False |] $ simpleField "allow_failover" [t| Bool |]
107
     , optionalField $ simpleField "target_node" [t| String |]
108
     ])
109
  , ("OpTagsSet",
110
     [ simpleField "kind" [t| TagObject |]
111
     , simpleField "tags" [t| [String]  |]
112
     , optionalNullSerField $ simpleField "name" [t| String |]
113
     ])
114
  , ("OpTagsDel",
115
     [ simpleField "kind" [t| TagObject |]
116
     , simpleField "tags" [t| [String]  |]
117
     , optionalNullSerField $ simpleField "name" [t| String |]
118
     ])
119
  ])
120

    
121
-- | Returns the OP_ID for a given opcode value.
122
$(genOpID ''OpCode "opID")
123

    
124
-- | A list of all defined/supported opcode IDs.
125
$(genAllOpIDs ''OpCode "allOpIDs")
126

    
127
instance JSON OpCode where
128
  readJSON = loadOpCode
129
  showJSON = saveOpCode