Revision 3b89cb1b

b/src/Ganeti/Query/Instance.hs
163 163
  ] ++
164 164

  
165 165
  -- Per-disk parameter fields
166
  fillNumberFields C.maxDisks
166
  instantiateIndexedFields C.maxDisks
167 167
  [ (fieldDefinitionCompleter "disk.size/%d" "Disk/%d" QFTUnit
168 168
     "Disk size of %s disk",
169
     getFillableField instDisks diskSize, QffNormal)
169
     getIndexedField instDisks diskSize, QffNormal)
170 170
  , (fieldDefinitionCompleter "disk.spindles/%d" "DiskSpindles/%d" QFTNumber
171 171
     "Spindles of %s disk",
172
     getFillableOptionalField instDisks diskSpindles, QffNormal)
172
     getIndexedOptionalField instDisks diskSpindles, QffNormal)
173 173
  , (fieldDefinitionCompleter "disk.name/%d" "DiskName/%d" QFTText
174 174
     "Name of %s disk",
175
     getFillableOptionalField instDisks diskName, QffNormal)
175
     getIndexedOptionalField instDisks diskName, QffNormal)
176 176
  , (fieldDefinitionCompleter "disk.uuid/%d" "DiskUUID/%d" QFTText
177 177
     "UUID of %s disk",
178
     getFillableField instDisks diskUuid, QffNormal)
178
     getIndexedField instDisks diskUuid, QffNormal)
179 179
  ] ++
180 180

  
181 181
  -- Aggregate nic parameter fields
......
226 226
  ] ++
227 227

  
228 228
  -- Per-nic parameter fields
229
  fillNumberFields C.maxNics
229
  instantiateIndexedFields C.maxNics
230 230
  [ (fieldDefinitionCompleter "nic.ip/%d" "NicIP/%d" QFTText
231 231
     ("IP address" ++ nicDescSuffix),
232
     getFillableOptionalField instNics nicIp, QffNormal)
232
     getIndexedOptionalField instNics nicIp, QffNormal)
233 233
  , (fieldDefinitionCompleter "nic.uuid/%d" "NicUUID/%d" QFTText
234 234
     ("UUID address" ++ nicDescSuffix),
235
     getFillableField instNics nicUuid, QffNormal)
235
     getIndexedField instNics nicUuid, QffNormal)
236 236
  , (fieldDefinitionCompleter "nic.mac/%d" "NicMAC/%d" QFTText
237 237
     ("MAC address" ++ nicDescSuffix),
238
     getFillableField instNics nicMac, QffNormal)
238
     getIndexedField instNics nicMac, QffNormal)
239 239
  , (fieldDefinitionCompleter "nic.name/%d" "NicName/%d" QFTText
240 240
     ("Name address" ++ nicDescSuffix),
241
     getFillableOptionalField instNics nicName, QffNormal)
241
     getIndexedOptionalField instNics nicName, QffNormal)
242 242
  , (fieldDefinitionCompleter "nic.network/%d" "NicNetwork/%d" QFTText
243 243
     ("Network" ++ nicDescSuffix),
244
     getFillableOptionalField instNics nicNetwork, QffNormal)
244
     getIndexedOptionalField instNics nicNetwork, QffNormal)
245 245
  , (fieldDefinitionCompleter "nic.mode/%d" "NicMode/%d" QFTText
246 246
     ("Mode" ++ nicDescSuffix),
247
     getFillableNicField nicpMode, QffNormal)
247
     getIndexedNicField nicpMode, QffNormal)
248 248
  , (fieldDefinitionCompleter "nic.link/%d" "NicLink/%d" QFTText
249 249
     ("Link" ++ nicDescSuffix),
250
     getFillableNicField nicpLink, QffNormal)
250
     getIndexedNicField nicpLink, QffNormal)
251 251
  , (fieldDefinitionCompleter "nic.network.name/%d" "NicNetworkName/%d" QFTText
252 252
     ("Network name" ++ nicDescSuffix),
253
     getFillableNicNetworkNameField, QffNormal)
253
     getIndexedNicNetworkNameField, QffNormal)
254 254
  , (fieldDefinitionCompleter "nic.bridge/%d" "NicBridge/%d" QFTText
255 255
     ("Bridge" ++ nicDescSuffix),
256
     getOptionalFillableNicField getNicBridge, QffNormal)
256
     getOptionalIndexedNicField getNicBridge, QffNormal)
257 257
  ] ++
258 258

  
259 259
  -- Live fields using special getters
......
303 303
  (Map.!) (fromContainer . clusterNicparams . configCluster $ cfg) C.ppDefault
304 304

  
305 305
-- | Returns a field that retrieves a given NIC's network name.
306
getFillableNicNetworkNameField :: Int -> FieldGetter Instance Runtime
307
getFillableNicNetworkNameField index =
306
getIndexedNicNetworkNameField :: Int -> FieldGetter Instance Runtime
307
getIndexedNicNetworkNameField index =
308 308
  FieldConfig (\cfg inst -> rsMaybeUnavail $ do
309 309
    nicObj <- maybeAt index $ instNics inst
310 310
    nicNetworkId <- nicNetwork nicObj
311 311
    return $ getNetworkName cfg nicNetworkId)
312 312

  
313 313
-- | Gets a fillable NIC field.
314
getFillableNicField :: (J.JSON a)
315
                    => (FilledNicParams -> a)
316
                    -> Int
317
                    -> FieldGetter Instance Runtime
318
getFillableNicField getter =
319
  getOptionalFillableNicField (\x -> Just . getter $ x)
314
getIndexedNicField :: (J.JSON a)
315
                   => (FilledNicParams -> a)
316
                   -> Int
317
                   -> FieldGetter Instance Runtime
318
getIndexedNicField getter =
319
  getOptionalIndexedNicField (\x -> Just . getter $ x)
320 320

  
321 321
-- | Gets an optional fillable NIC field.
322
getOptionalFillableNicField :: (J.JSON a)
323
                            => (FilledNicParams -> Maybe a)
324
                            -> Int
325
                            -> FieldGetter Instance Runtime
326
getOptionalFillableNicField =
327
  getFillableFieldWithDefault
322
getOptionalIndexedNicField :: (J.JSON a)
323
                           => (FilledNicParams -> Maybe a)
324
                           -> Int
325
                           -> FieldGetter Instance Runtime
326
getOptionalIndexedNicField =
327
  getIndexedFieldWithDefault
328 328
    (map nicNicparams . instNics) (\x _ -> getDefaultNicParams x) fillNicParams
329 329

  
330 330
-- | Creates a function which produces a 'FieldGetter' when fed an index. Works
331 331
-- for fields that should be filled out through the use of a default.
332
getFillableFieldWithDefault :: (J.JSON c)
332
getIndexedFieldWithDefault :: (J.JSON c)
333 333
  => (Instance -> [a])             -- ^ Extracts a list of incomplete objects
334 334
  -> (ConfigData -> Instance -> b) -- ^ Extracts the default object
335 335
  -> (b -> a -> b)                 -- ^ Fills the default object
336 336
  -> (b -> Maybe c)                -- ^ Extracts an obj property
337 337
  -> Int                           -- ^ Index in list to use
338 338
  -> FieldGetter Instance Runtime  -- ^ Result
339
getFillableFieldWithDefault
339
getIndexedFieldWithDefault
340 340
  listGetter defaultGetter fillFn propertyGetter index =
341 341
  FieldConfig (\cfg inst -> rsMaybeUnavail $ do
342 342
                              incompleteObj <- maybeAt index $ listGetter inst
......
346 346

  
347 347
-- | Creates a function which produces a 'FieldGetter' when fed an index. Works
348 348
-- for fields that may not return a value, expressed through the Maybe monad.
349
getFillableOptionalField :: (J.JSON b)
350
                         => (Instance -> [a]) -- ^ Extracts a list of objects
351
                         -> (a -> Maybe b)    -- ^ Possibly gets a property
352
                                              -- from an object
353
                         -> Int               -- ^ Index in list to use
354
                         -> FieldGetter Instance Runtime -- ^ Result
355
getFillableOptionalField extractor optPropertyGetter index =
349
getIndexedOptionalField :: (J.JSON b)
350
                        => (Instance -> [a]) -- ^ Extracts a list of objects
351
                        -> (a -> Maybe b)    -- ^ Possibly gets a property
352
                                             -- from an object
353
                        -> Int               -- ^ Index in list to use
354
                        -> FieldGetter Instance Runtime -- ^ Result
355
getIndexedOptionalField extractor optPropertyGetter index =
356 356
  FieldSimple(\inst -> rsMaybeUnavail $ do
357 357
                         obj <- maybeAt index $ extractor inst
358 358
                         optPropertyGetter obj)
359 359

  
360 360
-- | Creates a function which produces a 'FieldGetter' when fed an index.
361 361
-- Works only for fields that surely return a value.
362
getFillableField :: (J.JSON b)
363
                 => (Instance -> [a]) -- ^ Extracts a list of objects
364
                 -> (a -> b)          -- ^ Gets a property from an object
365
                 -> Int               -- ^ Index in list to use
366
                 -> FieldGetter Instance Runtime -- ^ Result
367
getFillableField extractor propertyGetter index =
362
getIndexedField :: (J.JSON b)
363
                => (Instance -> [a]) -- ^ Extracts a list of objects
364
                -> (a -> b)          -- ^ Gets a property from an object
365
                -> Int               -- ^ Index in list to use
366
                -> FieldGetter Instance Runtime -- ^ Result
367
getIndexedField extractor propertyGetter index =
368 368
  let optPropertyGetter = Just . propertyGetter
369
  in getFillableOptionalField extractor optPropertyGetter index
369
  in getIndexedOptionalField extractor optPropertyGetter index
370 370

  
371 371
-- | Retrieves a value from an array at an index, using the Maybe monad to
372 372
-- indicate failure.
......
404 404
fillIncompleteFields (iDef, iGet, mode) firstVal secondVal =
405 405
  (iDef firstVal secondVal, iGet firstVal, mode)
406 406

  
407
-- | Given fields that describe lists, fill their definitions with appropriate
408
-- index representations.
409
fillNumberFields :: (Integral t1)
410
                 => Int
411
                 -> [(t1 -> String -> FieldDefinition,
412
                      t1 -> FieldGetter a b,
413
                      QffMode)]
414
                 -> FieldList a b
415
fillNumberFields numFills fieldsToFill = do
416
  index <- take numFills [0..]
417
  field <- fieldsToFill
407
-- | Given indexed fields that describe lists, complete / instantiate them for
408
-- a given list size.
409
instantiateIndexedFields :: (Show t1, Integral t1)
410
                         => Int            -- ^ The size of the list
411
                         -> [(t1 -> String -> FieldDefinition,
412
                              t1 -> FieldGetter a b,
413
                              QffMode)]    -- ^ The indexed fields
414
                         -> FieldList a b  -- ^ A list of complete fields
415
instantiateIndexedFields listSize fields = do
416
  index <- take listSize [0..]
417
  field <- fields
418 418
  return . fillIncompleteFields field index . formatOrdinal $ index + 1
419 419

  
420 420
-- * Various helper functions for property retrieval

Also available in: Unified diff