Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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
  , collectLiveData
33
  ) where
34

    
35
-- FIXME: everything except Runtime(..) and fieldsMap
36
-- is only exported for testing.
37

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

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

    
50
-- | There is no actual runtime.
51
data Runtime = Runtime
52

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

    
94
-- | The group fields map.
95
fieldsMap :: FieldMap Network Runtime
96
fieldsMap =
97
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields
98

    
99
-- TODO: the following fields are not implemented yet: external_reservations
100

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

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

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

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

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

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

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

    
150
-- | Dummy function for collecting live data (which networks don't have).
151
collectLiveData :: Bool -> ConfigData -> [Network] -> IO [(Network, Runtime)]
152
collectLiveData _ _ = return . map (\n -> (n, Runtime))