Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Network.hs @ c92b4671

History | View | Annotate | Download (6.8 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 36162faf Iustin Pop
  , fieldsMap
31 05092772 Helga Velroyen
  ) where
32 05092772 Helga Velroyen
33 d286d795 Hrvoje Ribicic
-- FIXME: everything except fieldsMap
34 834bea99 Helga Velroyen
-- is only exported for testing.
35 834bea99 Helga Velroyen
36 05092772 Helga Velroyen
import qualified Data.Map as Map
37 2d3d843d Helga Velroyen
import Data.Maybe (fromMaybe, mapMaybe)
38 bac48afe Iustin Pop
import Data.List (find, foldl', intercalate)
39 05092772 Helga Velroyen
40 2d3d843d Helga Velroyen
import Ganeti.JSON
41 ae960018 Helga Velroyen
import Ganeti.Network
42 05092772 Helga Velroyen
import Ganeti.Objects
43 05092772 Helga Velroyen
import Ganeti.Query.Language
44 05092772 Helga Velroyen
import Ganeti.Query.Common
45 05092772 Helga Velroyen
import Ganeti.Query.Types
46 2d3d843d Helga Velroyen
import Ganeti.Types
47 05092772 Helga Velroyen
48 d286d795 Hrvoje Ribicic
networkFields :: FieldList Network NoDataRuntime
49 05092772 Helga Velroyen
networkFields =
50 60a67263 Iustin Pop
  [ (FieldDefinition "name" "Network" QFTText "Name",
51 05092772 Helga Velroyen
     FieldSimple (rsNormal . networkName), QffNormal)
52 05092772 Helga Velroyen
  , (FieldDefinition "network" "Subnet" QFTText "IPv4 subnet",
53 05092772 Helga Velroyen
     FieldSimple (rsNormal . networkNetwork), QffNormal)
54 05092772 Helga Velroyen
  , (FieldDefinition "gateway" "Gateway" QFTOther "IPv4 gateway",
55 05092772 Helga Velroyen
     FieldSimple (rsMaybeUnavail . networkGateway), QffNormal)
56 05092772 Helga Velroyen
  , (FieldDefinition "network6" "IPv6Subnet" QFTOther "IPv6 subnet",
57 05092772 Helga Velroyen
     FieldSimple (rsMaybeUnavail . networkNetwork6), QffNormal)
58 05092772 Helga Velroyen
  , (FieldDefinition "gateway6" "IPv6Gateway" QFTOther "IPv6 gateway",
59 05092772 Helga Velroyen
     FieldSimple (rsMaybeUnavail . networkGateway6), QffNormal)
60 05092772 Helga Velroyen
  , (FieldDefinition "mac_prefix" "MacPrefix" QFTOther "MAC address prefix",
61 05092772 Helga Velroyen
     FieldSimple (rsMaybeUnavail . networkMacPrefix), QffNormal)
62 60a67263 Iustin Pop
  , (FieldDefinition "free_count" "FreeCount" QFTNumber "Number of available\
63 60a67263 Iustin Pop
                                                       \ addresses",
64 ae960018 Helga Velroyen
     FieldSimple (rsMaybeNoData . fmap getFreeCount . createAddressPool),
65 ae960018 Helga Velroyen
     QffNormal)
66 60a67263 Iustin Pop
  , (FieldDefinition "map" "Map" QFTText "Actual mapping",
67 ae960018 Helga Velroyen
     FieldSimple (rsMaybeNoData . fmap getMap . createAddressPool),
68 ae960018 Helga Velroyen
     QffNormal)
69 60a67263 Iustin Pop
  , (FieldDefinition "reserved_count" "ReservedCount" QFTNumber
70 60a67263 Iustin Pop
       "Number of reserved addresses",
71 ae960018 Helga Velroyen
     FieldSimple (rsMaybeNoData . fmap getReservedCount . createAddressPool),
72 ae960018 Helga Velroyen
     QffNormal)
73 60a67263 Iustin Pop
  , (FieldDefinition "group_list" "GroupList" QFTOther
74 60a67263 Iustin Pop
       "List of nodegroups (group name, NIC mode, NIC link)",
75 05092772 Helga Velroyen
     FieldConfig (\cfg -> rsNormal . getGroupConnections cfg . networkUuid),
76 ae960018 Helga Velroyen
     QffNormal)
77 60a67263 Iustin Pop
  , (FieldDefinition "group_cnt" "NodeGroups" QFTNumber "Number of nodegroups",
78 ae960018 Helga Velroyen
     FieldConfig (\cfg -> rsNormal . length . getGroupConnections cfg
79 ae960018 Helga Velroyen
       . networkUuid), QffNormal)
80 125df9a8 Helga Velroyen
  , (FieldDefinition "inst_list" "InstanceList" QFTOther "List of instances",
81 125df9a8 Helga Velroyen
     FieldConfig (\cfg -> rsNormal . getInstances cfg . networkUuid),
82 125df9a8 Helga Velroyen
     QffNormal)
83 60a67263 Iustin Pop
  , (FieldDefinition "inst_cnt" "Instances" QFTNumber "Number of instances",
84 125df9a8 Helga Velroyen
     FieldConfig (\cfg -> rsNormal . length . getInstances cfg
85 125df9a8 Helga Velroyen
       . networkUuid), QffNormal)
86 bac48afe Iustin Pop
  , (FieldDefinition "external_reservations" "ExternalReservations" QFTText
87 bac48afe Iustin Pop
     "External reservations",
88 bac48afe Iustin Pop
     FieldSimple getExtReservationsString, QffNormal)
89 05092772 Helga Velroyen
  ] ++
90 beb9c009 Christos Stavrakakis
  timeStampFields ++
91 05092772 Helga Velroyen
  uuidFields "Network" ++
92 05092772 Helga Velroyen
  serialFields "Network" ++
93 05092772 Helga Velroyen
  tagsFields
94 05092772 Helga Velroyen
95 05092772 Helga Velroyen
-- | The group fields map.
96 d286d795 Hrvoje Ribicic
fieldsMap :: FieldMap Network NoDataRuntime
97 c92b4671 Klaus Aehlig
fieldsMap = fieldListToFieldMap networkFields
98 05092772 Helga Velroyen
99 834bea99 Helga Velroyen
-- TODO: the following fields are not implemented yet: external_reservations
100 2d3d843d Helga Velroyen
101 2d3d843d Helga Velroyen
-- | Given a network's UUID, this function lists all connections from
102 2d3d843d Helga Velroyen
-- the network to nodegroups including the respective mode and links.
103 2d3d843d Helga Velroyen
getGroupConnections :: ConfigData -> String -> [(String, String, String)]
104 2d3d843d Helga Velroyen
getGroupConnections cfg network_uuid =
105 2d3d843d Helga Velroyen
  mapMaybe (getGroupConnection network_uuid)
106 2d3d843d Helga Velroyen
  ((Map.elems . fromContainer . configNodegroups) cfg)
107 2d3d843d Helga Velroyen
108 2d3d843d Helga Velroyen
-- | Given a network's UUID and a node group, this function assembles
109 2d3d843d Helga Velroyen
-- a tuple of the group's name, the mode and the link by which the
110 2d3d843d Helga Velroyen
-- network is connected to the group. Returns 'Nothing' if the network
111 2d3d843d Helga Velroyen
-- is not connected to the group.
112 2d3d843d Helga Velroyen
getGroupConnection :: String -> NodeGroup -> Maybe (String, String, String)
113 2d3d843d Helga Velroyen
getGroupConnection network_uuid group =
114 2d3d843d Helga Velroyen
  let networks = fromContainer . groupNetworks $ group
115 2d3d843d Helga Velroyen
  in case Map.lookup network_uuid networks of
116 2d3d843d Helga Velroyen
    Nothing -> Nothing
117 2d3d843d Helga Velroyen
    Just net ->
118 2d3d843d Helga Velroyen
      Just (groupName group, getNicMode net, getNicLink net)
119 2d3d843d Helga Velroyen
120 2d3d843d Helga Velroyen
-- | Retrieves the network's mode and formats it human-readable,
121 2d3d843d Helga Velroyen
-- also in case it is not available.
122 2d3d843d Helga Velroyen
getNicMode :: PartialNicParams -> String
123 2d3d843d Helga Velroyen
getNicMode nic_params =
124 2d3d843d Helga Velroyen
  maybe "-" nICModeToRaw $ nicpModeP nic_params
125 2d3d843d Helga Velroyen
126 2d3d843d Helga Velroyen
-- | Retrieves the network's link and formats it human-readable, also in
127 2d3d843d Helga Velroyen
-- case it it not available.
128 2d3d843d Helga Velroyen
getNicLink :: PartialNicParams -> String
129 2d3d843d Helga Velroyen
getNicLink nic_params = fromMaybe "-" (nicpLinkP nic_params)
130 2d3d843d Helga Velroyen
131 125df9a8 Helga Velroyen
-- | Retrieves the network's instances' names.
132 125df9a8 Helga Velroyen
getInstances :: ConfigData -> String -> [String]
133 125df9a8 Helga Velroyen
getInstances cfg network_uuid =
134 0c6d6a52 Helga Velroyen
  map instName (filter (instIsConnected cfg network_uuid)
135 125df9a8 Helga Velroyen
    ((Map.elems . fromContainer . configInstances) cfg))
136 125df9a8 Helga Velroyen
137 125df9a8 Helga Velroyen
-- | Helper function that checks if an instance is linked to the given network.
138 0c6d6a52 Helga Velroyen
instIsConnected :: ConfigData -> String -> Instance -> Bool
139 0c6d6a52 Helga Velroyen
instIsConnected cfg network_uuid inst =
140 0c6d6a52 Helga Velroyen
  network_uuid `elem` mapMaybe (getNetworkUuid cfg)
141 0c6d6a52 Helga Velroyen
    (mapMaybe nicNetwork (instNics inst))
142 0c6d6a52 Helga Velroyen
143 0c6d6a52 Helga Velroyen
-- | Helper function to look up a network's UUID by its name
144 0c6d6a52 Helga Velroyen
getNetworkUuid :: ConfigData -> String -> Maybe String
145 0c6d6a52 Helga Velroyen
getNetworkUuid cfg name =
146 0c6d6a52 Helga Velroyen
  let net = find (\n -> name == fromNonEmpty (networkName n))
147 0c6d6a52 Helga Velroyen
               ((Map.elems . fromContainer . configNetworks) cfg)
148 0c6d6a52 Helga Velroyen
  in fmap networkUuid net
149 d5b2753a Iustin Pop
150 bac48afe Iustin Pop
-- | Computes the reservations list for a network.
151 bac48afe Iustin Pop
--
152 bac48afe Iustin Pop
-- This doesn't use the netmask for validation of the length, instead
153 bac48afe Iustin Pop
-- simply iterating over the reservations string.
154 bac48afe Iustin Pop
getReservations :: Ip4Network -> String -> [Ip4Address]
155 bac48afe Iustin Pop
getReservations (Ip4Network net _) =
156 bac48afe Iustin Pop
  reverse .
157 bac48afe Iustin Pop
  fst .
158 bac48afe Iustin Pop
  foldl' (\(accu, addr) c ->
159 bac48afe Iustin Pop
            let addr' = nextIp4Address addr
160 bac48afe Iustin Pop
                accu' = case c of
161 bac48afe Iustin Pop
                          '1' -> addr:accu
162 bac48afe Iustin Pop
                          '0' -> accu
163 bac48afe Iustin Pop
                          _ -> -- FIXME: the reservations string
164 bac48afe Iustin Pop
                               -- should be a proper type
165 bac48afe Iustin Pop
                               accu
166 bac48afe Iustin Pop
            in (accu', addr')) ([], net)
167 bac48afe Iustin Pop
168 bac48afe Iustin Pop
-- | Computes the external reservations as string for a network.
169 bac48afe Iustin Pop
getExtReservationsString :: Network -> ResultEntry
170 bac48afe Iustin Pop
getExtReservationsString net =
171 bac48afe Iustin Pop
  let addrs = getReservations (networkNetwork net)
172 bac48afe Iustin Pop
              (fromMaybe "" $ networkExtReservations net)
173 bac48afe Iustin Pop
  in rsNormal . intercalate ", " $ map show addrs