{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.MediaStore.PutCorsPolicy
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Sets the cross-origin resource sharing (CORS) configuration on a
-- container so that the container can service cross-origin requests. For
-- example, you might want to enable a request whose origin is
-- http:\/\/www.example.com to access your AWS Elemental MediaStore
-- container at my.example.container.com by using the browser\'s
-- XMLHttpRequest capability.
--
-- To enable CORS on a container, you attach a CORS policy to the
-- container. In the CORS policy, you configure rules that identify origins
-- and the HTTP methods that can be executed on your container. The policy
-- can contain up to 398,000 characters. You can add up to 100 rules to a
-- CORS policy. If more than one rule applies, the service uses the first
-- applicable rule listed.
--
-- To learn more about CORS, see
-- <https://docs.aws.amazon.com/mediastore/latest/ug/cors-policy.html Cross-Origin Resource Sharing (CORS) in AWS Elemental MediaStore>.
module Amazonka.MediaStore.PutCorsPolicy
  ( -- * Creating a Request
    PutCorsPolicy (..),
    newPutCorsPolicy,

    -- * Request Lenses
    putCorsPolicy_containerName,
    putCorsPolicy_corsPolicy,

    -- * Destructuring the Response
    PutCorsPolicyResponse (..),
    newPutCorsPolicyResponse,

    -- * Response Lenses
    putCorsPolicyResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import Amazonka.MediaStore.Types
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | /See:/ 'newPutCorsPolicy' smart constructor.
data PutCorsPolicy = PutCorsPolicy'
  { -- | The name of the container that you want to assign the CORS policy to.
    PutCorsPolicy -> Text
containerName :: Prelude.Text,
    -- | The CORS policy to apply to the container.
    PutCorsPolicy -> NonEmpty CorsRule
corsPolicy :: Prelude.NonEmpty CorsRule
  }
  deriving (PutCorsPolicy -> PutCorsPolicy -> Bool
(PutCorsPolicy -> PutCorsPolicy -> Bool)
-> (PutCorsPolicy -> PutCorsPolicy -> Bool) -> Eq PutCorsPolicy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PutCorsPolicy -> PutCorsPolicy -> Bool
$c/= :: PutCorsPolicy -> PutCorsPolicy -> Bool
== :: PutCorsPolicy -> PutCorsPolicy -> Bool
$c== :: PutCorsPolicy -> PutCorsPolicy -> Bool
Prelude.Eq, ReadPrec [PutCorsPolicy]
ReadPrec PutCorsPolicy
Int -> ReadS PutCorsPolicy
ReadS [PutCorsPolicy]
(Int -> ReadS PutCorsPolicy)
-> ReadS [PutCorsPolicy]
-> ReadPrec PutCorsPolicy
-> ReadPrec [PutCorsPolicy]
-> Read PutCorsPolicy
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PutCorsPolicy]
$creadListPrec :: ReadPrec [PutCorsPolicy]
readPrec :: ReadPrec PutCorsPolicy
$creadPrec :: ReadPrec PutCorsPolicy
readList :: ReadS [PutCorsPolicy]
$creadList :: ReadS [PutCorsPolicy]
readsPrec :: Int -> ReadS PutCorsPolicy
$creadsPrec :: Int -> ReadS PutCorsPolicy
Prelude.Read, Int -> PutCorsPolicy -> ShowS
[PutCorsPolicy] -> ShowS
PutCorsPolicy -> String
(Int -> PutCorsPolicy -> ShowS)
-> (PutCorsPolicy -> String)
-> ([PutCorsPolicy] -> ShowS)
-> Show PutCorsPolicy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutCorsPolicy] -> ShowS
$cshowList :: [PutCorsPolicy] -> ShowS
show :: PutCorsPolicy -> String
$cshow :: PutCorsPolicy -> String
showsPrec :: Int -> PutCorsPolicy -> ShowS
$cshowsPrec :: Int -> PutCorsPolicy -> ShowS
Prelude.Show, (forall x. PutCorsPolicy -> Rep PutCorsPolicy x)
-> (forall x. Rep PutCorsPolicy x -> PutCorsPolicy)
-> Generic PutCorsPolicy
forall x. Rep PutCorsPolicy x -> PutCorsPolicy
forall x. PutCorsPolicy -> Rep PutCorsPolicy x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PutCorsPolicy x -> PutCorsPolicy
$cfrom :: forall x. PutCorsPolicy -> Rep PutCorsPolicy x
Prelude.Generic)

-- |
-- Create a value of 'PutCorsPolicy' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'containerName', 'putCorsPolicy_containerName' - The name of the container that you want to assign the CORS policy to.
--
-- 'corsPolicy', 'putCorsPolicy_corsPolicy' - The CORS policy to apply to the container.
newPutCorsPolicy ::
  -- | 'containerName'
  Prelude.Text ->
  -- | 'corsPolicy'
  Prelude.NonEmpty CorsRule ->
  PutCorsPolicy
newPutCorsPolicy :: Text -> NonEmpty CorsRule -> PutCorsPolicy
newPutCorsPolicy Text
pContainerName_ NonEmpty CorsRule
pCorsPolicy_ =
  PutCorsPolicy' :: Text -> NonEmpty CorsRule -> PutCorsPolicy
PutCorsPolicy'
    { $sel:containerName:PutCorsPolicy' :: Text
containerName = Text
pContainerName_,
      $sel:corsPolicy:PutCorsPolicy' :: NonEmpty CorsRule
corsPolicy = Tagged (NonEmpty CorsRule) (Identity (NonEmpty CorsRule))
-> Tagged (NonEmpty CorsRule) (Identity (NonEmpty CorsRule))
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced (Tagged (NonEmpty CorsRule) (Identity (NonEmpty CorsRule))
 -> Tagged (NonEmpty CorsRule) (Identity (NonEmpty CorsRule)))
-> NonEmpty CorsRule -> NonEmpty CorsRule
forall t b. AReview t b -> b -> t
Lens.# NonEmpty CorsRule
pCorsPolicy_
    }

-- | The name of the container that you want to assign the CORS policy to.
putCorsPolicy_containerName :: Lens.Lens' PutCorsPolicy Prelude.Text
putCorsPolicy_containerName :: (Text -> f Text) -> PutCorsPolicy -> f PutCorsPolicy
putCorsPolicy_containerName = (PutCorsPolicy -> Text)
-> (PutCorsPolicy -> Text -> PutCorsPolicy)
-> Lens PutCorsPolicy PutCorsPolicy Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutCorsPolicy' {Text
containerName :: Text
$sel:containerName:PutCorsPolicy' :: PutCorsPolicy -> Text
containerName} -> Text
containerName) (\s :: PutCorsPolicy
s@PutCorsPolicy' {} Text
a -> PutCorsPolicy
s {$sel:containerName:PutCorsPolicy' :: Text
containerName = Text
a} :: PutCorsPolicy)

-- | The CORS policy to apply to the container.
putCorsPolicy_corsPolicy :: Lens.Lens' PutCorsPolicy (Prelude.NonEmpty CorsRule)
putCorsPolicy_corsPolicy :: (NonEmpty CorsRule -> f (NonEmpty CorsRule))
-> PutCorsPolicy -> f PutCorsPolicy
putCorsPolicy_corsPolicy = (PutCorsPolicy -> NonEmpty CorsRule)
-> (PutCorsPolicy -> NonEmpty CorsRule -> PutCorsPolicy)
-> Lens
     PutCorsPolicy PutCorsPolicy (NonEmpty CorsRule) (NonEmpty CorsRule)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutCorsPolicy' {NonEmpty CorsRule
corsPolicy :: NonEmpty CorsRule
$sel:corsPolicy:PutCorsPolicy' :: PutCorsPolicy -> NonEmpty CorsRule
corsPolicy} -> NonEmpty CorsRule
corsPolicy) (\s :: PutCorsPolicy
s@PutCorsPolicy' {} NonEmpty CorsRule
a -> PutCorsPolicy
s {$sel:corsPolicy:PutCorsPolicy' :: NonEmpty CorsRule
corsPolicy = NonEmpty CorsRule
a} :: PutCorsPolicy) ((NonEmpty CorsRule -> f (NonEmpty CorsRule))
 -> PutCorsPolicy -> f PutCorsPolicy)
-> ((NonEmpty CorsRule -> f (NonEmpty CorsRule))
    -> NonEmpty CorsRule -> f (NonEmpty CorsRule))
-> (NonEmpty CorsRule -> f (NonEmpty CorsRule))
-> PutCorsPolicy
-> f PutCorsPolicy
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (NonEmpty CorsRule -> f (NonEmpty CorsRule))
-> NonEmpty CorsRule -> f (NonEmpty CorsRule)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.AWSRequest PutCorsPolicy where
  type
    AWSResponse PutCorsPolicy =
      PutCorsPolicyResponse
  request :: PutCorsPolicy -> Request PutCorsPolicy
request = Service -> PutCorsPolicy -> Request PutCorsPolicy
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy PutCorsPolicy
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PutCorsPolicy)))
response =
    (Int
 -> ResponseHeaders
 -> ()
 -> Either String (AWSResponse PutCorsPolicy))
-> Logger
-> Service
-> Proxy PutCorsPolicy
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse PutCorsPolicy)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> () -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveEmpty
      ( \Int
s ResponseHeaders
h ()
x ->
          Int -> PutCorsPolicyResponse
PutCorsPolicyResponse'
            (Int -> PutCorsPolicyResponse)
-> Either String Int -> Either String PutCorsPolicyResponse
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Int -> Either String Int
forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (Int -> Int
forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable PutCorsPolicy

instance Prelude.NFData PutCorsPolicy

instance Core.ToHeaders PutCorsPolicy where
  toHeaders :: PutCorsPolicy -> ResponseHeaders
toHeaders =
    ResponseHeaders -> PutCorsPolicy -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const
      ( [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"MediaStore_20170901.PutCorsPolicy" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Core.ToJSON PutCorsPolicy where
  toJSON :: PutCorsPolicy -> Value
toJSON PutCorsPolicy' {NonEmpty CorsRule
Text
corsPolicy :: NonEmpty CorsRule
containerName :: Text
$sel:corsPolicy:PutCorsPolicy' :: PutCorsPolicy -> NonEmpty CorsRule
$sel:containerName:PutCorsPolicy' :: PutCorsPolicy -> Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"ContainerName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
containerName),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"CorsPolicy" Text -> NonEmpty CorsRule -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= NonEmpty CorsRule
corsPolicy)
          ]
      )

instance Core.ToPath PutCorsPolicy where
  toPath :: PutCorsPolicy -> ByteString
toPath = ByteString -> PutCorsPolicy -> ByteString
forall a b. a -> b -> a
Prelude.const ByteString
"/"

instance Core.ToQuery PutCorsPolicy where
  toQuery :: PutCorsPolicy -> QueryString
toQuery = QueryString -> PutCorsPolicy -> QueryString
forall a b. a -> b -> a
Prelude.const QueryString
forall a. Monoid a => a
Prelude.mempty

-- | /See:/ 'newPutCorsPolicyResponse' smart constructor.
data PutCorsPolicyResponse = PutCorsPolicyResponse'
  { -- | The response's http status code.
    PutCorsPolicyResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (PutCorsPolicyResponse -> PutCorsPolicyResponse -> Bool
(PutCorsPolicyResponse -> PutCorsPolicyResponse -> Bool)
-> (PutCorsPolicyResponse -> PutCorsPolicyResponse -> Bool)
-> Eq PutCorsPolicyResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PutCorsPolicyResponse -> PutCorsPolicyResponse -> Bool
$c/= :: PutCorsPolicyResponse -> PutCorsPolicyResponse -> Bool
== :: PutCorsPolicyResponse -> PutCorsPolicyResponse -> Bool
$c== :: PutCorsPolicyResponse -> PutCorsPolicyResponse -> Bool
Prelude.Eq, ReadPrec [PutCorsPolicyResponse]
ReadPrec PutCorsPolicyResponse
Int -> ReadS PutCorsPolicyResponse
ReadS [PutCorsPolicyResponse]
(Int -> ReadS PutCorsPolicyResponse)
-> ReadS [PutCorsPolicyResponse]
-> ReadPrec PutCorsPolicyResponse
-> ReadPrec [PutCorsPolicyResponse]
-> Read PutCorsPolicyResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PutCorsPolicyResponse]
$creadListPrec :: ReadPrec [PutCorsPolicyResponse]
readPrec :: ReadPrec PutCorsPolicyResponse
$creadPrec :: ReadPrec PutCorsPolicyResponse
readList :: ReadS [PutCorsPolicyResponse]
$creadList :: ReadS [PutCorsPolicyResponse]
readsPrec :: Int -> ReadS PutCorsPolicyResponse
$creadsPrec :: Int -> ReadS PutCorsPolicyResponse
Prelude.Read, Int -> PutCorsPolicyResponse -> ShowS
[PutCorsPolicyResponse] -> ShowS
PutCorsPolicyResponse -> String
(Int -> PutCorsPolicyResponse -> ShowS)
-> (PutCorsPolicyResponse -> String)
-> ([PutCorsPolicyResponse] -> ShowS)
-> Show PutCorsPolicyResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PutCorsPolicyResponse] -> ShowS
$cshowList :: [PutCorsPolicyResponse] -> ShowS
show :: PutCorsPolicyResponse -> String
$cshow :: PutCorsPolicyResponse -> String
showsPrec :: Int -> PutCorsPolicyResponse -> ShowS
$cshowsPrec :: Int -> PutCorsPolicyResponse -> ShowS
Prelude.Show, (forall x. PutCorsPolicyResponse -> Rep PutCorsPolicyResponse x)
-> (forall x. Rep PutCorsPolicyResponse x -> PutCorsPolicyResponse)
-> Generic PutCorsPolicyResponse
forall x. Rep PutCorsPolicyResponse x -> PutCorsPolicyResponse
forall x. PutCorsPolicyResponse -> Rep PutCorsPolicyResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PutCorsPolicyResponse x -> PutCorsPolicyResponse
$cfrom :: forall x. PutCorsPolicyResponse -> Rep PutCorsPolicyResponse x
Prelude.Generic)

-- |
-- Create a value of 'PutCorsPolicyResponse' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'httpStatus', 'putCorsPolicyResponse_httpStatus' - The response's http status code.
newPutCorsPolicyResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  PutCorsPolicyResponse
newPutCorsPolicyResponse :: Int -> PutCorsPolicyResponse
newPutCorsPolicyResponse Int
pHttpStatus_ =
  PutCorsPolicyResponse' :: Int -> PutCorsPolicyResponse
PutCorsPolicyResponse' {$sel:httpStatus:PutCorsPolicyResponse' :: Int
httpStatus = Int
pHttpStatus_}

-- | The response's http status code.
putCorsPolicyResponse_httpStatus :: Lens.Lens' PutCorsPolicyResponse Prelude.Int
putCorsPolicyResponse_httpStatus :: (Int -> f Int) -> PutCorsPolicyResponse -> f PutCorsPolicyResponse
putCorsPolicyResponse_httpStatus = (PutCorsPolicyResponse -> Int)
-> (PutCorsPolicyResponse -> Int -> PutCorsPolicyResponse)
-> Lens PutCorsPolicyResponse PutCorsPolicyResponse Int Int
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PutCorsPolicyResponse' {Int
httpStatus :: Int
$sel:httpStatus:PutCorsPolicyResponse' :: PutCorsPolicyResponse -> Int
httpStatus} -> Int
httpStatus) (\s :: PutCorsPolicyResponse
s@PutCorsPolicyResponse' {} Int
a -> PutCorsPolicyResponse
s {$sel:httpStatus:PutCorsPolicyResponse' :: Int
httpStatus = Int
a} :: PutCorsPolicyResponse)

instance Prelude.NFData PutCorsPolicyResponse