Types.hs 7.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{-# LANGUAGE TemplateHaskell #-}

{-| Some common Ganeti types.

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

-}

{-

Copyright (C) 2012 Google Inc.

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

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

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

-}

module Ganeti.Types
  ( AllocPolicy(..)
  , allocPolicyFromRaw
  , allocPolicyToRaw
  , InstanceStatus(..)
  , instanceStatusFromRaw
  , instanceStatusToRaw
  , DiskTemplate(..)
  , diskTemplateToRaw
  , diskTemplateFromRaw
Iustin Pop's avatar
Iustin Pop committed
43
44
45
46
47
48
49
50
51
  , NonNegative
  , fromNonNegative
  , mkNonNegative
  , Positive
  , fromPositive
  , mkPositive
  , NonEmpty
  , fromNonEmpty
  , mkNonEmpty
Iustin Pop's avatar
Iustin Pop committed
52
53
54
55
56
  , MigrationMode(..)
  , VerifyOptionalChecks(..)
  , DdmSimple(..)
  , CVErrorCode(..)
  , cVErrorCodeToRaw
57
58
  ) where

Iustin Pop's avatar
Iustin Pop committed
59
60
import qualified Text.JSON as JSON

61
62
import qualified Ganeti.Constants as C
import qualified Ganeti.THH as THH
63
import Ganeti.JSON
64

Iustin Pop's avatar
Iustin Pop committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
-- * Generic types

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

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

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

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

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

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

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

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

instance (JSON.JSON a) => JSON.JSON (NonEmpty a) where
  showJSON = JSON.showJSON . fromNonEmpty
  readJSON v = JSON.readJSON v >>= mkNonEmpty

-- * Ganeti types

110
111
112
113
114
115
116
117
118
119
120
121
-- | Instance disk template type.
$(THH.declareSADT "DiskTemplate"
       [ ("DTDiskless",   'C.dtDiskless)
       , ("DTFile",       'C.dtFile)
       , ("DTSharedFile", 'C.dtSharedFile)
       , ("DTPlain",      'C.dtPlain)
       , ("DTBlock",      'C.dtBlock)
       , ("DTDrbd8",      'C.dtDrbd8)
       , ("DTRbd",        'C.dtRbd)
       ])
$(THH.makeJSONInstance ''DiskTemplate)

122
123
124
125
instance HasStringRepr DiskTemplate where
  fromStringRepr = diskTemplateFromRaw
  toStringRepr = diskTemplateToRaw

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

-- | The Instance real state type. FIXME: this could be improved to
-- just wrap a /NormalState AdminStatus | ErrorState ErrorCondition/.
$(THH.declareSADT "InstanceStatus"
       [ ("StatusDown",    'C.inststAdmindown)
       , ("StatusOffline", 'C.inststAdminoffline)
       , ("ErrorDown",     'C.inststErrordown)
       , ("ErrorUp",       'C.inststErrorup)
       , ("NodeDown",      'C.inststNodedown)
       , ("NodeOffline",   'C.inststNodeoffline)
       , ("Running",       'C.inststRunning)
       , ("WrongNode",     'C.inststWrongnode)
       ])
$(THH.makeJSONInstance ''InstanceStatus)
Iustin Pop's avatar
Iustin Pop committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208

-- | Migration mode.
$(THH.declareSADT "MigrationMode"
     [ ("MigrationLive",    'C.htMigrationLive)
     , ("MigrationNonLive", 'C.htMigrationNonlive)
     ])
$(THH.makeJSONInstance ''MigrationMode)

-- | Verify optional checks.
$(THH.declareSADT "VerifyOptionalChecks"
     [ ("VerifyNPlusOneMem", 'C.verifyNplusoneMem)
     ])
$(THH.makeJSONInstance ''VerifyOptionalChecks)

-- | Cluster verify error codes.
$(THH.declareSADT "CVErrorCode"
  [ ("CvECLUSTERCFG",           'C.cvEclustercfgCode)
  , ("CvECLUSTERCERT",          'C.cvEclustercertCode)
  , ("CvECLUSTERFILECHECK",     'C.cvEclusterfilecheckCode)
  , ("CvECLUSTERDANGLINGNODES", 'C.cvEclusterdanglingnodesCode)
  , ("CvECLUSTERDANGLINGINST",  'C.cvEclusterdanglinginstCode)
  , ("CvEINSTANCEBADNODE",      'C.cvEinstancebadnodeCode)
  , ("CvEINSTANCEDOWN",         'C.cvEinstancedownCode)
  , ("CvEINSTANCELAYOUT",       'C.cvEinstancelayoutCode)
  , ("CvEINSTANCEMISSINGDISK",  'C.cvEinstancemissingdiskCode)
  , ("CvEINSTANCEFAULTYDISK",   'C.cvEinstancefaultydiskCode)
  , ("CvEINSTANCEWRONGNODE",    'C.cvEinstancewrongnodeCode)
  , ("CvEINSTANCESPLITGROUPS",  'C.cvEinstancesplitgroupsCode)
  , ("CvEINSTANCEPOLICY",       'C.cvEinstancepolicyCode)
  , ("CvENODEDRBD",             'C.cvEnodedrbdCode)
  , ("CvENODEDRBDHELPER",       'C.cvEnodedrbdhelperCode)
  , ("CvENODEFILECHECK",        'C.cvEnodefilecheckCode)
  , ("CvENODEHOOKS",            'C.cvEnodehooksCode)
  , ("CvENODEHV",               'C.cvEnodehvCode)
  , ("CvENODELVM",              'C.cvEnodelvmCode)
  , ("CvENODEN1",               'C.cvEnoden1Code)
  , ("CvENODENET",              'C.cvEnodenetCode)
  , ("CvENODEOS",               'C.cvEnodeosCode)
  , ("CvENODEORPHANINSTANCE",   'C.cvEnodeorphaninstanceCode)
  , ("CvENODEORPHANLV",         'C.cvEnodeorphanlvCode)
  , ("CvENODERPC",              'C.cvEnoderpcCode)
  , ("CvENODESSH",              'C.cvEnodesshCode)
  , ("CvENODEVERSION",          'C.cvEnodeversionCode)
  , ("CvENODESETUP",            'C.cvEnodesetupCode)
  , ("CvENODETIME",             'C.cvEnodetimeCode)
  , ("CvENODEOOBPATH",          'C.cvEnodeoobpathCode)
  , ("CvENODEUSERSCRIPTS",      'C.cvEnodeuserscriptsCode)
  , ("CvENODEFILESTORAGEPATHS", 'C.cvEnodefilestoragepathsCode)
  ])
$(THH.makeJSONInstance ''CVErrorCode)

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