1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package iam
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/iam/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13)
14
15// Lists all IAM users, groups, and roles that the specified managed policy is
16// attached to. You can use the optional EntityFilter parameter to limit the
17// results to a particular type of entity (users, groups, or roles). For example,
18// to list only the roles that are attached to the specified policy, set
19// EntityFilter to Role. You can paginate the results using the MaxItems and Marker
20// parameters.
21func (c *Client) ListEntitiesForPolicy(ctx context.Context, params *ListEntitiesForPolicyInput, optFns ...func(*Options)) (*ListEntitiesForPolicyOutput, error) {
22	if params == nil {
23		params = &ListEntitiesForPolicyInput{}
24	}
25
26	result, metadata, err := c.invokeOperation(ctx, "ListEntitiesForPolicy", params, optFns, addOperationListEntitiesForPolicyMiddlewares)
27	if err != nil {
28		return nil, err
29	}
30
31	out := result.(*ListEntitiesForPolicyOutput)
32	out.ResultMetadata = metadata
33	return out, nil
34}
35
36type ListEntitiesForPolicyInput struct {
37
38	// The Amazon Resource Name (ARN) of the IAM policy for which you want the
39	// versions. For more information about ARNs, see Amazon Resource Names (ARNs)
40	// (https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html) in
41	// the AWS General Reference.
42	//
43	// This member is required.
44	PolicyArn *string
45
46	// The entity type to use for filtering the results. For example, when EntityFilter
47	// is Role, only the roles that are attached to the specified policy are returned.
48	// This parameter is optional. If it is not included, all attached entities (users,
49	// groups, and roles) are returned. The argument for this parameter must be one of
50	// the valid values listed below.
51	EntityFilter types.EntityType
52
53	// Use this parameter only when paginating results and only after you receive a
54	// response indicating that the results are truncated. Set it to the value of the
55	// Marker element in the response that you received to indicate where the next call
56	// should start.
57	Marker *string
58
59	// Use this only when paginating results to indicate the maximum number of items
60	// you want in the response. If additional items exist beyond the maximum you
61	// specify, the IsTruncated response element is true. If you do not include this
62	// parameter, the number of items defaults to 100. Note that IAM might return fewer
63	// results, even when there are more results available. In that case, the
64	// IsTruncated response element returns true, and Marker contains a value to
65	// include in the subsequent call that tells the service where to continue from.
66	MaxItems *int32
67
68	// The path prefix for filtering the results. This parameter is optional. If it is
69	// not included, it defaults to a slash (/), listing all entities. This parameter
70	// allows (through its regex pattern (http://wikipedia.org/wiki/regex)) a string of
71	// characters consisting of either a forward slash (/) by itself or a string that
72	// must begin and end with forward slashes. In addition, it can contain any ASCII
73	// character from the ! (\u0021) through the DEL character (\u007F), including most
74	// punctuation characters, digits, and upper and lowercased letters.
75	PathPrefix *string
76
77	// The policy usage method to use for filtering the results. To list only
78	// permissions policies, set PolicyUsageFilter to PermissionsPolicy. To list only
79	// the policies used to set permissions boundaries, set the value to
80	// PermissionsBoundary. This parameter is optional. If it is not included, all
81	// policies are returned.
82	PolicyUsageFilter types.PolicyUsageType
83}
84
85// Contains the response to a successful ListEntitiesForPolicy request.
86type ListEntitiesForPolicyOutput struct {
87
88	// A flag that indicates whether there are more items to return. If your results
89	// were truncated, you can make a subsequent pagination request using the Marker
90	// request parameter to retrieve more items. Note that IAM might return fewer than
91	// the MaxItems number of results even when there are more results available. We
92	// recommend that you check IsTruncated after every call to ensure that you receive
93	// all your results.
94	IsTruncated bool
95
96	// When IsTruncated is true, this element is present and contains the value to use
97	// for the Marker parameter in a subsequent pagination request.
98	Marker *string
99
100	// A list of IAM groups that the policy is attached to.
101	PolicyGroups []types.PolicyGroup
102
103	// A list of IAM roles that the policy is attached to.
104	PolicyRoles []types.PolicyRole
105
106	// A list of IAM users that the policy is attached to.
107	PolicyUsers []types.PolicyUser
108
109	// Metadata pertaining to the operation's result.
110	ResultMetadata middleware.Metadata
111}
112
113func addOperationListEntitiesForPolicyMiddlewares(stack *middleware.Stack, options Options) (err error) {
114	err = stack.Serialize.Add(&awsAwsquery_serializeOpListEntitiesForPolicy{}, middleware.After)
115	if err != nil {
116		return err
117	}
118	err = stack.Deserialize.Add(&awsAwsquery_deserializeOpListEntitiesForPolicy{}, middleware.After)
119	if err != nil {
120		return err
121	}
122	if err = addSetLoggerMiddleware(stack, options); err != nil {
123		return err
124	}
125	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
126		return err
127	}
128	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
129		return err
130	}
131	if err = addResolveEndpointMiddleware(stack, options); err != nil {
132		return err
133	}
134	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
135		return err
136	}
137	if err = addRetryMiddlewares(stack, options); err != nil {
138		return err
139	}
140	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
141		return err
142	}
143	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
144		return err
145	}
146	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
147		return err
148	}
149	if err = addClientUserAgent(stack); err != nil {
150		return err
151	}
152	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
153		return err
154	}
155	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
156		return err
157	}
158	if err = addOpListEntitiesForPolicyValidationMiddleware(stack); err != nil {
159		return err
160	}
161	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opListEntitiesForPolicy(options.Region), middleware.Before); err != nil {
162		return err
163	}
164	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
165		return err
166	}
167	if err = addResponseErrorMiddleware(stack); err != nil {
168		return err
169	}
170	if err = addRequestResponseLogging(stack, options); err != nil {
171		return err
172	}
173	return nil
174}
175
176// ListEntitiesForPolicyAPIClient is a client that implements the
177// ListEntitiesForPolicy operation.
178type ListEntitiesForPolicyAPIClient interface {
179	ListEntitiesForPolicy(context.Context, *ListEntitiesForPolicyInput, ...func(*Options)) (*ListEntitiesForPolicyOutput, error)
180}
181
182var _ ListEntitiesForPolicyAPIClient = (*Client)(nil)
183
184// ListEntitiesForPolicyPaginatorOptions is the paginator options for
185// ListEntitiesForPolicy
186type ListEntitiesForPolicyPaginatorOptions struct {
187	// Use this only when paginating results to indicate the maximum number of items
188	// you want in the response. If additional items exist beyond the maximum you
189	// specify, the IsTruncated response element is true. If you do not include this
190	// parameter, the number of items defaults to 100. Note that IAM might return fewer
191	// results, even when there are more results available. In that case, the
192	// IsTruncated response element returns true, and Marker contains a value to
193	// include in the subsequent call that tells the service where to continue from.
194	Limit int32
195
196	// Set to true if pagination should stop if the service returns a pagination token
197	// that matches the most recent token provided to the service.
198	StopOnDuplicateToken bool
199}
200
201// ListEntitiesForPolicyPaginator is a paginator for ListEntitiesForPolicy
202type ListEntitiesForPolicyPaginator struct {
203	options   ListEntitiesForPolicyPaginatorOptions
204	client    ListEntitiesForPolicyAPIClient
205	params    *ListEntitiesForPolicyInput
206	nextToken *string
207	firstPage bool
208}
209
210// NewListEntitiesForPolicyPaginator returns a new ListEntitiesForPolicyPaginator
211func NewListEntitiesForPolicyPaginator(client ListEntitiesForPolicyAPIClient, params *ListEntitiesForPolicyInput, optFns ...func(*ListEntitiesForPolicyPaginatorOptions)) *ListEntitiesForPolicyPaginator {
212	if params == nil {
213		params = &ListEntitiesForPolicyInput{}
214	}
215
216	options := ListEntitiesForPolicyPaginatorOptions{}
217	if params.MaxItems != nil {
218		options.Limit = *params.MaxItems
219	}
220
221	for _, fn := range optFns {
222		fn(&options)
223	}
224
225	return &ListEntitiesForPolicyPaginator{
226		options:   options,
227		client:    client,
228		params:    params,
229		firstPage: true,
230	}
231}
232
233// HasMorePages returns a boolean indicating whether more pages are available
234func (p *ListEntitiesForPolicyPaginator) HasMorePages() bool {
235	return p.firstPage || p.nextToken != nil
236}
237
238// NextPage retrieves the next ListEntitiesForPolicy page.
239func (p *ListEntitiesForPolicyPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListEntitiesForPolicyOutput, error) {
240	if !p.HasMorePages() {
241		return nil, fmt.Errorf("no more pages available")
242	}
243
244	params := *p.params
245	params.Marker = p.nextToken
246
247	var limit *int32
248	if p.options.Limit > 0 {
249		limit = &p.options.Limit
250	}
251	params.MaxItems = limit
252
253	result, err := p.client.ListEntitiesForPolicy(ctx, &params, optFns...)
254	if err != nil {
255		return nil, err
256	}
257	p.firstPage = false
258
259	prevToken := p.nextToken
260	p.nextToken = result.Marker
261
262	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
263		p.nextToken = nil
264	}
265
266	return result, nil
267}
268
269func newServiceMetadataMiddleware_opListEntitiesForPolicy(region string) *awsmiddleware.RegisterServiceMetadata {
270	return &awsmiddleware.RegisterServiceMetadata{
271		Region:        region,
272		ServiceID:     ServiceID,
273		SigningName:   "iam",
274		OperationName: "ListEntitiesForPolicy",
275	}
276}
277