{-# 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.AppMesh.Types.WeightedTarget
-- 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.AppMesh.Types.WeightedTarget 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

-- | An object that represents a target and its relative weight. Traffic is
-- distributed across targets according to their relative weight. For
-- example, a weighted target with a relative weight of 50 receives five
-- times as much traffic as one with a relative weight of 10. The total
-- weight for all targets combined must be less than or equal to 100.
--
-- /See:/ 'newWeightedTarget' smart constructor.
data WeightedTarget = WeightedTarget'
  { -- | The targeted port of the weighted object.
    WeightedTarget -> Maybe Natural
port :: Prelude.Maybe Prelude.Natural,
    -- | The virtual node to associate with the weighted target.
    WeightedTarget -> Text
virtualNode :: Prelude.Text,
    -- | The relative weight of the weighted target.
    WeightedTarget -> Natural
weight :: Prelude.Natural
  }
  deriving (WeightedTarget -> WeightedTarget -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: WeightedTarget -> WeightedTarget -> Bool
$c/= :: WeightedTarget -> WeightedTarget -> Bool
== :: WeightedTarget -> WeightedTarget -> Bool
$c== :: WeightedTarget -> WeightedTarget -> Bool
Prelude.Eq, ReadPrec [WeightedTarget]
ReadPrec WeightedTarget
Int -> ReadS WeightedTarget
ReadS [WeightedTarget]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [WeightedTarget]
$creadListPrec :: ReadPrec [WeightedTarget]
readPrec :: ReadPrec WeightedTarget
$creadPrec :: ReadPrec WeightedTarget
readList :: ReadS [WeightedTarget]
$creadList :: ReadS [WeightedTarget]
readsPrec :: Int -> ReadS WeightedTarget
$creadsPrec :: Int -> ReadS WeightedTarget
Prelude.Read, Int -> WeightedTarget -> ShowS
[WeightedTarget] -> ShowS
WeightedTarget -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [WeightedTarget] -> ShowS
$cshowList :: [WeightedTarget] -> ShowS
show :: WeightedTarget -> String
$cshow :: WeightedTarget -> String
showsPrec :: Int -> WeightedTarget -> ShowS
$cshowsPrec :: Int -> WeightedTarget -> ShowS
Prelude.Show, forall x. Rep WeightedTarget x -> WeightedTarget
forall x. WeightedTarget -> Rep WeightedTarget x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep WeightedTarget x -> WeightedTarget
$cfrom :: forall x. WeightedTarget -> Rep WeightedTarget x
Prelude.Generic)

-- |
-- Create a value of 'WeightedTarget' 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:
--
-- 'port', 'weightedTarget_port' - The targeted port of the weighted object.
--
-- 'virtualNode', 'weightedTarget_virtualNode' - The virtual node to associate with the weighted target.
--
-- 'weight', 'weightedTarget_weight' - The relative weight of the weighted target.
newWeightedTarget ::
  -- | 'virtualNode'
  Prelude.Text ->
  -- | 'weight'
  Prelude.Natural ->
  WeightedTarget
newWeightedTarget :: Text -> Natural -> WeightedTarget
newWeightedTarget Text
pVirtualNode_ Natural
pWeight_ =
  WeightedTarget'
    { $sel:port:WeightedTarget' :: Maybe Natural
port = forall a. Maybe a
Prelude.Nothing,
      $sel:virtualNode:WeightedTarget' :: Text
virtualNode = Text
pVirtualNode_,
      $sel:weight:WeightedTarget' :: Natural
weight = Natural
pWeight_
    }

-- | The targeted port of the weighted object.
weightedTarget_port :: Lens.Lens' WeightedTarget (Prelude.Maybe Prelude.Natural)
weightedTarget_port :: Lens' WeightedTarget (Maybe Natural)
weightedTarget_port = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\WeightedTarget' {Maybe Natural
port :: Maybe Natural
$sel:port:WeightedTarget' :: WeightedTarget -> Maybe Natural
port} -> Maybe Natural
port) (\s :: WeightedTarget
s@WeightedTarget' {} Maybe Natural
a -> WeightedTarget
s {$sel:port:WeightedTarget' :: Maybe Natural
port = Maybe Natural
a} :: WeightedTarget)

-- | The virtual node to associate with the weighted target.
weightedTarget_virtualNode :: Lens.Lens' WeightedTarget Prelude.Text
weightedTarget_virtualNode :: Lens' WeightedTarget Text
weightedTarget_virtualNode = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\WeightedTarget' {Text
virtualNode :: Text
$sel:virtualNode:WeightedTarget' :: WeightedTarget -> Text
virtualNode} -> Text
virtualNode) (\s :: WeightedTarget
s@WeightedTarget' {} Text
a -> WeightedTarget
s {$sel:virtualNode:WeightedTarget' :: Text
virtualNode = Text
a} :: WeightedTarget)

-- | The relative weight of the weighted target.
weightedTarget_weight :: Lens.Lens' WeightedTarget Prelude.Natural
weightedTarget_weight :: Lens' WeightedTarget Natural
weightedTarget_weight = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\WeightedTarget' {Natural
weight :: Natural
$sel:weight:WeightedTarget' :: WeightedTarget -> Natural
weight} -> Natural
weight) (\s :: WeightedTarget
s@WeightedTarget' {} Natural
a -> WeightedTarget
s {$sel:weight:WeightedTarget' :: Natural
weight = Natural
a} :: WeightedTarget)

instance Data.FromJSON WeightedTarget where
  parseJSON :: Value -> Parser WeightedTarget
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"WeightedTarget"
      ( \Object
x ->
          Maybe Natural -> Text -> Natural -> WeightedTarget
WeightedTarget'
            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
"port")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"virtualNode")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"weight")
      )

instance Prelude.Hashable WeightedTarget where
  hashWithSalt :: Int -> WeightedTarget -> Int
hashWithSalt Int
_salt WeightedTarget' {Natural
Maybe Natural
Text
weight :: Natural
virtualNode :: Text
port :: Maybe Natural
$sel:weight:WeightedTarget' :: WeightedTarget -> Natural
$sel:virtualNode:WeightedTarget' :: WeightedTarget -> Text
$sel:port:WeightedTarget' :: WeightedTarget -> Maybe Natural
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
port
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
virtualNode
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Natural
weight

instance Prelude.NFData WeightedTarget where
  rnf :: WeightedTarget -> ()
rnf WeightedTarget' {Natural
Maybe Natural
Text
weight :: Natural
virtualNode :: Text
port :: Maybe Natural
$sel:weight:WeightedTarget' :: WeightedTarget -> Natural
$sel:virtualNode:WeightedTarget' :: WeightedTarget -> Text
$sel:port:WeightedTarget' :: WeightedTarget -> Maybe Natural
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
port
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
virtualNode
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Natural
weight

instance Data.ToJSON WeightedTarget where
  toJSON :: WeightedTarget -> Value
toJSON WeightedTarget' {Natural
Maybe Natural
Text
weight :: Natural
virtualNode :: Text
port :: Maybe Natural
$sel:weight:WeightedTarget' :: WeightedTarget -> Natural
$sel:virtualNode:WeightedTarget' :: WeightedTarget -> Text
$sel:port:WeightedTarget' :: WeightedTarget -> Maybe Natural
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"port" 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 Natural
port,
            forall a. a -> Maybe a
Prelude.Just (Key
"virtualNode" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
virtualNode),
            forall a. a -> Maybe a
Prelude.Just (Key
"weight" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Natural
weight)
          ]
      )