-- |
-- Module      : Amazonka.Endpoint
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : provisional
-- Portability : non-portable (GHC extensions)
module Amazonka.Endpoint
  ( -- * Endpoint
    setEndpoint,
    defaultEndpoint,
  )
where

import Amazonka.Data.ByteString
import Amazonka.Lens ((%~), (.~))
import Amazonka.Prelude
import Amazonka.Types
import qualified Data.CaseInsensitive as CI

-- | A convenience function for overriding the 'Service' 'Endpoint'.
--
-- /See:/ 'serviceEndpoint'.
setEndpoint ::
  -- | Whether to use HTTPS (ie. SSL).
  Bool ->
  -- | The hostname to connect to.
  ByteString ->
  -- | The port number to connect to.
  Int ->
  -- | The service configuration to override.
  Service ->
  Service
setEndpoint :: Bool -> ByteString -> Int -> Service -> Service
setEndpoint Bool
s ByteString
h Int
p = (Endpoint -> Identity Endpoint) -> Service -> Identity Service
Setter' Service Endpoint
serviceEndpoint ((Endpoint -> Identity Endpoint) -> Service -> Identity Service)
-> (Endpoint -> Endpoint) -> Service -> Service
forall s t a b. ASetter s t a b -> (a -> b) -> s -> t
%~ Endpoint -> Endpoint
addr
  where
    addr :: Endpoint -> Endpoint
addr =
      ((Bool -> Identity Bool) -> Endpoint -> Identity Endpoint
Lens' Endpoint Bool
endpointSecure ((Bool -> Identity Bool) -> Endpoint -> Identity Endpoint)
-> Bool -> Endpoint -> Endpoint
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Bool
s)
        (Endpoint -> Endpoint)
-> (Endpoint -> Endpoint) -> Endpoint -> Endpoint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ByteString -> Identity ByteString)
-> Endpoint -> Identity Endpoint
Lens' Endpoint ByteString
endpointHost ((ByteString -> Identity ByteString)
 -> Endpoint -> Identity Endpoint)
-> ByteString -> Endpoint -> Endpoint
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ByteString
h)
        (Endpoint -> Endpoint)
-> (Endpoint -> Endpoint) -> Endpoint -> Endpoint
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Int -> Identity Int) -> Endpoint -> Identity Endpoint
Lens' Endpoint Int
endpointPort ((Int -> Identity Int) -> Endpoint -> Identity Endpoint)
-> Int -> Endpoint -> Endpoint
forall s t a b. ASetter s t a b -> b -> s -> t
.~ Int
p)

-- | Determine the full host address and credential scope
-- within the specified 'Region'.
defaultEndpoint :: Service -> Region -> Endpoint
defaultEndpoint :: Service -> Region -> Endpoint
defaultEndpoint (Service -> ByteString
_serviceEndpointPrefix -> ByteString
p) Region
r = CI ByteString -> Endpoint
go (ByteString -> CI ByteString
forall s. FoldCase s => s -> CI s
CI.mk ByteString
p)
  where
    go :: CI ByteString -> Endpoint
go = \case
      CI ByteString
"iam"
        | Bool
china -> ByteString -> Endpoint
region ByteString
"iam.cn-north-1.amazonaws.com.cn"
        | Bool
govcloud -> ByteString -> Endpoint
region ByteString
"iam.us-gov.amazonaws.com"
        | Bool
otherwise -> ByteString -> Endpoint
global ByteString
"iam.amazonaws.com"
      CI ByteString
"sdb"
        | Bool
virginia -> ByteString -> Endpoint
region ByteString
"sdb.amazonaws.com"
      CI ByteString
"sts"
        | Bool
china -> ByteString -> Endpoint
region ByteString
"sts.cn-north-1.amazonaws.com.cn"
        | Bool
govcloud -> ByteString -> Endpoint
region (ByteString
"sts." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
reg ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
".amazonaws.com")
        | Bool
otherwise -> ByteString -> Endpoint
global ByteString
"sts.amazonaws.com"
      CI ByteString
"rds"
        | Bool
virginia -> ByteString -> Endpoint
global ByteString
"rds.amazonaws.com"
      CI ByteString
"route53"
        | Bool -> Bool
not Bool
china -> ByteString -> Endpoint
global ByteString
"route53.amazonaws.com"
      CI ByteString
"emr"
        | Bool
virginia -> ByteString -> Endpoint
global ByteString
"elasticmapreduce.us-east-1.amazonaws.com"
        | Bool
china -> ByteString -> Endpoint
region ByteString
"elasticmapreduce.cn-north-1.amazonaws.com.cn"
        | Bool
frankfurt -> ByteString -> Endpoint
region ByteString
"elasticmapreduce.eu-central-1.amazonaws.com"
        | Bool
otherwise -> ByteString -> Endpoint
region (ByteString
reg ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
".elasticmapreduce.amazonaws.com")
      CI ByteString
"sqs"
        | Bool
virginia -> ByteString -> Endpoint
global ByteString
"queue.amazonaws.com"
        | Bool
china -> ByteString -> Endpoint
region (ByteString
reg ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
".queue.amazonaws.com.cn")
      CI ByteString
"importexport"
        | Bool -> Bool
not Bool
china -> ByteString -> Endpoint
region ByteString
"importexport.amazonaws.com"
      CI ByteString
"cloudfront"
        | Bool -> Bool
not Bool
china -> ByteString -> Endpoint
global ByteString
"cloudfront.amazonaws.com"
      CI ByteString
"waf"
        | Bool -> Bool
not Bool
china -> ByteString -> Endpoint
global ByteString
"waf.amazonaws.com"
      CI ByteString
_other
        | Bool
china -> ByteString -> Endpoint
region (ByteString
p ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
reg ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
".amazonaws.com.cn")
        | Bool
otherwise -> ByteString -> Endpoint
region (ByteString
p ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
"." ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
reg ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
".amazonaws.com")

    virginia :: Bool
virginia = Region
r Region -> Region -> Bool
forall a. Eq a => a -> a -> Bool
== Region
NorthVirginia
    frankfurt :: Bool
frankfurt = Region
r Region -> Region -> Bool
forall a. Eq a => a -> a -> Bool
== Region
Frankfurt
    china :: Bool
china = Region
r Region -> Region -> Bool
forall a. Eq a => a -> a -> Bool
== Region
Beijing
    govcloud :: Bool
govcloud = Region
r Region -> Region -> Bool
forall a. Eq a => a -> a -> Bool
== Region
GovCloudEast Bool -> Bool -> Bool
|| Region
r Region -> Region -> Bool
forall a. Eq a => a -> a -> Bool
== Region
GovCloudWest

    region :: ByteString -> Endpoint
region ByteString
h =
      Endpoint :: ByteString -> Bool -> Int -> ByteString -> Endpoint
Endpoint
        { $sel:_endpointHost:Endpoint :: ByteString
_endpointHost = ByteString
h,
          $sel:_endpointSecure:Endpoint :: Bool
_endpointSecure = Bool
True,
          $sel:_endpointPort:Endpoint :: Int
_endpointPort = Int
443,
          $sel:_endpointScope:Endpoint :: ByteString
_endpointScope = ByteString
reg
        }

    global :: ByteString -> Endpoint
global ByteString
h =
      Endpoint :: ByteString -> Bool -> Int -> ByteString -> Endpoint
Endpoint
        { $sel:_endpointHost:Endpoint :: ByteString
_endpointHost = ByteString
h,
          $sel:_endpointSecure:Endpoint :: Bool
_endpointSecure = Bool
True,
          $sel:_endpointPort:Endpoint :: Int
_endpointPort = Int
443,
          $sel:_endpointScope:Endpoint :: ByteString
_endpointScope = ByteString
"us-east-1"
        }

    reg :: ByteString
reg = Region -> ByteString
forall a. ToByteString a => a -> ByteString
toBS Region
r