{-# 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.DynamoDB.CreateGlobalTable
-- 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)
--
-- Creates a global table from an existing table. A global table creates a
-- replication relationship between two or more DynamoDB tables with the
-- same table name in the provided Regions.
--
-- This operation only applies to
-- <https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/globaltables.V1.html Version 2017.11.29>
-- of global tables.
--
-- If you want to add a new replica table to a global table, each of the
-- following conditions must be true:
--
-- -   The table must have the same primary key as all of the other
--     replicas.
--
-- -   The table must have the same name as all of the other replicas.
--
-- -   The table must have DynamoDB Streams enabled, with the stream
--     containing both the new and the old images of the item.
--
-- -   None of the replica tables in the global table can contain any data.
--
-- If global secondary indexes are specified, then the following conditions
-- must also be met:
--
-- -   The global secondary indexes must have the same name.
--
-- -   The global secondary indexes must have the same hash key and sort
--     key (if present).
--
-- If local secondary indexes are specified, then the following conditions
-- must also be met:
--
-- -   The local secondary indexes must have the same name.
--
-- -   The local secondary indexes must have the same hash key and sort key
--     (if present).
--
-- Write capacity settings should be set consistently across your replica
-- tables and secondary indexes. DynamoDB strongly recommends enabling auto
-- scaling to manage the write capacity settings for all of your global
-- tables replicas and indexes.
--
-- If you prefer to manage write capacity settings manually, you should
-- provision equal replicated write capacity units to your replica tables.
-- You should also provision equal replicated write capacity units to
-- matching secondary indexes across your global table.
module Amazonka.DynamoDB.CreateGlobalTable
  ( -- * Creating a Request
    CreateGlobalTable (..),
    newCreateGlobalTable,

    -- * Request Lenses
    createGlobalTable_globalTableName,
    createGlobalTable_replicationGroup,

    -- * Destructuring the Response
    CreateGlobalTableResponse (..),
    newCreateGlobalTableResponse,

    -- * Response Lenses
    createGlobalTableResponse_globalTableDescription,
    createGlobalTableResponse_httpStatus,
  )
where

import qualified Amazonka.Core as Core
import Amazonka.DynamoDB.Types
import qualified Amazonka.Lens as Lens
import qualified Amazonka.Prelude as Prelude
import qualified Amazonka.Request as Request
import qualified Amazonka.Response as Response

-- | /See:/ 'newCreateGlobalTable' smart constructor.
data CreateGlobalTable = CreateGlobalTable'
  { -- | The global table name.
    CreateGlobalTable -> Text
globalTableName :: Prelude.Text,
    -- | The Regions where the global table needs to be created.
    CreateGlobalTable -> [Replica]
replicationGroup :: [Replica]
  }
  deriving (CreateGlobalTable -> CreateGlobalTable -> Bool
(CreateGlobalTable -> CreateGlobalTable -> Bool)
-> (CreateGlobalTable -> CreateGlobalTable -> Bool)
-> Eq CreateGlobalTable
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateGlobalTable -> CreateGlobalTable -> Bool
$c/= :: CreateGlobalTable -> CreateGlobalTable -> Bool
== :: CreateGlobalTable -> CreateGlobalTable -> Bool
$c== :: CreateGlobalTable -> CreateGlobalTable -> Bool
Prelude.Eq, ReadPrec [CreateGlobalTable]
ReadPrec CreateGlobalTable
Int -> ReadS CreateGlobalTable
ReadS [CreateGlobalTable]
(Int -> ReadS CreateGlobalTable)
-> ReadS [CreateGlobalTable]
-> ReadPrec CreateGlobalTable
-> ReadPrec [CreateGlobalTable]
-> Read CreateGlobalTable
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CreateGlobalTable]
$creadListPrec :: ReadPrec [CreateGlobalTable]
readPrec :: ReadPrec CreateGlobalTable
$creadPrec :: ReadPrec CreateGlobalTable
readList :: ReadS [CreateGlobalTable]
$creadList :: ReadS [CreateGlobalTable]
readsPrec :: Int -> ReadS CreateGlobalTable
$creadsPrec :: Int -> ReadS CreateGlobalTable
Prelude.Read, Int -> CreateGlobalTable -> ShowS
[CreateGlobalTable] -> ShowS
CreateGlobalTable -> String
(Int -> CreateGlobalTable -> ShowS)
-> (CreateGlobalTable -> String)
-> ([CreateGlobalTable] -> ShowS)
-> Show CreateGlobalTable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateGlobalTable] -> ShowS
$cshowList :: [CreateGlobalTable] -> ShowS
show :: CreateGlobalTable -> String
$cshow :: CreateGlobalTable -> String
showsPrec :: Int -> CreateGlobalTable -> ShowS
$cshowsPrec :: Int -> CreateGlobalTable -> ShowS
Prelude.Show, (forall x. CreateGlobalTable -> Rep CreateGlobalTable x)
-> (forall x. Rep CreateGlobalTable x -> CreateGlobalTable)
-> Generic CreateGlobalTable
forall x. Rep CreateGlobalTable x -> CreateGlobalTable
forall x. CreateGlobalTable -> Rep CreateGlobalTable x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CreateGlobalTable x -> CreateGlobalTable
$cfrom :: forall x. CreateGlobalTable -> Rep CreateGlobalTable x
Prelude.Generic)

-- |
-- Create a value of 'CreateGlobalTable' 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:
--
-- 'globalTableName', 'createGlobalTable_globalTableName' - The global table name.
--
-- 'replicationGroup', 'createGlobalTable_replicationGroup' - The Regions where the global table needs to be created.
newCreateGlobalTable ::
  -- | 'globalTableName'
  Prelude.Text ->
  CreateGlobalTable
newCreateGlobalTable :: Text -> CreateGlobalTable
newCreateGlobalTable Text
pGlobalTableName_ =
  CreateGlobalTable' :: Text -> [Replica] -> CreateGlobalTable
CreateGlobalTable'
    { $sel:globalTableName:CreateGlobalTable' :: Text
globalTableName =
        Text
pGlobalTableName_,
      $sel:replicationGroup:CreateGlobalTable' :: [Replica]
replicationGroup = [Replica]
forall a. Monoid a => a
Prelude.mempty
    }

-- | The global table name.
createGlobalTable_globalTableName :: Lens.Lens' CreateGlobalTable Prelude.Text
createGlobalTable_globalTableName :: (Text -> f Text) -> CreateGlobalTable -> f CreateGlobalTable
createGlobalTable_globalTableName = (CreateGlobalTable -> Text)
-> (CreateGlobalTable -> Text -> CreateGlobalTable)
-> Lens CreateGlobalTable CreateGlobalTable Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateGlobalTable' {Text
globalTableName :: Text
$sel:globalTableName:CreateGlobalTable' :: CreateGlobalTable -> Text
globalTableName} -> Text
globalTableName) (\s :: CreateGlobalTable
s@CreateGlobalTable' {} Text
a -> CreateGlobalTable
s {$sel:globalTableName:CreateGlobalTable' :: Text
globalTableName = Text
a} :: CreateGlobalTable)

-- | The Regions where the global table needs to be created.
createGlobalTable_replicationGroup :: Lens.Lens' CreateGlobalTable [Replica]
createGlobalTable_replicationGroup :: ([Replica] -> f [Replica])
-> CreateGlobalTable -> f CreateGlobalTable
createGlobalTable_replicationGroup = (CreateGlobalTable -> [Replica])
-> (CreateGlobalTable -> [Replica] -> CreateGlobalTable)
-> Lens CreateGlobalTable CreateGlobalTable [Replica] [Replica]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateGlobalTable' {[Replica]
replicationGroup :: [Replica]
$sel:replicationGroup:CreateGlobalTable' :: CreateGlobalTable -> [Replica]
replicationGroup} -> [Replica]
replicationGroup) (\s :: CreateGlobalTable
s@CreateGlobalTable' {} [Replica]
a -> CreateGlobalTable
s {$sel:replicationGroup:CreateGlobalTable' :: [Replica]
replicationGroup = [Replica]
a} :: CreateGlobalTable) (([Replica] -> f [Replica])
 -> CreateGlobalTable -> f CreateGlobalTable)
-> (([Replica] -> f [Replica]) -> [Replica] -> f [Replica])
-> ([Replica] -> f [Replica])
-> CreateGlobalTable
-> f CreateGlobalTable
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. ([Replica] -> f [Replica]) -> [Replica] -> f [Replica]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.AWSRequest CreateGlobalTable where
  type
    AWSResponse CreateGlobalTable =
      CreateGlobalTableResponse
  request :: CreateGlobalTable -> Request CreateGlobalTable
request = Service -> CreateGlobalTable -> Request CreateGlobalTable
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy CreateGlobalTable
-> ClientResponse ClientBody
-> m (Either
        Error (ClientResponse (AWSResponse CreateGlobalTable)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse CreateGlobalTable))
-> Logger
-> Service
-> Proxy CreateGlobalTable
-> ClientResponse ClientBody
-> m (Either
        Error (ClientResponse (AWSResponse CreateGlobalTable)))
forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> Logger
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe GlobalTableDescription -> Int -> CreateGlobalTableResponse
CreateGlobalTableResponse'
            (Maybe GlobalTableDescription -> Int -> CreateGlobalTableResponse)
-> Either String (Maybe GlobalTableDescription)
-> Either String (Int -> CreateGlobalTableResponse)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x Object -> Text -> Either String (Maybe GlobalTableDescription)
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"GlobalTableDescription")
            Either String (Int -> CreateGlobalTableResponse)
-> Either String Int -> Either String CreateGlobalTableResponse
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Int -> Either String Int
forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (Int -> Int
forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable CreateGlobalTable

instance Prelude.NFData CreateGlobalTable

instance Core.ToHeaders CreateGlobalTable where
  toHeaders :: CreateGlobalTable -> ResponseHeaders
toHeaders =
    ResponseHeaders -> CreateGlobalTable -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const
      ( [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"X-Amz-Target"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"DynamoDB_20120810.CreateGlobalTable" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"application/x-amz-json-1.0" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Core.ToJSON CreateGlobalTable where
  toJSON :: CreateGlobalTable -> Value
toJSON CreateGlobalTable' {[Replica]
Text
replicationGroup :: [Replica]
globalTableName :: Text
$sel:replicationGroup:CreateGlobalTable' :: CreateGlobalTable -> [Replica]
$sel:globalTableName:CreateGlobalTable' :: CreateGlobalTable -> Text
..} =
    [Pair] -> Value
Core.object
      ( [Maybe Pair] -> [Pair]
forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"GlobalTableName" Text -> Text -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= Text
globalTableName),
            Pair -> Maybe Pair
forall a. a -> Maybe a
Prelude.Just
              (Text
"ReplicationGroup" Text -> [Replica] -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= [Replica]
replicationGroup)
          ]
      )

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

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

-- | /See:/ 'newCreateGlobalTableResponse' smart constructor.
data CreateGlobalTableResponse = CreateGlobalTableResponse'
  { -- | Contains the details of the global table.
    CreateGlobalTableResponse -> Maybe GlobalTableDescription
globalTableDescription :: Prelude.Maybe GlobalTableDescription,
    -- | The response's http status code.
    CreateGlobalTableResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (CreateGlobalTableResponse -> CreateGlobalTableResponse -> Bool
(CreateGlobalTableResponse -> CreateGlobalTableResponse -> Bool)
-> (CreateGlobalTableResponse -> CreateGlobalTableResponse -> Bool)
-> Eq CreateGlobalTableResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateGlobalTableResponse -> CreateGlobalTableResponse -> Bool
$c/= :: CreateGlobalTableResponse -> CreateGlobalTableResponse -> Bool
== :: CreateGlobalTableResponse -> CreateGlobalTableResponse -> Bool
$c== :: CreateGlobalTableResponse -> CreateGlobalTableResponse -> Bool
Prelude.Eq, ReadPrec [CreateGlobalTableResponse]
ReadPrec CreateGlobalTableResponse
Int -> ReadS CreateGlobalTableResponse
ReadS [CreateGlobalTableResponse]
(Int -> ReadS CreateGlobalTableResponse)
-> ReadS [CreateGlobalTableResponse]
-> ReadPrec CreateGlobalTableResponse
-> ReadPrec [CreateGlobalTableResponse]
-> Read CreateGlobalTableResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CreateGlobalTableResponse]
$creadListPrec :: ReadPrec [CreateGlobalTableResponse]
readPrec :: ReadPrec CreateGlobalTableResponse
$creadPrec :: ReadPrec CreateGlobalTableResponse
readList :: ReadS [CreateGlobalTableResponse]
$creadList :: ReadS [CreateGlobalTableResponse]
readsPrec :: Int -> ReadS CreateGlobalTableResponse
$creadsPrec :: Int -> ReadS CreateGlobalTableResponse
Prelude.Read, Int -> CreateGlobalTableResponse -> ShowS
[CreateGlobalTableResponse] -> ShowS
CreateGlobalTableResponse -> String
(Int -> CreateGlobalTableResponse -> ShowS)
-> (CreateGlobalTableResponse -> String)
-> ([CreateGlobalTableResponse] -> ShowS)
-> Show CreateGlobalTableResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateGlobalTableResponse] -> ShowS
$cshowList :: [CreateGlobalTableResponse] -> ShowS
show :: CreateGlobalTableResponse -> String
$cshow :: CreateGlobalTableResponse -> String
showsPrec :: Int -> CreateGlobalTableResponse -> ShowS
$cshowsPrec :: Int -> CreateGlobalTableResponse -> ShowS
Prelude.Show, (forall x.
 CreateGlobalTableResponse -> Rep CreateGlobalTableResponse x)
-> (forall x.
    Rep CreateGlobalTableResponse x -> CreateGlobalTableResponse)
-> Generic CreateGlobalTableResponse
forall x.
Rep CreateGlobalTableResponse x -> CreateGlobalTableResponse
forall x.
CreateGlobalTableResponse -> Rep CreateGlobalTableResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x.
Rep CreateGlobalTableResponse x -> CreateGlobalTableResponse
$cfrom :: forall x.
CreateGlobalTableResponse -> Rep CreateGlobalTableResponse x
Prelude.Generic)

-- |
-- Create a value of 'CreateGlobalTableResponse' 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:
--
-- 'globalTableDescription', 'createGlobalTableResponse_globalTableDescription' - Contains the details of the global table.
--
-- 'httpStatus', 'createGlobalTableResponse_httpStatus' - The response's http status code.
newCreateGlobalTableResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  CreateGlobalTableResponse
newCreateGlobalTableResponse :: Int -> CreateGlobalTableResponse
newCreateGlobalTableResponse Int
pHttpStatus_ =
  CreateGlobalTableResponse' :: Maybe GlobalTableDescription -> Int -> CreateGlobalTableResponse
CreateGlobalTableResponse'
    { $sel:globalTableDescription:CreateGlobalTableResponse' :: Maybe GlobalTableDescription
globalTableDescription =
        Maybe GlobalTableDescription
forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:CreateGlobalTableResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | Contains the details of the global table.
createGlobalTableResponse_globalTableDescription :: Lens.Lens' CreateGlobalTableResponse (Prelude.Maybe GlobalTableDescription)
createGlobalTableResponse_globalTableDescription :: (Maybe GlobalTableDescription -> f (Maybe GlobalTableDescription))
-> CreateGlobalTableResponse -> f CreateGlobalTableResponse
createGlobalTableResponse_globalTableDescription = (CreateGlobalTableResponse -> Maybe GlobalTableDescription)
-> (CreateGlobalTableResponse
    -> Maybe GlobalTableDescription -> CreateGlobalTableResponse)
-> Lens
     CreateGlobalTableResponse
     CreateGlobalTableResponse
     (Maybe GlobalTableDescription)
     (Maybe GlobalTableDescription)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateGlobalTableResponse' {Maybe GlobalTableDescription
globalTableDescription :: Maybe GlobalTableDescription
$sel:globalTableDescription:CreateGlobalTableResponse' :: CreateGlobalTableResponse -> Maybe GlobalTableDescription
globalTableDescription} -> Maybe GlobalTableDescription
globalTableDescription) (\s :: CreateGlobalTableResponse
s@CreateGlobalTableResponse' {} Maybe GlobalTableDescription
a -> CreateGlobalTableResponse
s {$sel:globalTableDescription:CreateGlobalTableResponse' :: Maybe GlobalTableDescription
globalTableDescription = Maybe GlobalTableDescription
a} :: CreateGlobalTableResponse)

-- | The response's http status code.
createGlobalTableResponse_httpStatus :: Lens.Lens' CreateGlobalTableResponse Prelude.Int
createGlobalTableResponse_httpStatus :: (Int -> f Int)
-> CreateGlobalTableResponse -> f CreateGlobalTableResponse
createGlobalTableResponse_httpStatus = (CreateGlobalTableResponse -> Int)
-> (CreateGlobalTableResponse -> Int -> CreateGlobalTableResponse)
-> Lens CreateGlobalTableResponse CreateGlobalTableResponse Int Int
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateGlobalTableResponse' {Int
httpStatus :: Int
$sel:httpStatus:CreateGlobalTableResponse' :: CreateGlobalTableResponse -> Int
httpStatus} -> Int
httpStatus) (\s :: CreateGlobalTableResponse
s@CreateGlobalTableResponse' {} Int
a -> CreateGlobalTableResponse
s {$sel:httpStatus:CreateGlobalTableResponse' :: Int
httpStatus = Int
a} :: CreateGlobalTableResponse)

instance Prelude.NFData CreateGlobalTableResponse