module Ganeti.Query.Network
( getGroupConnection
, getNetworkUuid
, instIsConnected
, fieldsMap
) where
import qualified Data.Map as Map
import Data.Maybe (fromMaybe, mapMaybe)
import Data.List (find, intercalate)
import Ganeti.JSON
import Ganeti.Network
import Ganeti.Objects
import qualified Ganeti.Objects.BitArray as BA
import Ganeti.Query.Language
import Ganeti.Query.Common
import Ganeti.Query.Types
import Ganeti.Types
networkFields :: FieldList Network NoDataRuntime
networkFields =
[ (FieldDefinition "name" "Network" QFTText "Name",
FieldSimple (rsNormal . networkName), QffNormal)
, (FieldDefinition "network" "Subnet" QFTText "IPv4 subnet",
FieldSimple (rsNormal . networkNetwork), QffNormal)
, (FieldDefinition "gateway" "Gateway" QFTOther "IPv4 gateway",
FieldSimple (rsMaybeUnavail . networkGateway), QffNormal)
, (FieldDefinition "network6" "IPv6Subnet" QFTOther "IPv6 subnet",
FieldSimple (rsMaybeUnavail . networkNetwork6), QffNormal)
, (FieldDefinition "gateway6" "IPv6Gateway" QFTOther "IPv6 gateway",
FieldSimple (rsMaybeUnavail . networkGateway6), QffNormal)
, (FieldDefinition "mac_prefix" "MacPrefix" QFTOther "MAC address prefix",
FieldSimple (rsMaybeUnavail . networkMacPrefix), QffNormal)
, (FieldDefinition "free_count" "FreeCount" QFTNumber "Number of available\
\ addresses",
FieldSimple (rsNormal . getFreeCount),
QffNormal)
, (FieldDefinition "map" "Map" QFTText "Actual mapping",
FieldSimple (rsNormal . getMap),
QffNormal)
, (FieldDefinition "reserved_count" "ReservedCount" QFTNumber
"Number of reserved addresses",
FieldSimple (rsNormal . getReservedCount),
QffNormal)
, (FieldDefinition "group_list" "GroupList" QFTOther
"List of nodegroups (group name, NIC mode, NIC link)",
FieldConfig (\cfg -> rsNormal . getGroupConnections cfg . networkUuid),
QffNormal)
, (FieldDefinition "group_cnt" "NodeGroups" QFTNumber "Number of nodegroups",
FieldConfig (\cfg -> rsNormal . length . getGroupConnections cfg
. networkUuid), QffNormal)
, (FieldDefinition "inst_list" "InstanceList" QFTOther "List of instances",
FieldConfig (\cfg -> rsNormal . getInstances cfg . networkUuid),
QffNormal)
, (FieldDefinition "inst_cnt" "Instances" QFTNumber "Number of instances",
FieldConfig (\cfg -> rsNormal . length . getInstances cfg
. networkUuid), QffNormal)
, (FieldDefinition "external_reservations" "ExternalReservations" QFTText
"External reservations",
FieldSimple getExtReservationsString, QffNormal)
] ++
timeStampFields ++
uuidFields "Network" ++
serialFields "Network" ++
tagsFields
fieldsMap :: FieldMap Network NoDataRuntime
fieldsMap = fieldListToFieldMap networkFields
getGroupConnections ::
ConfigData -> String -> [(String, String, String, String)]
getGroupConnections cfg network_uuid =
mapMaybe (getGroupConnection network_uuid)
((Map.elems . fromContainer . configNodegroups) cfg)
getGroupConnection ::
String -> NodeGroup -> Maybe (String, String, String, String)
getGroupConnection network_uuid group =
let networks = fromContainer . groupNetworks $ group
in case Map.lookup network_uuid networks of
Nothing -> Nothing
Just net ->
Just (groupName group, getNicMode net, getNicLink net, getNicVlan net)
getNicMode :: PartialNicParams -> String
getNicMode nic_params =
maybe "-" nICModeToRaw $ nicpModeP nic_params
getNicLink :: PartialNicParams -> String
getNicLink nic_params = fromMaybe "-" (nicpLinkP nic_params)
getNicVlan :: PartialNicParams -> String
getNicVlan nic_params = fromMaybe "-" (nicpVlanP nic_params)
getInstances :: ConfigData -> String -> [String]
getInstances cfg network_uuid =
mapMaybe instName (filter (instIsConnected network_uuid)
((Map.elems . fromContainer . configInstances) cfg))
instIsConnected :: String -> Instance -> Bool
instIsConnected network_uuid inst =
network_uuid `elem` mapMaybe nicNetwork (instNics inst)
getNetworkUuid :: ConfigData -> String -> Maybe String
getNetworkUuid cfg name =
let net = find (\n -> name == fromNonEmpty (networkName n))
((Map.elems . fromContainer . configNetworks) cfg)
in fmap networkUuid net
getReservations :: Ip4Network -> Maybe AddressPool -> [Ip4Address]
getReservations _ Nothing = []
getReservations net (Just pool) =
map snd . filter fst
$ zip (BA.toList . apReservations $ pool)
(iterate nextIp4Address $ ip4netAddr net)
getExtReservationsString :: Network -> ResultEntry
getExtReservationsString net =
let addrs = getReservations (networkNetwork net)
(networkExtReservations net)
in rsNormal . intercalate ", " $ map show addrs