Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Objects.hs @ 1a0dacf6

History | View | Annotate | Download (25.9 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Implementation of the Ganeti config objects.
4

    
5
Some object fields are not implemented yet, and as such they are
6
commented out below.
7

    
8
-}
9

    
10
{-
11

    
12
Copyright (C) 2011, 2012, 2013, 2014 Google Inc.
13

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

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

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

    
29
-}
30

    
31
module Ganeti.Objects
32
  ( HvParams
33
  , OsParams
34
  , OsParamsPrivate
35
  , PartialNicParams(..)
36
  , FilledNicParams(..)
37
  , fillNicParams
38
  , allNicParamFields
39
  , PartialNic(..)
40
  , FileDriver(..)
41
  , DiskLogicalId(..)
42
  , Disk(..)
43
  , includesLogicalId
44
  , DiskTemplate(..)
45
  , PartialBeParams(..)
46
  , FilledBeParams(..)
47
  , fillBeParams
48
  , allBeParamFields
49
  , Instance(..)
50
  , toDictInstance
51
  , PartialNDParams(..)
52
  , FilledNDParams(..)
53
  , fillNDParams
54
  , allNDParamFields
55
  , Node(..)
56
  , AllocPolicy(..)
57
  , FilledISpecParams(..)
58
  , PartialISpecParams(..)
59
  , fillISpecParams
60
  , allISpecParamFields
61
  , MinMaxISpecs(..)
62
  , FilledIPolicy(..)
63
  , PartialIPolicy(..)
64
  , fillIPolicy
65
  , DiskParams
66
  , NodeGroup(..)
67
  , IpFamily(..)
68
  , ipFamilyToVersion
69
  , fillDict
70
  , ClusterHvParams
71
  , OsHvParams
72
  , ClusterBeParams
73
  , ClusterOsParams
74
  , ClusterOsParamsPrivate
75
  , ClusterNicParams
76
  , Cluster(..)
77
  , ConfigData(..)
78
  , TimeStampObject(..)
79
  , UuidObject(..)
80
  , SerialNoObject(..)
81
  , TagsObject(..)
82
  , DictObject(..) -- re-exported from THH
83
  , TagSet -- re-exported from THH
84
  , Network(..)
85
  , Ip4Address(..)
86
  , Ip4Network(..)
87
  , readIp4Address
88
  , nextIp4Address
89
  , IAllocatorParams
90
  ) where
91

    
92
import Control.Applicative
93
import Data.List (foldl')
94
import Data.Maybe
95
import qualified Data.Map as Map
96
import qualified Data.Set as Set
97
import Data.Word
98
import System.Time (ClockTime(..))
99
import Text.JSON (showJSON, readJSON, JSON, JSValue(..), fromJSString)
100
import qualified Text.JSON as J
101

    
102
import qualified AutoConf
103
import qualified Ganeti.Constants as C
104
import qualified Ganeti.ConstantUtils as ConstantUtils
105
import Ganeti.JSON
106
import Ganeti.Types
107
import Ganeti.THH
108
import Ganeti.Utils (sepSplit, tryRead, parseUnitAssumeBinary)
109

    
110
-- * Generic definitions
111

    
112
-- | Fills one map with keys from the other map, if not already
113
-- existing. Mirrors objects.py:FillDict.
114
fillDict :: (Ord k) => Map.Map k v -> Map.Map k v -> [k] -> Map.Map k v
115
fillDict defaults custom skip_keys =
116
  let updated = Map.union custom defaults
117
  in foldl' (flip Map.delete) updated skip_keys
118

    
119
-- | The hypervisor parameter type. This is currently a simple map,
120
-- without type checking on key/value pairs.
121
type HvParams = Container JSValue
122

    
123
-- | The OS parameters type. This is, and will remain, a string
124
-- container, since the keys are dynamically declared by the OSes, and
125
-- the values are always strings.
126
type OsParams = Container String
127
type OsParamsPrivate = Container (Private String)
128

    
129
-- | Class of objects that have timestamps.
130
class TimeStampObject a where
131
  cTimeOf :: a -> ClockTime
132
  mTimeOf :: a -> ClockTime
133

    
134
-- | Class of objects that have an UUID.
135
class UuidObject a where
136
  uuidOf :: a -> String
137

    
138
-- | Class of object that have a serial number.
139
class SerialNoObject a where
140
  serialOf :: a -> Int
141

    
142
-- | Class of objects that have tags.
143
class TagsObject a where
144
  tagsOf :: a -> Set.Set String
145

    
146
-- * Network definitions
147

    
148
-- ** Ipv4 types
149

    
150
-- | Custom type for a simple IPv4 address.
151
data Ip4Address = Ip4Address Word8 Word8 Word8 Word8
152
                  deriving Eq
153

    
154
instance Show Ip4Address where
155
  show (Ip4Address a b c d) = show a ++ "." ++ show b ++ "." ++
156
                              show c ++ "." ++ show d
157

    
158
-- | Parses an IPv4 address from a string.
159
readIp4Address :: (Applicative m, Monad m) => String -> m Ip4Address
160
readIp4Address s =
161
  case sepSplit '.' s of
162
    [a, b, c, d] -> Ip4Address <$>
163
                      tryRead "first octect" a <*>
164
                      tryRead "second octet" b <*>
165
                      tryRead "third octet"  c <*>
166
                      tryRead "fourth octet" d
167
    _ -> fail $ "Can't parse IPv4 address from string " ++ s
168

    
169
-- | JSON instance for 'Ip4Address'.
170
instance JSON Ip4Address where
171
  showJSON = showJSON . show
172
  readJSON (JSString s) = readIp4Address (fromJSString s)
173
  readJSON v = fail $ "Invalid JSON value " ++ show v ++ " for an IPv4 address"
174

    
175
-- | \"Next\" address implementation for IPv4 addresses.
176
--
177
-- Note that this loops! Note also that this is a very dumb
178
-- implementation.
179
nextIp4Address :: Ip4Address -> Ip4Address
180
nextIp4Address (Ip4Address a b c d) =
181
  let inc xs y = if all (==0) xs then y + 1 else y
182
      d' = d + 1
183
      c' = inc [d'] c
184
      b' = inc [c', d'] b
185
      a' = inc [b', c', d'] a
186
  in Ip4Address a' b' c' d'
187

    
188
-- | Custom type for an IPv4 network.
189
data Ip4Network = Ip4Network Ip4Address Word8
190
                  deriving Eq
191

    
192
instance Show Ip4Network where
193
  show (Ip4Network ip netmask) = show ip ++ "/" ++ show netmask
194

    
195
-- | JSON instance for 'Ip4Network'.
196
instance JSON Ip4Network where
197
  showJSON = showJSON . show
198
  readJSON (JSString s) =
199
    case sepSplit '/' (fromJSString s) of
200
      [ip, nm] -> do
201
        ip' <- readIp4Address ip
202
        nm' <- tryRead "parsing netmask" nm
203
        if nm' >= 0 && nm' <= 32
204
          then return $ Ip4Network ip' nm'
205
          else fail $ "Invalid netmask " ++ show nm' ++ " from string " ++
206
                      fromJSString s
207
      _ -> fail $ "Can't parse IPv4 network from string " ++ fromJSString s
208
  readJSON v = fail $ "Invalid JSON value " ++ show v ++ " for an IPv4 network"
209

    
210
-- ** Ganeti \"network\" config object.
211

    
212
-- FIXME: Not all types might be correct here, since they
213
-- haven't been exhaustively deduced from the python code yet.
214
$(buildObject "Network" "network" $
215
  [ simpleField "name"             [t| NonEmptyString |]
216
  , optionalField $
217
    simpleField "mac_prefix"       [t| String |]
218
  , simpleField "network"          [t| Ip4Network |]
219
  , optionalField $
220
    simpleField "network6"         [t| String |]
221
  , optionalField $
222
    simpleField "gateway"          [t| Ip4Address |]
223
  , optionalField $
224
    simpleField "gateway6"         [t| String |]
225
  , optionalField $
226
    simpleField "reservations"     [t| String |]
227
  , optionalField $
228
    simpleField "ext_reservations" [t| String |]
229
  ]
230
  ++ uuidFields
231
  ++ timeStampFields
232
  ++ serialFields
233
  ++ tagsFields)
234

    
235
instance SerialNoObject Network where
236
  serialOf = networkSerial
237

    
238
instance TagsObject Network where
239
  tagsOf = networkTags
240

    
241
instance UuidObject Network where
242
  uuidOf = networkUuid
243

    
244
instance TimeStampObject Network where
245
  cTimeOf = networkCtime
246
  mTimeOf = networkMtime
247

    
248
-- * NIC definitions
249

    
250
$(buildParam "Nic" "nicp"
251
  [ simpleField "mode" [t| NICMode |]
252
  , simpleField "link" [t| String  |]
253
  , simpleField "vlan" [t| String |]
254
  ])
255

    
256
$(buildObject "PartialNic" "nic" $
257
  [ simpleField "mac" [t| String |]
258
  , optionalField $ simpleField "ip" [t| String |]
259
  , simpleField "nicparams" [t| PartialNicParams |]
260
  , optionalField $ simpleField "network" [t| String |]
261
  , optionalField $ simpleField "name" [t| String |]
262
  ] ++ uuidFields)
263

    
264
instance UuidObject PartialNic where
265
  uuidOf = nicUuid
266

    
267
-- * Disk definitions
268

    
269
-- | Constant for the dev_type key entry in the disk config.
270
devType :: String
271
devType = "dev_type"
272

    
273
-- | The disk configuration type. This includes the disk type itself,
274
-- for a more complete consistency. Note that since in the Python
275
-- code-base there's no authoritative place where we document the
276
-- logical id, this is probably a good reference point.
277
data DiskLogicalId
278
  = LIDPlain String String  -- ^ Volume group, logical volume
279
  | LIDDrbd8 String String Int Int Int String
280
  -- ^ NodeA, NodeB, Port, MinorA, MinorB, Secret
281
  | LIDFile FileDriver String -- ^ Driver, path
282
  | LIDSharedFile FileDriver String -- ^ Driver, path
283
  | LIDBlockDev BlockDriver String -- ^ Driver, path (must be under /dev)
284
  | LIDRados String String -- ^ Unused, path
285
  | LIDExt String String -- ^ ExtProvider, unique name
286
    deriving (Show, Eq)
287

    
288
-- | Mapping from a logical id to a disk type.
289
lidDiskType :: DiskLogicalId -> DiskTemplate
290
lidDiskType (LIDPlain {}) = DTPlain
291
lidDiskType (LIDDrbd8 {}) = DTDrbd8
292
lidDiskType (LIDFile  {}) = DTFile
293
lidDiskType (LIDSharedFile  {}) = DTSharedFile
294
lidDiskType (LIDBlockDev {}) = DTBlock
295
lidDiskType (LIDRados {}) = DTRbd
296
lidDiskType (LIDExt {}) = DTExt
297

    
298
-- | Builds the extra disk_type field for a given logical id.
299
lidEncodeType :: DiskLogicalId -> [(String, JSValue)]
300
lidEncodeType v = [(devType, showJSON . lidDiskType $ v)]
301

    
302
-- | Custom encoder for DiskLogicalId (logical id only).
303
encodeDLId :: DiskLogicalId -> JSValue
304
encodeDLId (LIDPlain vg lv) = JSArray [showJSON vg, showJSON lv]
305
encodeDLId (LIDDrbd8 nodeA nodeB port minorA minorB key) =
306
  JSArray [ showJSON nodeA, showJSON nodeB, showJSON port
307
          , showJSON minorA, showJSON minorB, showJSON key ]
308
encodeDLId (LIDRados pool name) = JSArray [showJSON pool, showJSON name]
309
encodeDLId (LIDFile driver name) = JSArray [showJSON driver, showJSON name]
310
encodeDLId (LIDSharedFile driver name) =
311
  JSArray [showJSON driver, showJSON name]
312
encodeDLId (LIDBlockDev driver name) = JSArray [showJSON driver, showJSON name]
313
encodeDLId (LIDExt extprovider name) =
314
  JSArray [showJSON extprovider, showJSON name]
315

    
316
-- | Custom encoder for DiskLogicalId, composing both the logical id
317
-- and the extra disk_type field.
318
encodeFullDLId :: DiskLogicalId -> (JSValue, [(String, JSValue)])
319
encodeFullDLId v = (encodeDLId v, lidEncodeType v)
320

    
321
-- | Custom decoder for DiskLogicalId. This is manual for now, since
322
-- we don't have yet automation for separate-key style fields.
323
decodeDLId :: [(String, JSValue)] -> JSValue -> J.Result DiskLogicalId
324
decodeDLId obj lid = do
325
  dtype <- fromObj obj devType
326
  case dtype of
327
    DTDrbd8 ->
328
      case lid of
329
        JSArray [nA, nB, p, mA, mB, k] -> do
330
          nA' <- readJSON nA
331
          nB' <- readJSON nB
332
          p'  <- readJSON p
333
          mA' <- readJSON mA
334
          mB' <- readJSON mB
335
          k'  <- readJSON k
336
          return $ LIDDrbd8 nA' nB' p' mA' mB' k'
337
        _ -> fail "Can't read logical_id for DRBD8 type"
338
    DTPlain ->
339
      case lid of
340
        JSArray [vg, lv] -> do
341
          vg' <- readJSON vg
342
          lv' <- readJSON lv
343
          return $ LIDPlain vg' lv'
344
        _ -> fail "Can't read logical_id for plain type"
345
    DTFile ->
346
      case lid of
347
        JSArray [driver, path] -> do
348
          driver' <- readJSON driver
349
          path'   <- readJSON path
350
          return $ LIDFile driver' path'
351
        _ -> fail "Can't read logical_id for file type"
352
    DTSharedFile ->
353
      case lid of
354
        JSArray [driver, path] -> do
355
          driver' <- readJSON driver
356
          path'   <- readJSON path
357
          return $ LIDSharedFile driver' path'
358
        _ -> fail "Can't read logical_id for shared file type"
359
    DTGluster ->
360
      case lid of
361
        JSArray [driver, path] -> do
362
          driver' <- readJSON driver
363
          path'   <- readJSON path
364
          return $ LIDSharedFile driver' path'
365
        _ -> fail "Can't read logical_id for shared file type"
366
    DTBlock ->
367
      case lid of
368
        JSArray [driver, path] -> do
369
          driver' <- readJSON driver
370
          path'   <- readJSON path
371
          return $ LIDBlockDev driver' path'
372
        _ -> fail "Can't read logical_id for blockdev type"
373
    DTRbd ->
374
      case lid of
375
        JSArray [driver, path] -> do
376
          driver' <- readJSON driver
377
          path'   <- readJSON path
378
          return $ LIDRados driver' path'
379
        _ -> fail "Can't read logical_id for rdb type"
380
    DTExt ->
381
      case lid of
382
        JSArray [extprovider, name] -> do
383
          extprovider' <- readJSON extprovider
384
          name'   <- readJSON name
385
          return $ LIDExt extprovider' name'
386
        _ -> fail "Can't read logical_id for extstorage type"
387
    DTDiskless ->
388
      fail "Retrieved 'diskless' disk."
389

    
390
-- | Disk data structure.
391
--
392
-- This is declared manually as it's a recursive structure, and our TH
393
-- code currently can't build it.
394
data Disk = Disk
395
  { diskLogicalId  :: DiskLogicalId
396
  , diskChildren   :: [Disk]
397
  , diskIvName     :: String
398
  , diskSize       :: Int
399
  , diskMode       :: DiskMode
400
  , diskName       :: Maybe String
401
  , diskInstance   :: Maybe String
402
  , diskSpindles   :: Maybe Int
403
  , diskUuid       :: String
404
  } deriving (Show, Eq)
405

    
406
$(buildObjectSerialisation "Disk" $
407
  [ customField 'decodeDLId 'encodeFullDLId ["dev_type"] $
408
      simpleField "logical_id"    [t| DiskLogicalId   |]
409
  , defaultField  [| [] |] $ simpleField "children" [t| [Disk] |]
410
  , defaultField [| "" |] $ simpleField "iv_name" [t| String |]
411
  , simpleField "size" [t| Int |]
412
  , defaultField [| DiskRdWr |] $ simpleField "mode" [t| DiskMode |]
413
  , optionalField $ simpleField "name" [t| String |]
414
  , optionalField $ simpleField "instance" [t| String |]
415
  , optionalField $ simpleField "spindles" [t| Int |]
416
  ]
417
  ++ uuidFields)
418

    
419
instance UuidObject Disk where
420
  uuidOf = diskUuid
421

    
422
-- | Determines whether a disk or one of his children has the given logical id
423
-- (determined by the volume group name and by the logical volume name).
424
-- This can be true only for DRBD or LVM disks.
425
includesLogicalId :: String -> String -> Disk -> Bool
426
includesLogicalId vg_name lv_name disk =
427
  case diskLogicalId disk of
428
    LIDPlain vg lv -> vg_name == vg && lv_name == lv
429
    LIDDrbd8 {} ->
430
      any (includesLogicalId vg_name lv_name) $ diskChildren disk
431
    _ -> False
432

    
433
-- * Instance definitions
434

    
435
$(buildParam "Be" "bep"
436
  [ specialNumericalField 'parseUnitAssumeBinary
437
      $ simpleField "minmem"      [t| Int  |]
438
  , specialNumericalField 'parseUnitAssumeBinary
439
      $ simpleField "maxmem"      [t| Int  |]
440
  , simpleField "vcpus"           [t| Int  |]
441
  , simpleField "auto_balance"    [t| Bool |]
442
  , simpleField "always_failover" [t| Bool |]
443
  , simpleField "spindle_use"     [t| Int  |]
444
  ])
445

    
446
$(buildObject "Instance" "inst" $
447
  [ simpleField "name"             [t| String             |]
448
  , simpleField "primary_node"     [t| String             |]
449
  , simpleField "os"               [t| String             |]
450
  , simpleField "hypervisor"       [t| Hypervisor         |]
451
  , simpleField "hvparams"         [t| HvParams           |]
452
  , simpleField "beparams"         [t| PartialBeParams    |]
453
  , simpleField "osparams"         [t| OsParams           |]
454
  , simpleField "osparams_private" [t| OsParamsPrivate    |]
455
  , simpleField "admin_state"      [t| AdminState         |]
456
  , simpleField "nics"             [t| [PartialNic]       |]
457
  , simpleField "disks"            [t| [String]           |]
458
  , simpleField "disk_template"    [t| DiskTemplate       |]
459
  , simpleField "disks_active"     [t| Bool               |]
460
  , optionalField $ simpleField "network_port" [t| Int  |]
461
  ]
462
  ++ timeStampFields
463
  ++ uuidFields
464
  ++ serialFields
465
  ++ tagsFields)
466

    
467
instance TimeStampObject Instance where
468
  cTimeOf = instCtime
469
  mTimeOf = instMtime
470

    
471
instance UuidObject Instance where
472
  uuidOf = instUuid
473

    
474
instance SerialNoObject Instance where
475
  serialOf = instSerial
476

    
477
instance TagsObject Instance where
478
  tagsOf = instTags
479

    
480
-- * IPolicy definitions
481

    
482
$(buildParam "ISpec" "ispec"
483
  [ simpleField ConstantUtils.ispecMemSize     [t| Int |]
484
  , simpleField ConstantUtils.ispecDiskSize    [t| Int |]
485
  , simpleField ConstantUtils.ispecDiskCount   [t| Int |]
486
  , simpleField ConstantUtils.ispecCpuCount    [t| Int |]
487
  , simpleField ConstantUtils.ispecNicCount    [t| Int |]
488
  , simpleField ConstantUtils.ispecSpindleUse  [t| Int |]
489
  ])
490

    
491
$(buildObject "MinMaxISpecs" "mmis"
492
  [ renameField "MinSpec" $ simpleField "min" [t| FilledISpecParams |]
493
  , renameField "MaxSpec" $ simpleField "max" [t| FilledISpecParams |]
494
  ])
495

    
496
-- | Custom partial ipolicy. This is not built via buildParam since it
497
-- has a special 2-level inheritance mode.
498
$(buildObject "PartialIPolicy" "ipolicy"
499
  [ optionalField . renameField "MinMaxISpecsP" $
500
    simpleField ConstantUtils.ispecsMinmax [t| [MinMaxISpecs] |]
501
  , optionalField . renameField "StdSpecP" $
502
    simpleField "std" [t| PartialISpecParams |]
503
  , optionalField . renameField "SpindleRatioP" $
504
    simpleField "spindle-ratio" [t| Double |]
505
  , optionalField . renameField "VcpuRatioP" $
506
    simpleField "vcpu-ratio" [t| Double |]
507
  , optionalField . renameField "DiskTemplatesP" $
508
    simpleField "disk-templates" [t| [DiskTemplate] |]
509
  ])
510

    
511
-- | Custom filled ipolicy. This is not built via buildParam since it
512
-- has a special 2-level inheritance mode.
513
$(buildObject "FilledIPolicy" "ipolicy"
514
  [ renameField "MinMaxISpecs" $
515
    simpleField ConstantUtils.ispecsMinmax [t| [MinMaxISpecs] |]
516
  , renameField "StdSpec" $ simpleField "std" [t| FilledISpecParams |]
517
  , simpleField "spindle-ratio"  [t| Double |]
518
  , simpleField "vcpu-ratio"     [t| Double |]
519
  , simpleField "disk-templates" [t| [DiskTemplate] |]
520
  ])
521

    
522
-- | Custom filler for the ipolicy types.
523
fillIPolicy :: FilledIPolicy -> PartialIPolicy -> FilledIPolicy
524
fillIPolicy (FilledIPolicy { ipolicyMinMaxISpecs  = fminmax
525
                           , ipolicyStdSpec       = fstd
526
                           , ipolicySpindleRatio  = fspindleRatio
527
                           , ipolicyVcpuRatio     = fvcpuRatio
528
                           , ipolicyDiskTemplates = fdiskTemplates})
529
            (PartialIPolicy { ipolicyMinMaxISpecsP  = pminmax
530
                            , ipolicyStdSpecP       = pstd
531
                            , ipolicySpindleRatioP  = pspindleRatio
532
                            , ipolicyVcpuRatioP     = pvcpuRatio
533
                            , ipolicyDiskTemplatesP = pdiskTemplates}) =
534
  FilledIPolicy { ipolicyMinMaxISpecs  = fromMaybe fminmax pminmax
535
                , ipolicyStdSpec       = case pstd of
536
                                         Nothing -> fstd
537
                                         Just p -> fillISpecParams fstd p
538
                , ipolicySpindleRatio  = fromMaybe fspindleRatio pspindleRatio
539
                , ipolicyVcpuRatio     = fromMaybe fvcpuRatio pvcpuRatio
540
                , ipolicyDiskTemplates = fromMaybe fdiskTemplates
541
                                         pdiskTemplates
542
                }
543
-- * Node definitions
544

    
545
$(buildParam "ND" "ndp"
546
  [ simpleField "oob_program"   [t| String |]
547
  , simpleField "spindle_count" [t| Int    |]
548
  , simpleField "exclusive_storage" [t| Bool |]
549
  , simpleField "ovs"           [t| Bool |]
550
  , simpleField "ovs_name"       [t| String |]
551
  , simpleField "ovs_link"       [t| String |]
552
  , simpleField "ssh_port"      [t| Int |]
553
  ])
554

    
555
$(buildObject "Node" "node" $
556
  [ simpleField "name"             [t| String |]
557
  , simpleField "primary_ip"       [t| String |]
558
  , simpleField "secondary_ip"     [t| String |]
559
  , simpleField "master_candidate" [t| Bool   |]
560
  , simpleField "offline"          [t| Bool   |]
561
  , simpleField "drained"          [t| Bool   |]
562
  , simpleField "group"            [t| String |]
563
  , simpleField "master_capable"   [t| Bool   |]
564
  , simpleField "vm_capable"       [t| Bool   |]
565
  , simpleField "ndparams"         [t| PartialNDParams |]
566
  , simpleField "powered"          [t| Bool   |]
567
  ]
568
  ++ timeStampFields
569
  ++ uuidFields
570
  ++ serialFields
571
  ++ tagsFields)
572

    
573
instance TimeStampObject Node where
574
  cTimeOf = nodeCtime
575
  mTimeOf = nodeMtime
576

    
577
instance UuidObject Node where
578
  uuidOf = nodeUuid
579

    
580
instance SerialNoObject Node where
581
  serialOf = nodeSerial
582

    
583
instance TagsObject Node where
584
  tagsOf = nodeTags
585

    
586
-- * NodeGroup definitions
587

    
588
-- | The disk parameters type.
589
type DiskParams = Container (Container JSValue)
590

    
591
-- | A mapping from network UUIDs to nic params of the networks.
592
type Networks = Container PartialNicParams
593

    
594
$(buildObject "NodeGroup" "group" $
595
  [ simpleField "name"         [t| String |]
596
  , defaultField [| [] |] $ simpleField "members" [t| [String] |]
597
  , simpleField "ndparams"     [t| PartialNDParams |]
598
  , simpleField "alloc_policy" [t| AllocPolicy     |]
599
  , simpleField "ipolicy"      [t| PartialIPolicy  |]
600
  , simpleField "diskparams"   [t| DiskParams      |]
601
  , simpleField "networks"     [t| Networks        |]
602
  ]
603
  ++ timeStampFields
604
  ++ uuidFields
605
  ++ serialFields
606
  ++ tagsFields)
607

    
608
instance TimeStampObject NodeGroup where
609
  cTimeOf = groupCtime
610
  mTimeOf = groupMtime
611

    
612
instance UuidObject NodeGroup where
613
  uuidOf = groupUuid
614

    
615
instance SerialNoObject NodeGroup where
616
  serialOf = groupSerial
617

    
618
instance TagsObject NodeGroup where
619
  tagsOf = groupTags
620

    
621
-- | IP family type
622
$(declareIADT "IpFamily"
623
  [ ("IpFamilyV4", 'AutoConf.pyAfInet4)
624
  , ("IpFamilyV6", 'AutoConf.pyAfInet6)
625
  ])
626
$(makeJSONInstance ''IpFamily)
627

    
628
-- | Conversion from IP family to IP version. This is needed because
629
-- Python uses both, depending on context.
630
ipFamilyToVersion :: IpFamily -> Int
631
ipFamilyToVersion IpFamilyV4 = C.ip4Version
632
ipFamilyToVersion IpFamilyV6 = C.ip6Version
633

    
634
-- | Cluster HvParams (hvtype to hvparams mapping).
635
type ClusterHvParams = Container HvParams
636

    
637
-- | Cluster Os-HvParams (os to hvparams mapping).
638
type OsHvParams = Container ClusterHvParams
639

    
640
-- | Cluser BeParams.
641
type ClusterBeParams = Container FilledBeParams
642

    
643
-- | Cluster OsParams.
644
type ClusterOsParams = Container OsParams
645
type ClusterOsParamsPrivate = Container (Private OsParams)
646

    
647
-- | Cluster NicParams.
648
type ClusterNicParams = Container FilledNicParams
649

    
650
-- | Cluster UID Pool, list (low, high) UID ranges.
651
type UidPool = [(Int, Int)]
652

    
653
-- | The iallocator parameters type.
654
type IAllocatorParams = Container JSValue
655

    
656
-- | The master candidate client certificate digests
657
type CandidateCertificates = Container String
658

    
659
-- * Cluster definitions
660
$(buildObject "Cluster" "cluster" $
661
  [ simpleField "rsahostkeypub"                  [t| String                 |]
662
  , optionalField $
663
    simpleField "dsahostkeypub"                  [t| String                 |]
664
  , simpleField "highest_used_port"              [t| Int                    |]
665
  , simpleField "tcpudp_port_pool"               [t| [Int]                  |]
666
  , simpleField "mac_prefix"                     [t| String                 |]
667
  , optionalField $
668
    simpleField "volume_group_name"              [t| String                 |]
669
  , simpleField "reserved_lvs"                   [t| [String]               |]
670
  , optionalField $
671
    simpleField "drbd_usermode_helper"           [t| String                 |]
672
  , simpleField "master_node"                    [t| String                 |]
673
  , simpleField "master_ip"                      [t| String                 |]
674
  , simpleField "master_netdev"                  [t| String                 |]
675
  , simpleField "master_netmask"                 [t| Int                    |]
676
  , simpleField "use_external_mip_script"        [t| Bool                   |]
677
  , simpleField "cluster_name"                   [t| String                 |]
678
  , simpleField "file_storage_dir"               [t| String                 |]
679
  , simpleField "shared_file_storage_dir"        [t| String                 |]
680
  , simpleField "gluster_storage_dir"            [t| String                 |]
681
  , simpleField "enabled_hypervisors"            [t| [Hypervisor]           |]
682
  , simpleField "hvparams"                       [t| ClusterHvParams        |]
683
  , simpleField "os_hvp"                         [t| OsHvParams             |]
684
  , simpleField "beparams"                       [t| ClusterBeParams        |]
685
  , simpleField "osparams"                       [t| ClusterOsParams        |]
686
  , simpleField "osparams_private_cluster"       [t| ClusterOsParamsPrivate |]
687
  , simpleField "nicparams"                      [t| ClusterNicParams       |]
688
  , simpleField "ndparams"                       [t| FilledNDParams         |]
689
  , simpleField "diskparams"                     [t| DiskParams             |]
690
  , simpleField "candidate_pool_size"            [t| Int                    |]
691
  , simpleField "modify_etc_hosts"               [t| Bool                   |]
692
  , simpleField "modify_ssh_setup"               [t| Bool                   |]
693
  , simpleField "maintain_node_health"           [t| Bool                   |]
694
  , simpleField "uid_pool"                       [t| UidPool                |]
695
  , simpleField "default_iallocator"             [t| String                 |]
696
  , simpleField "default_iallocator_params"      [t| IAllocatorParams       |]
697
  , simpleField "hidden_os"                      [t| [String]               |]
698
  , simpleField "blacklisted_os"                 [t| [String]               |]
699
  , simpleField "primary_ip_family"              [t| IpFamily               |]
700
  , simpleField "prealloc_wipe_disks"            [t| Bool                   |]
701
  , simpleField "ipolicy"                        [t| FilledIPolicy          |]
702
  , simpleField "enabled_disk_templates"         [t| [DiskTemplate]         |]
703
  , simpleField "candidate_certs"                [t| CandidateCertificates  |]
704
  , simpleField "max_running_jobs"               [t| Int                    |]
705
  , simpleField "instance_communication_network" [t| String                 |]
706
 ]
707
 ++ timeStampFields
708
 ++ uuidFields
709
 ++ serialFields
710
 ++ tagsFields)
711

    
712
instance TimeStampObject Cluster where
713
  cTimeOf = clusterCtime
714
  mTimeOf = clusterMtime
715

    
716
instance UuidObject Cluster where
717
  uuidOf = clusterUuid
718

    
719
instance SerialNoObject Cluster where
720
  serialOf = clusterSerial
721

    
722
instance TagsObject Cluster where
723
  tagsOf = clusterTags
724

    
725
-- * ConfigData definitions
726

    
727
$(buildObject "ConfigData" "config" $
728
--  timeStampFields ++
729
  [ simpleField "version"    [t| Int                 |]
730
  , simpleField "cluster"    [t| Cluster             |]
731
  , simpleField "nodes"      [t| Container Node      |]
732
  , simpleField "nodegroups" [t| Container NodeGroup |]
733
  , simpleField "instances"  [t| Container Instance  |]
734
  , simpleField "networks"   [t| Container Network   |]
735
  , simpleField "disks"      [t| Container Disk      |]
736
  ]
737
  ++ timeStampFields
738
  ++ serialFields)
739

    
740
instance SerialNoObject ConfigData where
741
  serialOf = configSerial
742

    
743
instance TimeStampObject ConfigData where
744
  cTimeOf = configCtime
745
  mTimeOf = configMtime