Revision bac48afe

b/src/Ganeti/Query/Network.hs
37 37

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

  
42 42
import Ganeti.JSON
43 43
import Ganeti.Network
......
88 88
  , (FieldDefinition "inst_cnt" "Instances" QFTNumber "Number of instances",
89 89
     FieldConfig (\cfg -> rsNormal . length . getInstances cfg
90 90
       . networkUuid), QffNormal)
91
  , (FieldDefinition "external_reservations" "ExternalReservations" QFTText
92
     "External reservations",
93
     FieldSimple getExtReservationsString, QffNormal)
91 94
  ] ++
92 95
  uuidFields "Network" ++
93 96
  serialFields "Network" ++
......
149 152
               ((Map.elems . fromContainer . configNetworks) cfg)
150 153
  in fmap networkUuid net
151 154

  
155
-- | Computes the reservations list for a network.
156
--
157
-- This doesn't use the netmask for validation of the length, instead
158
-- simply iterating over the reservations string.
159
getReservations :: Ip4Network -> String -> [Ip4Address]
160
getReservations (Ip4Network net _) =
161
  reverse .
162
  fst .
163
  foldl' (\(accu, addr) c ->
164
            let addr' = nextIp4Address addr
165
                accu' = case c of
166
                          '1' -> addr:accu
167
                          '0' -> accu
168
                          _ -> -- FIXME: the reservations string
169
                               -- should be a proper type
170
                               accu
171
            in (accu', addr')) ([], net)
172

  
173
-- | Computes the external reservations as string for a network.
174
getExtReservationsString :: Network -> ResultEntry
175
getExtReservationsString net =
176
  let addrs = getReservations (networkNetwork net)
177
              (fromMaybe "" $ networkExtReservations net)
178
  in rsNormal . intercalate ", " $ map show addrs
179

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

Also available in: Unified diff