root / src / Ganeti / Query / Network.hs @ 36162faf
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, 2013 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 |
, Runtime(..) |
31 |
, fieldsMap |
32 |
) where |
33 |
|
34 |
-- FIXME: everything except Runtime(..) and fieldsMap |
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 |
-- | There is no actual runtime. |
50 |
data Runtime = Runtime |
51 |
|
52 |
networkFields :: FieldList Network Runtime |
53 |
networkFields = |
54 |
[ (FieldDefinition "name" "Name" QFTText "Network name", |
55 |
FieldSimple (rsNormal . networkName), QffNormal) |
56 |
, (FieldDefinition "network" "Subnet" QFTText "IPv4 subnet", |
57 |
FieldSimple (rsNormal . networkNetwork), QffNormal) |
58 |
, (FieldDefinition "gateway" "Gateway" QFTOther "IPv4 gateway", |
59 |
FieldSimple (rsMaybeUnavail . networkGateway), QffNormal) |
60 |
, (FieldDefinition "network6" "IPv6Subnet" QFTOther "IPv6 subnet", |
61 |
FieldSimple (rsMaybeUnavail . networkNetwork6), QffNormal) |
62 |
, (FieldDefinition "gateway6" "IPv6Gateway" QFTOther "IPv6 gateway", |
63 |
FieldSimple (rsMaybeUnavail . networkGateway6), QffNormal) |
64 |
, (FieldDefinition "mac_prefix" "MacPrefix" QFTOther "MAC address prefix", |
65 |
FieldSimple (rsMaybeUnavail . networkMacPrefix), QffNormal) |
66 |
, (FieldDefinition "free_count" "FreeCount" QFTOther "Number of free IPs", |
67 |
FieldSimple (rsMaybeNoData . fmap getFreeCount . createAddressPool), |
68 |
QffNormal) |
69 |
, (FieldDefinition "map" "Map" QFTText "Map of the network's reserved IPs", |
70 |
FieldSimple (rsMaybeNoData . fmap getMap . createAddressPool), |
71 |
QffNormal) |
72 |
, (FieldDefinition "reserved_count" "ReservedCount" QFTOther |
73 |
"Number of reserved IPs", |
74 |
FieldSimple (rsMaybeNoData . fmap getReservedCount . createAddressPool), |
75 |
QffNormal) |
76 |
, (FieldDefinition "group_list" "GroupList" QFTOther "List of node groups", |
77 |
FieldConfig (\cfg -> rsNormal . getGroupConnections cfg . networkUuid), |
78 |
QffNormal) |
79 |
, (FieldDefinition "group_cnt" "GroupCount" QFTOther "Number of node groups", |
80 |
FieldConfig (\cfg -> rsNormal . length . getGroupConnections cfg |
81 |
. networkUuid), QffNormal) |
82 |
, (FieldDefinition "inst_list" "InstanceList" QFTOther "List of instances", |
83 |
FieldConfig (\cfg -> rsNormal . getInstances cfg . networkUuid), |
84 |
QffNormal) |
85 |
, (FieldDefinition "inst_cnt" "InstanceCount" QFTOther "Number of instances", |
86 |
FieldConfig (\cfg -> rsNormal . length . getInstances cfg |
87 |
. networkUuid), QffNormal) |
88 |
] ++ |
89 |
uuidFields "Network" ++ |
90 |
serialFields "Network" ++ |
91 |
tagsFields |
92 |
|
93 |
-- | The group fields map. |
94 |
fieldsMap :: FieldMap Network Runtime |
95 |
fieldsMap = |
96 |
Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields |
97 |
|
98 |
-- TODO: the following fields are not implemented yet: external_reservations |
99 |
|
100 |
-- | Given a network's UUID, this function lists all connections from |
101 |
-- the network to nodegroups including the respective mode and links. |
102 |
getGroupConnections :: ConfigData -> String -> [(String, String, String)] |
103 |
getGroupConnections cfg network_uuid = |
104 |
mapMaybe (getGroupConnection network_uuid) |
105 |
((Map.elems . fromContainer . configNodegroups) cfg) |
106 |
|
107 |
-- | Given a network's UUID and a node group, this function assembles |
108 |
-- a tuple of the group's name, the mode and the link by which the |
109 |
-- network is connected to the group. Returns 'Nothing' if the network |
110 |
-- is not connected to the group. |
111 |
getGroupConnection :: String -> NodeGroup -> Maybe (String, String, String) |
112 |
getGroupConnection network_uuid group = |
113 |
let networks = fromContainer . groupNetworks $ group |
114 |
in case Map.lookup network_uuid networks of |
115 |
Nothing -> Nothing |
116 |
Just net -> |
117 |
Just (groupName group, getNicMode net, getNicLink net) |
118 |
|
119 |
-- | Retrieves the network's mode and formats it human-readable, |
120 |
-- also in case it is not available. |
121 |
getNicMode :: PartialNicParams -> String |
122 |
getNicMode nic_params = |
123 |
maybe "-" nICModeToRaw $ nicpModeP nic_params |
124 |
|
125 |
-- | Retrieves the network's link and formats it human-readable, also in |
126 |
-- case it it not available. |
127 |
getNicLink :: PartialNicParams -> String |
128 |
getNicLink nic_params = fromMaybe "-" (nicpLinkP nic_params) |
129 |
|
130 |
-- | Retrieves the network's instances' names. |
131 |
getInstances :: ConfigData -> String -> [String] |
132 |
getInstances cfg network_uuid = |
133 |
map instName (filter (instIsConnected cfg network_uuid) |
134 |
((Map.elems . fromContainer . configInstances) cfg)) |
135 |
|
136 |
-- | Helper function that checks if an instance is linked to the given network. |
137 |
instIsConnected :: ConfigData -> String -> Instance -> Bool |
138 |
instIsConnected cfg network_uuid inst = |
139 |
network_uuid `elem` mapMaybe (getNetworkUuid cfg) |
140 |
(mapMaybe nicNetwork (instNics inst)) |
141 |
|
142 |
-- | Helper function to look up a network's UUID by its name |
143 |
getNetworkUuid :: ConfigData -> String -> Maybe String |
144 |
getNetworkUuid cfg name = |
145 |
let net = find (\n -> name == fromNonEmpty (networkName n)) |
146 |
((Map.elems . fromContainer . configNetworks) cfg) |
147 |
in fmap networkUuid net |