Revision ee8bb326

b/src/Ganeti/Query/Instance.hs
770 770
  in zip hvs . map ((Map.!) hvParamMap . hypervisorToRaw) $ hvs
771 771

  
772 772
-- | Collect live data from RPC query if enabled.
773
collectLiveData :: Bool -> ConfigData -> [Instance] -> IO [(Instance, Runtime)]
774
collectLiveData liveDataEnabled cfg instances
773
collectLiveData :: Bool        -- ^ Live queries allowed
774
                -> ConfigData  -- ^ The cluster config
775
                -> [String]    -- ^ The requested fields
776
                -> [Instance]  -- ^ The instance objects
777
                -> IO [(Instance, Runtime)]
778
collectLiveData liveDataEnabled cfg fields instances
775 779
  | not liveDataEnabled = return . zip instances . repeat . Left .
776 780
                            RpcResultError $ "Live data disabled"
777 781
  | otherwise = do
......
780 784
                            map (getNode cfg . instPrimaryNode) instances
781 785
          goodNodes = nodesWithValidConfig cfg instanceNodes
782 786
      instInfoRes <- executeRpcCall goodNodes (RpcCallAllInstancesInfo hvSpecs)
783
      consInfoRes <- case getAllConsoleParams cfg instances of
784
        Bad _ -> return . zip goodNodes . repeat . Left $ RpcResultError
785
                   "Cannot construct parameters for console info call"
786
        Ok  p -> executeRpcCalls $ consoleParamsToCalls p
787
      consInfoRes <-
788
        if "console" `elem` fields
789
          then case getAllConsoleParams cfg instances of
790
            Ok  p -> executeRpcCalls $ consoleParamsToCalls p
791
            Bad _ -> return . zip goodNodes . repeat . Left $
792
              RpcResultError "Cannot construct parameters for console info call"
793
          else return [] -- The information is not necessary
787 794
      return . zip instances .
788 795
        map (extractLiveInfo instInfoRes consInfoRes) $ instances
b/src/Ganeti/Query/Query.hs
83 83
import Ganeti.Types
84 84
import Ganeti.Utils
85 85

  
86
-- | Collector type
87
data CollectorType a b
88
  = CollectorSimple     (Bool -> ConfigData -> [a] -> IO [(a, b)])
89
  | CollectorFieldAware (Bool -> ConfigData -> [String] -> [a] -> IO [(a, b)])
90

  
86 91
-- * Helper functions
87 92

  
88 93
-- | Builds an unknown field definition.
......
170 175
-- The gathered data, or the failure to get it, is expressed through a runtime
171 176
-- object. The type of a runtime object is determined by every query type for
172 177
-- itself, and used exclusively by that query.
173
genericQuery :: FieldMap a b       -- ^ Field map
174
             -> (Bool -> ConfigData -> [a] -> IO [(a, b)]) -- ^ Collector
178
genericQuery :: FieldMap a b       -- ^ Maps field names to field definitions
179
             -> CollectorType a b  -- ^ Collector of live data
175 180
             -> (a -> String)      -- ^ Object to name function
176 181
             -> (ConfigData -> Container a) -- ^ Get all objects from config
177 182
             -> (ConfigData -> String -> ErrorResult a) -- ^ Lookup object
......
192 197
             [] -> Ok . niceSortKey nameFn .
193 198
                   Map.elems . fromContainer $ configFn cfg
194 199
             _  -> mapM (getFn cfg) wanted
195
  -- runs first pass of the filter, without a runtime context; this
196
  -- will limit the objects that we'll contact for exports
200
  -- Run the first pass of the filter, without a runtime context; this will
201
  -- limit the objects that we'll contact for exports
197 202
  fobjects <- resultT $ filterM (\n -> evaluateFilter cfg Nothing n cfilter)
198 203
                        objects
199
  -- here run the runtime data gathering...
200
  runtimes <- lift $ collector live' cfg fobjects
201
  -- ... then filter again the results, based on gathered runtime data
204
  -- Gather the runtime data
205
  runtimes <- case collector of
206
    CollectorSimple     collFn -> lift $ collFn live' cfg fobjects
207
    CollectorFieldAware collFn -> lift $ collFn live' cfg fields fobjects
208
  -- Filter the results again, based on the gathered data
202 209
  let fdata = map (\(obj, runtime) ->
203 210
                     map (execGetter cfg runtime obj) fgetters)
204 211
              runtimes
......
225 232
           -> IO (ErrorResult QueryResult) -- ^ Result
226 233

  
227 234
queryInner cfg live (Query (ItemTypeOpCode QRNode) fields qfilter) wanted =
228
  genericQuery Node.fieldsMap Node.collectLiveData nodeName configNodes getNode
229
               cfg live fields qfilter wanted
235
  genericQuery Node.fieldsMap (CollectorSimple Node.collectLiveData) nodeName
236
               configNodes getNode cfg live fields qfilter wanted
230 237

  
231 238
queryInner cfg live (Query (ItemTypeOpCode QRInstance) fields qfilter) wanted =
232
  genericQuery Instance.fieldsMap Instance.collectLiveData instName
233
               configInstances getInstance cfg live fields qfilter wanted
239
  genericQuery Instance.fieldsMap (CollectorFieldAware Instance.collectLiveData)
240
               instName configInstances getInstance cfg live fields qfilter
241
               wanted
234 242

  
235 243
queryInner cfg live (Query (ItemTypeOpCode QRGroup) fields qfilter) wanted =
236
  genericQuery Group.fieldsMap dummyCollectLiveData groupName configNodegroups
237
               getGroup cfg live fields qfilter wanted
244
  genericQuery Group.fieldsMap (CollectorSimple dummyCollectLiveData) groupName
245
               configNodegroups getGroup cfg live fields qfilter wanted
238 246

  
239 247
queryInner cfg live (Query (ItemTypeOpCode QRNetwork) fields qfilter) wanted =
240
  genericQuery Network.fieldsMap dummyCollectLiveData
248
  genericQuery Network.fieldsMap (CollectorSimple dummyCollectLiveData)
241 249
               (fromNonEmpty . networkName)
242 250
               configNetworks getNetwork cfg live fields qfilter wanted
243 251

  
244 252
queryInner cfg live (Query (ItemTypeOpCode QRExport) fields qfilter) wanted =
245
  genericQuery Export.fieldsMap Export.collectLiveData nodeName configNodes
246
               getNode cfg live fields qfilter wanted
253
  genericQuery Export.fieldsMap (CollectorSimple Export.collectLiveData)
254
               nodeName configNodes getNode cfg live fields qfilter wanted
247 255

  
248 256
queryInner _ _ (Query qkind _ _) _ =
249 257
  return . Bad . GenericError $ "Query '" ++ show qkind ++ "' not supported"

Also available in: Unified diff