{-# 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.ECS.Types.LoadBalancer
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.ECS.Types.LoadBalancer where

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

-- | The load balancer configuration to use with a service or task set.
--
-- For specific notes and restrictions regarding the use of load balancers
-- with services and task sets, see the CreateService and CreateTaskSet
-- actions.
--
-- When you add, update, or remove a load balancer configuration, Amazon
-- ECS starts a new deployment with the updated Elastic Load Balancing
-- configuration. This causes tasks to register to and deregister from load
-- balancers.
--
-- We recommend that you verify this on a test environment before you
-- update the Elastic Load Balancing configuration.
--
-- A service-linked role is required for services that use multiple target
-- groups. For more information, see
-- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using-service-linked-roles.html Using service-linked roles>
-- in the /Amazon Elastic Container Service Developer Guide/.
--
-- /See:/ 'newLoadBalancer' smart constructor.
data LoadBalancer = LoadBalancer'
  { -- | The name of the container (as it appears in a container definition) to
    -- associate with the load balancer.
    LoadBalancer -> Maybe Text
containerName :: Prelude.Maybe Prelude.Text,
    -- | The port on the container to associate with the load balancer. This port
    -- must correspond to a @containerPort@ in the task definition the tasks in
    -- the service are using. For tasks that use the EC2 launch type, the
    -- container instance they\'re launched on must allow ingress traffic on
    -- the @hostPort@ of the port mapping.
    LoadBalancer -> Maybe Int
containerPort :: Prelude.Maybe Prelude.Int,
    -- | The name of the load balancer to associate with the Amazon ECS service
    -- or task set.
    --
    -- A load balancer name is only specified when using a Classic Load
    -- Balancer. If you are using an Application Load Balancer or a Network
    -- Load Balancer the load balancer name parameter should be omitted.
    LoadBalancer -> Maybe Text
loadBalancerName :: Prelude.Maybe Prelude.Text,
    -- | The full Amazon Resource Name (ARN) of the Elastic Load Balancing target
    -- group or groups associated with a service or task set.
    --
    -- A target group ARN is only specified when using an Application Load
    -- Balancer or Network Load Balancer. If you\'re using a Classic Load
    -- Balancer, omit the target group ARN.
    --
    -- For services using the @ECS@ deployment controller, you can specify one
    -- or multiple target groups. For more information, see
    -- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/register-multiple-targetgroups.html Registering multiple target groups with a service>
    -- in the /Amazon Elastic Container Service Developer Guide/.
    --
    -- For services using the @CODE_DEPLOY@ deployment controller, you\'re
    -- required to define two target groups for the load balancer. For more
    -- information, see
    -- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/deployment-type-bluegreen.html Blue\/green deployment with CodeDeploy>
    -- in the /Amazon Elastic Container Service Developer Guide/.
    --
    -- If your service\'s task definition uses the @awsvpc@ network mode, you
    -- must choose @ip@ as the target type, not @instance@. Do this when
    -- creating your target groups because tasks that use the @awsvpc@ network
    -- mode are associated with an elastic network interface, not an Amazon EC2
    -- instance. This network mode is required for the Fargate launch type.
    LoadBalancer -> Maybe Text
targetGroupArn :: Prelude.Maybe Prelude.Text
  }
  deriving (LoadBalancer -> LoadBalancer -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LoadBalancer -> LoadBalancer -> Bool
$c/= :: LoadBalancer -> LoadBalancer -> Bool
== :: LoadBalancer -> LoadBalancer -> Bool
$c== :: LoadBalancer -> LoadBalancer -> Bool
Prelude.Eq, ReadPrec [LoadBalancer]
ReadPrec LoadBalancer
Int -> ReadS LoadBalancer
ReadS [LoadBalancer]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [LoadBalancer]
$creadListPrec :: ReadPrec [LoadBalancer]
readPrec :: ReadPrec LoadBalancer
$creadPrec :: ReadPrec LoadBalancer
readList :: ReadS [LoadBalancer]
$creadList :: ReadS [LoadBalancer]
readsPrec :: Int -> ReadS LoadBalancer
$creadsPrec :: Int -> ReadS LoadBalancer
Prelude.Read, Int -> LoadBalancer -> ShowS
[LoadBalancer] -> ShowS
LoadBalancer -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LoadBalancer] -> ShowS
$cshowList :: [LoadBalancer] -> ShowS
show :: LoadBalancer -> String
$cshow :: LoadBalancer -> String
showsPrec :: Int -> LoadBalancer -> ShowS
$cshowsPrec :: Int -> LoadBalancer -> ShowS
Prelude.Show, forall x. Rep LoadBalancer x -> LoadBalancer
forall x. LoadBalancer -> Rep LoadBalancer x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep LoadBalancer x -> LoadBalancer
$cfrom :: forall x. LoadBalancer -> Rep LoadBalancer x
Prelude.Generic)

-- |
-- Create a value of 'LoadBalancer' 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', 'loadBalancer_containerName' - The name of the container (as it appears in a container definition) to
-- associate with the load balancer.
--
-- 'containerPort', 'loadBalancer_containerPort' - The port on the container to associate with the load balancer. This port
-- must correspond to a @containerPort@ in the task definition the tasks in
-- the service are using. For tasks that use the EC2 launch type, the
-- container instance they\'re launched on must allow ingress traffic on
-- the @hostPort@ of the port mapping.
--
-- 'loadBalancerName', 'loadBalancer_loadBalancerName' - The name of the load balancer to associate with the Amazon ECS service
-- or task set.
--
-- A load balancer name is only specified when using a Classic Load
-- Balancer. If you are using an Application Load Balancer or a Network
-- Load Balancer the load balancer name parameter should be omitted.
--
-- 'targetGroupArn', 'loadBalancer_targetGroupArn' - The full Amazon Resource Name (ARN) of the Elastic Load Balancing target
-- group or groups associated with a service or task set.
--
-- A target group ARN is only specified when using an Application Load
-- Balancer or Network Load Balancer. If you\'re using a Classic Load
-- Balancer, omit the target group ARN.
--
-- For services using the @ECS@ deployment controller, you can specify one
-- or multiple target groups. For more information, see
-- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/register-multiple-targetgroups.html Registering multiple target groups with a service>
-- in the /Amazon Elastic Container Service Developer Guide/.
--
-- For services using the @CODE_DEPLOY@ deployment controller, you\'re
-- required to define two target groups for the load balancer. For more
-- information, see
-- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/deployment-type-bluegreen.html Blue\/green deployment with CodeDeploy>
-- in the /Amazon Elastic Container Service Developer Guide/.
--
-- If your service\'s task definition uses the @awsvpc@ network mode, you
-- must choose @ip@ as the target type, not @instance@. Do this when
-- creating your target groups because tasks that use the @awsvpc@ network
-- mode are associated with an elastic network interface, not an Amazon EC2
-- instance. This network mode is required for the Fargate launch type.
newLoadBalancer ::
  LoadBalancer
newLoadBalancer :: LoadBalancer
newLoadBalancer =
  LoadBalancer'
    { $sel:containerName:LoadBalancer' :: Maybe Text
containerName = forall a. Maybe a
Prelude.Nothing,
      $sel:containerPort:LoadBalancer' :: Maybe Int
containerPort = forall a. Maybe a
Prelude.Nothing,
      $sel:loadBalancerName:LoadBalancer' :: Maybe Text
loadBalancerName = forall a. Maybe a
Prelude.Nothing,
      $sel:targetGroupArn:LoadBalancer' :: Maybe Text
targetGroupArn = forall a. Maybe a
Prelude.Nothing
    }

-- | The name of the container (as it appears in a container definition) to
-- associate with the load balancer.
loadBalancer_containerName :: Lens.Lens' LoadBalancer (Prelude.Maybe Prelude.Text)
loadBalancer_containerName :: Lens' LoadBalancer (Maybe Text)
loadBalancer_containerName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\LoadBalancer' {Maybe Text
containerName :: Maybe Text
$sel:containerName:LoadBalancer' :: LoadBalancer -> Maybe Text
containerName} -> Maybe Text
containerName) (\s :: LoadBalancer
s@LoadBalancer' {} Maybe Text
a -> LoadBalancer
s {$sel:containerName:LoadBalancer' :: Maybe Text
containerName = Maybe Text
a} :: LoadBalancer)

-- | The port on the container to associate with the load balancer. This port
-- must correspond to a @containerPort@ in the task definition the tasks in
-- the service are using. For tasks that use the EC2 launch type, the
-- container instance they\'re launched on must allow ingress traffic on
-- the @hostPort@ of the port mapping.
loadBalancer_containerPort :: Lens.Lens' LoadBalancer (Prelude.Maybe Prelude.Int)
loadBalancer_containerPort :: Lens' LoadBalancer (Maybe Int)
loadBalancer_containerPort = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\LoadBalancer' {Maybe Int
containerPort :: Maybe Int
$sel:containerPort:LoadBalancer' :: LoadBalancer -> Maybe Int
containerPort} -> Maybe Int
containerPort) (\s :: LoadBalancer
s@LoadBalancer' {} Maybe Int
a -> LoadBalancer
s {$sel:containerPort:LoadBalancer' :: Maybe Int
containerPort = Maybe Int
a} :: LoadBalancer)

-- | The name of the load balancer to associate with the Amazon ECS service
-- or task set.
--
-- A load balancer name is only specified when using a Classic Load
-- Balancer. If you are using an Application Load Balancer or a Network
-- Load Balancer the load balancer name parameter should be omitted.
loadBalancer_loadBalancerName :: Lens.Lens' LoadBalancer (Prelude.Maybe Prelude.Text)
loadBalancer_loadBalancerName :: Lens' LoadBalancer (Maybe Text)
loadBalancer_loadBalancerName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\LoadBalancer' {Maybe Text
loadBalancerName :: Maybe Text
$sel:loadBalancerName:LoadBalancer' :: LoadBalancer -> Maybe Text
loadBalancerName} -> Maybe Text
loadBalancerName) (\s :: LoadBalancer
s@LoadBalancer' {} Maybe Text
a -> LoadBalancer
s {$sel:loadBalancerName:LoadBalancer' :: Maybe Text
loadBalancerName = Maybe Text
a} :: LoadBalancer)

-- | The full Amazon Resource Name (ARN) of the Elastic Load Balancing target
-- group or groups associated with a service or task set.
--
-- A target group ARN is only specified when using an Application Load
-- Balancer or Network Load Balancer. If you\'re using a Classic Load
-- Balancer, omit the target group ARN.
--
-- For services using the @ECS@ deployment controller, you can specify one
-- or multiple target groups. For more information, see
-- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/register-multiple-targetgroups.html Registering multiple target groups with a service>
-- in the /Amazon Elastic Container Service Developer Guide/.
--
-- For services using the @CODE_DEPLOY@ deployment controller, you\'re
-- required to define two target groups for the load balancer. For more
-- information, see
-- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/deployment-type-bluegreen.html Blue\/green deployment with CodeDeploy>
-- in the /Amazon Elastic Container Service Developer Guide/.
--
-- If your service\'s task definition uses the @awsvpc@ network mode, you
-- must choose @ip@ as the target type, not @instance@. Do this when
-- creating your target groups because tasks that use the @awsvpc@ network
-- mode are associated with an elastic network interface, not an Amazon EC2
-- instance. This network mode is required for the Fargate launch type.
loadBalancer_targetGroupArn :: Lens.Lens' LoadBalancer (Prelude.Maybe Prelude.Text)
loadBalancer_targetGroupArn :: Lens' LoadBalancer (Maybe Text)
loadBalancer_targetGroupArn = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\LoadBalancer' {Maybe Text
targetGroupArn :: Maybe Text
$sel:targetGroupArn:LoadBalancer' :: LoadBalancer -> Maybe Text
targetGroupArn} -> Maybe Text
targetGroupArn) (\s :: LoadBalancer
s@LoadBalancer' {} Maybe Text
a -> LoadBalancer
s {$sel:targetGroupArn:LoadBalancer' :: Maybe Text
targetGroupArn = Maybe Text
a} :: LoadBalancer)

instance Data.FromJSON LoadBalancer where
  parseJSON :: Value -> Parser LoadBalancer
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"LoadBalancer"
      ( \Object
x ->
          Maybe Text -> Maybe Int -> Maybe Text -> Maybe Text -> LoadBalancer
LoadBalancer'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"containerName")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"containerPort")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"loadBalancerName")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"targetGroupArn")
      )

instance Prelude.Hashable LoadBalancer where
  hashWithSalt :: Int -> LoadBalancer -> Int
hashWithSalt Int
_salt LoadBalancer' {Maybe Int
Maybe Text
targetGroupArn :: Maybe Text
loadBalancerName :: Maybe Text
containerPort :: Maybe Int
containerName :: Maybe Text
$sel:targetGroupArn:LoadBalancer' :: LoadBalancer -> Maybe Text
$sel:loadBalancerName:LoadBalancer' :: LoadBalancer -> Maybe Text
$sel:containerPort:LoadBalancer' :: LoadBalancer -> Maybe Int
$sel:containerName:LoadBalancer' :: LoadBalancer -> Maybe Text
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
containerName
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Int
containerPort
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
loadBalancerName
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
targetGroupArn

instance Prelude.NFData LoadBalancer where
  rnf :: LoadBalancer -> ()
rnf LoadBalancer' {Maybe Int
Maybe Text
targetGroupArn :: Maybe Text
loadBalancerName :: Maybe Text
containerPort :: Maybe Int
containerName :: Maybe Text
$sel:targetGroupArn:LoadBalancer' :: LoadBalancer -> Maybe Text
$sel:loadBalancerName:LoadBalancer' :: LoadBalancer -> Maybe Text
$sel:containerPort:LoadBalancer' :: LoadBalancer -> Maybe Int
$sel:containerName:LoadBalancer' :: LoadBalancer -> Maybe Text
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
containerName
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Int
containerPort
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
loadBalancerName
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
targetGroupArn

instance Data.ToJSON LoadBalancer where
  toJSON :: LoadBalancer -> Value
toJSON LoadBalancer' {Maybe Int
Maybe Text
targetGroupArn :: Maybe Text
loadBalancerName :: Maybe Text
containerPort :: Maybe Int
containerName :: Maybe Text
$sel:targetGroupArn:LoadBalancer' :: LoadBalancer -> Maybe Text
$sel:loadBalancerName:LoadBalancer' :: LoadBalancer -> Maybe Text
$sel:containerPort:LoadBalancer' :: LoadBalancer -> Maybe Int
$sel:containerName:LoadBalancer' :: LoadBalancer -> Maybe Text
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"containerName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
containerName,
            (Key
"containerPort" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Int
containerPort,
            (Key
"loadBalancerName" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
loadBalancerName,
            (Key
"targetGroupArn" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
targetGroupArn
          ]
      )