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

import qualified Amazonka.Core as Core
import qualified Amazonka.Lens as Lens
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 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
(WeightedTarget -> WeightedTarget -> Bool)
-> (WeightedTarget -> WeightedTarget -> Bool) -> Eq WeightedTarget
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]
(Int -> ReadS WeightedTarget)
-> ReadS [WeightedTarget]
-> ReadPrec WeightedTarget
-> ReadPrec [WeightedTarget]
-> Read 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
(Int -> WeightedTarget -> ShowS)
-> (WeightedTarget -> String)
-> ([WeightedTarget] -> ShowS)
-> Show WeightedTarget
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. WeightedTarget -> Rep WeightedTarget x)
-> (forall x. Rep WeightedTarget x -> WeightedTarget)
-> Generic WeightedTarget
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:
--
-- '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' :: Text -> Natural -> WeightedTarget
WeightedTarget'
    { $sel:virtualNode:WeightedTarget' :: Text
virtualNode = Text
pVirtualNode_,
      $sel:weight:WeightedTarget' :: Natural
weight = Natural
pWeight_
    }

-- | The virtual node to associate with the weighted target.
weightedTarget_virtualNode :: Lens.Lens' WeightedTarget Prelude.Text
weightedTarget_virtualNode :: (Text -> f Text) -> WeightedTarget -> f WeightedTarget
weightedTarget_virtualNode = (WeightedTarget -> Text)
-> (WeightedTarget -> Text -> WeightedTarget)
-> Lens WeightedTarget WeightedTarget Text Text
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 :: (Natural -> f Natural) -> WeightedTarget -> f WeightedTarget
weightedTarget_weight = (WeightedTarget -> Natural)
-> (WeightedTarget -> Natural -> WeightedTarget)
-> Lens WeightedTarget WeightedTarget Natural Natural
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 Core.FromJSON WeightedTarget where
  parseJSON :: Value -> Parser WeightedTarget
parseJSON =
    String
-> (Object -> Parser WeightedTarget)
-> Value
-> Parser WeightedTarget
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"WeightedTarget"
      ( \Object
x ->
          Text -> Natural -> WeightedTarget
WeightedTarget'
            (Text -> Natural -> WeightedTarget)
-> Parser Text -> Parser (Natural -> WeightedTarget)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"virtualNode")
            Parser (Natural -> WeightedTarget)
-> Parser Natural -> Parser WeightedTarget
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser Natural
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"weight")
      )

instance Prelude.Hashable WeightedTarget

instance Prelude.NFData WeightedTarget

instance Core.ToJSON WeightedTarget where
  toJSON :: WeightedTarget -> Value
toJSON WeightedTarget' {Natural
Text
weight :: Natural
virtualNode :: Text
$sel:weight:WeightedTarget' :: WeightedTarget -> Natural
$sel:virtualNode:WeightedTarget' :: WeightedTarget -> Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"virtualNode" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
virtualNode),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just (Text
"weight" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Natural
weight)
          ]
      )