diff --git a/.gitignore b/.gitignore index a4ee41a..0b4de9a 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ cabal.sandbox.config *.eventlog .stack-work/ cabal.project.local +*~ \ No newline at end of file diff --git a/examples/ServiceSendRestart.hs b/examples/ServiceSendRestart.hs new file mode 100755 index 0000000..156b8f9 --- /dev/null +++ b/examples/ServiceSendRestart.hs @@ -0,0 +1,30 @@ +#!/usr/bin/env stack +-- stack --resolver lts-11.1 runghc --package minio-hs + +-- +-- Minio Haskell SDK, (C) 2017, 2018 Minio, Inc. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +{-# LANGUAGE OverloadedStrings #-} +import Network.Minio +import Network.Minio.AdminAPI + +import Prelude + +main :: IO () +main = do + res <- runMinio def $ + serviceSendAction ServiceActionRestart + print res diff --git a/examples/ServiceSendStop.hs b/examples/ServiceSendStop.hs new file mode 100755 index 0000000..545ed92 --- /dev/null +++ b/examples/ServiceSendStop.hs @@ -0,0 +1,30 @@ +#!/usr/bin/env stack +-- stack --resolver lts-11.1 runghc --package minio-hs + +-- +-- Minio Haskell SDK, (C) 2017, 2018 Minio, Inc. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +{-# LANGUAGE OverloadedStrings #-} +import Network.Minio +import Network.Minio.AdminAPI + +import Prelude + +main :: IO () +main = do + res <- runMinio def $ + serviceSendAction ServiceActionStop + print res diff --git a/examples/ServiceStatus.hs b/examples/ServiceStatus.hs new file mode 100755 index 0000000..79a7349 --- /dev/null +++ b/examples/ServiceStatus.hs @@ -0,0 +1,30 @@ +#!/usr/bin/env stack +-- stack --resolver lts-11.1 runghc --package minio-hs + +-- +-- Minio Haskell SDK, (C) 2017, 2018 Minio, Inc. +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +{-# LANGUAGE OverloadedStrings #-} +import Network.Minio +import Network.Minio.AdminAPI + +import Prelude + +main :: IO () +main = do + res <- runMinio def $ + serviceStatus + print res diff --git a/src/Network/Minio/AdminAPI.hs b/src/Network/Minio/AdminAPI.hs index 49607ae..3a74081 100644 --- a/src/Network/Minio/AdminAPI.hs +++ b/src/Network/Minio/AdminAPI.hs @@ -44,6 +44,13 @@ module Network.Minio.AdminAPI , NodeSummary(..) , setConfig , getConfig + + , ServerVersion(..) + , ServiceStatus(..) + , serviceStatus + + , ServiceAction(..) + , serviceSendAction ) where import Data.Aeson (FromJSON, ToJSON, Value (Object), @@ -220,6 +227,40 @@ instance FromJSON ServerInfo where <*> v .: "addr" <*> v .: "data" +data ServerVersion = ServerVersion + { svVersion :: Text + , svCommitId :: Text + } deriving (Eq, Show) + +instance FromJSON ServerVersion where + parseJSON = withObject "ServerVersion" $ \v -> ServerVersion + <$> v .: "version" + <*> v .: "commitID" + +data ServiceStatus = ServiceStatus + { ssVersion :: ServerVersion + , ssUptime :: NominalDiffTime + } deriving (Eq, Show) + +instance FromJSON ServiceStatus where + parseJSON = withObject "ServiceStatus" $ \v -> do + serverVersion <- v .: "serverVersion" + uptimeNs <- v .: "uptime" + let uptime = uptimeNs / 1e9 + return $ ServiceStatus serverVersion uptime + +data ServiceAction = ServiceActionRestart + | ServiceActionStop + deriving (Eq, Show) + +instance ToJSON ServiceAction where + toJSON a = object [ "action" .= serviceActionToText a ] + +serviceActionToText :: ServiceAction -> Text +serviceActionToText a = case a of + ServiceActionRestart -> "restart" + ServiceActionStop -> "stop" + adminPath :: ByteString adminPath = "/minio/admin" @@ -344,6 +385,34 @@ healPath bucket prefix = do <> fromMaybe "" prefix else encodeUtf8 $ "v1/heal/" +-- | Get server version and uptime. +serviceStatus :: Minio ServiceStatus +serviceStatus = do + rsp <- executeAdminRequest AdminReqInfo { ariMethod = HT.methodGet + , ariPayload = PayloadBS B.empty + , ariPayloadHash = Nothing + , ariPath = "v1/service" + , ariHeaders = [] + , ariQueryParams = [] + } + + let rspBS = NC.responseBody rsp + case eitherDecode rspBS of + Right ss -> return ss + Left err -> throwIO $ MErrVJsonParse $ T.pack err + +-- | Send service restart or stop action to Minio server. +serviceSendAction :: ServiceAction -> Minio () +serviceSendAction action = do + let payload = PayloadBS $ LBS.toStrict $ A.encode action + void $ executeAdminRequest AdminReqInfo { ariMethod = HT.methodPost + , ariPayload = payload + , ariPayloadHash = Nothing + , ariPath = "v1/service" + , ariHeaders = [] + , ariQueryParams = [] + } + -- | Get the current config file from server. getConfig :: Minio ByteString getConfig = do