Revision 05092772

b/Makefile.am
529 529
	src/Ganeti/Query/Group.hs \
530 530
	src/Ganeti/Query/Job.hs \
531 531
	src/Ganeti/Query/Language.hs \
532
	src/Ganeti/Query/Network.hs \
532 533
	src/Ganeti/Query/Node.hs \
533 534
	src/Ganeti/Query/Query.hs \
534 535
	src/Ganeti/Query/Server.hs \
b/src/Ganeti/Config.hs
42 42
    , getGroupNodes
43 43
    , getGroupInstances
44 44
    , getGroupOfNode
45
    , getGroupConnections
45 46
    , getInstPrimaryNode
46 47
    , getInstMinorsForNode
48
    , getNetwork
47 49
    , buildLinkIpInstnameMap
48 50
    , instNodes
49 51
    ) where
50 52

  
51 53
import Control.Monad (liftM)
52 54
import Data.List (foldl')
55
import Data.Maybe (fromMaybe, mapMaybe)
53 56
import qualified Data.Map as M
54 57
import qualified Data.Set as S
55 58
import qualified Text.JSON as J
......
210 213
      ginsts = map (getNodeInstances cfg) gnodes in
211 214
  (concatMap fst ginsts, concatMap snd ginsts)
212 215

  
216
-- | Looks up a network. If looking up by uuid fails, we look up
217
-- by name.
218
getNetwork :: ConfigData -> String -> ErrorResult Network
219
getNetwork cfg name =
220
  let networks = fromContainer (configNetworks cfg)
221
  in case getItem "Network" name networks of
222
       Ok net -> Ok net
223
       Bad _ -> let by_name = M.mapKeys
224
                              (fromNonEmpty . networkName . (M.!) networks)
225
                              networks
226
                in getItem "Network" name by_name
227

  
228
-- | Given a network's UUID, this function lists all connections from
229
-- the network to nodegroups including the respective mode and links.
230
getGroupConnections :: ConfigData -> String -> [(String, String, String)]
231
getGroupConnections cfg network_uuid =
232
  mapMaybe (getGroupConnection network_uuid)
233
  ((M.elems . fromContainer . configNodegroups) cfg)
234

  
235
-- | Given a network's UUID and a node group, this function assembles
236
-- a tuple of the group's name, the mode and the link by which the
237
-- network is connected to the group. Returns 'Nothing' if the network
238
-- is not connected to the group.
239
getGroupConnection :: String -> NodeGroup -> Maybe (String, String, String)
240
getGroupConnection network_uuid group =
241
  let networks = fromContainer . groupNetworks $ group
242
  in case M.lookup network_uuid networks of
243
    Nothing -> Nothing
244
    Just network ->
245
      Just (groupName group, getNicMode network, getNicLink network)
246

  
247
-- | Retrieves the network's mode and formats it human-readable,
248
-- also in case it is not available.
249
getNicMode :: PartialNicParams -> String
250
getNicMode nic_params =
251
  maybe "-" nICModeToRaw $ nicpModeP nic_params
252

  
253
-- | Retrieves the network's link and formats it human-readable, also in
254
-- case it it not available.
255
getNicLink :: PartialNicParams -> String
256
getNicLink nic_params = fromMaybe "-" (nicpLinkP nic_params)
257

  
213 258
-- | Looks up an instance's primary node.
214 259
getInstPrimaryNode :: ConfigData -> String -> ErrorResult Node
215 260
getInstPrimaryNode cfg name =
b/src/Ganeti/Query/Language.hs
113 113
  , ("QRGroup",    'C.qrGroup )
114 114
  , ("QROs",       'C.qrOs )
115 115
  , ("QRExport",   'C.qrExport )
116
  , ("QRNetwork",  'C.qrNetwork )
116 117
  ])
117 118
$(makeJSONInstance ''QueryTypeOp)
118 119

  
b/src/Ganeti/Query/Network.hs
1
{-| Implementation of the Ganeti Query2 node group queries.
2

  
3
 -}
4

  
5
{-
6

  
7
Copyright (C) 2012 Google Inc.
8

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

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

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

  
24
-}
25

  
26
module Ganeti.Query.Network
27
  ( NetworkRuntime(..)
28
  , networkFieldsMap
29
  ) where
30

  
31
import qualified Data.Map as Map
32

  
33
import Ganeti.Config
34
import Ganeti.Objects
35
import Ganeti.Query.Language
36
import Ganeti.Query.Common
37
import Ganeti.Query.Types
38

  
39
data NetworkRuntime = NetworkRuntime
40

  
41
networkFields :: FieldList Network NetworkRuntime
42
networkFields =
43
  [ (FieldDefinition "name" "Name" QFTText "Network name",
44
     FieldSimple (rsNormal . networkName), QffNormal)
45
  , (FieldDefinition "network" "Subnet" QFTText "IPv4 subnet",
46
     FieldSimple (rsNormal . networkNetwork), QffNormal)
47
  , (FieldDefinition "gateway" "Gateway" QFTOther "IPv4 gateway",
48
     FieldSimple (rsMaybeUnavail . networkGateway), QffNormal)
49
  , (FieldDefinition "network6" "IPv6Subnet" QFTOther "IPv6 subnet",
50
     FieldSimple (rsMaybeUnavail . networkNetwork6), QffNormal)
51
  , (FieldDefinition "gateway6" "IPv6Gateway" QFTOther "IPv6 gateway",
52
     FieldSimple (rsMaybeUnavail . networkGateway6), QffNormal)
53
  , (FieldDefinition "mac_prefix" "MacPrefix" QFTOther "MAC address prefix",
54
     FieldSimple (rsMaybeUnavail . networkMacPrefix), QffNormal)
55
  , (FieldDefinition "network_type" "NetworkType" QFTOther "Network type",
56
     FieldSimple (rsMaybeUnavail . networkNetworkType), QffNormal)
57
  , (FieldDefinition "group_list" "GroupList" QFTOther "List of node groups",
58
     FieldConfig (\cfg -> rsNormal . getGroupConnections cfg . networkUuid),
59
       QffNormal)
60
  ] ++
61
  uuidFields "Network" ++
62
  serialFields "Network" ++
63
  tagsFields
64

  
65
-- | The group fields map.
66
networkFieldsMap :: FieldMap Network NetworkRuntime
67
networkFieldsMap =
68
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields
69

  
70
-- TODO: the following fields are not implemented yet: external_reservations,
71
-- free_count, group_cnt, inst_cnt, inst_list, map, reserved_count, serial_no,
72
-- tags, uuid
b/src/Ganeti/Query/Query.hs
71 71
import qualified Ganeti.Query.Job as Query.Job
72 72
import Ganeti.Query.Group
73 73
import Ganeti.Query.Language
74
import Ganeti.Query.Network
74 75
import Ganeti.Query.Node
75 76
import Ganeti.Query.Types
76 77
import Ganeti.Path
......
197 198
  fgroups <- filterM (\n -> evaluateFilter cfg Nothing n cfilter) groups
198 199
  let fdata = map (\node ->
199 200
                       map (execGetter cfg GroupRuntime node) fgetters) fgroups
200
  return QueryResult {qresFields = fdefs, qresData = fdata }
201
  return QueryResult { qresFields = fdefs, qresData = fdata }
202

  
203
queryInner cfg _ (Query (ItemTypeOpCode QRNetwork) fields qfilter) wanted =
204
  return $ do
205
  cfilter <- compileFilter networkFieldsMap qfilter
206
  let selected = getSelectedFields networkFieldsMap fields
207
      (fdefs, fgetters, _) = unzip3 selected
208
  networks <- case wanted of
209
                [] -> Ok . niceSortKey (fromNonEmpty . networkName) .
210
                      Map.elems . fromContainer $ configNetworks cfg
211
                _  -> mapM (getNetwork cfg) wanted
212
  fnetworks <- filterM (\n -> evaluateFilter cfg Nothing n cfilter) networks
213
  let fdata = map (\network ->
214
                   map (execGetter cfg NetworkRuntime network) fgetters)
215
                   fnetworks
216
  return QueryResult { qresFields = fdefs, qresData = fdata }
201 217

  
202 218
queryInner _ _ (Query qkind _ _) _ =
203 219
  return . Bad . GenericError $ "Query '" ++ show qkind ++ "' not supported"

Also available in: Unified diff