{-# 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.GuardDuty.CreateMembers
-- 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 member accounts of the current AWS account by specifying a list
-- of AWS account IDs. This step is a prerequisite for managing the
-- associated member accounts either by invitation or through an
-- organization.
--
-- When using @Create Members@ as an organizations delegated administrator
-- this action will enable GuardDuty in the added member accounts, with the
-- exception of the organization delegated administrator account, which
-- must enable GuardDuty prior to being added as a member.
--
-- If you are adding accounts by invitation use this action after GuardDuty
-- has been enabled in potential member accounts and before using
-- <https://docs.aws.amazon.com/guardduty/latest/APIReference/API_InviteMembers.html Invite Members>
-- .
module Amazonka.GuardDuty.CreateMembers
  ( -- * Creating a Request
    CreateMembers (..),
    newCreateMembers,

    -- * Request Lenses
    createMembers_detectorId,
    createMembers_accountDetails,

    -- * Destructuring the Response
    CreateMembersResponse (..),
    newCreateMembersResponse,

    -- * Response Lenses
    createMembersResponse_httpStatus,
    createMembersResponse_unprocessedAccounts,
  )
where

import qualified Amazonka.Core as Core
import Amazonka.GuardDuty.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:/ 'newCreateMembers' smart constructor.
data CreateMembers = CreateMembers'
  { -- | The unique ID of the detector of the GuardDuty account that you want to
    -- associate member accounts with.
    CreateMembers -> Text
detectorId :: Prelude.Text,
    -- | A list of account ID and email address pairs of the accounts that you
    -- want to associate with the GuardDuty administrator account.
    CreateMembers -> NonEmpty AccountDetail
accountDetails :: Prelude.NonEmpty AccountDetail
  }
  deriving (CreateMembers -> CreateMembers -> Bool
(CreateMembers -> CreateMembers -> Bool)
-> (CreateMembers -> CreateMembers -> Bool) -> Eq CreateMembers
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateMembers -> CreateMembers -> Bool
$c/= :: CreateMembers -> CreateMembers -> Bool
== :: CreateMembers -> CreateMembers -> Bool
$c== :: CreateMembers -> CreateMembers -> Bool
Prelude.Eq, ReadPrec [CreateMembers]
ReadPrec CreateMembers
Int -> ReadS CreateMembers
ReadS [CreateMembers]
(Int -> ReadS CreateMembers)
-> ReadS [CreateMembers]
-> ReadPrec CreateMembers
-> ReadPrec [CreateMembers]
-> Read CreateMembers
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CreateMembers]
$creadListPrec :: ReadPrec [CreateMembers]
readPrec :: ReadPrec CreateMembers
$creadPrec :: ReadPrec CreateMembers
readList :: ReadS [CreateMembers]
$creadList :: ReadS [CreateMembers]
readsPrec :: Int -> ReadS CreateMembers
$creadsPrec :: Int -> ReadS CreateMembers
Prelude.Read, Int -> CreateMembers -> ShowS
[CreateMembers] -> ShowS
CreateMembers -> String
(Int -> CreateMembers -> ShowS)
-> (CreateMembers -> String)
-> ([CreateMembers] -> ShowS)
-> Show CreateMembers
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateMembers] -> ShowS
$cshowList :: [CreateMembers] -> ShowS
show :: CreateMembers -> String
$cshow :: CreateMembers -> String
showsPrec :: Int -> CreateMembers -> ShowS
$cshowsPrec :: Int -> CreateMembers -> ShowS
Prelude.Show, (forall x. CreateMembers -> Rep CreateMembers x)
-> (forall x. Rep CreateMembers x -> CreateMembers)
-> Generic CreateMembers
forall x. Rep CreateMembers x -> CreateMembers
forall x. CreateMembers -> Rep CreateMembers x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CreateMembers x -> CreateMembers
$cfrom :: forall x. CreateMembers -> Rep CreateMembers x
Prelude.Generic)

-- |
-- Create a value of 'CreateMembers' 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:
--
-- 'detectorId', 'createMembers_detectorId' - The unique ID of the detector of the GuardDuty account that you want to
-- associate member accounts with.
--
-- 'accountDetails', 'createMembers_accountDetails' - A list of account ID and email address pairs of the accounts that you
-- want to associate with the GuardDuty administrator account.
newCreateMembers ::
  -- | 'detectorId'
  Prelude.Text ->
  -- | 'accountDetails'
  Prelude.NonEmpty AccountDetail ->
  CreateMembers
newCreateMembers :: Text -> NonEmpty AccountDetail -> CreateMembers
newCreateMembers Text
pDetectorId_ NonEmpty AccountDetail
pAccountDetails_ =
  CreateMembers' :: Text -> NonEmpty AccountDetail -> CreateMembers
CreateMembers'
    { $sel:detectorId:CreateMembers' :: Text
detectorId = Text
pDetectorId_,
      $sel:accountDetails:CreateMembers' :: NonEmpty AccountDetail
accountDetails =
        Tagged (NonEmpty AccountDetail) (Identity (NonEmpty AccountDetail))
-> Tagged
     (NonEmpty AccountDetail) (Identity (NonEmpty AccountDetail))
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced (Tagged
   (NonEmpty AccountDetail) (Identity (NonEmpty AccountDetail))
 -> Tagged
      (NonEmpty AccountDetail) (Identity (NonEmpty AccountDetail)))
-> NonEmpty AccountDetail -> NonEmpty AccountDetail
forall t b. AReview t b -> b -> t
Lens.# NonEmpty AccountDetail
pAccountDetails_
    }

-- | The unique ID of the detector of the GuardDuty account that you want to
-- associate member accounts with.
createMembers_detectorId :: Lens.Lens' CreateMembers Prelude.Text
createMembers_detectorId :: (Text -> f Text) -> CreateMembers -> f CreateMembers
createMembers_detectorId = (CreateMembers -> Text)
-> (CreateMembers -> Text -> CreateMembers)
-> Lens CreateMembers CreateMembers Text Text
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateMembers' {Text
detectorId :: Text
$sel:detectorId:CreateMembers' :: CreateMembers -> Text
detectorId} -> Text
detectorId) (\s :: CreateMembers
s@CreateMembers' {} Text
a -> CreateMembers
s {$sel:detectorId:CreateMembers' :: Text
detectorId = Text
a} :: CreateMembers)

-- | A list of account ID and email address pairs of the accounts that you
-- want to associate with the GuardDuty administrator account.
createMembers_accountDetails :: Lens.Lens' CreateMembers (Prelude.NonEmpty AccountDetail)
createMembers_accountDetails :: (NonEmpty AccountDetail -> f (NonEmpty AccountDetail))
-> CreateMembers -> f CreateMembers
createMembers_accountDetails = (CreateMembers -> NonEmpty AccountDetail)
-> (CreateMembers -> NonEmpty AccountDetail -> CreateMembers)
-> Lens
     CreateMembers
     CreateMembers
     (NonEmpty AccountDetail)
     (NonEmpty AccountDetail)
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateMembers' {NonEmpty AccountDetail
accountDetails :: NonEmpty AccountDetail
$sel:accountDetails:CreateMembers' :: CreateMembers -> NonEmpty AccountDetail
accountDetails} -> NonEmpty AccountDetail
accountDetails) (\s :: CreateMembers
s@CreateMembers' {} NonEmpty AccountDetail
a -> CreateMembers
s {$sel:accountDetails:CreateMembers' :: NonEmpty AccountDetail
accountDetails = NonEmpty AccountDetail
a} :: CreateMembers) ((NonEmpty AccountDetail -> f (NonEmpty AccountDetail))
 -> CreateMembers -> f CreateMembers)
-> ((NonEmpty AccountDetail -> f (NonEmpty AccountDetail))
    -> NonEmpty AccountDetail -> f (NonEmpty AccountDetail))
-> (NonEmpty AccountDetail -> f (NonEmpty AccountDetail))
-> CreateMembers
-> f CreateMembers
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. (NonEmpty AccountDetail -> f (NonEmpty AccountDetail))
-> NonEmpty AccountDetail -> f (NonEmpty AccountDetail)
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Core.AWSRequest CreateMembers where
  type
    AWSResponse CreateMembers =
      CreateMembersResponse
  request :: CreateMembers -> Request CreateMembers
request = Service -> CreateMembers -> Request CreateMembers
forall a. (ToRequest a, ToJSON a) => Service -> a -> Request a
Request.postJSON Service
defaultService
  response :: Logger
-> Service
-> Proxy CreateMembers
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse CreateMembers)))
response =
    (Int
 -> ResponseHeaders
 -> Object
 -> Either String (AWSResponse CreateMembers))
-> Logger
-> Service
-> Proxy CreateMembers
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse CreateMembers)))
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 ->
          Int -> [UnprocessedAccount] -> CreateMembersResponse
CreateMembersResponse'
            (Int -> [UnprocessedAccount] -> CreateMembersResponse)
-> Either String Int
-> Either String ([UnprocessedAccount] -> CreateMembersResponse)
forall (f :: * -> *) a b. Functor 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))
            Either String ([UnprocessedAccount] -> CreateMembersResponse)
-> Either String [UnprocessedAccount]
-> Either String CreateMembersResponse
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> ( Object
x Object -> Text -> Either String (Maybe [UnprocessedAccount])
forall a. FromJSON a => Object -> Text -> Either String (Maybe a)
Core..?> Text
"unprocessedAccounts"
                            Either String (Maybe [UnprocessedAccount])
-> [UnprocessedAccount] -> Either String [UnprocessedAccount]
forall (f :: * -> *) a. Functor f => f (Maybe a) -> a -> f a
Core..!@ [UnprocessedAccount]
forall a. Monoid a => a
Prelude.mempty
                        )
      )

instance Prelude.Hashable CreateMembers

instance Prelude.NFData CreateMembers

instance Core.ToHeaders CreateMembers where
  toHeaders :: CreateMembers -> ResponseHeaders
toHeaders =
    ResponseHeaders -> CreateMembers -> ResponseHeaders
forall a b. a -> b -> a
Prelude.const
      ( [ResponseHeaders] -> ResponseHeaders
forall a. Monoid a => [a] -> a
Prelude.mconcat
          [ HeaderName
"Content-Type"
              HeaderName -> ByteString -> ResponseHeaders
forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Core.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Core.ToJSON CreateMembers where
  toJSON :: CreateMembers -> Value
toJSON CreateMembers' {NonEmpty AccountDetail
Text
accountDetails :: NonEmpty AccountDetail
detectorId :: Text
$sel:accountDetails:CreateMembers' :: CreateMembers -> NonEmpty AccountDetail
$sel:detectorId:CreateMembers' :: CreateMembers -> 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
"accountDetails" Text -> NonEmpty AccountDetail -> Pair
forall kv v. (KeyValue kv, ToJSON v) => Text -> v -> kv
Core..= NonEmpty AccountDetail
accountDetails)
          ]
      )

instance Core.ToPath CreateMembers where
  toPath :: CreateMembers -> ByteString
toPath CreateMembers' {NonEmpty AccountDetail
Text
accountDetails :: NonEmpty AccountDetail
detectorId :: Text
$sel:accountDetails:CreateMembers' :: CreateMembers -> NonEmpty AccountDetail
$sel:detectorId:CreateMembers' :: CreateMembers -> Text
..} =
    [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
Prelude.mconcat
      [ByteString
"/detector/", Text -> ByteString
forall a. ToByteString a => a -> ByteString
Core.toBS Text
detectorId, ByteString
"/member"]

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

-- | /See:/ 'newCreateMembersResponse' smart constructor.
data CreateMembersResponse = CreateMembersResponse'
  { -- | The response's http status code.
    CreateMembersResponse -> Int
httpStatus :: Prelude.Int,
    -- | A list of objects that include the @accountIds@ of the unprocessed
    -- accounts and a result string that explains why each was unprocessed.
    CreateMembersResponse -> [UnprocessedAccount]
unprocessedAccounts :: [UnprocessedAccount]
  }
  deriving (CreateMembersResponse -> CreateMembersResponse -> Bool
(CreateMembersResponse -> CreateMembersResponse -> Bool)
-> (CreateMembersResponse -> CreateMembersResponse -> Bool)
-> Eq CreateMembersResponse
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CreateMembersResponse -> CreateMembersResponse -> Bool
$c/= :: CreateMembersResponse -> CreateMembersResponse -> Bool
== :: CreateMembersResponse -> CreateMembersResponse -> Bool
$c== :: CreateMembersResponse -> CreateMembersResponse -> Bool
Prelude.Eq, ReadPrec [CreateMembersResponse]
ReadPrec CreateMembersResponse
Int -> ReadS CreateMembersResponse
ReadS [CreateMembersResponse]
(Int -> ReadS CreateMembersResponse)
-> ReadS [CreateMembersResponse]
-> ReadPrec CreateMembersResponse
-> ReadPrec [CreateMembersResponse]
-> Read CreateMembersResponse
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CreateMembersResponse]
$creadListPrec :: ReadPrec [CreateMembersResponse]
readPrec :: ReadPrec CreateMembersResponse
$creadPrec :: ReadPrec CreateMembersResponse
readList :: ReadS [CreateMembersResponse]
$creadList :: ReadS [CreateMembersResponse]
readsPrec :: Int -> ReadS CreateMembersResponse
$creadsPrec :: Int -> ReadS CreateMembersResponse
Prelude.Read, Int -> CreateMembersResponse -> ShowS
[CreateMembersResponse] -> ShowS
CreateMembersResponse -> String
(Int -> CreateMembersResponse -> ShowS)
-> (CreateMembersResponse -> String)
-> ([CreateMembersResponse] -> ShowS)
-> Show CreateMembersResponse
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CreateMembersResponse] -> ShowS
$cshowList :: [CreateMembersResponse] -> ShowS
show :: CreateMembersResponse -> String
$cshow :: CreateMembersResponse -> String
showsPrec :: Int -> CreateMembersResponse -> ShowS
$cshowsPrec :: Int -> CreateMembersResponse -> ShowS
Prelude.Show, (forall x. CreateMembersResponse -> Rep CreateMembersResponse x)
-> (forall x. Rep CreateMembersResponse x -> CreateMembersResponse)
-> Generic CreateMembersResponse
forall x. Rep CreateMembersResponse x -> CreateMembersResponse
forall x. CreateMembersResponse -> Rep CreateMembersResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CreateMembersResponse x -> CreateMembersResponse
$cfrom :: forall x. CreateMembersResponse -> Rep CreateMembersResponse x
Prelude.Generic)

-- |
-- Create a value of 'CreateMembersResponse' 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:
--
-- 'httpStatus', 'createMembersResponse_httpStatus' - The response's http status code.
--
-- 'unprocessedAccounts', 'createMembersResponse_unprocessedAccounts' - A list of objects that include the @accountIds@ of the unprocessed
-- accounts and a result string that explains why each was unprocessed.
newCreateMembersResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  CreateMembersResponse
newCreateMembersResponse :: Int -> CreateMembersResponse
newCreateMembersResponse Int
pHttpStatus_ =
  CreateMembersResponse' :: Int -> [UnprocessedAccount] -> CreateMembersResponse
CreateMembersResponse'
    { $sel:httpStatus:CreateMembersResponse' :: Int
httpStatus = Int
pHttpStatus_,
      $sel:unprocessedAccounts:CreateMembersResponse' :: [UnprocessedAccount]
unprocessedAccounts = [UnprocessedAccount]
forall a. Monoid a => a
Prelude.mempty
    }

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

-- | A list of objects that include the @accountIds@ of the unprocessed
-- accounts and a result string that explains why each was unprocessed.
createMembersResponse_unprocessedAccounts :: Lens.Lens' CreateMembersResponse [UnprocessedAccount]
createMembersResponse_unprocessedAccounts :: ([UnprocessedAccount] -> f [UnprocessedAccount])
-> CreateMembersResponse -> f CreateMembersResponse
createMembersResponse_unprocessedAccounts = (CreateMembersResponse -> [UnprocessedAccount])
-> (CreateMembersResponse
    -> [UnprocessedAccount] -> CreateMembersResponse)
-> Lens
     CreateMembersResponse
     CreateMembersResponse
     [UnprocessedAccount]
     [UnprocessedAccount]
forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CreateMembersResponse' {[UnprocessedAccount]
unprocessedAccounts :: [UnprocessedAccount]
$sel:unprocessedAccounts:CreateMembersResponse' :: CreateMembersResponse -> [UnprocessedAccount]
unprocessedAccounts} -> [UnprocessedAccount]
unprocessedAccounts) (\s :: CreateMembersResponse
s@CreateMembersResponse' {} [UnprocessedAccount]
a -> CreateMembersResponse
s {$sel:unprocessedAccounts:CreateMembersResponse' :: [UnprocessedAccount]
unprocessedAccounts = [UnprocessedAccount]
a} :: CreateMembersResponse) (([UnprocessedAccount] -> f [UnprocessedAccount])
 -> CreateMembersResponse -> f CreateMembersResponse)
-> (([UnprocessedAccount] -> f [UnprocessedAccount])
    -> [UnprocessedAccount] -> f [UnprocessedAccount])
-> ([UnprocessedAccount] -> f [UnprocessedAccount])
-> CreateMembersResponse
-> f CreateMembersResponse
forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. ([UnprocessedAccount] -> f [UnprocessedAccount])
-> [UnprocessedAccount] -> f [UnprocessedAccount]
forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

instance Prelude.NFData CreateMembersResponse