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

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

-- | __[Snapshot and AMI policies only]__ Specifies a retention rule for
-- snapshots created by snapshot policies, or for AMIs created by AMI
-- policies.
--
-- For snapshot policies that have an ArchiveRule, this retention rule
-- applies to standard tier retention. When the retention threshold is met,
-- snapshots are moved from the standard to the archive tier.
--
-- For snapshot policies that do not have an __ArchiveRule__, snapshots are
-- permanently deleted when this retention threshold is met.
--
-- You can retain snapshots based on either a count or a time interval.
--
-- -   __Count-based retention__
--
--     You must specify __Count__. If you specify an ArchiveRule for the
--     schedule, then you can specify a retention count of @0@ to archive
--     snapshots immediately after creation. If you specify a
--     FastRestoreRule, ShareRule, or a CrossRegionCopyRule, then you must
--     specify a retention count of @1@ or more.
--
-- -   __Age-based retention__
--
--     You must specify __Interval__ and __IntervalUnit__. If you specify
--     an ArchiveRule for the schedule, then you can specify a retention
--     interval of @0@ days to archive snapshots immediately after
--     creation. If you specify a FastRestoreRule, ShareRule, or a
--     CrossRegionCopyRule, then you must specify a retention interval of
--     @1@ day or more.
--
-- /See:/ 'newRetainRule' smart constructor.
data RetainRule = RetainRule'
  { -- | The number of snapshots to retain for each volume, up to a maximum of
    -- 1000. For example if you want to retain a maximum of three snapshots,
    -- specify @3@. When the fourth snapshot is created, the oldest retained
    -- snapshot is deleted, or it is moved to the archive tier if you have
    -- specified an ArchiveRule.
    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. For example, to retain
    -- snapshots for 3 months, specify @Interval=3@ and @IntervalUnit=MONTHS@.
    -- Once the snapshot has been retained for 3 months, it is deleted, or it
    -- is moved to the archive tier if you have specified an ArchiveRule.
    RetainRule -> Maybe RetentionIntervalUnitValues
intervalUnit :: Prelude.Maybe RetentionIntervalUnitValues
  }
  deriving (RetainRule -> RetainRule -> Bool
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]
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
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. 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. For example if you want to retain a maximum of three snapshots,
-- specify @3@. When the fourth snapshot is created, the oldest retained
-- snapshot is deleted, or it is moved to the archive tier if you have
-- specified an ArchiveRule.
--
-- '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. For example, to retain
-- snapshots for 3 months, specify @Interval=3@ and @IntervalUnit=MONTHS@.
-- Once the snapshot has been retained for 3 months, it is deleted, or it
-- is moved to the archive tier if you have specified an ArchiveRule.
newRetainRule ::
  RetainRule
newRetainRule :: RetainRule
newRetainRule =
  RetainRule'
    { $sel:count:RetainRule' :: Maybe Natural
count = forall a. Maybe a
Prelude.Nothing,
      $sel:interval:RetainRule' :: Maybe Natural
interval = forall a. Maybe a
Prelude.Nothing,
      $sel:intervalUnit:RetainRule' :: Maybe RetentionIntervalUnitValues
intervalUnit = forall a. Maybe a
Prelude.Nothing
    }

-- | The number of snapshots to retain for each volume, up to a maximum of
-- 1000. For example if you want to retain a maximum of three snapshots,
-- specify @3@. When the fourth snapshot is created, the oldest retained
-- snapshot is deleted, or it is moved to the archive tier if you have
-- specified an ArchiveRule.
retainRule_count :: Lens.Lens' RetainRule (Prelude.Maybe Prelude.Natural)
retainRule_count :: Lens' RetainRule (Maybe Natural)
retainRule_count = 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 :: Lens' RetainRule (Maybe Natural)
retainRule_interval = 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. For example, to retain
-- snapshots for 3 months, specify @Interval=3@ and @IntervalUnit=MONTHS@.
-- Once the snapshot has been retained for 3 months, it is deleted, or it
-- is moved to the archive tier if you have specified an ArchiveRule.
retainRule_intervalUnit :: Lens.Lens' RetainRule (Prelude.Maybe RetentionIntervalUnitValues)
retainRule_intervalUnit :: Lens' RetainRule (Maybe RetentionIntervalUnitValues)
retainRule_intervalUnit = 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 Data.FromJSON RetainRule where
  parseJSON :: Value -> Parser RetainRule
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"RetainRule"
      ( \Object
x ->
          Maybe Natural
-> Maybe Natural -> Maybe RetentionIntervalUnitValues -> RetainRule
RetainRule'
            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
"Count")
            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
"Interval")
            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
"IntervalUnit")
      )

instance Prelude.Hashable RetainRule where
  hashWithSalt :: Int -> RetainRule -> Int
hashWithSalt Int
_salt 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
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
count
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
interval
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe RetentionIntervalUnitValues
intervalUnit

instance Prelude.NFData RetainRule where
  rnf :: RetainRule -> ()
rnf 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
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
count
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
interval
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe RetentionIntervalUnitValues
intervalUnit

instance Data.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
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"Count" 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
count,
            (Key
"Interval" 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
interval,
            (Key
"IntervalUnit" 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 RetentionIntervalUnitValues
intervalUnit
          ]
      )