{-# 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.CloudFront.Types.GeoRestriction
-- 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.CloudFront.Types.GeoRestriction where

import Amazonka.CloudFront.Types.GeoRestrictionType
import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | A complex type that controls the countries in which your content is
-- distributed. CloudFront determines the location of your users using
-- @MaxMind@ GeoIP databases.
--
-- /See:/ 'newGeoRestriction' smart constructor.
data GeoRestriction = GeoRestriction'
  { -- | A complex type that contains a @Location@ element for each country in
    -- which you want CloudFront either to distribute your content
    -- (@whitelist@) or not distribute your content (@blacklist@).
    --
    -- The @Location@ element is a two-letter, uppercase country code for a
    -- country that you want to include in your @blacklist@ or @whitelist@.
    -- Include one @Location@ element for each country.
    --
    -- CloudFront and @MaxMind@ both use @ISO 3166@ country codes. For the
    -- current list of countries and the corresponding codes, see
    -- @ISO 3166-1-alpha-2@ code on the /International Organization for
    -- Standardization/ website. You can also refer to the country list on the
    -- CloudFront console, which includes both country names and codes.
    GeoRestriction -> Maybe [Text]
items :: Prelude.Maybe [Prelude.Text],
    -- | The method that you want to use to restrict distribution of your content
    -- by country:
    --
    -- -   @none@: No geo restriction is enabled, meaning access to content is
    --     not restricted by client geo location.
    --
    -- -   @blacklist@: The @Location@ elements specify the countries in which
    --     you don\'t want CloudFront to distribute your content.
    --
    -- -   @whitelist@: The @Location@ elements specify the countries in which
    --     you want CloudFront to distribute your content.
    GeoRestriction -> GeoRestrictionType
restrictionType :: GeoRestrictionType,
    -- | When geo restriction is @enabled@, this is the number of countries in
    -- your @whitelist@ or @blacklist@. Otherwise, when it is not enabled,
    -- @Quantity@ is @0@, and you can omit @Items@.
    GeoRestriction -> Int
quantity :: Prelude.Int
  }
  deriving (GeoRestriction -> GeoRestriction -> Bool
(GeoRestriction -> GeoRestriction -> Bool)
-> (GeoRestriction -> GeoRestriction -> Bool) -> Eq GeoRestriction
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GeoRestriction -> GeoRestriction -> Bool
$c/= :: GeoRestriction -> GeoRestriction -> Bool
== :: GeoRestriction -> GeoRestriction -> Bool
$c== :: GeoRestriction -> GeoRestriction -> Bool
Prelude.Eq, ReadPrec [GeoRestriction]
ReadPrec GeoRestriction
Int -> ReadS GeoRestriction
ReadS [GeoRestriction]
(Int -> ReadS GeoRestriction)
-> ReadS [GeoRestriction]
-> ReadPrec GeoRestriction
-> ReadPrec [GeoRestriction]
-> Read GeoRestriction
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [GeoRestriction]
$creadListPrec :: ReadPrec [GeoRestriction]
readPrec :: ReadPrec GeoRestriction
$creadPrec :: ReadPrec GeoRestriction
readList :: ReadS [GeoRestriction]
$creadList :: ReadS [GeoRestriction]
readsPrec :: Int -> ReadS GeoRestriction
$creadsPrec :: Int -> ReadS GeoRestriction
Prelude.Read, Int -> GeoRestriction -> ShowS
[GeoRestriction] -> ShowS
GeoRestriction -> String
(Int -> GeoRestriction -> ShowS)
-> (GeoRestriction -> String)
-> ([GeoRestriction] -> ShowS)
-> Show GeoRestriction
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GeoRestriction] -> ShowS
$cshowList :: [GeoRestriction] -> ShowS
show :: GeoRestriction -> String
$cshow :: GeoRestriction -> String
showsPrec :: Int -> GeoRestriction -> ShowS
$cshowsPrec :: Int -> GeoRestriction -> ShowS
Prelude.Show, (forall x. GeoRestriction -> Rep GeoRestriction x)
-> (forall x. Rep GeoRestriction x -> GeoRestriction)
-> Generic GeoRestriction
forall x. Rep GeoRestriction x -> GeoRestriction
forall x. GeoRestriction -> Rep GeoRestriction x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GeoRestriction x -> GeoRestriction
$cfrom :: forall x. GeoRestriction -> Rep GeoRestriction x
Prelude.Generic)

-- |
-- Create a value of 'GeoRestriction' 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:
--
-- 'items', 'geoRestriction_items' - A complex type that contains a @Location@ element for each country in
-- which you want CloudFront either to distribute your content
-- (@whitelist@) or not distribute your content (@blacklist@).
--
-- The @Location@ element is a two-letter, uppercase country code for a
-- country that you want to include in your @blacklist@ or @whitelist@.
-- Include one @Location@ element for each country.
--
-- CloudFront and @MaxMind@ both use @ISO 3166@ country codes. For the
-- current list of countries and the corresponding codes, see
-- @ISO 3166-1-alpha-2@ code on the /International Organization for
-- Standardization/ website. You can also refer to the country list on the
-- CloudFront console, which includes both country names and codes.
--
-- 'restrictionType', 'geoRestriction_restrictionType' - The method that you want to use to restrict distribution of your content
-- by country:
--
-- -   @none@: No geo restriction is enabled, meaning access to content is
--     not restricted by client geo location.
--
-- -   @blacklist@: The @Location@ elements specify the countries in which
--     you don\'t want CloudFront to distribute your content.
--
-- -   @whitelist@: The @Location@ elements specify the countries in which
--     you want CloudFront to distribute your content.
--
-- 'quantity', 'geoRestriction_quantity' - When geo restriction is @enabled@, this is the number of countries in
-- your @whitelist@ or @blacklist@. Otherwise, when it is not enabled,
-- @Quantity@ is @0@, and you can omit @Items@.
newGeoRestriction ::
  -- | 'restrictionType'
  GeoRestrictionType ->
  -- | 'quantity'
  Prelude.Int ->
  GeoRestriction
newGeoRestriction :: GeoRestrictionType -> Int -> GeoRestriction
newGeoRestriction GeoRestrictionType
pRestrictionType_ Int
pQuantity_ =
  GeoRestriction' :: Maybe [Text] -> GeoRestrictionType -> Int -> GeoRestriction
GeoRestriction'
    { $sel:items:GeoRestriction' :: Maybe [Text]
items = Maybe [Text]
forall a. Maybe a
Prelude.Nothing,
      $sel:restrictionType:GeoRestriction' :: GeoRestrictionType
restrictionType = GeoRestrictionType
pRestrictionType_,
      $sel:quantity:GeoRestriction' :: Int
quantity = Int
pQuantity_
    }

-- | A complex type that contains a @Location@ element for each country in
-- which you want CloudFront either to distribute your content
-- (@whitelist@) or not distribute your content (@blacklist@).
--
-- The @Location@ element is a two-letter, uppercase country code for a
-- country that you want to include in your @blacklist@ or @whitelist@.
-- Include one @Location@ element for each country.
--
-- CloudFront and @MaxMind@ both use @ISO 3166@ country codes. For the
-- current list of countries and the corresponding codes, see
-- @ISO 3166-1-alpha-2@ code on the /International Organization for
-- Standardization/ website. You can also refer to the country list on the
-- CloudFront console, which includes both country names and codes.
geoRestriction_items :: Lens.Lens' GeoRestriction (Prelude.Maybe [Prelude.Text])
geoRestriction_items :: (Maybe [Text] -> f (Maybe [Text]))
-> GeoRestriction -> f GeoRestriction
geoRestriction_items = (GeoRestriction -> Maybe [Text])
-> (GeoRestriction -> Maybe [Text] -> GeoRestriction)
-> Lens GeoRestriction GeoRestriction (Maybe [Text]) (Maybe [Text])
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GeoRestriction' {Maybe [Text]
items :: Maybe [Text]
$sel:items:GeoRestriction' :: GeoRestriction -> Maybe [Text]
items} -> Maybe [Text]
items) (\s :: GeoRestriction
s@GeoRestriction' {} Maybe [Text]
a -> GeoRestriction
s {$sel:items:GeoRestriction' :: Maybe [Text]
items = Maybe [Text]
a} :: GeoRestriction) ((Maybe [Text] -> f (Maybe [Text]))
 -> GeoRestriction -> f GeoRestriction)
-> ((Maybe [Text] -> f (Maybe [Text]))
    -> Maybe [Text] -> f (Maybe [Text]))
-> (Maybe [Text] -> f (Maybe [Text]))
-> GeoRestriction
-> f GeoRestriction
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

-- | The method that you want to use to restrict distribution of your content
-- by country:
--
-- -   @none@: No geo restriction is enabled, meaning access to content is
--     not restricted by client geo location.
--
-- -   @blacklist@: The @Location@ elements specify the countries in which
--     you don\'t want CloudFront to distribute your content.
--
-- -   @whitelist@: The @Location@ elements specify the countries in which
--     you want CloudFront to distribute your content.
geoRestriction_restrictionType :: Lens.Lens' GeoRestriction GeoRestrictionType
geoRestriction_restrictionType :: (GeoRestrictionType -> f GeoRestrictionType)
-> GeoRestriction -> f GeoRestriction
geoRestriction_restrictionType = (GeoRestriction -> GeoRestrictionType)
-> (GeoRestriction -> GeoRestrictionType -> GeoRestriction)
-> Lens
     GeoRestriction GeoRestriction GeoRestrictionType GeoRestrictionType
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GeoRestriction' {GeoRestrictionType
restrictionType :: GeoRestrictionType
$sel:restrictionType:GeoRestriction' :: GeoRestriction -> GeoRestrictionType
restrictionType} -> GeoRestrictionType
restrictionType) (\s :: GeoRestriction
s@GeoRestriction' {} GeoRestrictionType
a -> GeoRestriction
s {$sel:restrictionType:GeoRestriction' :: GeoRestrictionType
restrictionType = GeoRestrictionType
a} :: GeoRestriction)

-- | When geo restriction is @enabled@, this is the number of countries in
-- your @whitelist@ or @blacklist@. Otherwise, when it is not enabled,
-- @Quantity@ is @0@, and you can omit @Items@.
geoRestriction_quantity :: Lens.Lens' GeoRestriction Prelude.Int
geoRestriction_quantity :: (Int -> f Int) -> GeoRestriction -> f GeoRestriction
geoRestriction_quantity = (GeoRestriction -> Int)
-> (GeoRestriction -> Int -> GeoRestriction)
-> Lens GeoRestriction GeoRestriction Int Int
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\GeoRestriction' {Int
quantity :: Int
$sel:quantity:GeoRestriction' :: GeoRestriction -> Int
quantity} -> Int
quantity) (\s :: GeoRestriction
s@GeoRestriction' {} Int
a -> GeoRestriction
s {$sel:quantity:GeoRestriction' :: Int
quantity = Int
a} :: GeoRestriction)

instance Core.FromXML GeoRestriction where
  parseXML :: [Node] -> Either String GeoRestriction
parseXML [Node]
x =
    Maybe [Text] -> GeoRestrictionType -> Int -> GeoRestriction
GeoRestriction'
      (Maybe [Text] -> GeoRestrictionType -> Int -> GeoRestriction)
-> Either String (Maybe [Text])
-> Either String (GeoRestrictionType -> Int -> GeoRestriction)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> ( [Node]
x [Node] -> Text -> Either String (Maybe [Node])
forall a. FromXML a => [Node] -> Text -> Either String (Maybe a)
Core..@? Text
"Items" Either String (Maybe [Node]) -> [Node] -> Either String [Node]
forall (f :: * -> *) a. Functor f => f (Maybe a) -> a -> f a
Core..!@ [Node]
forall a. Monoid a => a
Prelude.mempty
                      Either String [Node]
-> ([Node] -> Either String (Maybe [Text]))
-> Either String (Maybe [Text])
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m 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
"Location")
                  )
      Either String (GeoRestrictionType -> Int -> GeoRestriction)
-> Either String GeoRestrictionType
-> Either String (Int -> GeoRestriction)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> ([Node]
x [Node] -> Text -> Either String GeoRestrictionType
forall a. FromXML a => [Node] -> Text -> Either String a
Core..@ Text
"RestrictionType")
      Either String (Int -> GeoRestriction)
-> Either String Int -> Either String GeoRestriction
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> ([Node]
x [Node] -> Text -> Either String Int
forall a. FromXML a => [Node] -> Text -> Either String a
Core..@ Text
"Quantity")

instance Prelude.Hashable GeoRestriction

instance Prelude.NFData GeoRestriction

instance Core.ToXML GeoRestriction where
  toXML :: GeoRestriction -> XML
toXML GeoRestriction' {Int
Maybe [Text]
GeoRestrictionType
quantity :: Int
restrictionType :: GeoRestrictionType
items :: Maybe [Text]
$sel:quantity:GeoRestriction' :: GeoRestriction -> Int
$sel:restrictionType:GeoRestriction' :: GeoRestriction -> GeoRestrictionType
$sel:items:GeoRestriction' :: GeoRestriction -> Maybe [Text]
..} =
    [XML] -> XML
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ Name
"Items"
          Name -> XML -> XML
forall a. ToXML a => Name -> a -> XML
Core.@= 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
"Location" ([Text] -> XML) -> Maybe [Text] -> Maybe XML
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe [Text]
items),
        Name
"RestrictionType" Name -> GeoRestrictionType -> XML
forall a. ToXML a => Name -> a -> XML
Core.@= GeoRestrictionType
restrictionType,
        Name
"Quantity" Name -> Int -> XML
forall a. ToXML a => Name -> a -> XML
Core.@= Int
quantity
      ]