Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Types.hs @ c65621d7

History | View | Annotate | Download (8.6 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Some common Ganeti types.
4

    
5
This holds types common to both core work, and to htools. Types that
6
are very core specific (e.g. configuration objects) should go in
7
'Ganeti.Objects', while types that are specific to htools in-memory
8
representation should go into 'Ganeti.HTools.Types'.
9

    
10
-}
11

    
12
{-
13

    
14
Copyright (C) 2012 Google Inc.
15

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

    
21
This program is distributed in the hope that it will be useful, but
22
WITHOUT ANY WARRANTY; without even the implied warranty of
23
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
24
General Public License for more details.
25

    
26
You should have received a copy of the GNU General Public License
27
along with this program; if not, write to the Free Software
28
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29
02110-1301, USA.
30

    
31
-}
32

    
33
module Ganeti.Types
34
  ( AllocPolicy(..)
35
  , allocPolicyFromRaw
36
  , allocPolicyToRaw
37
  , InstanceStatus(..)
38
  , instanceStatusFromRaw
39
  , instanceStatusToRaw
40
  , DiskTemplate(..)
41
  , diskTemplateToRaw
42
  , diskTemplateFromRaw
43
  , NonNegative
44
  , fromNonNegative
45
  , mkNonNegative
46
  , Positive
47
  , fromPositive
48
  , mkPositive
49
  , NonEmpty
50
  , fromNonEmpty
51
  , mkNonEmpty
52
  , NonEmptyString
53
  , MigrationMode(..)
54
  , VerifyOptionalChecks(..)
55
  , DdmSimple(..)
56
  , CVErrorCode(..)
57
  , cVErrorCodeToRaw
58
  , Hypervisor(..)
59
  , OobCommand(..)
60
  , StorageType(..)
61
  , NodeEvacMode(..)
62
  , FileDriver(..)
63
  ) where
64

    
65
import qualified Text.JSON as JSON
66

    
67
import qualified Ganeti.Constants as C
68
import qualified Ganeti.THH as THH
69
import Ganeti.JSON
70

    
71
-- * Generic types
72

    
73
-- | Type that holds a non-negative value.
74
newtype NonNegative a = NonNegative { fromNonNegative :: a }
75
  deriving (Show, Read, Eq)
76

    
77
-- | Smart constructor for 'NonNegative'.
78
mkNonNegative :: (Monad m, Num a, Ord a, Show a) => a -> m (NonNegative a)
79
mkNonNegative i | i >= 0 = return (NonNegative i)
80
                | otherwise = fail $ "Invalid value for non-negative type '" ++
81
                              show i ++ "'"
82

    
83
instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (NonNegative a) where
84
  showJSON = JSON.showJSON . fromNonNegative
85
  readJSON v = JSON.readJSON v >>= mkNonNegative
86

    
87
-- | Type that holds a positive value.
88
newtype Positive a = Positive { fromPositive :: a }
89
  deriving (Show, Read, Eq)
90

    
91
-- | Smart constructor for 'Positive'.
92
mkPositive :: (Monad m, Num a, Ord a, Show a) => a -> m (Positive a)
93
mkPositive i | i > 0 = return (Positive i)
94
             | otherwise = fail $ "Invalid value for positive type '" ++
95
                           show i ++ "'"
96

    
97
instance (JSON.JSON a, Num a, Ord a, Show a) => JSON.JSON (Positive a) where
98
  showJSON = JSON.showJSON . fromPositive
99
  readJSON v = JSON.readJSON v >>= mkPositive
100

    
101
-- | Type that holds a non-null list.
102
newtype NonEmpty a = NonEmpty { fromNonEmpty :: [a] }
103
  deriving (Show, Read, Eq)
104

    
105
-- | Smart constructor for 'NonEmpty'.
106
mkNonEmpty :: (Monad m) => [a] -> m (NonEmpty a)
107
mkNonEmpty [] = fail "Received empty value for non-empty list"
108
mkNonEmpty xs = return (NonEmpty xs)
109

    
110
instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
111
  showJSON = JSON.showJSON . fromNonEmpty
112
  readJSON v = JSON.readJSON v >>= mkNonEmpty
113

    
114
-- | A simple type alias for non-empty strings.
115
type NonEmptyString = NonEmpty Char
116

    
117
-- * Ganeti types
118

    
119
-- | Instance disk template type.
120
$(THH.declareSADT "DiskTemplate"
121
       [ ("DTDiskless",   'C.dtDiskless)
122
       , ("DTFile",       'C.dtFile)
123
       , ("DTSharedFile", 'C.dtSharedFile)
124
       , ("DTPlain",      'C.dtPlain)
125
       , ("DTBlock",      'C.dtBlock)
126
       , ("DTDrbd8",      'C.dtDrbd8)
127
       , ("DTRbd",        'C.dtRbd)
128
       ])
129
$(THH.makeJSONInstance ''DiskTemplate)
130

    
131
instance HasStringRepr DiskTemplate where
132
  fromStringRepr = diskTemplateFromRaw
133
  toStringRepr = diskTemplateToRaw
134

    
135
-- | The Group allocation policy type.
136
--
137
-- Note that the order of constructors is important as the automatic
138
-- Ord instance will order them in the order they are defined, so when
139
-- changing this data type be careful about the interaction with the
140
-- desired sorting order.
141
$(THH.declareSADT "AllocPolicy"
142
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
143
       , ("AllocLastResort",  'C.allocPolicyLastResort)
144
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
145
       ])
146
$(THH.makeJSONInstance ''AllocPolicy)
147

    
148
-- | The Instance real state type. FIXME: this could be improved to
149
-- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
150
$(THH.declareSADT "InstanceStatus"
151
       [ ("StatusDown",    'C.inststAdmindown)
152
       , ("StatusOffline", 'C.inststAdminoffline)
153
       , ("ErrorDown",     'C.inststErrordown)
154
       , ("ErrorUp",       'C.inststErrorup)
155
       , ("NodeDown",      'C.inststNodedown)
156
       , ("NodeOffline",   'C.inststNodeoffline)
157
       , ("Running",       'C.inststRunning)
158
       , ("WrongNode",     'C.inststWrongnode)
159
       ])
160
$(THH.makeJSONInstance ''InstanceStatus)
161

    
162
-- | Migration mode.
163
$(THH.declareSADT "MigrationMode"
164
     [ ("MigrationLive",    'C.htMigrationLive)
165
     , ("MigrationNonLive", 'C.htMigrationNonlive)
166
     ])
167
$(THH.makeJSONInstance ''MigrationMode)
168

    
169
-- | Verify optional checks.
170
$(THH.declareSADT "VerifyOptionalChecks"
171
     [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
172
     ])
173
$(THH.makeJSONInstance ''VerifyOptionalChecks)
174

    
175
-- | Cluster verify error codes.
176
$(THH.declareSADT "CVErrorCode"
177
  [ ("CvECLUSTERCFG",           'C.cvEclustercfgCode)
178
  , ("CvECLUSTERCERT",          'C.cvEclustercertCode)
179
  , ("CvECLUSTERFILECHECK",     'C.cvEclusterfilecheckCode)
180
  , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
181
  , ("CvECLUSTERDANGLINGINST",  'C.cvEclusterdanglinginstCode)
182
  , ("CvEINSTANCEBADNODE",      'C.cvEinstancebadnodeCode)
183
  , ("CvEINSTANCEDOWN",         'C.cvEinstancedownCode)
184
  , ("CvEINSTANCELAYOUT",       'C.cvEinstancelayoutCode)
185
  , ("CvEINSTANCEMISSINGDISK",  'C.cvEinstancemissingdiskCode)
186
  , ("CvEINSTANCEFAULTYDISK",   'C.cvEinstancefaultydiskCode)
187
  , ("CvEINSTANCEWRONGNODE",    'C.cvEinstancewrongnodeCode)
188
  , ("CvEINSTANCESPLITGROUPS",  'C.cvEinstancesplitgroupsCode)
189
  , ("CvEINSTANCEPOLICY",       'C.cvEinstancepolicyCode)
190
  , ("CvENODEDRBD",             'C.cvEnodedrbdCode)
191
  , ("CvENODEDRBDHELPER",       'C.cvEnodedrbdhelperCode)
192
  , ("CvENODEFILECHECK",        'C.cvEnodefilecheckCode)
193
  , ("CvENODEHOOKS",            'C.cvEnodehooksCode)
194
  , ("CvENODEHV",               'C.cvEnodehvCode)
195
  , ("CvENODELVM",              'C.cvEnodelvmCode)
196
  , ("CvENODEN1",               'C.cvEnoden1Code)
197
  , ("CvENODENET",              'C.cvEnodenetCode)
198
  , ("CvENODEOS",               'C.cvEnodeosCode)
199
  , ("CvENODEORPHANINSTANCE",   'C.cvEnodeorphaninstanceCode)
200
  , ("CvENODEORPHANLV",         'C.cvEnodeorphanlvCode)
201
  , ("CvENODERPC",              'C.cvEnoderpcCode)
202
  , ("CvENODESSH",              'C.cvEnodesshCode)
203
  , ("CvENODEVERSION",          'C.cvEnodeversionCode)
204
  , ("CvENODESETUP",            'C.cvEnodesetupCode)
205
  , ("CvENODETIME",             'C.cvEnodetimeCode)
206
  , ("CvENODEOOBPATH",          'C.cvEnodeoobpathCode)
207
  , ("CvENODEUSERSCRIPTS",      'C.cvEnodeuserscriptsCode)
208
  , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
209
  ])
210
$(THH.makeJSONInstance ''CVErrorCode)
211

    
212
-- | Dynamic device modification, just add\/remove version.
213
$(THH.declareSADT "DdmSimple"
214
     [ ("DdmSimpleAdd",    'C.ddmAdd)
215
     , ("DdmSimpleRemove", 'C.ddmRemove)
216
     ])
217
$(THH.makeJSONInstance ''DdmSimple)
218

    
219
-- | Hypervisor type definitions.
220
$(THH.declareSADT "Hypervisor"
221
  [ ( "Kvm",    'C.htKvm )
222
  , ( "XenPvm", 'C.htXenPvm )
223
  , ( "Chroot", 'C.htChroot )
224
  , ( "XenHvm", 'C.htXenHvm )
225
  , ( "Lxc",    'C.htLxc )
226
  , ( "Fake",   'C.htFake )
227
  ])
228
$(THH.makeJSONInstance ''Hypervisor)
229

    
230
-- | Oob command type.
231
$(THH.declareSADT "OobCommand"
232
  [ ("OobHealth",      'C.oobHealth)
233
  , ("OobPowerCycle",  'C.oobPowerCycle)
234
  , ("OobPowerOff",    'C.oobPowerOff)
235
  , ("OobPowerOn",     'C.oobPowerOn)
236
  , ("OobPowerStatus", 'C.oobPowerStatus)
237
  ])
238
$(THH.makeJSONInstance ''OobCommand)
239

    
240
-- | Storage type.
241
$(THH.declareSADT "StorageType"
242
  [ ("StorageFile", 'C.stFile)
243
  , ("StorageLvmPv", 'C.stLvmPv)
244
  , ("StorageLvmVg", 'C.stLvmVg)
245
  ])
246
$(THH.makeJSONInstance ''StorageType)
247

    
248
-- | Node evac modes.
249
$(THH.declareSADT "NodeEvacMode"
250
  [ ("NEvacPrimary",   'C.iallocatorNevacPri)
251
  , ("NEvacSecondary", 'C.iallocatorNevacSec)
252
  , ("NEvacAll",       'C.iallocatorNevacAll)
253
  ])
254
$(THH.makeJSONInstance ''NodeEvacMode)
255

    
256
-- | The file driver type.
257
$(THH.declareSADT "FileDriver"
258
  [ ("FileLoop",   'C.fdLoop)
259
  , ("FileBlktap", 'C.fdBlktap)
260
  ])
261
$(THH.makeJSONInstance ''FileDriver)