From a895fa19c63a48af6bdeda8383fdf68fdaa7420e Mon Sep 17 00:00:00 2001 From: Michele Tartara <mtartara@google.com> Date: Thu, 14 Mar 2013 11:50:41 +0000 Subject: [PATCH] Add data collector version data type Define the new data type and update the DRBD data collector to use it. Signed-off-by: Michele Tartara <mtartara@google.com> Reviewed-by: Bernardo Dal Seno <bdalseno@google.com> --- src/Ganeti/DataCollectors/Drbd.hs | 7 ++++++- src/Ganeti/DataCollectors/Types.hs | 20 ++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/Ganeti/DataCollectors/Drbd.hs b/src/Ganeti/DataCollectors/Drbd.hs index b81973061..92f53eef2 100644 --- a/src/Ganeti/DataCollectors/Drbd.hs +++ b/src/Ganeti/DataCollectors/Drbd.hs @@ -28,6 +28,7 @@ module Ganeti.DataCollectors.Drbd , options , arguments , dcName + , dcVersion ) where @@ -67,6 +68,10 @@ defaultCharNum = 80*20 dcName :: String dcName = "drbd" +-- | The version of this data collector. +dcVersion :: DCVersion +dcVersion = DCVerBuiltin + -- | The version number for the data format of this data collector. dcFormatVersion :: Int dcFormatVersion = 1 @@ -119,7 +124,7 @@ buildDRBDReport statusFile pairingFile = do show (Prelude.take defaultCharNum $ unpack unparsedText) ++ "\n" ++ show contexts ++ "\n" ++ errorMessage A.Done _ drbdStatus -> return $ J.showJSON drbdStatus - buildReport dcName Nothing dcFormatVersion jsonData + buildReport dcName dcVersion dcFormatVersion jsonData -- | Main function. main :: Options -> [String] -> IO () diff --git a/src/Ganeti/DataCollectors/Types.hs b/src/Ganeti/DataCollectors/Types.hs index c423a47e6..acd2dbcb2 100644 --- a/src/Ganeti/DataCollectors/Types.hs +++ b/src/Ganeti/DataCollectors/Types.hs @@ -6,7 +6,7 @@ {- -Copyright (C) 2012 Google Inc. +Copyright (C) 2012, 2013 Google Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -27,20 +27,29 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA module Ganeti.DataCollectors.Types ( DCReport(..) + , DCVersion(..) , buildReport ) where -import Data.Maybe import Text.JSON import Ganeti.Constants as C import Ganeti.THH import Ganeti.Utils (getCurrentTime) +-- | Type representing the version number of a data collector. +data DCVersion = DCVerBuiltin | DCVersion String deriving (Show, Eq) + +-- | The JSON instance for DCVersion. +instance JSON DCVersion where + showJSON DCVerBuiltin = showJSON C.builtinDataCollectorVersion + showJSON (DCVersion v) = showJSON v + readJSON = error "JSON read instance not implemented for type DCVersion" + -- | This is the format of the report produced by each data collector. $(buildObject "DCReport" "dcReport" [ simpleField "name" [t| String |] - , simpleField "version" [t| String |] + , simpleField "version" [t| DCVersion |] , simpleField "format_version" [t| Int |] , simpleField "timestamp" [t| Integer |] , simpleField "data" [t| JSValue |] @@ -50,9 +59,8 @@ $(buildObject "DCReport" "dcReport" -- timestamp (rounded up to seconds). -- If the version is not specified, it will be set to the value indicating -- a builtin collector. -buildReport :: String -> Maybe String -> Int -> JSValue -> IO DCReport +buildReport :: String -> DCVersion -> Int -> JSValue -> IO DCReport buildReport name version format_version jsonData = do now <- getCurrentTime let timestamp = now * 1000000000 :: Integer - ver = fromMaybe C.builtinDataCollectorVersion version - return $ DCReport name ver format_version timestamp jsonData + return $ DCReport name version format_version timestamp jsonData -- GitLab