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 information about the versions of the specified managed policy, including
16// the version that is currently set as the policy's default version. For more
17// information about managed policies, see Managed Policies and Inline Policies
18// (https://docs.aws.amazon.com/IAM/latest/UserGuide/policies-managed-vs-inline.html)
19// in the IAM User Guide.
20func (c *Client) ListPolicyVersions(ctx context.Context, params *ListPolicyVersionsInput, optFns ...func(*Options)) (*ListPolicyVersionsOutput, error) {
21	if params == nil {
22		params = &ListPolicyVersionsInput{}
23	}
24
25	result, metadata, err := c.invokeOperation(ctx, "ListPolicyVersions", params, optFns, addOperationListPolicyVersionsMiddlewares)
26	if err != nil {
27		return nil, err
28	}
29
30	out := result.(*ListPolicyVersionsOutput)
31	out.ResultMetadata = metadata
32	return out, nil
33}
34
35type ListPolicyVersionsInput struct {
36
37	// The Amazon Resource Name (ARN) of the IAM policy for which you want the
38	// versions. For more information about ARNs, see Amazon Resource Names (ARNs) and
39	// AWS Service Namespaces
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	// Use this parameter only when paginating results and only after you receive a
47	// response indicating that the results are truncated. Set it to the value of the
48	// Marker element in the response that you received to indicate where the next call
49	// should start.
50	Marker *string
51
52	// Use this only when paginating results to indicate the maximum number of items
53	// you want in the response. If additional items exist beyond the maximum you
54	// specify, the IsTruncated response element is true. If you do not include this
55	// parameter, the number of items defaults to 100. Note that IAM might return fewer
56	// results, even when there are more results available. In that case, the
57	// IsTruncated response element returns true, and Marker contains a value to
58	// include in the subsequent call that tells the service where to continue from.
59	MaxItems *int32
60}
61
62// Contains the response to a successful ListPolicyVersions request.
63type ListPolicyVersionsOutput struct {
64
65	// A flag that indicates whether there are more items to return. If your results
66	// were truncated, you can make a subsequent pagination request using the Marker
67	// request parameter to retrieve more items. Note that IAM might return fewer than
68	// the MaxItems number of results even when there are more results available. We
69	// recommend that you check IsTruncated after every call to ensure that you receive
70	// all your results.
71	IsTruncated bool
72
73	// When IsTruncated is true, this element is present and contains the value to use
74	// for the Marker parameter in a subsequent pagination request.
75	Marker *string
76
77	// A list of policy versions. For more information about managed policy versions,
78	// see Versioning for Managed Policies
79	// (https://docs.aws.amazon.com/IAM/latest/UserGuide/policies-managed-versions.html)
80	// in the IAM User Guide.
81	Versions []types.PolicyVersion
82
83	// Metadata pertaining to the operation's result.
84	ResultMetadata middleware.Metadata
85}
86
87func addOperationListPolicyVersionsMiddlewares(stack *middleware.Stack, options Options) (err error) {
88	err = stack.Serialize.Add(&awsAwsquery_serializeOpListPolicyVersions{}, middleware.After)
89	if err != nil {
90		return err
91	}
92	err = stack.Deserialize.Add(&awsAwsquery_deserializeOpListPolicyVersions{}, middleware.After)
93	if err != nil {
94		return err
95	}
96	if err = addSetLoggerMiddleware(stack, options); err != nil {
97		return err
98	}
99	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
100		return err
101	}
102	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
103		return err
104	}
105	if err = addResolveEndpointMiddleware(stack, options); err != nil {
106		return err
107	}
108	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
109		return err
110	}
111	if err = addRetryMiddlewares(stack, options); err != nil {
112		return err
113	}
114	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
115		return err
116	}
117	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
118		return err
119	}
120	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
121		return err
122	}
123	if err = addClientUserAgent(stack); err != nil {
124		return err
125	}
126	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
127		return err
128	}
129	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
130		return err
131	}
132	if err = addOpListPolicyVersionsValidationMiddleware(stack); err != nil {
133		return err
134	}
135	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opListPolicyVersions(options.Region), middleware.Before); err != nil {
136		return err
137	}
138	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
139		return err
140	}
141	if err = addResponseErrorMiddleware(stack); err != nil {
142		return err
143	}
144	if err = addRequestResponseLogging(stack, options); err != nil {
145		return err
146	}
147	return nil
148}
149
150// ListPolicyVersionsAPIClient is a client that implements the ListPolicyVersions
151// operation.
152type ListPolicyVersionsAPIClient interface {
153	ListPolicyVersions(context.Context, *ListPolicyVersionsInput, ...func(*Options)) (*ListPolicyVersionsOutput, error)
154}
155
156var _ ListPolicyVersionsAPIClient = (*Client)(nil)
157
158// ListPolicyVersionsPaginatorOptions is the paginator options for
159// ListPolicyVersions
160type ListPolicyVersionsPaginatorOptions struct {
161	// Use this only when paginating results to indicate the maximum number of items
162	// you want in the response. If additional items exist beyond the maximum you
163	// specify, the IsTruncated response element is true. If you do not include this
164	// parameter, the number of items defaults to 100. Note that IAM might return fewer
165	// results, even when there are more results available. In that case, the
166	// IsTruncated response element returns true, and Marker contains a value to
167	// include in the subsequent call that tells the service where to continue from.
168	Limit int32
169
170	// Set to true if pagination should stop if the service returns a pagination token
171	// that matches the most recent token provided to the service.
172	StopOnDuplicateToken bool
173}
174
175// ListPolicyVersionsPaginator is a paginator for ListPolicyVersions
176type ListPolicyVersionsPaginator struct {
177	options   ListPolicyVersionsPaginatorOptions
178	client    ListPolicyVersionsAPIClient
179	params    *ListPolicyVersionsInput
180	nextToken *string
181	firstPage bool
182}
183
184// NewListPolicyVersionsPaginator returns a new ListPolicyVersionsPaginator
185func NewListPolicyVersionsPaginator(client ListPolicyVersionsAPIClient, params *ListPolicyVersionsInput, optFns ...func(*ListPolicyVersionsPaginatorOptions)) *ListPolicyVersionsPaginator {
186	options := ListPolicyVersionsPaginatorOptions{}
187	if params.MaxItems != nil {
188		options.Limit = *params.MaxItems
189	}
190
191	for _, fn := range optFns {
192		fn(&options)
193	}
194
195	if params == nil {
196		params = &ListPolicyVersionsInput{}
197	}
198
199	return &ListPolicyVersionsPaginator{
200		options:   options,
201		client:    client,
202		params:    params,
203		firstPage: true,
204	}
205}
206
207// HasMorePages returns a boolean indicating whether more pages are available
208func (p *ListPolicyVersionsPaginator) HasMorePages() bool {
209	return p.firstPage || p.nextToken != nil
210}
211
212// NextPage retrieves the next ListPolicyVersions page.
213func (p *ListPolicyVersionsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListPolicyVersionsOutput, error) {
214	if !p.HasMorePages() {
215		return nil, fmt.Errorf("no more pages available")
216	}
217
218	params := *p.params
219	params.Marker = p.nextToken
220
221	var limit *int32
222	if p.options.Limit > 0 {
223		limit = &p.options.Limit
224	}
225	params.MaxItems = limit
226
227	result, err := p.client.ListPolicyVersions(ctx, &params, optFns...)
228	if err != nil {
229		return nil, err
230	}
231	p.firstPage = false
232
233	prevToken := p.nextToken
234	p.nextToken = result.Marker
235
236	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
237		p.nextToken = nil
238	}
239
240	return result, nil
241}
242
243func newServiceMetadataMiddleware_opListPolicyVersions(region string) *awsmiddleware.RegisterServiceMetadata {
244	return &awsmiddleware.RegisterServiceMetadata{
245		Region:        region,
246		ServiceID:     ServiceID,
247		SigningName:   "iam",
248		OperationName: "ListPolicyVersions",
249	}
250}
251