{-# 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.DLM.Types.RetainRule
-- 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.DLM.Types.RetainRule where

import qualified Amazonka.Core as Core
import Amazonka.DLM.Types.RetentionIntervalUnitValues
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude

-- | Specifies the retention rule for a lifecycle policy. You can retain
-- snapshots based on either a count or a time interval.
--
-- /See:/ 'newRetainRule' smart constructor.
data RetainRule = RetainRule'
  { -- | The number of snapshots to retain for each volume, up to a maximum of
    -- 1000.
    RetainRule -> Maybe Natural
count :: Prelude.Maybe Prelude.Natural,
    -- | The amount of time to retain each snapshot. The maximum is 100 years.
    -- This is equivalent to 1200 months, 5200 weeks, or 36500 days.
    RetainRule -> Maybe Natural
interval :: Prelude.Maybe Prelude.Natural,
    -- | The unit of time for time-based retention.
    RetainRule -> Maybe RetentionIntervalUnitValues
intervalUnit :: Prelude.Maybe RetentionIntervalUnitValues
  }
  deriving (RetainRule -> RetainRule -> Bool
(RetainRule -> RetainRule -> Bool)
-> (RetainRule -> RetainRule -> Bool) -> Eq RetainRule
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: RetainRule -> RetainRule -> Bool
$c/= :: RetainRule -> RetainRule -> Bool
== :: RetainRule -> RetainRule -> Bool
$c== :: RetainRule -> RetainRule -> Bool
Prelude.Eq, ReadPrec [RetainRule]
ReadPrec RetainRule
Int -> ReadS RetainRule
ReadS [RetainRule]
(Int -> ReadS RetainRule)
-> ReadS [RetainRule]
-> ReadPrec RetainRule
-> ReadPrec [RetainRule]
-> Read RetainRule
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [RetainRule]
$creadListPrec :: ReadPrec [RetainRule]
readPrec :: ReadPrec RetainRule
$creadPrec :: ReadPrec RetainRule
readList :: ReadS [RetainRule]
$creadList :: ReadS [RetainRule]
readsPrec :: Int -> ReadS RetainRule
$creadsPrec :: Int -> ReadS RetainRule
Prelude.Read, Int -> RetainRule -> ShowS
[RetainRule] -> ShowS
RetainRule -> String
(Int -> RetainRule -> ShowS)
-> (RetainRule -> String)
-> ([RetainRule] -> ShowS)
-> Show RetainRule
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [RetainRule] -> ShowS
$cshowList :: [RetainRule] -> ShowS
show :: RetainRule -> String
$cshow :: RetainRule -> String
showsPrec :: Int -> RetainRule -> ShowS
$cshowsPrec :: Int -> RetainRule -> ShowS
Prelude.Show, (forall x. RetainRule -> Rep RetainRule x)
-> (forall x. Rep RetainRule x -> RetainRule) -> Generic RetainRule
forall x. Rep RetainRule x -> RetainRule
forall x. RetainRule -> Rep RetainRule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep RetainRule x -> RetainRule
$cfrom :: forall x. RetainRule -> Rep RetainRule x
Prelude.Generic)

-- |
-- Create a value of 'RetainRule' 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:
--
-- 'count', 'retainRule_count' - The number of snapshots to retain for each volume, up to a maximum of
-- 1000.
--
-- 'interval', 'retainRule_interval' - The amount of time to retain each snapshot. The maximum is 100 years.
-- This is equivalent to 1200 months, 5200 weeks, or 36500 days.
--
-- 'intervalUnit', 'retainRule_intervalUnit' - The unit of time for time-based retention.
newRetainRule ::
  RetainRule
newRetainRule :: RetainRule
newRetainRule =
  RetainRule' :: Maybe Natural
-> Maybe Natural -> Maybe RetentionIntervalUnitValues -> RetainRule
RetainRule'
    { $sel:count:RetainRule' :: Maybe Natural
count = Maybe Natural
forall a. Maybe a
Prelude.Nothing,
      $sel:interval:RetainRule' :: Maybe Natural
interval = Maybe Natural
forall a. Maybe a
Prelude.Nothing,
      $sel:intervalUnit:RetainRule' :: Maybe RetentionIntervalUnitValues
intervalUnit = Maybe RetentionIntervalUnitValues
forall a. Maybe a
Prelude.Nothing
    }

-- | The number of snapshots to retain for each volume, up to a maximum of
-- 1000.
retainRule_count :: Lens.Lens' RetainRule (Prelude.Maybe Prelude.Natural)
retainRule_count :: (Maybe Natural -> f (Maybe Natural)) -> RetainRule -> f RetainRule
retainRule_count = (RetainRule -> Maybe Natural)
-> (RetainRule -> Maybe Natural -> RetainRule)
-> Lens RetainRule RetainRule (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RetainRule' {Maybe Natural
count :: Maybe Natural
$sel:count:RetainRule' :: RetainRule -> Maybe Natural
count} -> Maybe Natural
count) (\s :: RetainRule
s@RetainRule' {} Maybe Natural
a -> RetainRule
s {$sel:count:RetainRule' :: Maybe Natural
count = Maybe Natural
a} :: RetainRule)

-- | The amount of time to retain each snapshot. The maximum is 100 years.
-- This is equivalent to 1200 months, 5200 weeks, or 36500 days.
retainRule_interval :: Lens.Lens' RetainRule (Prelude.Maybe Prelude.Natural)
retainRule_interval :: (Maybe Natural -> f (Maybe Natural)) -> RetainRule -> f RetainRule
retainRule_interval = (RetainRule -> Maybe Natural)
-> (RetainRule -> Maybe Natural -> RetainRule)
-> Lens RetainRule RetainRule (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RetainRule' {Maybe Natural
interval :: Maybe Natural
$sel:interval:RetainRule' :: RetainRule -> Maybe Natural
interval} -> Maybe Natural
interval) (\s :: RetainRule
s@RetainRule' {} Maybe Natural
a -> RetainRule
s {$sel:interval:RetainRule' :: Maybe Natural
interval = Maybe Natural
a} :: RetainRule)

-- | The unit of time for time-based retention.
retainRule_intervalUnit :: Lens.Lens' RetainRule (Prelude.Maybe RetentionIntervalUnitValues)
retainRule_intervalUnit :: (Maybe RetentionIntervalUnitValues
 -> f (Maybe RetentionIntervalUnitValues))
-> RetainRule -> f RetainRule
retainRule_intervalUnit = (RetainRule -> Maybe RetentionIntervalUnitValues)
-> (RetainRule -> Maybe RetentionIntervalUnitValues -> RetainRule)
-> Lens
     RetainRule
     RetainRule
     (Maybe RetentionIntervalUnitValues)
     (Maybe RetentionIntervalUnitValues)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\RetainRule' {Maybe RetentionIntervalUnitValues
intervalUnit :: Maybe RetentionIntervalUnitValues
$sel:intervalUnit:RetainRule' :: RetainRule -> Maybe RetentionIntervalUnitValues
intervalUnit} -> Maybe RetentionIntervalUnitValues
intervalUnit) (\s :: RetainRule
s@RetainRule' {} Maybe RetentionIntervalUnitValues
a -> RetainRule
s {$sel:intervalUnit:RetainRule' :: Maybe RetentionIntervalUnitValues
intervalUnit = Maybe RetentionIntervalUnitValues
a} :: RetainRule)

instance Core.FromJSON RetainRule where
  parseJSON :: Value -> Parser RetainRule
parseJSON =
    String
-> (Object -> Parser RetainRule) -> Value -> Parser RetainRule
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"RetainRule"
      ( \Object
x ->
          Maybe Natural
-> Maybe Natural -> Maybe RetentionIntervalUnitValues -> RetainRule
RetainRule'
            (Maybe Natural
 -> Maybe Natural
 -> Maybe RetentionIntervalUnitValues
 -> RetainRule)
-> Parser (Maybe Natural)
-> Parser
     (Maybe Natural -> Maybe RetentionIntervalUnitValues -> RetainRule)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Parser (Maybe Natural)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"Count")
            Parser
  (Maybe Natural -> Maybe RetentionIntervalUnitValues -> RetainRule)
-> Parser (Maybe Natural)
-> Parser (Maybe RetentionIntervalUnitValues -> RetainRule)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe Natural)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"Interval")
            Parser (Maybe RetentionIntervalUnitValues -> RetainRule)
-> Parser (Maybe RetentionIntervalUnitValues) -> Parser RetainRule
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (Maybe RetentionIntervalUnitValues)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
Core..:? Text
"IntervalUnit")
      )

instance Prelude.Hashable RetainRule

instance Prelude.NFData RetainRule

instance Core.ToJSON RetainRule where
  toJSON :: RetainRule -> Value
toJSON RetainRule' {Maybe Natural
Maybe RetentionIntervalUnitValues
intervalUnit :: Maybe RetentionIntervalUnitValues
interval :: Maybe Natural
count :: Maybe Natural
$sel:intervalUnit:RetainRule' :: RetainRule -> Maybe RetentionIntervalUnitValues
$sel:interval:RetainRule' :: RetainRule -> Maybe Natural
$sel:count:RetainRule' :: RetainRule -> Maybe Natural
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"Count" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Natural -> Pair) -> Maybe Natural -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
count,
            (Text
"Interval" Text -> Natural -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (Natural -> Pair) -> Maybe Natural -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Natural
interval,
            (Text
"IntervalUnit" Text -> RetentionIntervalUnitValues -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..=) (RetentionIntervalUnitValues -> Pair)
-> Maybe RetentionIntervalUnitValues -> Maybe Pair
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe RetentionIntervalUnitValues
intervalUnit
          ]
      )