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

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

-- |
-- Module      : Amazonka.S3.Types.CORSRule
-- 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)
module Amazonka.S3.Types.CORSRule where

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude
import Amazonka.S3.Internal

-- | Specifies a cross-origin access rule for an Amazon S3 bucket.
--
-- /See:/ 'newCORSRule' smart constructor.
data CORSRule = CORSRule'
  { -- | The time in seconds that your browser is to cache the preflight response
    -- for the specified resource.
    CORSRule -> Maybe Int
maxAgeSeconds :: Prelude.Maybe Prelude.Int,
    -- | Headers that are specified in the @Access-Control-Request-Headers@
    -- header. These headers are allowed in a preflight OPTIONS request. In
    -- response to any preflight OPTIONS request, Amazon S3 returns any
    -- requested headers that are allowed.
    CORSRule -> Maybe [Text]
allowedHeaders :: Prelude.Maybe [Prelude.Text],
    -- | One or more headers in the response that you want customers to be able
    -- to access from their applications (for example, from a JavaScript
    -- @XMLHttpRequest@ object).
    CORSRule -> Maybe [Text]
exposeHeaders :: Prelude.Maybe [Prelude.Text],
    -- | Unique identifier for the rule. The value cannot be longer than 255
    -- characters.
    CORSRule -> Maybe Text
id :: Prelude.Maybe Prelude.Text,
    -- | An HTTP method that you allow the origin to execute. Valid values are
    -- @GET@, @PUT@, @HEAD@, @POST@, and @DELETE@.
    CORSRule -> [Text]
allowedMethods :: [Prelude.Text],
    -- | One or more origins you want customers to be able to access the bucket
    -- from.
    CORSRule -> [Text]
allowedOrigins :: [Prelude.Text]
  }
  deriving (CORSRule -> CORSRule -> Bool
(CORSRule -> CORSRule -> Bool)
-> (CORSRule -> CORSRule -> Bool) -> Eq CORSRule
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CORSRule -> CORSRule -> Bool
$c/= :: CORSRule -> CORSRule -> Bool
== :: CORSRule -> CORSRule -> Bool
$c== :: CORSRule -> CORSRule -> Bool
Prelude.Eq, ReadPrec [CORSRule]
ReadPrec CORSRule
Int -> ReadS CORSRule
ReadS [CORSRule]
(Int -> ReadS CORSRule)
-> ReadS [CORSRule]
-> ReadPrec CORSRule
-> ReadPrec [CORSRule]
-> Read CORSRule
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CORSRule]
$creadListPrec :: ReadPrec [CORSRule]
readPrec :: ReadPrec CORSRule
$creadPrec :: ReadPrec CORSRule
readList :: ReadS [CORSRule]
$creadList :: ReadS [CORSRule]
readsPrec :: Int -> ReadS CORSRule
$creadsPrec :: Int -> ReadS CORSRule
Prelude.Read, Int -> CORSRule -> ShowS
[CORSRule] -> ShowS
CORSRule -> String
(Int -> CORSRule -> ShowS)
-> (CORSRule -> String) -> ([CORSRule] -> ShowS) -> Show CORSRule
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CORSRule] -> ShowS
$cshowList :: [CORSRule] -> ShowS
show :: CORSRule -> String
$cshow :: CORSRule -> String
showsPrec :: Int -> CORSRule -> ShowS
$cshowsPrec :: Int -> CORSRule -> ShowS
Prelude.Show, (forall x. CORSRule -> Rep CORSRule x)
-> (forall x. Rep CORSRule x -> CORSRule) -> Generic CORSRule
forall x. Rep CORSRule x -> CORSRule
forall x. CORSRule -> Rep CORSRule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CORSRule x -> CORSRule
$cfrom :: forall x. CORSRule -> Rep CORSRule x
Prelude.Generic)

-- |
-- Create a value of 'CORSRule' 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:
--
-- 'maxAgeSeconds', 'cORSRule_maxAgeSeconds' - The time in seconds that your browser is to cache the preflight response
-- for the specified resource.
--
-- 'allowedHeaders', 'cORSRule_allowedHeaders' - Headers that are specified in the @Access-Control-Request-Headers@
-- header. These headers are allowed in a preflight OPTIONS request. In
-- response to any preflight OPTIONS request, Amazon S3 returns any
-- requested headers that are allowed.
--
-- 'exposeHeaders', 'cORSRule_exposeHeaders' - One or more headers in the response that you want customers to be able
-- to access from their applications (for example, from a JavaScript
-- @XMLHttpRequest@ object).
--
-- 'id', 'cORSRule_id' - Unique identifier for the rule. The value cannot be longer than 255
-- characters.
--
-- 'allowedMethods', 'cORSRule_allowedMethods' - An HTTP method that you allow the origin to execute. Valid values are
-- @GET@, @PUT@, @HEAD@, @POST@, and @DELETE@.
--
-- 'allowedOrigins', 'cORSRule_allowedOrigins' - One or more origins you want customers to be able to access the bucket
-- from.
newCORSRule ::
  CORSRule
newCORSRule :: CORSRule
newCORSRule =
  CORSRule' :: Maybe Int
-> Maybe [Text]
-> Maybe [Text]
-> Maybe Text
-> [Text]
-> [Text]
-> CORSRule
CORSRule'
    { $sel:maxAgeSeconds:CORSRule' :: Maybe Int
maxAgeSeconds = Maybe Int
forall a. Maybe a
Prelude.Nothing,
      $sel:allowedHeaders:CORSRule' :: Maybe [Text]
allowedHeaders = Maybe [Text]
forall a. Maybe a
Prelude.Nothing,
      $sel:exposeHeaders:CORSRule' :: Maybe [Text]
exposeHeaders = Maybe [Text]
forall a. Maybe a
Prelude.Nothing,
      $sel:id:CORSRule' :: Maybe Text
id = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:allowedMethods:CORSRule' :: [Text]
allowedMethods = [Text]
forall a. Monoid a => a
Prelude.mempty,
      $sel:allowedOrigins:CORSRule' :: [Text]
allowedOrigins = [Text]
forall a. Monoid a => a
Prelude.mempty
    }

-- | The time in seconds that your browser is to cache the preflight response
-- for the specified resource.
cORSRule_maxAgeSeconds :: Lens.Lens' CORSRule (Prelude.Maybe Prelude.Int)
cORSRule_maxAgeSeconds :: (Maybe Int -> f (Maybe Int)) -> CORSRule -> f CORSRule
cORSRule_maxAgeSeconds = (CORSRule -> Maybe Int)
-> (CORSRule -> Maybe Int -> CORSRule)
-> Lens CORSRule CORSRule (Maybe Int) (Maybe Int)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CORSRule' {Maybe Int
maxAgeSeconds :: Maybe Int
$sel:maxAgeSeconds:CORSRule' :: CORSRule -> Maybe Int
maxAgeSeconds} -> Maybe Int
maxAgeSeconds) (\s :: CORSRule
s@CORSRule' {} Maybe Int
a -> CORSRule
s {$sel:maxAgeSeconds:CORSRule' :: Maybe Int
maxAgeSeconds = Maybe Int
a} :: CORSRule)

-- | Headers that are specified in the @Access-Control-Request-Headers@
-- header. These headers are allowed in a preflight OPTIONS request. In
-- response to any preflight OPTIONS request, Amazon S3 returns any
-- requested headers that are allowed.
cORSRule_allowedHeaders :: Lens.Lens' CORSRule (Prelude.Maybe [Prelude.Text])
cORSRule_allowedHeaders :: (Maybe [Text] -> f (Maybe [Text])) -> CORSRule -> f CORSRule
cORSRule_allowedHeaders = (CORSRule -> Maybe [Text])
-> (CORSRule -> Maybe [Text] -> CORSRule)
-> Lens CORSRule CORSRule (Maybe [Text]) (Maybe [Text])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CORSRule' {Maybe [Text]
allowedHeaders :: Maybe [Text]
$sel:allowedHeaders:CORSRule' :: CORSRule -> Maybe [Text]
allowedHeaders} -> Maybe [Text]
allowedHeaders) (\s :: CORSRule
s@CORSRule' {} Maybe [Text]
a -> CORSRule
s {$sel:allowedHeaders:CORSRule' :: Maybe [Text]
allowedHeaders = Maybe [Text]
a} :: CORSRule) ((Maybe [Text] -> f (Maybe [Text])) -> CORSRule -> f CORSRule)
-> ((Maybe [Text] -> f (Maybe [Text]))
    -> Maybe [Text] -> f (Maybe [Text]))
-> (Maybe [Text] -> f (Maybe [Text]))
-> CORSRule
-> f CORSRule
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso [Text] [Text] [Text] [Text]
-> Iso (Maybe [Text]) (Maybe [Text]) (Maybe [Text]) (Maybe [Text])
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping AnIso [Text] [Text] [Text] [Text]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | One or more headers in the response that you want customers to be able
-- to access from their applications (for example, from a JavaScript
-- @XMLHttpRequest@ object).
cORSRule_exposeHeaders :: Lens.Lens' CORSRule (Prelude.Maybe [Prelude.Text])
cORSRule_exposeHeaders :: (Maybe [Text] -> f (Maybe [Text])) -> CORSRule -> f CORSRule
cORSRule_exposeHeaders = (CORSRule -> Maybe [Text])
-> (CORSRule -> Maybe [Text] -> CORSRule)
-> Lens CORSRule CORSRule (Maybe [Text]) (Maybe [Text])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CORSRule' {Maybe [Text]
exposeHeaders :: Maybe [Text]
$sel:exposeHeaders:CORSRule' :: CORSRule -> Maybe [Text]
exposeHeaders} -> Maybe [Text]
exposeHeaders) (\s :: CORSRule
s@CORSRule' {} Maybe [Text]
a -> CORSRule
s {$sel:exposeHeaders:CORSRule' :: Maybe [Text]
exposeHeaders = Maybe [Text]
a} :: CORSRule) ((Maybe [Text] -> f (Maybe [Text])) -> CORSRule -> f CORSRule)
-> ((Maybe [Text] -> f (Maybe [Text]))
    -> Maybe [Text] -> f (Maybe [Text]))
-> (Maybe [Text] -> f (Maybe [Text]))
-> CORSRule
-> f CORSRule
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. AnIso [Text] [Text] [Text] [Text]
-> Iso (Maybe [Text]) (Maybe [Text]) (Maybe [Text]) (Maybe [Text])
forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping AnIso [Text] [Text] [Text] [Text]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | Unique identifier for the rule. The value cannot be longer than 255
-- characters.
cORSRule_id :: Lens.Lens' CORSRule (Prelude.Maybe Prelude.Text)
cORSRule_id :: (Maybe Text -> f (Maybe Text)) -> CORSRule -> f CORSRule
cORSRule_id = (CORSRule -> Maybe Text)
-> (CORSRule -> Maybe Text -> CORSRule)
-> Lens CORSRule CORSRule (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CORSRule' {Maybe Text
id :: Maybe Text
$sel:id:CORSRule' :: CORSRule -> Maybe Text
id} -> Maybe Text
id) (\s :: CORSRule
s@CORSRule' {} Maybe Text
a -> CORSRule
s {$sel:id:CORSRule' :: Maybe Text
id = Maybe Text
a} :: CORSRule)

-- | An HTTP method that you allow the origin to execute. Valid values are
-- @GET@, @PUT@, @HEAD@, @POST@, and @DELETE@.
cORSRule_allowedMethods :: Lens.Lens' CORSRule [Prelude.Text]
cORSRule_allowedMethods :: ([Text] -> f [Text]) -> CORSRule -> f CORSRule
cORSRule_allowedMethods = (CORSRule -> [Text])
-> (CORSRule -> [Text] -> CORSRule)
-> Lens CORSRule CORSRule [Text] [Text]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CORSRule' {[Text]
allowedMethods :: [Text]
$sel:allowedMethods:CORSRule' :: CORSRule -> [Text]
allowedMethods} -> [Text]
allowedMethods) (\s :: CORSRule
s@CORSRule' {} [Text]
a -> CORSRule
s {$sel:allowedMethods:CORSRule' :: [Text]
allowedMethods = [Text]
a} :: CORSRule) (([Text] -> f [Text]) -> CORSRule -> f CORSRule)
-> (([Text] -> f [Text]) -> [Text] -> f [Text])
-> ([Text] -> f [Text])
-> CORSRule
-> f CORSRule
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. ([Text] -> f [Text]) -> [Text] -> f [Text]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | One or more origins you want customers to be able to access the bucket
-- from.
cORSRule_allowedOrigins :: Lens.Lens' CORSRule [Prelude.Text]
cORSRule_allowedOrigins :: ([Text] -> f [Text]) -> CORSRule -> f CORSRule
cORSRule_allowedOrigins = (CORSRule -> [Text])
-> (CORSRule -> [Text] -> CORSRule)
-> Lens CORSRule CORSRule [Text] [Text]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CORSRule' {[Text]
allowedOrigins :: [Text]
$sel:allowedOrigins:CORSRule' :: CORSRule -> [Text]
allowedOrigins} -> [Text]
allowedOrigins) (\s :: CORSRule
s@CORSRule' {} [Text]
a -> CORSRule
s {$sel:allowedOrigins:CORSRule' :: [Text]
allowedOrigins = [Text]
a} :: CORSRule) (([Text] -> f [Text]) -> CORSRule -> f CORSRule)
-> (([Text] -> f [Text]) -> [Text] -> f [Text])
-> ([Text] -> f [Text])
-> CORSRule
-> f CORSRule
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. ([Text] -> f [Text]) -> [Text] -> f [Text]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.FromXML CORSRule where
  parseXML :: [Node] -> Either String CORSRule
parseXML [Node]
x =
    Maybe Int
-> Maybe [Text]
-> Maybe [Text]
-> Maybe Text
-> [Text]
-> [Text]
-> CORSRule
CORSRule'
      (Maybe Int
 -> Maybe [Text]
 -> Maybe [Text]
 -> Maybe Text
 -> [Text]
 -> [Text]
 -> CORSRule)
-> Either String (Maybe Int)
-> Either
     String
     (Maybe [Text]
      -> Maybe [Text] -> Maybe Text -> [Text] -> [Text] -> CORSRule)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> ([Node]
x [Node] -> Text -> Either String (Maybe Int)
forall a. FromXML a => [Node] -> Text -> Either String (Maybe a)
Core..@? Text
"MaxAgeSeconds")
      Either
  String
  (Maybe [Text]
   -> Maybe [Text] -> Maybe Text -> [Text] -> [Text] -> CORSRule)
-> Either String (Maybe [Text])
-> Either
     String (Maybe [Text] -> Maybe Text -> [Text] -> [Text] -> CORSRule)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (([Node] -> Either String [Text])
-> [Node] -> Either String (Maybe [Text])
forall (f :: * -> *) a b.
Applicative f =>
([a] -> f b) -> [a] -> f (Maybe b)
Core.may (Text -> [Node] -> Either String [Text]
forall a. FromXML a => Text -> [Node] -> Either String [a]
Core.parseXMLList Text
"AllowedHeader") [Node]
x)
      Either
  String (Maybe [Text] -> Maybe Text -> [Text] -> [Text] -> CORSRule)
-> Either String (Maybe [Text])
-> Either String (Maybe Text -> [Text] -> [Text] -> CORSRule)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (([Node] -> Either String [Text])
-> [Node] -> Either String (Maybe [Text])
forall (f :: * -> *) a b.
Applicative f =>
([a] -> f b) -> [a] -> f (Maybe b)
Core.may (Text -> [Node] -> Either String [Text]
forall a. FromXML a => Text -> [Node] -> Either String [a]
Core.parseXMLList Text
"ExposeHeader") [Node]
x)
      Either String (Maybe Text -> [Text] -> [Text] -> CORSRule)
-> Either String (Maybe Text)
-> Either String ([Text] -> [Text] -> CORSRule)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> ([Node]
x [Node] -> Text -> Either String (Maybe Text)
forall a. FromXML a => [Node] -> Text -> Either String (Maybe a)
Core..@? Text
"ID")
      Either String ([Text] -> [Text] -> CORSRule)
-> Either String [Text] -> Either String ([Text] -> CORSRule)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Text -> [Node] -> Either String [Text]
forall a. FromXML a => Text -> [Node] -> Either String [a]
Core.parseXMLList Text
"AllowedMethod" [Node]
x)
      Either String ([Text] -> CORSRule)
-> Either String [Text] -> Either String CORSRule
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Text -> [Node] -> Either String [Text]
forall a. FromXML a => Text -> [Node] -> Either String [a]
Core.parseXMLList Text
"AllowedOrigin" [Node]
x)

instance Prelude.Hashable CORSRule

instance Prelude.NFData CORSRule

instance Core.ToXML CORSRule where
  toXML :: CORSRule -> XML
toXML CORSRule' {[Text]
Maybe Int
Maybe [Text]
Maybe Text
allowedOrigins :: [Text]
allowedMethods :: [Text]
id :: Maybe Text
exposeHeaders :: Maybe [Text]
allowedHeaders :: Maybe [Text]
maxAgeSeconds :: Maybe Int
$sel:allowedOrigins:CORSRule' :: CORSRule -> [Text]
$sel:allowedMethods:CORSRule' :: CORSRule -> [Text]
$sel:id:CORSRule' :: CORSRule -> Maybe Text
$sel:exposeHeaders:CORSRule' :: CORSRule -> Maybe [Text]
$sel:allowedHeaders:CORSRule' :: CORSRule -> Maybe [Text]
$sel:maxAgeSeconds:CORSRule' :: CORSRule -> Maybe Int
..} =
    [XML] -> XML
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ Name
"MaxAgeSeconds" Name -> Maybe Int -> XML
forall a. ToXML a => Name -> a -> XML
Core.@= Maybe Int
maxAgeSeconds,
        Maybe XML -> XML
forall a. ToXML a => a -> XML
Core.toXML
          ( Name -> [Text] -> XML
forall a. (IsList a, ToXML (Item a)) => Name -> a -> XML
Core.toXMLList Name
"AllowedHeader"
              ([Text] -> XML) -> Maybe [Text] -> Maybe XML
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
allowedHeaders
          ),
        Maybe XML -> XML
forall a. ToXML a => a -> XML
Core.toXML
          ( Name -> [Text] -> XML
forall a. (IsList a, ToXML (Item a)) => Name -> a -> XML
Core.toXMLList Name
"ExposeHeader"
              ([Text] -> XML) -> Maybe [Text] -> Maybe XML
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
exposeHeaders
          ),
        Name
"ID" Name -> Maybe Text -> XML
forall a. ToXML a => Name -> a -> XML
Core.@= Maybe Text
id,
        Name -> [Text] -> XML
forall a. (IsList a, ToXML (Item a)) => Name -> a -> XML
Core.toXMLList Name
"AllowedMethod" [Text]
allowedMethods,
        Name -> [Text] -> XML
forall a. (IsList a, ToXML (Item a)) => Name -> a -> XML
Core.toXMLList Name
"AllowedOrigin" [Text]
allowedOrigins
      ]