Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Network.hs @ 834bea99

History | View | Annotate | Download (5.7 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
  ( getGroupConnection
28
  , getNetworkUuid
29
  , instIsConnected
30
  , NetworkRuntime(..)
31
  , networkFieldsMap
32
  ) where
33

    
34
-- FIXME: everything except NetworkRuntime(..) and networkFieldsMap
35
-- is only exported for testing.
36

    
37
import qualified Data.Map as Map
38
import Data.Maybe (fromMaybe, mapMaybe)
39
import Data.List (find)
40

    
41
import Ganeti.JSON
42
import Ganeti.Network
43
import Ganeti.Objects
44
import Ganeti.Query.Language
45
import Ganeti.Query.Common
46
import Ganeti.Query.Types
47
import Ganeti.Types
48

    
49
data NetworkRuntime = NetworkRuntime
50

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

    
92
-- | The group fields map.
93
networkFieldsMap :: FieldMap Network NetworkRuntime
94
networkFieldsMap =
95
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields
96

    
97
-- TODO: the following fields are not implemented yet: external_reservations
98

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

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

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

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

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

    
135
-- | Helper function that checks if an instance is linked to the given network.
136
instIsConnected :: ConfigData -> String -> Instance -> Bool
137
instIsConnected cfg network_uuid inst =
138
  network_uuid `elem` mapMaybe (getNetworkUuid cfg)
139
    (mapMaybe nicNetwork (instNics inst))
140

    
141
-- | Helper function to look up a network's UUID by its name
142
getNetworkUuid :: ConfigData -> String -> Maybe String
143
getNetworkUuid cfg name =
144
  let net = find (\n -> name == fromNonEmpty (networkName n))
145
               ((Map.elems . fromContainer . configNetworks) cfg)
146
  in fmap networkUuid net