{-# 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.PlacementStrategy
-- 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.ECS.Types.PlacementStrategy where

import qualified Amazonka.Core as Core
import Amazonka.ECS.Types.PlacementStrategyType
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | The task placement strategy for a task or service. For more information,
-- see
-- <https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-placement-strategies.html Task Placement Strategies>
-- in the /Amazon Elastic Container Service Developer Guide/.
--
-- /See:/ 'newPlacementStrategy' smart constructor.
data PlacementStrategy = PlacementStrategy'
  { -- | The field to apply the placement strategy against. For the @spread@
    -- placement strategy, valid values are @instanceId@ (or @host@, which has
    -- the same effect), or any platform or custom attribute that is applied to
    -- a container instance, such as @attribute:ecs.availability-zone@. For the
    -- @binpack@ placement strategy, valid values are @cpu@ and @memory@. For
    -- the @random@ placement strategy, this field is not used.
    PlacementStrategy -> Maybe Text
field :: Prelude.Maybe Prelude.Text,
    -- | The type of placement strategy. The @random@ placement strategy randomly
    -- places tasks on available candidates. The @spread@ placement strategy
    -- spreads placement across available candidates evenly based on the
    -- @field@ parameter. The @binpack@ strategy places tasks on available
    -- candidates that have the least available amount of the resource that is
    -- specified with the @field@ parameter. For example, if you binpack on
    -- memory, a task is placed on the instance with the least amount of
    -- remaining memory (but still enough to run the task).
    PlacementStrategy -> Maybe PlacementStrategyType
type' :: Prelude.Maybe PlacementStrategyType
  }
  deriving (PlacementStrategy -> PlacementStrategy -> Bool
(PlacementStrategy -> PlacementStrategy -> Bool)
-> (PlacementStrategy -> PlacementStrategy -> Bool)
-> Eq PlacementStrategy
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PlacementStrategy -> PlacementStrategy -> Bool
$c/= :: PlacementStrategy -> PlacementStrategy -> Bool
== :: PlacementStrategy -> PlacementStrategy -> Bool
$c== :: PlacementStrategy -> PlacementStrategy -> Bool
Prelude.Eq, ReadPrec [PlacementStrategy]
ReadPrec PlacementStrategy
Int -> ReadS PlacementStrategy
ReadS [PlacementStrategy]
(Int -> ReadS PlacementStrategy)
-> ReadS [PlacementStrategy]
-> ReadPrec PlacementStrategy
-> ReadPrec [PlacementStrategy]
-> Read PlacementStrategy
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [PlacementStrategy]
$creadListPrec :: ReadPrec [PlacementStrategy]
readPrec :: ReadPrec PlacementStrategy
$creadPrec :: ReadPrec PlacementStrategy
readList :: ReadS [PlacementStrategy]
$creadList :: ReadS [PlacementStrategy]
readsPrec :: Int -> ReadS PlacementStrategy
$creadsPrec :: Int -> ReadS PlacementStrategy
Prelude.Read, Int -> PlacementStrategy -> ShowS
[PlacementStrategy] -> ShowS
PlacementStrategy -> String
(Int -> PlacementStrategy -> ShowS)
-> (PlacementStrategy -> String)
-> ([PlacementStrategy] -> ShowS)
-> Show PlacementStrategy
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PlacementStrategy] -> ShowS
$cshowList :: [PlacementStrategy] -> ShowS
show :: PlacementStrategy -> String
$cshow :: PlacementStrategy -> String
showsPrec :: Int -> PlacementStrategy -> ShowS
$cshowsPrec :: Int -> PlacementStrategy -> ShowS
Prelude.Show, (forall x. PlacementStrategy -> Rep PlacementStrategy x)
-> (forall x. Rep PlacementStrategy x -> PlacementStrategy)
-> Generic PlacementStrategy
forall x. Rep PlacementStrategy x -> PlacementStrategy
forall x. PlacementStrategy -> Rep PlacementStrategy x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep PlacementStrategy x -> PlacementStrategy
$cfrom :: forall x. PlacementStrategy -> Rep PlacementStrategy x
Prelude.Generic)

-- |
-- Create a value of 'PlacementStrategy' 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:
--
-- 'field', 'placementStrategy_field' - The field to apply the placement strategy against. For the @spread@
-- placement strategy, valid values are @instanceId@ (or @host@, which has
-- the same effect), or any platform or custom attribute that is applied to
-- a container instance, such as @attribute:ecs.availability-zone@. For the
-- @binpack@ placement strategy, valid values are @cpu@ and @memory@. For
-- the @random@ placement strategy, this field is not used.
--
-- 'type'', 'placementStrategy_type' - The type of placement strategy. The @random@ placement strategy randomly
-- places tasks on available candidates. The @spread@ placement strategy
-- spreads placement across available candidates evenly based on the
-- @field@ parameter. The @binpack@ strategy places tasks on available
-- candidates that have the least available amount of the resource that is
-- specified with the @field@ parameter. For example, if you binpack on
-- memory, a task is placed on the instance with the least amount of
-- remaining memory (but still enough to run the task).
newPlacementStrategy ::
  PlacementStrategy
newPlacementStrategy :: PlacementStrategy
newPlacementStrategy =
  PlacementStrategy' :: Maybe Text -> Maybe PlacementStrategyType -> PlacementStrategy
PlacementStrategy'
    { $sel:field:PlacementStrategy' :: Maybe Text
field = Maybe Text
forall a. Maybe a
Prelude.Nothing,
      $sel:type':PlacementStrategy' :: Maybe PlacementStrategyType
type' = Maybe PlacementStrategyType
forall a. Maybe a
Prelude.Nothing
    }

-- | The field to apply the placement strategy against. For the @spread@
-- placement strategy, valid values are @instanceId@ (or @host@, which has
-- the same effect), or any platform or custom attribute that is applied to
-- a container instance, such as @attribute:ecs.availability-zone@. For the
-- @binpack@ placement strategy, valid values are @cpu@ and @memory@. For
-- the @random@ placement strategy, this field is not used.
placementStrategy_field :: Lens.Lens' PlacementStrategy (Prelude.Maybe Prelude.Text)
placementStrategy_field :: (Maybe Text -> f (Maybe Text))
-> PlacementStrategy -> f PlacementStrategy
placementStrategy_field = (PlacementStrategy -> Maybe Text)
-> (PlacementStrategy -> Maybe Text -> PlacementStrategy)
-> Lens
     PlacementStrategy PlacementStrategy (Maybe Text) (Maybe Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PlacementStrategy' {Maybe Text
field :: Maybe Text
$sel:field:PlacementStrategy' :: PlacementStrategy -> Maybe Text
field} -> Maybe Text
field) (\s :: PlacementStrategy
s@PlacementStrategy' {} Maybe Text
a -> PlacementStrategy
s {$sel:field:PlacementStrategy' :: Maybe Text
field = Maybe Text
a} :: PlacementStrategy)

-- | The type of placement strategy. The @random@ placement strategy randomly
-- places tasks on available candidates. The @spread@ placement strategy
-- spreads placement across available candidates evenly based on the
-- @field@ parameter. The @binpack@ strategy places tasks on available
-- candidates that have the least available amount of the resource that is
-- specified with the @field@ parameter. For example, if you binpack on
-- memory, a task is placed on the instance with the least amount of
-- remaining memory (but still enough to run the task).
placementStrategy_type :: Lens.Lens' PlacementStrategy (Prelude.Maybe PlacementStrategyType)
placementStrategy_type :: (Maybe PlacementStrategyType -> f (Maybe PlacementStrategyType))
-> PlacementStrategy -> f PlacementStrategy
placementStrategy_type = (PlacementStrategy -> Maybe PlacementStrategyType)
-> (PlacementStrategy
    -> Maybe PlacementStrategyType -> PlacementStrategy)
-> Lens
     PlacementStrategy
     PlacementStrategy
     (Maybe PlacementStrategyType)
     (Maybe PlacementStrategyType)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\PlacementStrategy' {Maybe PlacementStrategyType
type' :: Maybe PlacementStrategyType
$sel:type':PlacementStrategy' :: PlacementStrategy -> Maybe PlacementStrategyType
type'} -> Maybe PlacementStrategyType
type') (\s :: PlacementStrategy
s@PlacementStrategy' {} Maybe PlacementStrategyType
a -> PlacementStrategy
s {$sel:type':PlacementStrategy' :: Maybe PlacementStrategyType
type' = Maybe PlacementStrategyType
a} :: PlacementStrategy)

instance Core.FromJSON PlacementStrategy where
  parseJSON :: Value -> Parser PlacementStrategy
parseJSON =
    String
-> (Object -> Parser PlacementStrategy)
-> Value
-> Parser PlacementStrategy
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"PlacementStrategy"
      ( \Object
x ->
          Maybe Text -> Maybe PlacementStrategyType -> PlacementStrategy
PlacementStrategy'
            (Maybe Text -> Maybe PlacementStrategyType -> PlacementStrategy)
-> Parser (Maybe Text)
-> Parser (Maybe PlacementStrategyType -> PlacementStrategy)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"field") Parser (Maybe PlacementStrategyType -> PlacementStrategy)
-> Parser (Maybe PlacementStrategyType) -> Parser PlacementStrategy
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe PlacementStrategyType)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"type")
      )

instance Prelude.Hashable PlacementStrategy

instance Prelude.NFData PlacementStrategy

instance Core.ToJSON PlacementStrategy where
  toJSON :: PlacementStrategy -> Value
toJSON PlacementStrategy' {Maybe Text
Maybe PlacementStrategyType
type' :: Maybe PlacementStrategyType
field :: Maybe Text
$sel:type':PlacementStrategy' :: PlacementStrategy -> Maybe PlacementStrategyType
$sel:field:PlacementStrategy' :: PlacementStrategy -> Maybe Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"field" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Text -> Pair) -> Maybe Text -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
field,
            (Text
"type" Text -> PlacementStrategyType -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (PlacementStrategyType -> Pair)
-> Maybe PlacementStrategyType -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe PlacementStrategyType
type'
          ]
      )