root / src / Ganeti / Query / Network.hs @ beb9c009
History | View | Annotate | Download (7.2 kB)
1 | 05092772 | Helga Velroyen | {-| Implementation of the Ganeti Query2 node group queries. |
---|---|---|---|
2 | 05092772 | Helga Velroyen | |
3 | 05092772 | Helga Velroyen | -} |
4 | 05092772 | Helga Velroyen | |
5 | 05092772 | Helga Velroyen | {- |
6 | 05092772 | Helga Velroyen | |
7 | 36162faf | Iustin Pop | Copyright (C) 2012, 2013 Google Inc. |
8 | 05092772 | Helga Velroyen | |
9 | 05092772 | Helga Velroyen | This program is free software; you can redistribute it and/or modify |
10 | 05092772 | Helga Velroyen | it under the terms of the GNU General Public License as published by |
11 | 05092772 | Helga Velroyen | the Free Software Foundation; either version 2 of the License, or |
12 | 05092772 | Helga Velroyen | (at your option) any later version. |
13 | 05092772 | Helga Velroyen | |
14 | 05092772 | Helga Velroyen | This program is distributed in the hope that it will be useful, but |
15 | 05092772 | Helga Velroyen | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | 05092772 | Helga Velroyen | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | 05092772 | Helga Velroyen | General Public License for more details. |
18 | 05092772 | Helga Velroyen | |
19 | 05092772 | Helga Velroyen | You should have received a copy of the GNU General Public License |
20 | 05092772 | Helga Velroyen | along with this program; if not, write to the Free Software |
21 | 05092772 | Helga Velroyen | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
22 | 05092772 | Helga Velroyen | 02110-1301, USA. |
23 | 05092772 | Helga Velroyen | |
24 | 05092772 | Helga Velroyen | -} |
25 | 05092772 | Helga Velroyen | |
26 | 05092772 | Helga Velroyen | module Ganeti.Query.Network |
27 | 834bea99 | Helga Velroyen | ( getGroupConnection |
28 | 834bea99 | Helga Velroyen | , getNetworkUuid |
29 | 834bea99 | Helga Velroyen | , instIsConnected |
30 | d5b2753a | Iustin Pop | , Runtime |
31 | 36162faf | Iustin Pop | , fieldsMap |
32 | d5b2753a | Iustin Pop | , collectLiveData |
33 | 05092772 | Helga Velroyen | ) where |
34 | 05092772 | Helga Velroyen | |
35 | 36162faf | Iustin Pop | -- FIXME: everything except Runtime(..) and fieldsMap |
36 | 834bea99 | Helga Velroyen | -- is only exported for testing. |
37 | 834bea99 | Helga Velroyen | |
38 | 05092772 | Helga Velroyen | import qualified Data.Map as Map |
39 | 2d3d843d | Helga Velroyen | import Data.Maybe (fromMaybe, mapMaybe) |
40 | bac48afe | Iustin Pop | import Data.List (find, foldl', intercalate) |
41 | 05092772 | Helga Velroyen | |
42 | 2d3d843d | Helga Velroyen | import Ganeti.JSON |
43 | ae960018 | Helga Velroyen | import Ganeti.Network |
44 | 05092772 | Helga Velroyen | import Ganeti.Objects |
45 | 05092772 | Helga Velroyen | import Ganeti.Query.Language |
46 | 05092772 | Helga Velroyen | import Ganeti.Query.Common |
47 | 05092772 | Helga Velroyen | import Ganeti.Query.Types |
48 | 2d3d843d | Helga Velroyen | import Ganeti.Types |
49 | 05092772 | Helga Velroyen | |
50 | 36162faf | Iustin Pop | -- | There is no actual runtime. |
51 | 36162faf | Iustin Pop | data Runtime = Runtime |
52 | 05092772 | Helga Velroyen | |
53 | 36162faf | Iustin Pop | networkFields :: FieldList Network Runtime |
54 | 05092772 | Helga Velroyen | networkFields = |
55 | 60a67263 | Iustin Pop | [ (FieldDefinition "name" "Network" QFTText "Name", |
56 | 05092772 | Helga Velroyen | FieldSimple (rsNormal . networkName), QffNormal) |
57 | 05092772 | Helga Velroyen | , (FieldDefinition "network" "Subnet" QFTText "IPv4 subnet", |
58 | 05092772 | Helga Velroyen | FieldSimple (rsNormal . networkNetwork), QffNormal) |
59 | 05092772 | Helga Velroyen | , (FieldDefinition "gateway" "Gateway" QFTOther "IPv4 gateway", |
60 | 05092772 | Helga Velroyen | FieldSimple (rsMaybeUnavail . networkGateway), QffNormal) |
61 | 05092772 | Helga Velroyen | , (FieldDefinition "network6" "IPv6Subnet" QFTOther "IPv6 subnet", |
62 | 05092772 | Helga Velroyen | FieldSimple (rsMaybeUnavail . networkNetwork6), QffNormal) |
63 | 05092772 | Helga Velroyen | , (FieldDefinition "gateway6" "IPv6Gateway" QFTOther "IPv6 gateway", |
64 | 05092772 | Helga Velroyen | FieldSimple (rsMaybeUnavail . networkGateway6), QffNormal) |
65 | 05092772 | Helga Velroyen | , (FieldDefinition "mac_prefix" "MacPrefix" QFTOther "MAC address prefix", |
66 | 05092772 | Helga Velroyen | FieldSimple (rsMaybeUnavail . networkMacPrefix), QffNormal) |
67 | 60a67263 | Iustin Pop | , (FieldDefinition "free_count" "FreeCount" QFTNumber "Number of available\ |
68 | 60a67263 | Iustin Pop | \ addresses", |
69 | ae960018 | Helga Velroyen | FieldSimple (rsMaybeNoData . fmap getFreeCount . createAddressPool), |
70 | ae960018 | Helga Velroyen | QffNormal) |
71 | 60a67263 | Iustin Pop | , (FieldDefinition "map" "Map" QFTText "Actual mapping", |
72 | ae960018 | Helga Velroyen | FieldSimple (rsMaybeNoData . fmap getMap . createAddressPool), |
73 | ae960018 | Helga Velroyen | QffNormal) |
74 | 60a67263 | Iustin Pop | , (FieldDefinition "reserved_count" "ReservedCount" QFTNumber |
75 | 60a67263 | Iustin Pop | "Number of reserved addresses", |
76 | ae960018 | Helga Velroyen | FieldSimple (rsMaybeNoData . fmap getReservedCount . createAddressPool), |
77 | ae960018 | Helga Velroyen | QffNormal) |
78 | 60a67263 | Iustin Pop | , (FieldDefinition "group_list" "GroupList" QFTOther |
79 | 60a67263 | Iustin Pop | "List of nodegroups (group name, NIC mode, NIC link)", |
80 | 05092772 | Helga Velroyen | FieldConfig (\cfg -> rsNormal . getGroupConnections cfg . networkUuid), |
81 | ae960018 | Helga Velroyen | QffNormal) |
82 | 60a67263 | Iustin Pop | , (FieldDefinition "group_cnt" "NodeGroups" QFTNumber "Number of nodegroups", |
83 | ae960018 | Helga Velroyen | FieldConfig (\cfg -> rsNormal . length . getGroupConnections cfg |
84 | ae960018 | Helga Velroyen | . networkUuid), QffNormal) |
85 | 125df9a8 | Helga Velroyen | , (FieldDefinition "inst_list" "InstanceList" QFTOther "List of instances", |
86 | 125df9a8 | Helga Velroyen | FieldConfig (\cfg -> rsNormal . getInstances cfg . networkUuid), |
87 | 125df9a8 | Helga Velroyen | QffNormal) |
88 | 60a67263 | Iustin Pop | , (FieldDefinition "inst_cnt" "Instances" QFTNumber "Number of instances", |
89 | 125df9a8 | Helga Velroyen | FieldConfig (\cfg -> rsNormal . length . getInstances cfg |
90 | 125df9a8 | Helga Velroyen | . networkUuid), QffNormal) |
91 | bac48afe | Iustin Pop | , (FieldDefinition "external_reservations" "ExternalReservations" QFTText |
92 | bac48afe | Iustin Pop | "External reservations", |
93 | bac48afe | Iustin Pop | FieldSimple getExtReservationsString, QffNormal) |
94 | 05092772 | Helga Velroyen | ] ++ |
95 | beb9c009 | Christos Stavrakakis | timeStampFields ++ |
96 | 05092772 | Helga Velroyen | uuidFields "Network" ++ |
97 | 05092772 | Helga Velroyen | serialFields "Network" ++ |
98 | 05092772 | Helga Velroyen | tagsFields |
99 | 05092772 | Helga Velroyen | |
100 | 05092772 | Helga Velroyen | -- | The group fields map. |
101 | 36162faf | Iustin Pop | fieldsMap :: FieldMap Network Runtime |
102 | 36162faf | Iustin Pop | fieldsMap = |
103 | 05092772 | Helga Velroyen | Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields |
104 | 05092772 | Helga Velroyen | |
105 | 834bea99 | Helga Velroyen | -- TODO: the following fields are not implemented yet: external_reservations |
106 | 2d3d843d | Helga Velroyen | |
107 | 2d3d843d | Helga Velroyen | -- | Given a network's UUID, this function lists all connections from |
108 | 2d3d843d | Helga Velroyen | -- the network to nodegroups including the respective mode and links. |
109 | 2d3d843d | Helga Velroyen | getGroupConnections :: ConfigData -> String -> [(String, String, String)] |
110 | 2d3d843d | Helga Velroyen | getGroupConnections cfg network_uuid = |
111 | 2d3d843d | Helga Velroyen | mapMaybe (getGroupConnection network_uuid) |
112 | 2d3d843d | Helga Velroyen | ((Map.elems . fromContainer . configNodegroups) cfg) |
113 | 2d3d843d | Helga Velroyen | |
114 | 2d3d843d | Helga Velroyen | -- | Given a network's UUID and a node group, this function assembles |
115 | 2d3d843d | Helga Velroyen | -- a tuple of the group's name, the mode and the link by which the |
116 | 2d3d843d | Helga Velroyen | -- network is connected to the group. Returns 'Nothing' if the network |
117 | 2d3d843d | Helga Velroyen | -- is not connected to the group. |
118 | 2d3d843d | Helga Velroyen | getGroupConnection :: String -> NodeGroup -> Maybe (String, String, String) |
119 | 2d3d843d | Helga Velroyen | getGroupConnection network_uuid group = |
120 | 2d3d843d | Helga Velroyen | let networks = fromContainer . groupNetworks $ group |
121 | 2d3d843d | Helga Velroyen | in case Map.lookup network_uuid networks of |
122 | 2d3d843d | Helga Velroyen | Nothing -> Nothing |
123 | 2d3d843d | Helga Velroyen | Just net -> |
124 | 2d3d843d | Helga Velroyen | Just (groupName group, getNicMode net, getNicLink net) |
125 | 2d3d843d | Helga Velroyen | |
126 | 2d3d843d | Helga Velroyen | -- | Retrieves the network's mode and formats it human-readable, |
127 | 2d3d843d | Helga Velroyen | -- also in case it is not available. |
128 | 2d3d843d | Helga Velroyen | getNicMode :: PartialNicParams -> String |
129 | 2d3d843d | Helga Velroyen | getNicMode nic_params = |
130 | 2d3d843d | Helga Velroyen | maybe "-" nICModeToRaw $ nicpModeP nic_params |
131 | 2d3d843d | Helga Velroyen | |
132 | 2d3d843d | Helga Velroyen | -- | Retrieves the network's link and formats it human-readable, also in |
133 | 2d3d843d | Helga Velroyen | -- case it it not available. |
134 | 2d3d843d | Helga Velroyen | getNicLink :: PartialNicParams -> String |
135 | 2d3d843d | Helga Velroyen | getNicLink nic_params = fromMaybe "-" (nicpLinkP nic_params) |
136 | 2d3d843d | Helga Velroyen | |
137 | 125df9a8 | Helga Velroyen | -- | Retrieves the network's instances' names. |
138 | 125df9a8 | Helga Velroyen | getInstances :: ConfigData -> String -> [String] |
139 | 125df9a8 | Helga Velroyen | getInstances cfg network_uuid = |
140 | 0c6d6a52 | Helga Velroyen | map instName (filter (instIsConnected cfg network_uuid) |
141 | 125df9a8 | Helga Velroyen | ((Map.elems . fromContainer . configInstances) cfg)) |
142 | 125df9a8 | Helga Velroyen | |
143 | 125df9a8 | Helga Velroyen | -- | Helper function that checks if an instance is linked to the given network. |
144 | 0c6d6a52 | Helga Velroyen | instIsConnected :: ConfigData -> String -> Instance -> Bool |
145 | 0c6d6a52 | Helga Velroyen | instIsConnected cfg network_uuid inst = |
146 | 0c6d6a52 | Helga Velroyen | network_uuid `elem` mapMaybe (getNetworkUuid cfg) |
147 | 0c6d6a52 | Helga Velroyen | (mapMaybe nicNetwork (instNics inst)) |
148 | 0c6d6a52 | Helga Velroyen | |
149 | 0c6d6a52 | Helga Velroyen | -- | Helper function to look up a network's UUID by its name |
150 | 0c6d6a52 | Helga Velroyen | getNetworkUuid :: ConfigData -> String -> Maybe String |
151 | 0c6d6a52 | Helga Velroyen | getNetworkUuid cfg name = |
152 | 0c6d6a52 | Helga Velroyen | let net = find (\n -> name == fromNonEmpty (networkName n)) |
153 | 0c6d6a52 | Helga Velroyen | ((Map.elems . fromContainer . configNetworks) cfg) |
154 | 0c6d6a52 | Helga Velroyen | in fmap networkUuid net |
155 | d5b2753a | Iustin Pop | |
156 | bac48afe | Iustin Pop | -- | Computes the reservations list for a network. |
157 | bac48afe | Iustin Pop | -- |
158 | bac48afe | Iustin Pop | -- This doesn't use the netmask for validation of the length, instead |
159 | bac48afe | Iustin Pop | -- simply iterating over the reservations string. |
160 | bac48afe | Iustin Pop | getReservations :: Ip4Network -> String -> [Ip4Address] |
161 | bac48afe | Iustin Pop | getReservations (Ip4Network net _) = |
162 | bac48afe | Iustin Pop | reverse . |
163 | bac48afe | Iustin Pop | fst . |
164 | bac48afe | Iustin Pop | foldl' (\(accu, addr) c -> |
165 | bac48afe | Iustin Pop | let addr' = nextIp4Address addr |
166 | bac48afe | Iustin Pop | accu' = case c of |
167 | bac48afe | Iustin Pop | '1' -> addr:accu |
168 | bac48afe | Iustin Pop | '0' -> accu |
169 | bac48afe | Iustin Pop | _ -> -- FIXME: the reservations string |
170 | bac48afe | Iustin Pop | -- should be a proper type |
171 | bac48afe | Iustin Pop | accu |
172 | bac48afe | Iustin Pop | in (accu', addr')) ([], net) |
173 | bac48afe | Iustin Pop | |
174 | bac48afe | Iustin Pop | -- | Computes the external reservations as string for a network. |
175 | bac48afe | Iustin Pop | getExtReservationsString :: Network -> ResultEntry |
176 | bac48afe | Iustin Pop | getExtReservationsString net = |
177 | bac48afe | Iustin Pop | let addrs = getReservations (networkNetwork net) |
178 | bac48afe | Iustin Pop | (fromMaybe "" $ networkExtReservations net) |
179 | bac48afe | Iustin Pop | in rsNormal . intercalate ", " $ map show addrs |
180 | bac48afe | Iustin Pop | |
181 | d5b2753a | Iustin Pop | -- | Dummy function for collecting live data (which networks don't have). |
182 | d5b2753a | Iustin Pop | collectLiveData :: Bool -> ConfigData -> [Network] -> IO [(Network, Runtime)] |
183 | d5b2753a | Iustin Pop | collectLiveData _ _ = return . map (\n -> (n, Runtime)) |