-- |
-- Module      : Amazonka.Endpoint
-- Copyright   : (c) 2013-2023 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.Prelude
import Amazonka.Types
import qualified Data.CaseInsensitive as CI

-- | A convenience function for overriding the 'Service' 'Endpoint'.
--
-- /See:/ 'endpoint'.
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
secure ByteString
host Int
port s :: Service
s@Service {Region -> Endpoint
$sel:endpoint:Service :: Service -> Region -> Endpoint
endpoint :: Region -> Endpoint
endpoint} =
  Service
s {$sel:endpoint:Service :: Region -> Endpoint
endpoint = \Region
r -> (Region -> Endpoint
endpoint Region
r) {Bool
$sel:secure:Endpoint :: Bool
secure :: Bool
secure, ByteString
$sel:host:Endpoint :: ByteString
host :: ByteString
host, Int
$sel:port:Endpoint :: Int
port :: Int
port}}

-- | Determine the full host address and credential scope
-- within the specified 'Region'.
defaultEndpoint :: Service -> Region -> Endpoint
defaultEndpoint :: Service -> Region -> Endpoint
defaultEndpoint Service {$sel:endpointPrefix:Service :: Service -> ByteString
endpointPrefix = ByteString
p} Region
r = CI ByteString -> Endpoint
go (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." forall a. Semigroup a => a -> a -> a
<> ByteString
reg 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 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 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 forall a. Semigroup a => a -> a -> a
<> ByteString
"." forall a. Semigroup a => a -> a -> a
<> ByteString
reg forall a. Semigroup a => a -> a -> a
<> ByteString
".amazonaws.com.cn")
        | Bool
otherwise -> ByteString -> Endpoint
region (ByteString
p forall a. Semigroup a => a -> a -> a
<> ByteString
"." forall a. Semigroup a => a -> a -> a
<> ByteString
reg forall a. Semigroup a => a -> a -> a
<> ByteString
".amazonaws.com")

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

    region :: ByteString -> Endpoint
region ByteString
host =
      Endpoint
        { ByteString
host :: ByteString
$sel:host:Endpoint :: ByteString
host,
          $sel:basePath:Endpoint :: RawPath
basePath = forall a. Monoid a => a
mempty,
          $sel:secure:Endpoint :: Bool
secure = Bool
True,
          $sel:port:Endpoint :: Int
port = Int
443,
          $sel:scope:Endpoint :: ByteString
scope = ByteString
reg
        }

    global :: ByteString -> Endpoint
global ByteString
host =
      Endpoint
        { ByteString
host :: ByteString
$sel:host:Endpoint :: ByteString
host,
          $sel:basePath:Endpoint :: RawPath
basePath = forall a. Monoid a => a
mempty,
          $sel:secure:Endpoint :: Bool
secure = Bool
True,
          $sel:port:Endpoint :: Int
port = Int
443,
          $sel:scope:Endpoint :: ByteString
scope = ByteString
"us-east-1"
        }

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