{-# 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.ShareRule
-- 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.ShareRule 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 a rule for sharing snapshots across Amazon Web Services
-- accounts.
--
-- /See:/ 'newShareRule' smart constructor.
data ShareRule = ShareRule'
  { -- | The unit of time for the automatic unsharing interval.
    ShareRule -> Maybe RetentionIntervalUnitValues
unshareIntervalUnit :: Prelude.Maybe RetentionIntervalUnitValues,
    -- | The period after which snapshots that are shared with other Amazon Web
    -- Services accounts are automatically unshared.
    ShareRule -> Maybe Natural
unshareInterval :: Prelude.Maybe Prelude.Natural,
    -- | The IDs of the Amazon Web Services accounts with which to share the
    -- snapshots.
    ShareRule -> NonEmpty Text
targetAccounts :: Prelude.NonEmpty Prelude.Text
  }
  deriving (ShareRule -> ShareRule -> Bool
(ShareRule -> ShareRule -> Bool)
-> (ShareRule -> ShareRule -> Bool) -> Eq ShareRule
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ShareRule -> ShareRule -> Bool
$c/= :: ShareRule -> ShareRule -> Bool
== :: ShareRule -> ShareRule -> Bool
$c== :: ShareRule -> ShareRule -> Bool
Prelude.Eq, ReadPrec [ShareRule]
ReadPrec ShareRule
Int -> ReadS ShareRule
ReadS [ShareRule]
(Int -> ReadS ShareRule)
-> ReadS [ShareRule]
-> ReadPrec ShareRule
-> ReadPrec [ShareRule]
-> Read ShareRule
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ShareRule]
$creadListPrec :: ReadPrec [ShareRule]
readPrec :: ReadPrec ShareRule
$creadPrec :: ReadPrec ShareRule
readList :: ReadS [ShareRule]
$creadList :: ReadS [ShareRule]
readsPrec :: Int -> ReadS ShareRule
$creadsPrec :: Int -> ReadS ShareRule
Prelude.Read, Int -> ShareRule -> ShowS
[ShareRule] -> ShowS
ShareRule -> String
(Int -> ShareRule -> ShowS)
-> (ShareRule -> String)
-> ([ShareRule] -> ShowS)
-> Show ShareRule
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShareRule] -> ShowS
$cshowList :: [ShareRule] -> ShowS
show :: ShareRule -> String
$cshow :: ShareRule -> String
showsPrec :: Int -> ShareRule -> ShowS
$cshowsPrec :: Int -> ShareRule -> ShowS
Prelude.Show, (forall x. ShareRule -> Rep ShareRule x)
-> (forall x. Rep ShareRule x -> ShareRule) -> Generic ShareRule
forall x. Rep ShareRule x -> ShareRule
forall x. ShareRule -> Rep ShareRule x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ShareRule x -> ShareRule
$cfrom :: forall x. ShareRule -> Rep ShareRule x
Prelude.Generic)

-- |
-- Create a value of 'ShareRule' 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:
--
-- 'unshareIntervalUnit', 'shareRule_unshareIntervalUnit' - The unit of time for the automatic unsharing interval.
--
-- 'unshareInterval', 'shareRule_unshareInterval' - The period after which snapshots that are shared with other Amazon Web
-- Services accounts are automatically unshared.
--
-- 'targetAccounts', 'shareRule_targetAccounts' - The IDs of the Amazon Web Services accounts with which to share the
-- snapshots.
newShareRule ::
  -- | 'targetAccounts'
  Prelude.NonEmpty Prelude.Text ->
  ShareRule
newShareRule :: NonEmpty Text -> ShareRule
newShareRule NonEmpty Text
pTargetAccounts_ =
  ShareRule' :: Maybe RetentionIntervalUnitValues
-> Maybe Natural -> NonEmpty Text -> ShareRule
ShareRule'
    { $sel:unshareIntervalUnit:ShareRule' :: Maybe RetentionIntervalUnitValues
unshareIntervalUnit = Maybe RetentionIntervalUnitValues
forall a. Maybe a
Prelude.Nothing,
      $sel:unshareInterval:ShareRule' :: Maybe Natural
unshareInterval = Maybe Natural
forall a. Maybe a
Prelude.Nothing,
      $sel:targetAccounts:ShareRule' :: NonEmpty Text
targetAccounts =
        Tagged (NonEmpty Text) (Identity (NonEmpty Text))
-> Tagged (NonEmpty Text) (Identity (NonEmpty Text))
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced (Tagged (NonEmpty Text) (Identity (NonEmpty Text))
 -> Tagged (NonEmpty Text) (Identity (NonEmpty Text)))
-> NonEmpty Text -> NonEmpty Text
forall t b. AReview t b -> b -> t
Lens.# NonEmpty Text
pTargetAccounts_
    }

-- | The unit of time for the automatic unsharing interval.
shareRule_unshareIntervalUnit :: Lens.Lens' ShareRule (Prelude.Maybe RetentionIntervalUnitValues)
shareRule_unshareIntervalUnit :: (Maybe RetentionIntervalUnitValues
 -> f (Maybe RetentionIntervalUnitValues))
-> ShareRule -> f ShareRule
shareRule_unshareIntervalUnit = (ShareRule -> Maybe RetentionIntervalUnitValues)
-> (ShareRule -> Maybe RetentionIntervalUnitValues -> ShareRule)
-> Lens
     ShareRule
     ShareRule
     (Maybe RetentionIntervalUnitValues)
     (Maybe RetentionIntervalUnitValues)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ShareRule' {Maybe RetentionIntervalUnitValues
unshareIntervalUnit :: Maybe RetentionIntervalUnitValues
$sel:unshareIntervalUnit:ShareRule' :: ShareRule -> Maybe RetentionIntervalUnitValues
unshareIntervalUnit} -> Maybe RetentionIntervalUnitValues
unshareIntervalUnit) (\s :: ShareRule
s@ShareRule' {} Maybe RetentionIntervalUnitValues
a -> ShareRule
s {$sel:unshareIntervalUnit:ShareRule' :: Maybe RetentionIntervalUnitValues
unshareIntervalUnit = Maybe RetentionIntervalUnitValues
a} :: ShareRule)

-- | The period after which snapshots that are shared with other Amazon Web
-- Services accounts are automatically unshared.
shareRule_unshareInterval :: Lens.Lens' ShareRule (Prelude.Maybe Prelude.Natural)
shareRule_unshareInterval :: (Maybe Natural -> f (Maybe Natural)) -> ShareRule -> f ShareRule
shareRule_unshareInterval = (ShareRule -> Maybe Natural)
-> (ShareRule -> Maybe Natural -> ShareRule)
-> Lens ShareRule ShareRule (Maybe Natural) (Maybe Natural)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ShareRule' {Maybe Natural
unshareInterval :: Maybe Natural
$sel:unshareInterval:ShareRule' :: ShareRule -> Maybe Natural
unshareInterval} -> Maybe Natural
unshareInterval) (\s :: ShareRule
s@ShareRule' {} Maybe Natural
a -> ShareRule
s {$sel:unshareInterval:ShareRule' :: Maybe Natural
unshareInterval = Maybe Natural
a} :: ShareRule)

-- | The IDs of the Amazon Web Services accounts with which to share the
-- snapshots.
shareRule_targetAccounts :: Lens.Lens' ShareRule (Prelude.NonEmpty Prelude.Text)
shareRule_targetAccounts :: (NonEmpty Text -> f (NonEmpty Text)) -> ShareRule -> f ShareRule
shareRule_targetAccounts = (ShareRule -> NonEmpty Text)
-> (ShareRule -> NonEmpty Text -> ShareRule)
-> Lens ShareRule ShareRule (NonEmpty Text) (NonEmpty Text)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ShareRule' {NonEmpty Text
targetAccounts :: NonEmpty Text
$sel:targetAccounts:ShareRule' :: ShareRule -> NonEmpty Text
targetAccounts} -> NonEmpty Text
targetAccounts) (\s :: ShareRule
s@ShareRule' {} NonEmpty Text
a -> ShareRule
s {$sel:targetAccounts:ShareRule' :: NonEmpty Text
targetAccounts = NonEmpty Text
a} :: ShareRule) ((NonEmpty Text -> f (NonEmpty Text)) -> ShareRule -> f ShareRule)
-> ((NonEmpty Text -> f (NonEmpty Text))
    -> NonEmpty Text -> f (NonEmpty Text))
-> (NonEmpty Text -> f (NonEmpty Text))
-> ShareRule
-> f ShareRule
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (NonEmpty Text -> f (NonEmpty Text))
-> NonEmpty Text -> f (NonEmpty Text)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.FromJSON ShareRule where
  parseJSON :: Value -> Parser ShareRule
parseJSON =
    String -> (Object -> Parser ShareRule) -> Value -> Parser ShareRule
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Core.withObject
      String
"ShareRule"
      ( \Object
x ->
          Maybe RetentionIntervalUnitValues
-> Maybe Natural -> NonEmpty Text -> ShareRule
ShareRule'
            (Maybe RetentionIntervalUnitValues
 -> Maybe Natural -> NonEmpty Text -> ShareRule)
-> Parser (Maybe RetentionIntervalUnitValues)
-> Parser (Maybe Natural -> NonEmpty Text -> ShareRule)
forall (f :: * -> *) a b. Functor 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
"UnshareIntervalUnit")
            Parser (Maybe Natural -> NonEmpty Text -> ShareRule)
-> Parser (Maybe Natural) -> Parser (NonEmpty Text -> ShareRule)
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
"UnshareInterval")
            Parser (NonEmpty Text -> ShareRule)
-> Parser (NonEmpty Text) -> Parser ShareRule
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x Object -> Text -> Parser (NonEmpty Text)
forall a. FromJSON a => Object -> Text -> Parser a
Core..: Text
"TargetAccounts")
      )

instance Prelude.Hashable ShareRule

instance Prelude.NFData ShareRule

instance Core.ToJSON ShareRule where
  toJSON :: ShareRule -> Value
toJSON ShareRule' {Maybe Natural
Maybe RetentionIntervalUnitValues
NonEmpty Text
targetAccounts :: NonEmpty Text
unshareInterval :: Maybe Natural
unshareIntervalUnit :: Maybe RetentionIntervalUnitValues
$sel:targetAccounts:ShareRule' :: ShareRule -> NonEmpty Text
$sel:unshareInterval:ShareRule' :: ShareRule -> Maybe Natural
$sel:unshareIntervalUnit:ShareRule' :: ShareRule -> Maybe RetentionIntervalUnitValues
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Text
"UnshareIntervalUnit" 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
unshareIntervalUnit,
            (Text
"UnshareInterval" 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
unshareInterval,
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"TargetAccounts" Text -> NonEmpty Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= NonEmpty Text
targetAccounts)
          ]
      )