Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Network.hs @ 125df9a8

History | View | Annotate | Download (5.4 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
  , (FieldDefinition "inst_list" "InstanceList" QFTOther "List of instances",
77
     FieldConfig (\cfg -> rsNormal . getInstances cfg . networkUuid),
78
     QffNormal)
79
  , (FieldDefinition "inst_cnt" "InstanceCount" QFTOther "Number of instances",
80
     FieldConfig (\cfg -> rsNormal . length . getInstances cfg
81
       . networkUuid), QffNormal)
82
  ] ++
83
  uuidFields "Network" ++
84
  serialFields "Network" ++
85
  tagsFields
86

    
87
-- | The group fields map.
88
networkFieldsMap :: FieldMap Network NetworkRuntime
89
networkFieldsMap =
90
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields
91

    
92
-- TODO: the following fields are not implemented yet: external_reservations,
93
-- inst_cnt, inst_list
94

    
95
-- | Given a network's UUID, this function lists all connections from
96
-- the network to nodegroups including the respective mode and links.
97
getGroupConnections :: ConfigData -> String -> [(String, String, String)]
98
getGroupConnections cfg network_uuid =
99
  mapMaybe (getGroupConnection network_uuid)
100
  ((Map.elems . fromContainer . configNodegroups) cfg)
101

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

    
114
-- | Retrieves the network's mode and formats it human-readable,
115
-- also in case it is not available.
116
getNicMode :: PartialNicParams -> String
117
getNicMode nic_params =
118
  maybe "-" nICModeToRaw $ nicpModeP nic_params
119

    
120
-- | Retrieves the network's link and formats it human-readable, also in
121
-- case it it not available.
122
getNicLink :: PartialNicParams -> String
123
getNicLink nic_params = fromMaybe "-" (nicpLinkP nic_params)
124

    
125
-- | Retrieves the network's instances' names.
126
getInstances :: ConfigData -> String -> [String]
127
getInstances cfg network_uuid =
128
  map instName (filter (instIsConnected network_uuid)
129
    ((Map.elems . fromContainer . configInstances) cfg))
130

    
131
-- | Helper function that checks if an instance is linked to the given network.
132
instIsConnected :: String -> Instance -> Bool
133
instIsConnected network_uuid inst =
134
  network_uuid `elem` map networkUuid (mapMaybe nicNetwork (instNics inst))