Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Network.hs @ 2d3d843d

History | View | Annotate | Download (4.6 kB)

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
import Data.Maybe (fromMaybe, mapMaybe)
33

    
34
import Ganeti.JSON
35
import Ganeti.Network
36
import Ganeti.Objects
37
import Ganeti.Query.Language
38
import Ganeti.Query.Common
39
import Ganeti.Query.Types
40
import Ganeti.Types
41

    
42
data NetworkRuntime = NetworkRuntime
43

    
44
networkFields :: FieldList Network NetworkRuntime
45
networkFields =
46
  [ (FieldDefinition "name" "Name" QFTText "Network name",
47
     FieldSimple (rsNormal . networkName), QffNormal)
48
  , (FieldDefinition "network" "Subnet" QFTText "IPv4 subnet",
49
     FieldSimple (rsNormal . networkNetwork), QffNormal)
50
  , (FieldDefinition "gateway" "Gateway" QFTOther "IPv4 gateway",
51
     FieldSimple (rsMaybeUnavail . networkGateway), QffNormal)
52
  , (FieldDefinition "network6" "IPv6Subnet" QFTOther "IPv6 subnet",
53
     FieldSimple (rsMaybeUnavail . networkNetwork6), QffNormal)
54
  , (FieldDefinition "gateway6" "IPv6Gateway" QFTOther "IPv6 gateway",
55
     FieldSimple (rsMaybeUnavail . networkGateway6), QffNormal)
56
  , (FieldDefinition "mac_prefix" "MacPrefix" QFTOther "MAC address prefix",
57
     FieldSimple (rsMaybeUnavail . networkMacPrefix), QffNormal)
58
  , (FieldDefinition "network_type" "NetworkType" QFTOther "Network type",
59
     FieldSimple (rsMaybeUnavail . networkNetworkType), QffNormal)
60
  , (FieldDefinition "free_count" "FreeCount" QFTOther "Number of free IPs",
61
     FieldSimple (rsMaybeNoData . fmap getFreeCount . createAddressPool),
62
     QffNormal)
63
  , (FieldDefinition "map" "Map" QFTText "Map of the network's reserved IPs",
64
     FieldSimple (rsMaybeNoData . fmap getMap . createAddressPool),
65
     QffNormal)
66
  , (FieldDefinition "reserved_count" "ReservedCount" QFTOther
67
       "Number of reserved IPs",
68
     FieldSimple (rsMaybeNoData . fmap getReservedCount . createAddressPool),
69
     QffNormal)
70
  , (FieldDefinition "group_list" "GroupList" QFTOther "List of node groups",
71
     FieldConfig (\cfg -> rsNormal . getGroupConnections cfg . networkUuid),
72
     QffNormal)
73
  , (FieldDefinition "group_cnt" "GroupCount" QFTOther "Number of node groups",
74
     FieldConfig (\cfg -> rsNormal . length . getGroupConnections cfg
75
       . networkUuid), QffNormal)
76
  ] ++
77
  uuidFields "Network" ++
78
  serialFields "Network" ++
79
  tagsFields
80

    
81
-- | The group fields map.
82
networkFieldsMap :: FieldMap Network NetworkRuntime
83
networkFieldsMap =
84
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields
85

    
86
-- TODO: the following fields are not implemented yet: external_reservations,
87
-- inst_cnt, inst_list
88

    
89
-- | Given a network's UUID, this function lists all connections from
90
-- the network to nodegroups including the respective mode and links.
91
getGroupConnections :: ConfigData -> String -> [(String, String, String)]
92
getGroupConnections cfg network_uuid =
93
  mapMaybe (getGroupConnection network_uuid)
94
  ((Map.elems . fromContainer . configNodegroups) cfg)
95

    
96
-- | Given a network's UUID and a node group, this function assembles
97
-- a tuple of the group's name, the mode and the link by which the
98
-- network is connected to the group. Returns 'Nothing' if the network
99
-- is not connected to the group.
100
getGroupConnection :: String -> NodeGroup -> Maybe (String, String, String)
101
getGroupConnection network_uuid group =
102
  let networks = fromContainer . groupNetworks $ group
103
  in case Map.lookup network_uuid networks of
104
    Nothing -> Nothing
105
    Just net ->
106
      Just (groupName group, getNicMode net, getNicLink net)
107

    
108
-- | Retrieves the network's mode and formats it human-readable,
109
-- also in case it is not available.
110
getNicMode :: PartialNicParams -> String
111
getNicMode nic_params =
112
  maybe "-" nICModeToRaw $ nicpModeP nic_params
113

    
114
-- | Retrieves the network's link and formats it human-readable, also in
115
-- case it it not available.
116
getNicLink :: PartialNicParams -> String
117
getNicLink nic_params = fromMaybe "-" (nicpLinkP nic_params)
118