{-# 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.Organizations.ListAccounts
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
--
-- Lists all the accounts in the organization. To request only the accounts
-- in a specified root or organizational unit (OU), use the
-- ListAccountsForParent operation instead.
--
-- Always check the @NextToken@ response parameter for a @null@ value when
-- calling a @List*@ operation. These operations can occasionally return an
-- empty set of results even when there are more results available. The
-- @NextToken@ response parameter value is @null@ /only/ when there are no
-- more results to display.
--
-- This operation can be called only from the organization\'s management
-- account or by a member account that is a delegated administrator for an
-- Amazon Web Services service.
--
-- This operation returns paginated results.
module Amazonka.Organizations.ListAccounts
  ( -- * Creating a Request
    ListAccounts (..),
    newListAccounts,

    -- * Request Lenses
    listAccounts_maxResults,
    listAccounts_nextToken,

    -- * Destructuring the Response
    ListAccountsResponse (..),
    newListAccountsResponse,

    -- * Response Lenses
    listAccountsResponse_accounts,
    listAccountsResponse_nextToken,
    listAccountsResponse_httpStatus,
  )
where

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

-- | /See:/ 'newListAccounts' smart constructor.
data ListAccounts = ListAccounts'
  { -- | The total number of results that you want included on each page of the
    -- response. If you do not include this parameter, it defaults to a value
    -- that is specific to the operation. If additional items exist beyond the
    -- maximum you specify, the @NextToken@ response element is present and has
    -- a value (is not null). Include that value as the @NextToken@ request
    -- parameter in the next call to the operation to get the next part of the
    -- results. Note that Organizations might return fewer results than the
    -- maximum even when there are more results available. You should check
    -- @NextToken@ after every operation to ensure that you receive all of the
    -- results.
    ListAccounts -> Maybe Natural
maxResults :: Prelude.Maybe Prelude.Natural,
    -- | The parameter for receiving additional results if you receive a
    -- @NextToken@ response in a previous request. A @NextToken@ response
    -- indicates that more output is available. Set this parameter to the value
    -- of the previous call\'s @NextToken@ response to indicate where the
    -- output should continue from.
    ListAccounts -> Maybe Text
nextToken :: Prelude.Maybe Prelude.Text
  }
  deriving (ListAccounts -> ListAccounts -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ListAccounts -> ListAccounts -> Bool
$c/= :: ListAccounts -> ListAccounts -> Bool
== :: ListAccounts -> ListAccounts -> Bool
$c== :: ListAccounts -> ListAccounts -> Bool
Prelude.Eq, ReadPrec [ListAccounts]
ReadPrec ListAccounts
Int -> ReadS ListAccounts
ReadS [ListAccounts]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [ListAccounts]
$creadListPrec :: ReadPrec [ListAccounts]
readPrec :: ReadPrec ListAccounts
$creadPrec :: ReadPrec ListAccounts
readList :: ReadS [ListAccounts]
$creadList :: ReadS [ListAccounts]
readsPrec :: Int -> ReadS ListAccounts
$creadsPrec :: Int -> ReadS ListAccounts
Prelude.Read, Int -> ListAccounts -> ShowS
[ListAccounts] -> ShowS
ListAccounts -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ListAccounts] -> ShowS
$cshowList :: [ListAccounts] -> ShowS
show :: ListAccounts -> String
$cshow :: ListAccounts -> String
showsPrec :: Int -> ListAccounts -> ShowS
$cshowsPrec :: Int -> ListAccounts -> ShowS
Prelude.Show, forall x. Rep ListAccounts x -> ListAccounts
forall x. ListAccounts -> Rep ListAccounts x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ListAccounts x -> ListAccounts
$cfrom :: forall x. ListAccounts -> Rep ListAccounts x
Prelude.Generic)

-- |
-- Create a value of 'ListAccounts' 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:
--
-- 'maxResults', 'listAccounts_maxResults' - The total number of results that you want included on each page of the
-- response. If you do not include this parameter, it defaults to a value
-- that is specific to the operation. If additional items exist beyond the
-- maximum you specify, the @NextToken@ response element is present and has
-- a value (is not null). Include that value as the @NextToken@ request
-- parameter in the next call to the operation to get the next part of the
-- results. Note that Organizations might return fewer results than the
-- maximum even when there are more results available. You should check
-- @NextToken@ after every operation to ensure that you receive all of the
-- results.
--
-- 'nextToken', 'listAccounts_nextToken' - The parameter for receiving additional results if you receive a
-- @NextToken@ response in a previous request. A @NextToken@ response
-- indicates that more output is available. Set this parameter to the value
-- of the previous call\'s @NextToken@ response to indicate where the
-- output should continue from.
newListAccounts ::
  ListAccounts
newListAccounts :: ListAccounts
newListAccounts =
  ListAccounts'
    { $sel:maxResults:ListAccounts' :: Maybe Natural
maxResults = forall a. Maybe a
Prelude.Nothing,
      $sel:nextToken:ListAccounts' :: Maybe Text
nextToken = forall a. Maybe a
Prelude.Nothing
    }

-- | The total number of results that you want included on each page of the
-- response. If you do not include this parameter, it defaults to a value
-- that is specific to the operation. If additional items exist beyond the
-- maximum you specify, the @NextToken@ response element is present and has
-- a value (is not null). Include that value as the @NextToken@ request
-- parameter in the next call to the operation to get the next part of the
-- results. Note that Organizations might return fewer results than the
-- maximum even when there are more results available. You should check
-- @NextToken@ after every operation to ensure that you receive all of the
-- results.
listAccounts_maxResults :: Lens.Lens' ListAccounts (Prelude.Maybe Prelude.Natural)
listAccounts_maxResults :: Lens' ListAccounts (Maybe Natural)
listAccounts_maxResults = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ListAccounts' {Maybe Natural
maxResults :: Maybe Natural
$sel:maxResults:ListAccounts' :: ListAccounts -> Maybe Natural
maxResults} -> Maybe Natural
maxResults) (\s :: ListAccounts
s@ListAccounts' {} Maybe Natural
a -> ListAccounts
s {$sel:maxResults:ListAccounts' :: Maybe Natural
maxResults = Maybe Natural
a} :: ListAccounts)

-- | The parameter for receiving additional results if you receive a
-- @NextToken@ response in a previous request. A @NextToken@ response
-- indicates that more output is available. Set this parameter to the value
-- of the previous call\'s @NextToken@ response to indicate where the
-- output should continue from.
listAccounts_nextToken :: Lens.Lens' ListAccounts (Prelude.Maybe Prelude.Text)
listAccounts_nextToken :: Lens' ListAccounts (Maybe Text)
listAccounts_nextToken = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ListAccounts' {Maybe Text
nextToken :: Maybe Text
$sel:nextToken:ListAccounts' :: ListAccounts -> Maybe Text
nextToken} -> Maybe Text
nextToken) (\s :: ListAccounts
s@ListAccounts' {} Maybe Text
a -> ListAccounts
s {$sel:nextToken:ListAccounts' :: Maybe Text
nextToken = Maybe Text
a} :: ListAccounts)

instance Core.AWSPager ListAccounts where
  page :: ListAccounts -> AWSResponse ListAccounts -> Maybe ListAccounts
page ListAccounts
rq AWSResponse ListAccounts
rs
    | forall a. AWSTruncated a => a -> Bool
Core.stop
        ( AWSResponse ListAccounts
rs
            forall s a. s -> Getting (First a) s a -> Maybe a
Lens.^? Lens' ListAccountsResponse (Maybe Text)
listAccountsResponse_nextToken
            forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall a b. Prism (Maybe a) (Maybe b) a b
Lens._Just
        ) =
        forall a. Maybe a
Prelude.Nothing
    | forall a. AWSTruncated a => a -> Bool
Core.stop
        ( AWSResponse ListAccounts
rs
            forall s a. s -> Getting (First a) s a -> Maybe a
Lens.^? Lens' ListAccountsResponse (Maybe [Account])
listAccountsResponse_accounts
            forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall a b. Prism (Maybe a) (Maybe b) a b
Lens._Just
        ) =
        forall a. Maybe a
Prelude.Nothing
    | Bool
Prelude.otherwise =
        forall a. a -> Maybe a
Prelude.Just
          forall a b. (a -> b) -> a -> b
Prelude.$ ListAccounts
rq
          forall a b. a -> (a -> b) -> b
Prelude.& Lens' ListAccounts (Maybe Text)
listAccounts_nextToken
          forall s t a b. ASetter s t a b -> b -> s -> t
Lens..~ AWSResponse ListAccounts
rs
          forall s a. s -> Getting (First a) s a -> Maybe a
Lens.^? Lens' ListAccountsResponse (Maybe Text)
listAccountsResponse_nextToken
          forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall a b. Prism (Maybe a) (Maybe b) a b
Lens._Just

instance Core.AWSRequest ListAccounts where
  type AWSResponse ListAccounts = ListAccountsResponse
  request :: (Service -> Service) -> ListAccounts -> Request ListAccounts
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 ListAccounts
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse ListAccounts)))
response =
    forall (m :: * -> *) a.
MonadResource m =>
(Int -> ResponseHeaders -> Object -> Either String (AWSResponse a))
-> (ByteStringLazy -> IO ByteStringLazy)
-> Service
-> Proxy a
-> ClientResponse ClientBody
-> m (Either Error (ClientResponse (AWSResponse a)))
Response.receiveJSON
      ( \Int
s ResponseHeaders
h Object
x ->
          Maybe [Account] -> Maybe Text -> Int -> ListAccountsResponse
ListAccountsResponse'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"Accounts" forall (f :: * -> *) a. Functor f => f (Maybe a) -> a -> f a
Core..!@ forall a. Monoid a => a
Prelude.mempty)
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Either String (Maybe a)
Data..?> Key
"NextToken")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (forall (f :: * -> *) a. Applicative f => a -> f a
Prelude.pure (forall a. Enum a => a -> Int
Prelude.fromEnum Int
s))
      )

instance Prelude.Hashable ListAccounts where
  hashWithSalt :: Int -> ListAccounts -> Int
hashWithSalt Int
_salt ListAccounts' {Maybe Natural
Maybe Text
nextToken :: Maybe Text
maxResults :: Maybe Natural
$sel:nextToken:ListAccounts' :: ListAccounts -> Maybe Text
$sel:maxResults:ListAccounts' :: ListAccounts -> Maybe Natural
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Natural
maxResults
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
nextToken

instance Prelude.NFData ListAccounts where
  rnf :: ListAccounts -> ()
rnf ListAccounts' {Maybe Natural
Maybe Text
nextToken :: Maybe Text
maxResults :: Maybe Natural
$sel:nextToken:ListAccounts' :: ListAccounts -> Maybe Text
$sel:maxResults:ListAccounts' :: ListAccounts -> Maybe Natural
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Natural
maxResults
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
nextToken

instance Data.ToHeaders ListAccounts where
  toHeaders :: ListAccounts -> ResponseHeaders
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 -> ResponseHeaders
Data.=# ( ByteString
"AWSOrganizationsV20161128.ListAccounts" ::
                          Prelude.ByteString
                      ),
            HeaderName
"Content-Type"
              forall a. ToHeader a => HeaderName -> a -> ResponseHeaders
Data.=# ( ByteString
"application/x-amz-json-1.1" ::
                          Prelude.ByteString
                      )
          ]
      )

instance Data.ToJSON ListAccounts where
  toJSON :: ListAccounts -> Value
toJSON ListAccounts' {Maybe Natural
Maybe Text
nextToken :: Maybe Text
maxResults :: Maybe Natural
$sel:nextToken:ListAccounts' :: ListAccounts -> Maybe Text
$sel:maxResults:ListAccounts' :: ListAccounts -> Maybe Natural
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"MaxResults" 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
maxResults,
            (Key
"NextToken" 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
nextToken
          ]
      )

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

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

-- | /See:/ 'newListAccountsResponse' smart constructor.
data ListAccountsResponse = ListAccountsResponse'
  { -- | A list of objects in the organization.
    ListAccountsResponse -> Maybe [Account]
accounts :: Prelude.Maybe [Account],
    -- | If present, indicates that more output is available than is included in
    -- the current response. Use this value in the @NextToken@ request
    -- parameter in a subsequent call to the operation to get the next part of
    -- the output. You should repeat this until the @NextToken@ response
    -- element comes back as @null@.
    ListAccountsResponse -> Maybe Text
nextToken :: Prelude.Maybe Prelude.Text,
    -- | The response's http status code.
    ListAccountsResponse -> Int
httpStatus :: Prelude.Int
  }
  deriving (ListAccountsResponse -> ListAccountsResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ListAccountsResponse -> ListAccountsResponse -> Bool
$c/= :: ListAccountsResponse -> ListAccountsResponse -> Bool
== :: ListAccountsResponse -> ListAccountsResponse -> Bool
$c== :: ListAccountsResponse -> ListAccountsResponse -> Bool
Prelude.Eq, Int -> ListAccountsResponse -> ShowS
[ListAccountsResponse] -> ShowS
ListAccountsResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ListAccountsResponse] -> ShowS
$cshowList :: [ListAccountsResponse] -> ShowS
show :: ListAccountsResponse -> String
$cshow :: ListAccountsResponse -> String
showsPrec :: Int -> ListAccountsResponse -> ShowS
$cshowsPrec :: Int -> ListAccountsResponse -> ShowS
Prelude.Show, forall x. Rep ListAccountsResponse x -> ListAccountsResponse
forall x. ListAccountsResponse -> Rep ListAccountsResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ListAccountsResponse x -> ListAccountsResponse
$cfrom :: forall x. ListAccountsResponse -> Rep ListAccountsResponse x
Prelude.Generic)

-- |
-- Create a value of 'ListAccountsResponse' 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:
--
-- 'accounts', 'listAccountsResponse_accounts' - A list of objects in the organization.
--
-- 'nextToken', 'listAccountsResponse_nextToken' - If present, indicates that more output is available than is included in
-- the current response. Use this value in the @NextToken@ request
-- parameter in a subsequent call to the operation to get the next part of
-- the output. You should repeat this until the @NextToken@ response
-- element comes back as @null@.
--
-- 'httpStatus', 'listAccountsResponse_httpStatus' - The response's http status code.
newListAccountsResponse ::
  -- | 'httpStatus'
  Prelude.Int ->
  ListAccountsResponse
newListAccountsResponse :: Int -> ListAccountsResponse
newListAccountsResponse Int
pHttpStatus_ =
  ListAccountsResponse'
    { $sel:accounts:ListAccountsResponse' :: Maybe [Account]
accounts = forall a. Maybe a
Prelude.Nothing,
      $sel:nextToken:ListAccountsResponse' :: Maybe Text
nextToken = forall a. Maybe a
Prelude.Nothing,
      $sel:httpStatus:ListAccountsResponse' :: Int
httpStatus = Int
pHttpStatus_
    }

-- | A list of objects in the organization.
listAccountsResponse_accounts :: Lens.Lens' ListAccountsResponse (Prelude.Maybe [Account])
listAccountsResponse_accounts :: Lens' ListAccountsResponse (Maybe [Account])
listAccountsResponse_accounts = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ListAccountsResponse' {Maybe [Account]
accounts :: Maybe [Account]
$sel:accounts:ListAccountsResponse' :: ListAccountsResponse -> Maybe [Account]
accounts} -> Maybe [Account]
accounts) (\s :: ListAccountsResponse
s@ListAccountsResponse' {} Maybe [Account]
a -> ListAccountsResponse
s {$sel:accounts:ListAccountsResponse' :: Maybe [Account]
accounts = Maybe [Account]
a} :: ListAccountsResponse) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | If present, indicates that more output is available than is included in
-- the current response. Use this value in the @NextToken@ request
-- parameter in a subsequent call to the operation to get the next part of
-- the output. You should repeat this until the @NextToken@ response
-- element comes back as @null@.
listAccountsResponse_nextToken :: Lens.Lens' ListAccountsResponse (Prelude.Maybe Prelude.Text)
listAccountsResponse_nextToken :: Lens' ListAccountsResponse (Maybe Text)
listAccountsResponse_nextToken = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\ListAccountsResponse' {Maybe Text
nextToken :: Maybe Text
$sel:nextToken:ListAccountsResponse' :: ListAccountsResponse -> Maybe Text
nextToken} -> Maybe Text
nextToken) (\s :: ListAccountsResponse
s@ListAccountsResponse' {} Maybe Text
a -> ListAccountsResponse
s {$sel:nextToken:ListAccountsResponse' :: Maybe Text
nextToken = Maybe Text
a} :: ListAccountsResponse)

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

instance Prelude.NFData ListAccountsResponse where
  rnf :: ListAccountsResponse -> ()
rnf ListAccountsResponse' {Int
Maybe [Account]
Maybe Text
httpStatus :: Int
nextToken :: Maybe Text
accounts :: Maybe [Account]
$sel:httpStatus:ListAccountsResponse' :: ListAccountsResponse -> Int
$sel:nextToken:ListAccountsResponse' :: ListAccountsResponse -> Maybe Text
$sel:accounts:ListAccountsResponse' :: ListAccountsResponse -> Maybe [Account]
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe [Account]
accounts
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
nextToken
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Int
httpStatus