1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package kms
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/kms/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13)
14
15// Gets a list of all grants for the specified customer master key (CMK). You must
16// specify the CMK in all requests. You can filter the grant list by grant ID or
17// grantee principal. The GranteePrincipal field in the ListGrants response usually
18// contains the user or role designated as the grantee principal in the grant.
19// However, when the grantee principal in the grant is an AWS service, the
20// GranteePrincipal field contains the service principal
21// (https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_principal.html#principal-services),
22// which might represent several different grantee principals. Cross-account use:
23// Yes. To perform this operation on a CMK in a different AWS account, specify the
24// key ARN in the value of the KeyId parameter. Required permissions:
25// kms:ListGrants
26// (https://docs.aws.amazon.com/kms/latest/developerguide/kms-api-permissions-reference.html)
27// (key policy) Related operations:
28//
29// * CreateGrant
30//
31// * ListRetirableGrants
32//
33// *
34// RetireGrant
35//
36// * RevokeGrant
37func (c *Client) ListGrants(ctx context.Context, params *ListGrantsInput, optFns ...func(*Options)) (*ListGrantsOutput, error) {
38	if params == nil {
39		params = &ListGrantsInput{}
40	}
41
42	result, metadata, err := c.invokeOperation(ctx, "ListGrants", params, optFns, addOperationListGrantsMiddlewares)
43	if err != nil {
44		return nil, err
45	}
46
47	out := result.(*ListGrantsOutput)
48	out.ResultMetadata = metadata
49	return out, nil
50}
51
52type ListGrantsInput struct {
53
54	// Returns only grants for the specified customer master key (CMK). This parameter
55	// is required. Specify the key ID or the Amazon Resource Name (ARN) of the CMK. To
56	// specify a CMK in a different AWS account, you must use the key ARN. For
57	// example:
58	//
59	// * Key ID: 1234abcd-12ab-34cd-56ef-1234567890ab
60	//
61	// * Key ARN:
62	// arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab
63	//
64	// To
65	// get the key ID and key ARN for a CMK, use ListKeys or DescribeKey.
66	//
67	// This member is required.
68	KeyId *string
69
70	// Returns only the grant with the specified grant ID. The grant ID uniquely
71	// identifies the grant.
72	GrantId *string
73
74	// Returns only grants where the specified principal is the grantee principal for
75	// the grant.
76	GranteePrincipal *string
77
78	// Use this parameter to specify the maximum number of items to return. When this
79	// value is present, AWS KMS does not return more than the specified number of
80	// items, but it might return fewer. This value is optional. If you include a
81	// value, it must be between 1 and 100, inclusive. If you do not include a value,
82	// it defaults to 50.
83	Limit *int32
84
85	// Use this parameter in a subsequent request after you receive a response with
86	// truncated results. Set it to the value of NextMarker from the truncated response
87	// you just received.
88	Marker *string
89}
90
91type ListGrantsOutput struct {
92
93	// A list of grants.
94	Grants []types.GrantListEntry
95
96	// When Truncated is true, this element is present and contains the value to use
97	// for the Marker parameter in a subsequent request.
98	NextMarker *string
99
100	// A flag that indicates whether there are more items in the list. When this value
101	// is true, the list in this response is truncated. To get more items, pass the
102	// value of the NextMarker element in thisresponse to the Marker parameter in a
103	// subsequent request.
104	Truncated bool
105
106	// Metadata pertaining to the operation's result.
107	ResultMetadata middleware.Metadata
108}
109
110func addOperationListGrantsMiddlewares(stack *middleware.Stack, options Options) (err error) {
111	err = stack.Serialize.Add(&awsAwsjson11_serializeOpListGrants{}, middleware.After)
112	if err != nil {
113		return err
114	}
115	err = stack.Deserialize.Add(&awsAwsjson11_deserializeOpListGrants{}, middleware.After)
116	if err != nil {
117		return err
118	}
119	if err = addSetLoggerMiddleware(stack, options); err != nil {
120		return err
121	}
122	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
123		return err
124	}
125	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
126		return err
127	}
128	if err = addResolveEndpointMiddleware(stack, options); err != nil {
129		return err
130	}
131	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
132		return err
133	}
134	if err = addRetryMiddlewares(stack, options); err != nil {
135		return err
136	}
137	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
138		return err
139	}
140	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
141		return err
142	}
143	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
144		return err
145	}
146	if err = addClientUserAgent(stack); err != nil {
147		return err
148	}
149	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
150		return err
151	}
152	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
153		return err
154	}
155	if err = addOpListGrantsValidationMiddleware(stack); err != nil {
156		return err
157	}
158	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opListGrants(options.Region), middleware.Before); err != nil {
159		return err
160	}
161	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
162		return err
163	}
164	if err = addResponseErrorMiddleware(stack); err != nil {
165		return err
166	}
167	if err = addRequestResponseLogging(stack, options); err != nil {
168		return err
169	}
170	return nil
171}
172
173// ListGrantsAPIClient is a client that implements the ListGrants operation.
174type ListGrantsAPIClient interface {
175	ListGrants(context.Context, *ListGrantsInput, ...func(*Options)) (*ListGrantsOutput, error)
176}
177
178var _ ListGrantsAPIClient = (*Client)(nil)
179
180// ListGrantsPaginatorOptions is the paginator options for ListGrants
181type ListGrantsPaginatorOptions struct {
182	// Use this parameter to specify the maximum number of items to return. When this
183	// value is present, AWS KMS does not return more than the specified number of
184	// items, but it might return fewer. This value is optional. If you include a
185	// value, it must be between 1 and 100, inclusive. If you do not include a value,
186	// it defaults to 50.
187	Limit int32
188
189	// Set to true if pagination should stop if the service returns a pagination token
190	// that matches the most recent token provided to the service.
191	StopOnDuplicateToken bool
192}
193
194// ListGrantsPaginator is a paginator for ListGrants
195type ListGrantsPaginator struct {
196	options   ListGrantsPaginatorOptions
197	client    ListGrantsAPIClient
198	params    *ListGrantsInput
199	nextToken *string
200	firstPage bool
201}
202
203// NewListGrantsPaginator returns a new ListGrantsPaginator
204func NewListGrantsPaginator(client ListGrantsAPIClient, params *ListGrantsInput, optFns ...func(*ListGrantsPaginatorOptions)) *ListGrantsPaginator {
205	if params == nil {
206		params = &ListGrantsInput{}
207	}
208
209	options := ListGrantsPaginatorOptions{}
210	if params.Limit != nil {
211		options.Limit = *params.Limit
212	}
213
214	for _, fn := range optFns {
215		fn(&options)
216	}
217
218	return &ListGrantsPaginator{
219		options:   options,
220		client:    client,
221		params:    params,
222		firstPage: true,
223	}
224}
225
226// HasMorePages returns a boolean indicating whether more pages are available
227func (p *ListGrantsPaginator) HasMorePages() bool {
228	return p.firstPage || p.nextToken != nil
229}
230
231// NextPage retrieves the next ListGrants page.
232func (p *ListGrantsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListGrantsOutput, error) {
233	if !p.HasMorePages() {
234		return nil, fmt.Errorf("no more pages available")
235	}
236
237	params := *p.params
238	params.Marker = p.nextToken
239
240	var limit *int32
241	if p.options.Limit > 0 {
242		limit = &p.options.Limit
243	}
244	params.Limit = limit
245
246	result, err := p.client.ListGrants(ctx, &params, optFns...)
247	if err != nil {
248		return nil, err
249	}
250	p.firstPage = false
251
252	prevToken := p.nextToken
253	p.nextToken = result.NextMarker
254
255	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
256		p.nextToken = nil
257	}
258
259	return result, nil
260}
261
262func newServiceMetadataMiddleware_opListGrants(region string) *awsmiddleware.RegisterServiceMetadata {
263	return &awsmiddleware.RegisterServiceMetadata{
264		Region:        region,
265		ServiceID:     ServiceID,
266		SigningName:   "kms",
267		OperationName: "ListGrants",
268	}
269}
270