{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-binds #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.Kinesis.MergeShards
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Merges two adjacent shards in a Kinesis data stream and combines them
-- into a single shard to reduce the stream\'s capacity to ingest and
-- transport data. This API is only supported for the data streams with the
-- provisioned capacity mode. Two shards are considered adjacent if the
-- union of the hash key ranges for the two shards form a contiguous set
-- with no gaps. For example, if you have two shards, one with a hash key
-- range of 276...381 and the other with a hash key range of 382...454,
-- then you could merge these two shards into a single shard that would
-- have a hash key range of 276...454. After the merge, the single child
-- shard receives data for all hash key values covered by the two parent
-- shards.
--
-- When invoking this API, it is recommended you use the @StreamARN@ input
-- parameter rather than the @StreamName@ input parameter.
--
-- @MergeShards@ is called when there is a need to reduce the overall
-- capacity of a stream because of excess capacity that is not being used.
-- You must specify the shard to be merged and the adjacent shard for a
-- stream. For more information about merging shards, see
-- <https://docs.aws.amazon.com/kinesis/latest/dev/kinesis-using-sdk-java-resharding-merge.html Merge Two Shards>
-- in the /Amazon Kinesis Data Streams Developer Guide/.
--
-- If the stream is in the @ACTIVE@ state, you can call @MergeShards@. If a
-- stream is in the @CREATING@, @UPDATING@, or @DELETING@ state,
-- @MergeShards@ returns a @ResourceInUseException@. If the specified
-- stream does not exist, @MergeShards@ returns a
-- @ResourceNotFoundException@.
--
-- You can use DescribeStreamSummary to check the state of the stream,
-- which is returned in @StreamStatus@.
--
-- @MergeShards@ is an asynchronous operation. Upon receiving a
-- @MergeShards@ request, Amazon Kinesis Data Streams immediately returns a
-- response and sets the @StreamStatus@ to @UPDATING@. After the operation
-- is completed, Kinesis Data Streams sets the @StreamStatus@ to @ACTIVE@.
-- Read and write operations continue to work while the stream is in the
-- @UPDATING@ state.
--
-- You use DescribeStreamSummary and the ListShards APIs to determine the
-- shard IDs that are specified in the @MergeShards@ request.
--
-- If you try to operate on too many streams in parallel using
-- CreateStream, DeleteStream, @MergeShards@, or SplitShard, you receive a
-- @LimitExceededException@.
--
-- @MergeShards@ has a limit of five transactions per second per account.
module Amazonka.Kinesis.MergeShards
  ( -- * Creating a Request
    MergeShards (..),
    newMergeShards,

    -- * Request Lenses
    mergeShards_streamARN,
    mergeShards_streamName,
    mergeShards_shardToMerge,
    mergeShards_adjacentShardToMerge,

    -- * Destructuring the Response
    MergeShardsResponse (..),
    newMergeShardsResponse,
  )
where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import Amazonka.Kinesis.Types
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | Represents the input for @MergeShards@.
--
-- /See:/ 'newMergeShards' smart constructor.
data MergeShards = MergeShards'
  { -- | The ARN of the stream.
    MergeShards -> Maybe Text
streamARN :: Prelude.Maybe Prelude.Text,
    -- | The name of the stream for the merge.
    MergeShards -> Maybe Text
streamName :: Prelude.Maybe Prelude.Text,
    -- | The shard ID of the shard to combine with the adjacent shard for the
    -- merge.
    MergeShards -> Text
shardToMerge :: Prelude.Text,
    -- | The shard ID of the adjacent shard for the merge.
    MergeShards -> Text
adjacentShardToMerge :: Prelude.Text
  }
  deriving (MergeShards -> MergeShards -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MergeShards -> MergeShards -> Bool
$c/= :: MergeShards -> MergeShards -> Bool
== :: MergeShards -> MergeShards -> Bool
$c== :: MergeShards -> MergeShards -> Bool
Prelude.Eq, ReadPrec [MergeShards]
ReadPrec MergeShards
Int -> ReadS MergeShards
ReadS [MergeShards]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [MergeShards]
$creadListPrec :: ReadPrec [MergeShards]
readPrec :: ReadPrec MergeShards
$creadPrec :: ReadPrec MergeShards
readList :: ReadS [MergeShards]
$creadList :: ReadS [MergeShards]
readsPrec :: Int -> ReadS MergeShards
$creadsPrec :: Int -> ReadS MergeShards
Prelude.Read, Int -> MergeShards -> ShowS
[MergeShards] -> ShowS
MergeShards -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MergeShards] -> ShowS
$cshowList :: [MergeShards] -> ShowS
show :: MergeShards -> String
$cshow :: MergeShards -> String
showsPrec :: Int -> MergeShards -> ShowS
$cshowsPrec :: Int -> MergeShards -> ShowS
Prelude.Show, forall x. Rep MergeShards x -> MergeShards
forall x. MergeShards -> Rep MergeShards x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MergeShards x -> MergeShards
$cfrom :: forall x. MergeShards -> Rep MergeShards x
Prelude.Generic)

-- |
-- Create a value of 'MergeShards' 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:
--
-- 'streamARN', 'mergeShards_streamARN' - The ARN of the stream.
--
-- 'streamName', 'mergeShards_streamName' - The name of the stream for the merge.
--
-- 'shardToMerge', 'mergeShards_shardToMerge' - The shard ID of the shard to combine with the adjacent shard for the
-- merge.
--
-- 'adjacentShardToMerge', 'mergeShards_adjacentShardToMerge' - The shard ID of the adjacent shard for the merge.
newMergeShards ::
  -- | 'shardToMerge'
  Prelude.Text ->
  -- | 'adjacentShardToMerge'
  Prelude.Text ->
  MergeShards
newMergeShards :: Text -> Text -> MergeShards
newMergeShards Text
pShardToMerge_ Text
pAdjacentShardToMerge_ =
  MergeShards'
    { $sel:streamARN:MergeShards' :: Maybe Text
streamARN = forall a. Maybe a
Prelude.Nothing,
      $sel:streamName:MergeShards' :: Maybe Text
streamName = forall a. Maybe a
Prelude.Nothing,
      $sel:shardToMerge:MergeShards' :: Text
shardToMerge = Text
pShardToMerge_,
      $sel:adjacentShardToMerge:MergeShards' :: Text
adjacentShardToMerge = Text
pAdjacentShardToMerge_
    }

-- | The ARN of the stream.
mergeShards_streamARN :: Lens.Lens' MergeShards (Prelude.Maybe Prelude.Text)
mergeShards_streamARN :: Lens' MergeShards (Maybe Text)
mergeShards_streamARN = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\MergeShards' {Maybe Text
streamARN :: Maybe Text
$sel:streamARN:MergeShards' :: MergeShards -> Maybe Text
streamARN} -> Maybe Text
streamARN) (\s :: MergeShards
s@MergeShards' {} Maybe Text
a -> MergeShards
s {$sel:streamARN:MergeShards' :: Maybe Text
streamARN = Maybe Text
a} :: MergeShards)

-- | The name of the stream for the merge.
mergeShards_streamName :: Lens.Lens' MergeShards (Prelude.Maybe Prelude.Text)
mergeShards_streamName :: Lens' MergeShards (Maybe Text)
mergeShards_streamName = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\MergeShards' {Maybe Text
streamName :: Maybe Text
$sel:streamName:MergeShards' :: MergeShards -> Maybe Text
streamName} -> Maybe Text
streamName) (\s :: MergeShards
s@MergeShards' {} Maybe Text
a -> MergeShards
s {$sel:streamName:MergeShards' :: Maybe Text
streamName = Maybe Text
a} :: MergeShards)

-- | The shard ID of the shard to combine with the adjacent shard for the
-- merge.
mergeShards_shardToMerge :: Lens.Lens' MergeShards Prelude.Text
mergeShards_shardToMerge :: Lens' MergeShards Text
mergeShards_shardToMerge = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\MergeShards' {Text
shardToMerge :: Text
$sel:shardToMerge:MergeShards' :: MergeShards -> Text
shardToMerge} -> Text
shardToMerge) (\s :: MergeShards
s@MergeShards' {} Text
a -> MergeShards
s {$sel:shardToMerge:MergeShards' :: Text
shardToMerge = Text
a} :: MergeShards)

-- | The shard ID of the adjacent shard for the merge.
mergeShards_adjacentShardToMerge :: Lens.Lens' MergeShards Prelude.Text
mergeShards_adjacentShardToMerge :: Lens' MergeShards Text
mergeShards_adjacentShardToMerge = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\MergeShards' {Text
adjacentShardToMerge :: Text
$sel:adjacentShardToMerge:MergeShards' :: MergeShards -> Text
adjacentShardToMerge} -> Text
adjacentShardToMerge) (\s :: MergeShards
s@MergeShards' {} Text
a -> MergeShards
s {$sel:adjacentShardToMerge:MergeShards' :: Text
adjacentShardToMerge = Text
a} :: MergeShards)

instance Core.AWSRequest MergeShards where
  type AWSResponse MergeShards = MergeShardsResponse
  request :: (Service -> Service) -> MergeShards -> Request MergeShards
request Service -> Service
overrides =
    forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON (Service -> Service
overrides Service
defaultService)
  response :: forall (m :: * -> *).
MonadResource m =>
(ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy MergeShards
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse MergeShards)))
response = forall (m :: * -> *) a.
MonadResource m =>
AWSResponse a
-> (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveNull MergeShardsResponse
MergeShardsResponse'

instance Prelude.Hashable MergeShards where
  hashWithSalt :: Int -> MergeShards -> Int
hashWithSalt Int
_salt MergeShards' {Maybe Text
Text
adjacentShardToMerge :: Text
shardToMerge :: Text
streamName :: Maybe Text
streamARN :: Maybe Text
$sel:adjacentShardToMerge:MergeShards' :: MergeShards -> Text
$sel:shardToMerge:MergeShards' :: MergeShards -> Text
$sel:streamName:MergeShards' :: MergeShards -> Maybe Text
$sel:streamARN:MergeShards' :: MergeShards -> Maybe Text
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
streamARN
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
streamName
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
shardToMerge
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Text
adjacentShardToMerge

instance Prelude.NFData MergeShards where
  rnf :: MergeShards -> ()
rnf MergeShards' {Maybe Text
Text
adjacentShardToMerge :: Text
shardToMerge :: Text
streamName :: Maybe Text
streamARN :: Maybe Text
$sel:adjacentShardToMerge:MergeShards' :: MergeShards -> Text
$sel:shardToMerge:MergeShards' :: MergeShards -> Text
$sel:streamName:MergeShards' :: MergeShards -> Maybe Text
$sel:streamARN:MergeShards' :: MergeShards -> Maybe Text
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
streamARN
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
streamName
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
shardToMerge
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Text
adjacentShardToMerge

instance Data.ToHeaders MergeShards where
  toHeaders :: MergeShards -> [Header]
toHeaders =
    forall a b. a -> b -> a
Prelude.const
      ( forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              forall a. ToHeader a => HeaderName -> a -> [Header]
Data.=# ( ByteString
"Kinesis_20131202.MergeShards" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              forall a. ToHeader a => HeaderName -> a -> [Header]
Data.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Data.ToJSON MergeShards where
  toJSON :: MergeShards -> Value
toJSON MergeShards' {Maybe Text
Text
adjacentShardToMerge :: Text
shardToMerge :: Text
streamName :: Maybe Text
streamARN :: Maybe Text
$sel:adjacentShardToMerge:MergeShards' :: MergeShards -> Text
$sel:shardToMerge:MergeShards' :: MergeShards -> Text
$sel:streamName:MergeShards' :: MergeShards -> Maybe Text
$sel:streamARN:MergeShards' :: MergeShards -> Maybe Text
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"StreamARN" 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 Text
streamARN,
            (Key
"StreamName" 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 Text
streamName,
            forall a. a -> Maybe a
Prelude.Just (Key
"ShardToMerge" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
shardToMerge),
            forall a. a -> Maybe a
Prelude.Just
              ( Key
"AdjacentShardToMerge"
                  forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Text
adjacentShardToMerge
              )
          ]
      )

instance Data.ToPath MergeShards where
  toPath :: MergeShards -> ByteString
toPath = forall a b. a -> b -> a
Prelude.const ByteString
"/"

instance Data.ToQuery MergeShards where
  toQuery :: MergeShards -> QueryString
toQuery = forall a b. a -> b -> a
Prelude.const forall a. Monoid a => a
Prelude.mempty

-- | /See:/ 'newMergeShardsResponse' smart constructor.
data MergeShardsResponse = MergeShardsResponse'
  {
  }
  deriving (MergeShardsResponse -> MergeShardsResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MergeShardsResponse -> MergeShardsResponse -> Bool
$c/= :: MergeShardsResponse -> MergeShardsResponse -> Bool
== :: MergeShardsResponse -> MergeShardsResponse -> Bool
$c== :: MergeShardsResponse -> MergeShardsResponse -> Bool
Prelude.Eq, ReadPrec [MergeShardsResponse]
ReadPrec MergeShardsResponse
Int -> ReadS MergeShardsResponse
ReadS [MergeShardsResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [MergeShardsResponse]
$creadListPrec :: ReadPrec [MergeShardsResponse]
readPrec :: ReadPrec MergeShardsResponse
$creadPrec :: ReadPrec MergeShardsResponse
readList :: ReadS [MergeShardsResponse]
$creadList :: ReadS [MergeShardsResponse]
readsPrec :: Int -> ReadS MergeShardsResponse
$creadsPrec :: Int -> ReadS MergeShardsResponse
Prelude.Read, Int -> MergeShardsResponse -> ShowS
[MergeShardsResponse] -> ShowS
MergeShardsResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MergeShardsResponse] -> ShowS
$cshowList :: [MergeShardsResponse] -> ShowS
show :: MergeShardsResponse -> String
$cshow :: MergeShardsResponse -> String
showsPrec :: Int -> MergeShardsResponse -> ShowS
$cshowsPrec :: Int -> MergeShardsResponse -> ShowS
Prelude.Show, forall x. Rep MergeShardsResponse x -> MergeShardsResponse
forall x. MergeShardsResponse -> Rep MergeShardsResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep MergeShardsResponse x -> MergeShardsResponse
$cfrom :: forall x. MergeShardsResponse -> Rep MergeShardsResponse x
Prelude.Generic)

-- |
-- Create a value of 'MergeShardsResponse' 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.
newMergeShardsResponse ::
  MergeShardsResponse
newMergeShardsResponse :: MergeShardsResponse
newMergeShardsResponse = MergeShardsResponse
MergeShardsResponse'

instance Prelude.NFData MergeShardsResponse where
  rnf :: MergeShardsResponse -> ()
rnf MergeShardsResponse
_ = ()