module DigitalOcean.SSHKey (
SSHKey(..),
listAllKeys
) where
import Control.Applicative ((<$>), (<*>))
import Control.Monad (liftM, mzero)
import Control.Monad.IO.Class
import Data.Aeson ((.:), FromJSON(..), Value(..), decode)
import DigitalOcean.Base(Authentication, requestGet)
data SSHKey = SSHKey {
id :: Integer,
fingerprint :: String,
publicKey :: String,
sshkeyName :: String
} deriving (Show, Read)
newtype SSHKeys = SSHKeys [SSHKey]
instance FromJSON SSHKeys where
parseJSON (Object v) = SSHKeys <$> v .: "ssh_keys"
parseJSON _ = mzero
instance FromJSON SSHKey where
parseJSON (Object v) =
SSHKey <$>
(v .: "id") <*>
(v .: "fingerprint") <*>
(v .: "public_key") <*>
(v .: "name")
parseJSON _ = mzero
listAllKeys :: Authentication -> (MonadIO m) => m (Maybe [SSHKey])
listAllKeys a = liftM toList $ liftM decode (requestGet "account/keys" a)
toList :: Maybe SSHKeys -> Maybe [SSHKey]
toList = \sks -> case sks of Just(SSHKeys l) -> Just l
Nothing -> Nothing