Add serviceStatus and serviceSendAction admin APIs (#100)

This commit is contained in:
Harshavardhana 2018-06-25 13:52:28 -07:00 committed by Krishnan Parthasarathi
parent d16698892b
commit 8273910084
5 changed files with 160 additions and 0 deletions

1
.gitignore vendored
View File

@ -17,3 +17,4 @@ cabal.sandbox.config
*.eventlog
.stack-work/
cabal.project.local
*~

30
examples/ServiceSendRestart.hs Executable file
View File

@ -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

30
examples/ServiceSendStop.hs Executable file
View File

@ -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

30
examples/ServiceStatus.hs Executable file
View File

@ -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

View File

@ -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