module Ganeti.Query.Common
( rsNoData
, rsUnavail
, rsNormal
, rsMaybe
, rsUnknown
, missingRuntime
, rpcErrorToStatus
, timeStampFields
, uuidFields
, serialFields
, tagsFields
, dictFieldGetter
, buildQFTLookup
, buildNdParamField
) where
import qualified Data.Map as Map
import Data.Maybe (fromMaybe)
import Text.JSON (JSON, showJSON)
import qualified Ganeti.Constants as C
import Ganeti.Config
import Ganeti.Objects
import Ganeti.Rpc
import Ganeti.Query.Language
import Ganeti.Query.Types
vTypeToQFT :: VType -> FieldType
vTypeToQFT VTypeString = QFTOther
vTypeToQFT VTypeMaybeString = QFTOther
vTypeToQFT VTypeBool = QFTBool
vTypeToQFT VTypeSize = QFTUnit
vTypeToQFT VTypeInt = QFTNumber
rsNoData :: ResultEntry
rsNoData = ResultEntry RSNoData Nothing
rsUnavail :: ResultEntry
rsUnavail = ResultEntry RSUnavail Nothing
rsNormal :: (JSON a) => a -> ResultEntry
rsNormal a = ResultEntry RSNormal $ Just (showJSON a)
rsMaybe :: (JSON a) => Maybe a -> ResultEntry
rsMaybe = maybe rsNoData rsNormal
rsUnknown :: ResultEntry
rsUnknown = ResultEntry RSUnknown Nothing
missingRuntime :: FieldGetter a b
missingRuntime = FieldRuntime (\_ _ -> ResultEntry RSNoData Nothing)
rpcErrorToStatus :: RpcError -> ResultStatus
rpcErrorToStatus (OfflineNodeError _) = RSOffline
rpcErrorToStatus _ = RSNoData
timeStampFields :: (TimeStampObject a) => FieldList a b
timeStampFields =
[ (FieldDefinition "ctime" "CTime" QFTTimestamp "Creation timestamp",
FieldSimple (rsNormal . cTimeOf), QffNormal)
, (FieldDefinition "mtime" "MTime" QFTTimestamp "Modification timestamp",
FieldSimple (rsNormal . mTimeOf), QffNormal)
]
uuidFields :: (UuidObject a) => String -> FieldList a b
uuidFields name =
[ (FieldDefinition "uuid" "UUID" QFTText (name ++ " UUID"),
FieldSimple (rsNormal . uuidOf), QffNormal) ]
serialFields :: (SerialNoObject a) => String -> FieldList a b
serialFields name =
[ (FieldDefinition "serial_no" "SerialNo" QFTNumber
(name ++ " object serial number, incremented on each modification"),
FieldSimple (rsNormal . serialOf), QffNormal) ]
tagsFields :: (TagsObject a) => FieldList a b
tagsFields =
[ (FieldDefinition "tags" "Tags" QFTOther "Tags",
FieldSimple (rsNormal . tagsOf), QffNormal) ]
dictFieldGetter :: (DictObject a) => String -> Maybe a -> ResultEntry
dictFieldGetter k = maybe rsNoData (rsMaybe . lookup k . toDict)
buildQFTLookup :: [(String, String)] -> Map.Map String FieldType
buildQFTLookup =
Map.fromList .
map (\(k, v) -> (k, maybe QFTOther vTypeToQFT (vTypeFromRaw v)))
ndParamTypes :: Map.Map String FieldType
ndParamTypes = buildQFTLookup C.ndsParameterTypes
ndParamTitles :: Map.Map String FieldTitle
ndParamTitles = Map.fromList C.ndsParameterTitles
ndParamGetter :: (NdParamObject a) =>
String
-> ConfigData -> a -> ResultEntry
ndParamGetter field config =
dictFieldGetter field . getNdParamsOf config
buildNdParamField :: (NdParamObject a) => String -> FieldData a b
buildNdParamField field =
let full_name = "ndp/" ++ field
title = fromMaybe field $ field `Map.lookup` ndParamTitles
qft = fromMaybe QFTOther $ field `Map.lookup` ndParamTypes
desc = "The \"" ++ field ++ "\" node parameter"
in (FieldDefinition full_name title qft desc,
FieldConfig (ndParamGetter field), QffNormal)