1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package organizations
4
5import (
6	"context"
7	"fmt"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
10	"github.com/aws/aws-sdk-go-v2/service/organizations/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13)
14
15// Lists all the accounts in the organization. To request only the accounts in a
16// specified root or organizational unit (OU), use the ListAccountsForParent
17// operation instead. Always check the NextToken response parameter for a null
18// value when calling a List* operation. These operations can occasionally return
19// an empty set of results even when there are more results available. The
20// NextToken response parameter value is null only when there are no more results
21// to display. This operation can be called only from the organization's management
22// account or by a member account that is a delegated administrator for an AWS
23// service.
24func (c *Client) ListAccounts(ctx context.Context, params *ListAccountsInput, optFns ...func(*Options)) (*ListAccountsOutput, error) {
25	if params == nil {
26		params = &ListAccountsInput{}
27	}
28
29	result, metadata, err := c.invokeOperation(ctx, "ListAccounts", params, optFns, addOperationListAccountsMiddlewares)
30	if err != nil {
31		return nil, err
32	}
33
34	out := result.(*ListAccountsOutput)
35	out.ResultMetadata = metadata
36	return out, nil
37}
38
39type ListAccountsInput struct {
40
41	// The total number of results that you want included on each page of the response.
42	// If you do not include this parameter, it defaults to a value that is specific to
43	// the operation. If additional items exist beyond the maximum you specify, the
44	// NextToken response element is present and has a value (is not null). Include
45	// that value as the NextToken request parameter in the next call to the operation
46	// to get the next part of the results. Note that Organizations might return fewer
47	// results than the maximum even when there are more results available. You should
48	// check NextToken after every operation to ensure that you receive all of the
49	// results.
50	MaxResults *int32
51
52	// The parameter for receiving additional results if you receive a NextToken
53	// response in a previous request. A NextToken response indicates that more output
54	// is available. Set this parameter to the value of the previous call's NextToken
55	// response to indicate where the output should continue from.
56	NextToken *string
57}
58
59type ListAccountsOutput struct {
60
61	// A list of objects in the organization.
62	Accounts []types.Account
63
64	// If present, indicates that more output is available than is included in the
65	// current response. Use this value in the NextToken request parameter in a
66	// subsequent call to the operation to get the next part of the output. You should
67	// repeat this until the NextToken response element comes back as null.
68	NextToken *string
69
70	// Metadata pertaining to the operation's result.
71	ResultMetadata middleware.Metadata
72}
73
74func addOperationListAccountsMiddlewares(stack *middleware.Stack, options Options) (err error) {
75	err = stack.Serialize.Add(&awsAwsjson11_serializeOpListAccounts{}, middleware.After)
76	if err != nil {
77		return err
78	}
79	err = stack.Deserialize.Add(&awsAwsjson11_deserializeOpListAccounts{}, middleware.After)
80	if err != nil {
81		return err
82	}
83	if err = addSetLoggerMiddleware(stack, options); err != nil {
84		return err
85	}
86	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
87		return err
88	}
89	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
90		return err
91	}
92	if err = addResolveEndpointMiddleware(stack, options); err != nil {
93		return err
94	}
95	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
96		return err
97	}
98	if err = addRetryMiddlewares(stack, options); err != nil {
99		return err
100	}
101	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
102		return err
103	}
104	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
105		return err
106	}
107	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
108		return err
109	}
110	if err = addClientUserAgent(stack); err != nil {
111		return err
112	}
113	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
114		return err
115	}
116	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
117		return err
118	}
119	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opListAccounts(options.Region), middleware.Before); err != nil {
120		return err
121	}
122	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
123		return err
124	}
125	if err = addResponseErrorMiddleware(stack); err != nil {
126		return err
127	}
128	if err = addRequestResponseLogging(stack, options); err != nil {
129		return err
130	}
131	return nil
132}
133
134// ListAccountsAPIClient is a client that implements the ListAccounts operation.
135type ListAccountsAPIClient interface {
136	ListAccounts(context.Context, *ListAccountsInput, ...func(*Options)) (*ListAccountsOutput, error)
137}
138
139var _ ListAccountsAPIClient = (*Client)(nil)
140
141// ListAccountsPaginatorOptions is the paginator options for ListAccounts
142type ListAccountsPaginatorOptions struct {
143	// The total number of results that you want included on each page of the response.
144	// If you do not include this parameter, it defaults to a value that is specific to
145	// the operation. If additional items exist beyond the maximum you specify, the
146	// NextToken response element is present and has a value (is not null). Include
147	// that value as the NextToken request parameter in the next call to the operation
148	// to get the next part of the results. Note that Organizations might return fewer
149	// results than the maximum even when there are more results available. You should
150	// check NextToken after every operation to ensure that you receive all of the
151	// results.
152	Limit int32
153
154	// Set to true if pagination should stop if the service returns a pagination token
155	// that matches the most recent token provided to the service.
156	StopOnDuplicateToken bool
157}
158
159// ListAccountsPaginator is a paginator for ListAccounts
160type ListAccountsPaginator struct {
161	options   ListAccountsPaginatorOptions
162	client    ListAccountsAPIClient
163	params    *ListAccountsInput
164	nextToken *string
165	firstPage bool
166}
167
168// NewListAccountsPaginator returns a new ListAccountsPaginator
169func NewListAccountsPaginator(client ListAccountsAPIClient, params *ListAccountsInput, optFns ...func(*ListAccountsPaginatorOptions)) *ListAccountsPaginator {
170	if params == nil {
171		params = &ListAccountsInput{}
172	}
173
174	options := ListAccountsPaginatorOptions{}
175	if params.MaxResults != nil {
176		options.Limit = *params.MaxResults
177	}
178
179	for _, fn := range optFns {
180		fn(&options)
181	}
182
183	return &ListAccountsPaginator{
184		options:   options,
185		client:    client,
186		params:    params,
187		firstPage: true,
188	}
189}
190
191// HasMorePages returns a boolean indicating whether more pages are available
192func (p *ListAccountsPaginator) HasMorePages() bool {
193	return p.firstPage || p.nextToken != nil
194}
195
196// NextPage retrieves the next ListAccounts page.
197func (p *ListAccountsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListAccountsOutput, error) {
198	if !p.HasMorePages() {
199		return nil, fmt.Errorf("no more pages available")
200	}
201
202	params := *p.params
203	params.NextToken = p.nextToken
204
205	var limit *int32
206	if p.options.Limit > 0 {
207		limit = &p.options.Limit
208	}
209	params.MaxResults = limit
210
211	result, err := p.client.ListAccounts(ctx, &params, optFns...)
212	if err != nil {
213		return nil, err
214	}
215	p.firstPage = false
216
217	prevToken := p.nextToken
218	p.nextToken = result.NextToken
219
220	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
221		p.nextToken = nil
222	}
223
224	return result, nil
225}
226
227func newServiceMetadataMiddleware_opListAccounts(region string) *awsmiddleware.RegisterServiceMetadata {
228	return &awsmiddleware.RegisterServiceMetadata{
229		Region:        region,
230		ServiceID:     ServiceID,
231		SigningName:   "organizations",
232		OperationName: "ListAccounts",
233	}
234}
235