{-# LANGUAGE CPP               #-}
{-# LANGUAGE DeriveAnyClass    #-}
{-# LANGUAGE DeriveGeneric     #-}
{-# LANGUAGE OverloadedStrings #-}

module Database.Vault.KVv2.Client.Types where

import           Control.Monad                       (mzero)
import           Data.Aeson
#if MIN_VERSION_aeson(2,0,0)
import qualified Data.Aeson.Key                      as K
import qualified Data.Aeson.KeyMap                   as KM
#endif
import qualified Data.ByteString                     as B
import           Data.Hashable
import           Data.HashMap.Strict
import qualified Data.Text                           as T
import           Data.Text.Read                      (decimal)
import           GHC.Generics
import           Network.HTTP.Client                 (Manager)
import           Text.Read                           (readMaybe)

import           Database.Vault.KVv2.Client.Internal

type VaultAddr = String

type VaultToken = String

type Error = String

type KVEnginePath = String

type DisableCertValidation =Bool

data VaultConnection =
  VaultConnection
    { VaultConnection -> VaultAddr
vaultAddr    :: !VaultAddr
    , VaultConnection -> VaultAddr
kvEnginePath :: !KVEnginePath
    , VaultConnection -> ByteString
vaultToken   :: !B.ByteString
    , VaultConnection -> Manager
manager      :: !Manager
    }

instance Show VaultConnection where
  show :: VaultConnection -> VaultAddr
show (VaultConnection VaultAddr
a VaultAddr
p ByteString
_ Manager
_) =
    ShowS
removeTrailingSlash VaultAddr
a VaultAddr -> ShowS
forall a. [a] -> [a] -> [a]
++ VaultAddr
"/v1/"
    VaultAddr -> ShowS
forall a. [a] -> [a] -> [a]
++ ShowS
removeTrailingSlash (ShowS
removeLeadingSlash VaultAddr
p)

newtype SecretVersions =
  SecretVersions [SecretVersion]
  deriving (Int -> SecretVersions -> ShowS
[SecretVersions] -> ShowS
SecretVersions -> VaultAddr
(Int -> SecretVersions -> ShowS)
-> (SecretVersions -> VaultAddr)
-> ([SecretVersions] -> ShowS)
-> Show SecretVersions
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretVersions -> ShowS
showsPrec :: Int -> SecretVersions -> ShowS
$cshow :: SecretVersions -> VaultAddr
show :: SecretVersions -> VaultAddr
$cshowList :: [SecretVersions] -> ShowS
showList :: [SecretVersions] -> ShowS
Show, SecretVersions -> SecretVersions -> Bool
(SecretVersions -> SecretVersions -> Bool)
-> (SecretVersions -> SecretVersions -> Bool) -> Eq SecretVersions
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SecretVersions -> SecretVersions -> Bool
== :: SecretVersions -> SecretVersions -> Bool
$c/= :: SecretVersions -> SecretVersions -> Bool
/= :: SecretVersions -> SecretVersions -> Bool
Eq)

instance ToJSON SecretVersions where
  toJSON :: SecretVersions -> Value
toJSON (SecretVersions [SecretVersion]
svs) =
    [Pair] -> Value
object
      [ Key
"versions" Key -> [Int] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ((\(SecretVersion Int
i) -> Int
i) (SecretVersion -> Int) -> [SecretVersion] -> [Int]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [SecretVersion]
svs) ]

newtype SecretVersion
  = SecretVersion Int
  deriving (Int -> SecretVersion -> ShowS
[SecretVersion] -> ShowS
SecretVersion -> VaultAddr
(Int -> SecretVersion -> ShowS)
-> (SecretVersion -> VaultAddr)
-> ([SecretVersion] -> ShowS)
-> Show SecretVersion
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretVersion -> ShowS
showsPrec :: Int -> SecretVersion -> ShowS
$cshow :: SecretVersion -> VaultAddr
show :: SecretVersion -> VaultAddr
$cshowList :: [SecretVersion] -> ShowS
showList :: [SecretVersion] -> ShowS
Show, SecretVersion -> SecretVersion -> Bool
(SecretVersion -> SecretVersion -> Bool)
-> (SecretVersion -> SecretVersion -> Bool) -> Eq SecretVersion
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SecretVersion -> SecretVersion -> Bool
== :: SecretVersion -> SecretVersion -> Bool
$c/= :: SecretVersion -> SecretVersion -> Bool
/= :: SecretVersion -> SecretVersion -> Bool
Eq, (forall x. SecretVersion -> Rep SecretVersion x)
-> (forall x. Rep SecretVersion x -> SecretVersion)
-> Generic SecretVersion
forall x. Rep SecretVersion x -> SecretVersion
forall x. SecretVersion -> Rep SecretVersion x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SecretVersion -> Rep SecretVersion x
from :: forall x. SecretVersion -> Rep SecretVersion x
$cto :: forall x. Rep SecretVersion x -> SecretVersion
to :: forall x. Rep SecretVersion x -> SecretVersion
Generic, Eq SecretVersion
Eq SecretVersion =>
(Int -> SecretVersion -> Int)
-> (SecretVersion -> Int) -> Hashable SecretVersion
Int -> SecretVersion -> Int
SecretVersion -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> SecretVersion -> Int
hashWithSalt :: Int -> SecretVersion -> Int
$chash :: SecretVersion -> Int
hash :: SecretVersion -> Int
Hashable)

newtype SecretMetadata =
  SecretMetadata (HashMap SecretVersion Metadata)
  deriving (Int -> SecretMetadata -> ShowS
[SecretMetadata] -> ShowS
SecretMetadata -> VaultAddr
(Int -> SecretMetadata -> ShowS)
-> (SecretMetadata -> VaultAddr)
-> ([SecretMetadata] -> ShowS)
-> Show SecretMetadata
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretMetadata -> ShowS
showsPrec :: Int -> SecretMetadata -> ShowS
$cshow :: SecretMetadata -> VaultAddr
show :: SecretMetadata -> VaultAddr
$cshowList :: [SecretMetadata] -> ShowS
showList :: [SecretMetadata] -> ShowS
Show, SecretMetadata -> SecretMetadata -> Bool
(SecretMetadata -> SecretMetadata -> Bool)
-> (SecretMetadata -> SecretMetadata -> Bool) -> Eq SecretMetadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SecretMetadata -> SecretMetadata -> Bool
== :: SecretMetadata -> SecretMetadata -> Bool
$c/= :: SecretMetadata -> SecretMetadata -> Bool
/= :: SecretMetadata -> SecretMetadata -> Bool
Eq)

instance FromJSON SecretMetadata where
  parseJSON :: Value -> Parser SecretMetadata
parseJSON (Object Object
o) =
#if MIN_VERSION_aeson(2,0,0)
    SecretMetadata -> Parser SecretMetadata
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HashMap SecretVersion Metadata -> SecretMetadata
SecretMetadata (HashMap SecretVersion Metadata -> SecretMetadata)
-> ([(SecretVersion, Metadata)] -> HashMap SecretVersion Metadata)
-> [(SecretVersion, Metadata)]
-> SecretMetadata
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(SecretVersion, Metadata)] -> HashMap SecretVersion Metadata
forall k v. (Eq k, Hashable k) => [(k, v)] -> HashMap k v
fromList ([(SecretVersion, Metadata)] -> SecretMetadata)
-> [(SecretVersion, Metadata)] -> SecretMetadata
forall a b. (a -> b) -> a -> b
$ Pair -> (SecretVersion, Metadata)
forall {b}. FromJSON b => Pair -> (SecretVersion, b)
trans (Pair -> (SecretVersion, Metadata))
-> [Pair] -> [(SecretVersion, Metadata)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashMap Key Value -> [Pair]
forall k v. HashMap k v -> [(k, v)]
toList (Object -> HashMap Key Value
forall v. KeyMap v -> HashMap Key v
KM.toHashMap Object
o))
#else
    pure (SecretMetadata . fromList $ trans <$> toList o)
#endif
    where
    trans :: Pair -> (SecretVersion, b)
trans Pair
p =
      case Pair
p of
        (Key
k,j :: Value
j@(Object Object
_)) -> do
#if MIN_VERSION_aeson(2,0,0)
          let d :: Either VaultAddr (Int, Text)
d = Reader Int
forall a. Integral a => Reader a
decimal (Key -> Text
K.toText Key
k)
#else
          let d = decimal k
#endif
          (Int -> SecretVersion
SecretVersion (Either VaultAddr (Int, Text) -> Int
forall {a} {b}. Either VaultAddr (a, b) -> a
fromDecimal Either VaultAddr (Int, Text)
d),Result b -> b
forall {a}. Result a -> a
fromSuccess (Result b -> b) -> Result b -> b
forall a b. (a -> b) -> a -> b
$ Value -> Result b
forall a. FromJSON a => Value -> Result a
fromJSON Value
j)
        Pair
_anyOther        -> VaultAddr -> (SecretVersion, b)
forall a. HasCallStack => VaultAddr -> a
error VaultAddr
"Expected JSON Object"
    fromDecimal :: Either VaultAddr (a, b) -> a
fromDecimal (Right (a
i,b
_)) = a
i
    fromDecimal (Left VaultAddr
e)      = VaultAddr -> a
forall a. HasCallStack => VaultAddr -> a
error VaultAddr
e
    fromSuccess :: Result a -> a
fromSuccess (Success a
s) = a
s
    fromSuccess (Error VaultAddr
e)   = VaultAddr -> a
forall a. HasCallStack => VaultAddr -> a
error VaultAddr
e
  parseJSON Value
_          = Parser SecretMetadata
forall a. Parser a
forall (m :: * -> *) a. MonadPlus m => m a
mzero

data Metadata =
  Metadata
    { Metadata -> Bool
destroyed     :: !Bool
    , Metadata -> Text
deletion_time :: !T.Text
    , Metadata -> Text
created_time  :: !T.Text
    } deriving (Int -> Metadata -> ShowS
[Metadata] -> ShowS
Metadata -> VaultAddr
(Int -> Metadata -> ShowS)
-> (Metadata -> VaultAddr)
-> ([Metadata] -> ShowS)
-> Show Metadata
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Metadata -> ShowS
showsPrec :: Int -> Metadata -> ShowS
$cshow :: Metadata -> VaultAddr
show :: Metadata -> VaultAddr
$cshowList :: [Metadata] -> ShowS
showList :: [Metadata] -> ShowS
Show, Metadata -> Metadata -> Bool
(Metadata -> Metadata -> Bool)
-> (Metadata -> Metadata -> Bool) -> Eq Metadata
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Metadata -> Metadata -> Bool
== :: Metadata -> Metadata -> Bool
$c/= :: Metadata -> Metadata -> Bool
/= :: Metadata -> Metadata -> Bool
Eq, (forall x. Metadata -> Rep Metadata x)
-> (forall x. Rep Metadata x -> Metadata) -> Generic Metadata
forall x. Rep Metadata x -> Metadata
forall x. Metadata -> Rep Metadata x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Metadata -> Rep Metadata x
from :: forall x. Metadata -> Rep Metadata x
$cto :: forall x. Rep Metadata x -> Metadata
to :: forall x. Rep Metadata x -> Metadata
Generic, [Metadata] -> Value
[Metadata] -> Encoding
Metadata -> Bool
Metadata -> Value
Metadata -> Encoding
(Metadata -> Value)
-> (Metadata -> Encoding)
-> ([Metadata] -> Value)
-> ([Metadata] -> Encoding)
-> (Metadata -> Bool)
-> ToJSON Metadata
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: Metadata -> Value
toJSON :: Metadata -> Value
$ctoEncoding :: Metadata -> Encoding
toEncoding :: Metadata -> Encoding
$ctoJSONList :: [Metadata] -> Value
toJSONList :: [Metadata] -> Value
$ctoEncodingList :: [Metadata] -> Encoding
toEncodingList :: [Metadata] -> Encoding
$comitField :: Metadata -> Bool
omitField :: Metadata -> Bool
ToJSON, Maybe Metadata
Value -> Parser [Metadata]
Value -> Parser Metadata
(Value -> Parser Metadata)
-> (Value -> Parser [Metadata])
-> Maybe Metadata
-> FromJSON Metadata
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser Metadata
parseJSON :: Value -> Parser Metadata
$cparseJSONList :: Value -> Parser [Metadata]
parseJSONList :: Value -> Parser [Metadata]
$comittedField :: Maybe Metadata
omittedField :: Maybe Metadata
FromJSON)

newtype SecretData =
  SecretData
    (HashMap T.Text T.Text)
    deriving (Int -> SecretData -> ShowS
[SecretData] -> ShowS
SecretData -> VaultAddr
(Int -> SecretData -> ShowS)
-> (SecretData -> VaultAddr)
-> ([SecretData] -> ShowS)
-> Show SecretData
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretData -> ShowS
showsPrec :: Int -> SecretData -> ShowS
$cshow :: SecretData -> VaultAddr
show :: SecretData -> VaultAddr
$cshowList :: [SecretData] -> ShowS
showList :: [SecretData] -> ShowS
Show, (forall x. SecretData -> Rep SecretData x)
-> (forall x. Rep SecretData x -> SecretData) -> Generic SecretData
forall x. Rep SecretData x -> SecretData
forall x. SecretData -> Rep SecretData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SecretData -> Rep SecretData x
from :: forall x. SecretData -> Rep SecretData x
$cto :: forall x. Rep SecretData x -> SecretData
to :: forall x. Rep SecretData x -> SecretData
Generic, [SecretData] -> Value
[SecretData] -> Encoding
SecretData -> Bool
SecretData -> Value
SecretData -> Encoding
(SecretData -> Value)
-> (SecretData -> Encoding)
-> ([SecretData] -> Value)
-> ([SecretData] -> Encoding)
-> (SecretData -> Bool)
-> ToJSON SecretData
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: SecretData -> Value
toJSON :: SecretData -> Value
$ctoEncoding :: SecretData -> Encoding
toEncoding :: SecretData -> Encoding
$ctoJSONList :: [SecretData] -> Value
toJSONList :: [SecretData] -> Value
$ctoEncodingList :: [SecretData] -> Encoding
toEncodingList :: [SecretData] -> Encoding
$comitField :: SecretData -> Bool
omitField :: SecretData -> Bool
ToJSON, Maybe SecretData
Value -> Parser [SecretData]
Value -> Parser SecretData
(Value -> Parser SecretData)
-> (Value -> Parser [SecretData])
-> Maybe SecretData
-> FromJSON SecretData
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser SecretData
parseJSON :: Value -> Parser SecretData
$cparseJSONList :: Value -> Parser [SecretData]
parseJSONList :: Value -> Parser [SecretData]
$comittedField :: Maybe SecretData
omittedField :: Maybe SecretData
FromJSON)

data SecretSettings =
  SecretSettings
    { SecretSettings -> Int
max_versions :: !Int
    , SecretSettings -> Bool
cas_required :: !Bool
    } deriving (Int -> SecretSettings -> ShowS
[SecretSettings] -> ShowS
SecretSettings -> VaultAddr
(Int -> SecretSettings -> ShowS)
-> (SecretSettings -> VaultAddr)
-> ([SecretSettings] -> ShowS)
-> Show SecretSettings
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretSettings -> ShowS
showsPrec :: Int -> SecretSettings -> ShowS
$cshow :: SecretSettings -> VaultAddr
show :: SecretSettings -> VaultAddr
$cshowList :: [SecretSettings] -> ShowS
showList :: [SecretSettings] -> ShowS
Show, (forall x. SecretSettings -> Rep SecretSettings x)
-> (forall x. Rep SecretSettings x -> SecretSettings)
-> Generic SecretSettings
forall x. Rep SecretSettings x -> SecretSettings
forall x. SecretSettings -> Rep SecretSettings x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SecretSettings -> Rep SecretSettings x
from :: forall x. SecretSettings -> Rep SecretSettings x
$cto :: forall x. Rep SecretSettings x -> SecretSettings
to :: forall x. Rep SecretSettings x -> SecretSettings
Generic, [SecretSettings] -> Value
[SecretSettings] -> Encoding
SecretSettings -> Bool
SecretSettings -> Value
SecretSettings -> Encoding
(SecretSettings -> Value)
-> (SecretSettings -> Encoding)
-> ([SecretSettings] -> Value)
-> ([SecretSettings] -> Encoding)
-> (SecretSettings -> Bool)
-> ToJSON SecretSettings
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: SecretSettings -> Value
toJSON :: SecretSettings -> Value
$ctoEncoding :: SecretSettings -> Encoding
toEncoding :: SecretSettings -> Encoding
$ctoJSONList :: [SecretSettings] -> Value
toJSONList :: [SecretSettings] -> Value
$ctoEncodingList :: [SecretSettings] -> Encoding
toEncodingList :: [SecretSettings] -> Encoding
$comitField :: SecretSettings -> Bool
omitField :: SecretSettings -> Bool
ToJSON, Maybe SecretSettings
Value -> Parser [SecretSettings]
Value -> Parser SecretSettings
(Value -> Parser SecretSettings)
-> (Value -> Parser [SecretSettings])
-> Maybe SecretSettings
-> FromJSON SecretSettings
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser SecretSettings
parseJSON :: Value -> Parser SecretSettings
$cparseJSONList :: Value -> Parser [SecretSettings]
parseJSONList :: Value -> Parser [SecretSettings]
$comittedField :: Maybe SecretSettings
omittedField :: Maybe SecretSettings
FromJSON)

newtype SecretPath =
  SecretPath
    { SecretPath -> VaultAddr
path :: String }
    deriving (Int -> SecretPath -> ShowS
[SecretPath] -> ShowS
SecretPath -> VaultAddr
(Int -> SecretPath -> ShowS)
-> (SecretPath -> VaultAddr)
-> ([SecretPath] -> ShowS)
-> Show SecretPath
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SecretPath -> ShowS
showsPrec :: Int -> SecretPath -> ShowS
$cshow :: SecretPath -> VaultAddr
show :: SecretPath -> VaultAddr
$cshowList :: [SecretPath] -> ShowS
showList :: [SecretPath] -> ShowS
Show, (forall x. SecretPath -> Rep SecretPath x)
-> (forall x. Rep SecretPath x -> SecretPath) -> Generic SecretPath
forall x. Rep SecretPath x -> SecretPath
forall x. SecretPath -> Rep SecretPath x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SecretPath -> Rep SecretPath x
from :: forall x. SecretPath -> Rep SecretPath x
$cto :: forall x. Rep SecretPath x -> SecretPath
to :: forall x. Rep SecretPath x -> SecretPath
Generic, [SecretPath] -> Value
[SecretPath] -> Encoding
SecretPath -> Bool
SecretPath -> Value
SecretPath -> Encoding
(SecretPath -> Value)
-> (SecretPath -> Encoding)
-> ([SecretPath] -> Value)
-> ([SecretPath] -> Encoding)
-> (SecretPath -> Bool)
-> ToJSON SecretPath
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: SecretPath -> Value
toJSON :: SecretPath -> Value
$ctoEncoding :: SecretPath -> Encoding
toEncoding :: SecretPath -> Encoding
$ctoJSONList :: [SecretPath] -> Value
toJSONList :: [SecretPath] -> Value
$ctoEncodingList :: [SecretPath] -> Encoding
toEncodingList :: [SecretPath] -> Encoding
$comitField :: SecretPath -> Bool
omitField :: SecretPath -> Bool
ToJSON)

data CheckAndSet
  = WriteAllowed
  | CreateOnly
  | CurrentVersion !Int
  deriving (Int -> CheckAndSet -> ShowS
[CheckAndSet] -> ShowS
CheckAndSet -> VaultAddr
(Int -> CheckAndSet -> ShowS)
-> (CheckAndSet -> VaultAddr)
-> ([CheckAndSet] -> ShowS)
-> Show CheckAndSet
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CheckAndSet -> ShowS
showsPrec :: Int -> CheckAndSet -> ShowS
$cshow :: CheckAndSet -> VaultAddr
show :: CheckAndSet -> VaultAddr
$cshowList :: [CheckAndSet] -> ShowS
showList :: [CheckAndSet] -> ShowS
Show, (forall x. CheckAndSet -> Rep CheckAndSet x)
-> (forall x. Rep CheckAndSet x -> CheckAndSet)
-> Generic CheckAndSet
forall x. Rep CheckAndSet x -> CheckAndSet
forall x. CheckAndSet -> Rep CheckAndSet x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. CheckAndSet -> Rep CheckAndSet x
from :: forall x. CheckAndSet -> Rep CheckAndSet x
$cto :: forall x. Rep CheckAndSet x -> CheckAndSet
to :: forall x. Rep CheckAndSet x -> CheckAndSet
Generic, [CheckAndSet] -> Value
[CheckAndSet] -> Encoding
CheckAndSet -> Bool
CheckAndSet -> Value
CheckAndSet -> Encoding
(CheckAndSet -> Value)
-> (CheckAndSet -> Encoding)
-> ([CheckAndSet] -> Value)
-> ([CheckAndSet] -> Encoding)
-> (CheckAndSet -> Bool)
-> ToJSON CheckAndSet
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: CheckAndSet -> Value
toJSON :: CheckAndSet -> Value
$ctoEncoding :: CheckAndSet -> Encoding
toEncoding :: CheckAndSet -> Encoding
$ctoJSONList :: [CheckAndSet] -> Value
toJSONList :: [CheckAndSet] -> Value
$ctoEncodingList :: [CheckAndSet] -> Encoding
toEncodingList :: [CheckAndSet] -> Encoding
$comitField :: CheckAndSet -> Bool
omitField :: CheckAndSet -> Bool
ToJSON)

newtype PutSecretOptions =
  PutSecretOptions
    { PutSecretOptions -> CheckAndSet
cas :: CheckAndSet }
    deriving (Int -> PutSecretOptions -> ShowS
[PutSecretOptions] -> ShowS
PutSecretOptions -> VaultAddr
(Int -> PutSecretOptions -> ShowS)
-> (PutSecretOptions -> VaultAddr)
-> ([PutSecretOptions] -> ShowS)
-> Show PutSecretOptions
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PutSecretOptions -> ShowS
showsPrec :: Int -> PutSecretOptions -> ShowS
$cshow :: PutSecretOptions -> VaultAddr
show :: PutSecretOptions -> VaultAddr
$cshowList :: [PutSecretOptions] -> ShowS
showList :: [PutSecretOptions] -> ShowS
Show)

instance ToJSON PutSecretOptions where
  toJSON :: PutSecretOptions -> Value
toJSON PutSecretOptions { cas :: PutSecretOptions -> CheckAndSet
cas = CheckAndSet
WriteAllowed } = [Pair] -> Value
object []
  toJSON PutSecretOptions { cas :: PutSecretOptions -> CheckAndSet
cas = CheckAndSet
CreateOnly }   = [Pair] -> Value
object [ Key
"cas" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number Scientific
0.0 ]
  toJSON PutSecretOptions { cas :: PutSecretOptions -> CheckAndSet
cas = CurrentVersion Int
v } =
    case VaultAddr -> Maybe Scientific
forall a. Read a => VaultAddr -> Maybe a
readMaybe (Int -> VaultAddr
forall a. Show a => a -> VaultAddr
show Int
v) of
      Just Scientific
s  -> [Pair] -> Value
object [ Key
"cas" Key -> Value -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Scientific -> Value
Number Scientific
s ]
      Maybe Scientific
Nothing -> VaultAddr -> Value
forall a. HasCallStack => VaultAddr -> a
error VaultAddr
"Expected type Int"

data PutSecretRequestBody =
  PutSecretRequestBody
    { PutSecretRequestBody -> PutSecretOptions
options  :: !PutSecretOptions
    , PutSecretRequestBody -> SecretData
put_data :: !SecretData
    }

instance ToJSON PutSecretRequestBody where
  toJSON :: PutSecretRequestBody -> Value
toJSON (PutSecretRequestBody PutSecretOptions
os SecretData
sd) =
    [Pair] -> Value
object
      [ Key
"options" Key -> PutSecretOptions -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= PutSecretOptions
os
      , Key
"data"    Key -> SecretData -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= SecretData
sd
      ]

data VaultKey
  = VaultKey    !String
  | VaultFolder !String
  deriving (Int -> VaultKey -> ShowS
[VaultKey] -> ShowS
VaultKey -> VaultAddr
(Int -> VaultKey -> ShowS)
-> (VaultKey -> VaultAddr)
-> ([VaultKey] -> ShowS)
-> Show VaultKey
forall a.
(Int -> a -> ShowS) -> (a -> VaultAddr) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> VaultKey -> ShowS
showsPrec :: Int -> VaultKey -> ShowS
$cshow :: VaultKey -> VaultAddr
show :: VaultKey -> VaultAddr
$cshowList :: [VaultKey] -> ShowS
showList :: [VaultKey] -> ShowS
Show)