module Aws.S3.Commands.PutBucket where

import           Aws.Core
import           Aws.S3.Core
import           Control.Monad
import           Data.Maybe
import qualified Data.Map             as M
import qualified Data.Text            as T
import qualified Data.Text.Encoding   as T
import qualified Network.HTTP.Conduit as HTTP
import qualified Text.XML             as XML

data PutBucket
    = PutBucket {
        PutBucket -> Bucket
pbBucket :: Bucket
      , PutBucket -> Maybe CannedAcl
pbCannedAcl :: Maybe CannedAcl
      , PutBucket -> Bucket
pbLocationConstraint :: LocationConstraint
      , PutBucket -> Maybe StorageClass
pbXStorageClass :: Maybe StorageClass -- ^ Google Cloud Storage S3 nonstandard extension
      }
    deriving (Int -> PutBucket -> ShowS
[PutBucket] -> ShowS
PutBucket -> String
(Int -> PutBucket -> ShowS)
-> (PutBucket -> String)
-> ([PutBucket] -> ShowS)
-> Show PutBucket
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PutBucket -> ShowS
showsPrec :: Int -> PutBucket -> ShowS
$cshow :: PutBucket -> String
show :: PutBucket -> String
$cshowList :: [PutBucket] -> ShowS
showList :: [PutBucket] -> ShowS
Show)

putBucket :: Bucket -> PutBucket
putBucket :: Bucket -> PutBucket
putBucket Bucket
bucket = Bucket
-> Maybe CannedAcl -> Bucket -> Maybe StorageClass -> PutBucket
PutBucket Bucket
bucket Maybe CannedAcl
forall a. Maybe a
Nothing Bucket
locationUsClassic Maybe StorageClass
forall a. Maybe a
Nothing

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

-- | ServiceConfiguration: 'S3Configuration'
instance SignQuery PutBucket where
    type ServiceConfiguration PutBucket = S3Configuration

    signQuery :: forall queryType.
PutBucket
-> ServiceConfiguration PutBucket queryType
-> SignatureData
-> SignedQuery
signQuery PutBucket{Maybe StorageClass
Maybe CannedAcl
Bucket
pbBucket :: PutBucket -> Bucket
pbCannedAcl :: PutBucket -> Maybe CannedAcl
pbLocationConstraint :: PutBucket -> Bucket
pbXStorageClass :: PutBucket -> Maybe StorageClass
pbBucket :: Bucket
pbCannedAcl :: Maybe CannedAcl
pbLocationConstraint :: Bucket
pbXStorageClass :: Maybe StorageClass
..} = S3Query
-> S3Configuration queryType -> SignatureData -> SignedQuery
forall qt.
S3Query -> S3Configuration qt -> SignatureData -> SignedQuery
s3SignQuery (S3Query {
                                             s3QMethod :: Method
s3QMethod       = Method
Put
                                           , s3QBucket :: Maybe ByteString
s3QBucket       = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ Bucket -> ByteString
T.encodeUtf8 Bucket
pbBucket
                                           , s3QSubresources :: Query
s3QSubresources = []
                                           , s3QQuery :: Query
s3QQuery        = []
                                           , s3QContentType :: Maybe ByteString
s3QContentType  = Maybe ByteString
forall a. Maybe a
Nothing
                                           , s3QContentMd5 :: Maybe (Digest MD5)
s3QContentMd5   = Maybe (Digest MD5)
forall a. Maybe a
Nothing
                                           , s3QObject :: Maybe ByteString
s3QObject       = Maybe ByteString
forall a. Maybe a
Nothing
                                           , s3QAmzHeaders :: RequestHeaders
s3QAmzHeaders   = case Maybe CannedAcl
pbCannedAcl of
                                                                 Maybe CannedAcl
Nothing -> []
                                                                 Just CannedAcl
acl -> [(HeaderName
"x-amz-acl", Bucket -> ByteString
T.encodeUtf8 (Bucket -> ByteString) -> Bucket -> ByteString
forall a b. (a -> b) -> a -> b
$ CannedAcl -> Bucket
writeCannedAcl CannedAcl
acl)]
                                           , s3QOtherHeaders :: RequestHeaders
s3QOtherHeaders = []
                                           , s3QRequestBody :: Maybe RequestBody
s3QRequestBody
                                               = Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Bool -> Bool
not ([Node] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [Node]
elts)) Maybe () -> Maybe RequestBody -> Maybe RequestBody
forall a b. Maybe a -> Maybe b -> Maybe b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>>
                                                 (RequestBody -> Maybe RequestBody
forall a. a -> Maybe a
Just (RequestBody -> Maybe RequestBody)
-> (Document -> RequestBody) -> Document -> Maybe RequestBody
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> RequestBody
HTTP.RequestBodyLBS (ByteString -> RequestBody)
-> (Document -> ByteString) -> Document -> RequestBody
forall b c a. (b -> c) -> (a -> b) -> a -> c
. RenderSettings -> Document -> ByteString
XML.renderLBS RenderSettings
forall a. Default a => a
XML.def)
                                                 XML.Document {
                                                          documentPrologue :: Prologue
XML.documentPrologue = [Miscellaneous] -> Maybe Doctype -> [Miscellaneous] -> Prologue
XML.Prologue [] Maybe Doctype
forall a. Maybe a
Nothing []
                                                        , documentRoot :: Element
XML.documentRoot = Element
root
                                                        , documentEpilogue :: [Miscellaneous]
XML.documentEpilogue = []
                                                        }
                                           })
        where root :: Element
root = XML.Element {
                               elementName :: Name
XML.elementName = Name
"{http://s3.amazonaws.com/doc/2006-03-01/}CreateBucketConfiguration"
                             , elementAttributes :: Map Name Bucket
XML.elementAttributes = Map Name Bucket
forall k a. Map k a
M.empty
                             , elementNodes :: [Node]
XML.elementNodes = [Node]
elts
                             }
              elts :: [Node]
elts = [Maybe Node] -> [Node]
forall a. [Maybe a] -> [a]
catMaybes
                             [ if Bucket -> Bool
T.null Bucket
pbLocationConstraint then Maybe Node
forall a. Maybe a
Nothing else Node -> Maybe Node
forall a. a -> Maybe a
Just (Bucket -> Node
locationconstraint Bucket
pbLocationConstraint)
                             , (StorageClass -> Node) -> Maybe StorageClass -> Maybe Node
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap StorageClass -> Node
storageclass Maybe StorageClass
pbXStorageClass
                             ]
              locationconstraint :: Bucket -> Node
locationconstraint Bucket
c = Element -> Node
XML.NodeElement (XML.Element {
                               elementName :: Name
XML.elementName = Name
"{http://s3.amazonaws.com/doc/2006-03-01/}LocationConstraint"
                             , elementAttributes :: Map Name Bucket
XML.elementAttributes = Map Name Bucket
forall k a. Map k a
M.empty
                             , elementNodes :: [Node]
XML.elementNodes = [Bucket -> Node
XML.NodeContent Bucket
c]
                             })
              storageclass :: StorageClass -> Node
storageclass StorageClass
c = Element -> Node
XML.NodeElement (XML.Element {
                               elementName :: Name
XML.elementName = Name
"StorageClass"
                             , elementAttributes :: Map Name Bucket
XML.elementAttributes = Map Name Bucket
forall k a. Map k a
M.empty
                             , elementNodes :: [Node]
XML.elementNodes = [Bucket -> Node
XML.NodeContent (StorageClass -> Bucket
writeStorageClass StorageClass
c)]
                             })

instance ResponseConsumer r PutBucketResponse where
    type ResponseMetadata PutBucketResponse = S3Metadata

    responseConsumer :: Request
-> r
-> IORef (ResponseMetadata PutBucketResponse)
-> HTTPResponseConsumer PutBucketResponse
responseConsumer Request
_ r
_ = HTTPResponseConsumer PutBucketResponse
-> IORef S3Metadata -> HTTPResponseConsumer PutBucketResponse
forall a.
HTTPResponseConsumer a
-> IORef S3Metadata -> HTTPResponseConsumer a
s3ResponseConsumer (HTTPResponseConsumer PutBucketResponse
 -> IORef S3Metadata -> HTTPResponseConsumer PutBucketResponse)
-> HTTPResponseConsumer PutBucketResponse
-> IORef S3Metadata
-> HTTPResponseConsumer PutBucketResponse
forall a b. (a -> b) -> a -> b
$ \Response (ConduitM () ByteString (ResourceT IO) ())
_ -> PutBucketResponse -> ResourceT IO PutBucketResponse
forall a. a -> ResourceT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return PutBucketResponse
PutBucketResponse

instance Transaction PutBucket PutBucketResponse

instance AsMemoryResponse PutBucketResponse where
    type MemoryResponse PutBucketResponse = PutBucketResponse
    loadToMemory :: PutBucketResponse
-> ResourceT IO (MemoryResponse PutBucketResponse)
loadToMemory = PutBucketResponse
-> ResourceT IO (MemoryResponse PutBucketResponse)
PutBucketResponse -> ResourceT IO PutBucketResponse
forall a. a -> ResourceT IO a
forall (m :: * -> *) a. Monad m => a -> m a
return