Revision 05092772 src/Ganeti/Config.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 =

Also available in: Unified diff