Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / Types.hs @ 6d558717

History | View | Annotate | Download (8.9 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
  , InstCreateMode(..)
64
  ) where
65

    
66
import qualified Text.JSON as JSON
67

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

    
72
-- * Generic types
73

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

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

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

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

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

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

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

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

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

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

    
118
-- * Ganeti types
119

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

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

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

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

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

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

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

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

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

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

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

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

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

    
264
-- | The instance create mode.
265
$(THH.declareSADT "InstCreateMode"
266
  [ ("InstCreate",       'C.instanceCreate)
267
  , ("InstImport",       'C.instanceImport)
268
  , ("InstRemoteImport", 'C.instanceRemoteImport)
269
  ])
270
$(THH.makeJSONInstance ''InstCreateMode)