Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / Query / Network.hs @ 0c6d6a52

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
  ( NetworkRuntime(..)
28
  , networkFieldsMap
29
  ) where
30

    
31
import qualified Data.Map as Map
32
import Data.Maybe (fromMaybe, mapMaybe)
33
import Data.List (find)
34

    
35
import Ganeti.JSON
36
import Ganeti.Network
37
import Ganeti.Objects
38
import Ganeti.Query.Language
39
import Ganeti.Query.Common
40
import Ganeti.Query.Types
41
import Ganeti.Types
42

    
43
data NetworkRuntime = NetworkRuntime
44

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

    
88
-- | The group fields map.
89
networkFieldsMap :: FieldMap Network NetworkRuntime
90
networkFieldsMap =
91
  Map.fromList $ map (\v@(f, _, _) -> (fdefName f, v)) networkFields
92

    
93
-- TODO: the following fields are not implemented yet: external_reservations,
94
-- inst_cnt, inst_list
95

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

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

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

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

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

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

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