Skip to content

Commit b4c9770

Browse files
fix!: [google-cloud-kms] Pagination feature is introduced for method ListKeyHandles in service Autokey (googleapis#13093)
BEGIN_COMMIT_OVERRIDE fix!: Pagination feature is introduced for method ListKeyHandles in service Autokey feat: Adding a state field for AutokeyConfig docs: Field service_resolvers in message .google.cloud.kms.v1.EkmConnection is Explicitly is marked as to have field behavior of Optional docs: A comment for field destroy_scheduled_duration in message .google.cloud.kms.v1.CryptoKey is updated for the default duration END_COMMIT_OVERRIDE - [ ] Regenerate this pull request now. feat: Adding a state field for AutokeyConfig docs: Field service_resolvers in message .google.cloud.kms.v1.EkmConnection is Explicitly is marked as to have field behavior of Optional docs: A comment for field `destroy_scheduled_duration` in message `.google.cloud.kms.v1.CryptoKey` is updated for the default duration PiperOrigin-RevId: 676068244 Source-Link: googleapis/googleapis@42492c9 Source-Link: https://github.com/googleapis/googleapis-gen/commit/47432180bdfba879fc7f82c4c451181702f25009 Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWttcy8uT3dsQm90LnlhbWwiLCJoIjoiNDc0MzIxODBiZGZiYTg3OWZjN2Y4MmM0YzQ1MTE4MTcwMmYyNTAwOSJ9 --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: ohmayr <omairnaveed@ymail.com>
1 parent e727cc0 commit b4c9770

22 files changed

Lines changed: 648 additions & 49 deletions

packages/google-cloud-kms/docs/kms_v1/autokey.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ Autokey
44
.. automodule:: google.cloud.kms_v1.services.autokey
55
:members:
66
:inherited-members:
7+
8+
.. automodule:: google.cloud.kms_v1.services.autokey.pagers
9+
:members:
10+
:inherited-members:

packages/google-cloud-kms/google/cloud/kms_v1/services/autokey/async_client.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
from google.iam.v1 import policy_pb2 # type: ignore
5050
from google.longrunning import operations_pb2 # type: ignore
5151

52+
from google.cloud.kms_v1.services.autokey import pagers
5253
from google.cloud.kms_v1.types import autokey
5354

5455
from .client import AutokeyClient
@@ -57,8 +58,9 @@
5758

5859

5960
class AutokeyAsyncClient:
60-
"""Provides interfaces for using Cloud KMS Autokey to provision new
61-
[CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
61+
"""Provides interfaces for using `Cloud KMS
62+
Autokey <https://cloud.google.com/kms/help/autokey>`__ to provision
63+
new [CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
6264
Managed Encryption Key (CMEK) use, on-demand. To support certain
6365
client tooling, this feature is modeled around a
6466
[KeyHandle][google.cloud.kms.v1.KeyHandle] resource: creating a
@@ -544,7 +546,7 @@ async def list_key_handles(
544546
retry: OptionalRetry = gapic_v1.method.DEFAULT,
545547
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
546548
metadata: Sequence[Tuple[str, str]] = (),
547-
) -> autokey.ListKeyHandlesResponse:
549+
) -> pagers.ListKeyHandlesAsyncPager:
548550
r"""Lists [KeyHandles][google.cloud.kms.v1.KeyHandle].
549551
550552
.. code-block:: python
@@ -568,10 +570,11 @@ async def sample_list_key_handles():
568570
)
569571
570572
# Make the request
571-
response = await client.list_key_handles(request=request)
573+
page_result = client.list_key_handles(request=request)
572574
573575
# Handle the response
574-
print(response)
576+
async for response in page_result:
577+
print(response)
575578
576579
Args:
577580
request (Optional[Union[google.cloud.kms_v1.types.ListKeyHandlesRequest, dict]]):
@@ -593,10 +596,13 @@ async def sample_list_key_handles():
593596
sent along with the request as metadata.
594597
595598
Returns:
596-
google.cloud.kms_v1.types.ListKeyHandlesResponse:
599+
google.cloud.kms_v1.services.autokey.pagers.ListKeyHandlesAsyncPager:
597600
Response message for
598601
[Autokey.ListKeyHandles][google.cloud.kms.v1.Autokey.ListKeyHandles].
599602
603+
Iterating over this object will yield results and
604+
resolve additional pages automatically.
605+
600606
"""
601607
# Create or coerce a protobuf request object.
602608
# - Quick check: If we got a request object, we should *not* have
@@ -641,6 +647,17 @@ async def sample_list_key_handles():
641647
metadata=metadata,
642648
)
643649

650+
# This method is paged; wrap the response in a pager, which provides
651+
# an `__aiter__` convenience method.
652+
response = pagers.ListKeyHandlesAsyncPager(
653+
method=rpc,
654+
request=request,
655+
response=response,
656+
retry=retry,
657+
timeout=timeout,
658+
metadata=metadata,
659+
)
660+
644661
# Done; return the response.
645662
return response
646663

@@ -855,6 +872,7 @@ async def set_iam_policy(
855872
**JSON Example**
856873
857874
::
875+
858876
{
859877
"bindings": [
860878
{

packages/google-cloud-kms/google/cloud/kms_v1/services/autokey/client.py

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
from google.iam.v1 import policy_pb2 # type: ignore
5656
from google.longrunning import operations_pb2 # type: ignore
5757

58+
from google.cloud.kms_v1.services.autokey import pagers
5859
from google.cloud.kms_v1.types import autokey
5960

6061
from .transports.base import DEFAULT_CLIENT_INFO, AutokeyTransport
@@ -99,8 +100,9 @@ def get_transport_class(
99100

100101

101102
class AutokeyClient(metaclass=AutokeyClientMeta):
102-
"""Provides interfaces for using Cloud KMS Autokey to provision new
103-
[CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
103+
"""Provides interfaces for using `Cloud KMS
104+
Autokey <https://cloud.google.com/kms/help/autokey>`__ to provision
105+
new [CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
104106
Managed Encryption Key (CMEK) use, on-demand. To support certain
105107
client tooling, this feature is modeled around a
106108
[KeyHandle][google.cloud.kms.v1.KeyHandle] resource: creating a
@@ -986,7 +988,7 @@ def list_key_handles(
986988
retry: OptionalRetry = gapic_v1.method.DEFAULT,
987989
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
988990
metadata: Sequence[Tuple[str, str]] = (),
989-
) -> autokey.ListKeyHandlesResponse:
991+
) -> pagers.ListKeyHandlesPager:
990992
r"""Lists [KeyHandles][google.cloud.kms.v1.KeyHandle].
991993
992994
.. code-block:: python
@@ -1010,10 +1012,11 @@ def sample_list_key_handles():
10101012
)
10111013
10121014
# Make the request
1013-
response = client.list_key_handles(request=request)
1015+
page_result = client.list_key_handles(request=request)
10141016
10151017
# Handle the response
1016-
print(response)
1018+
for response in page_result:
1019+
print(response)
10171020
10181021
Args:
10191022
request (Union[google.cloud.kms_v1.types.ListKeyHandlesRequest, dict]):
@@ -1035,10 +1038,13 @@ def sample_list_key_handles():
10351038
sent along with the request as metadata.
10361039
10371040
Returns:
1038-
google.cloud.kms_v1.types.ListKeyHandlesResponse:
1041+
google.cloud.kms_v1.services.autokey.pagers.ListKeyHandlesPager:
10391042
Response message for
10401043
[Autokey.ListKeyHandles][google.cloud.kms.v1.Autokey.ListKeyHandles].
10411044
1045+
Iterating over this object will yield results and
1046+
resolve additional pages automatically.
1047+
10421048
"""
10431049
# Create or coerce a protobuf request object.
10441050
# - Quick check: If we got a request object, we should *not* have
@@ -1080,6 +1086,17 @@ def sample_list_key_handles():
10801086
metadata=metadata,
10811087
)
10821088

1089+
# This method is paged; wrap the response in a pager, which provides
1090+
# an `__iter__` convenience method.
1091+
response = pagers.ListKeyHandlesPager(
1092+
method=rpc,
1093+
request=request,
1094+
response=response,
1095+
retry=retry,
1096+
timeout=timeout,
1097+
metadata=metadata,
1098+
)
1099+
10831100
# Done; return the response.
10841101
return response
10851102

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2024 Google LLC
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
from typing import (
17+
Any,
18+
AsyncIterator,
19+
Awaitable,
20+
Callable,
21+
Iterator,
22+
Optional,
23+
Sequence,
24+
Tuple,
25+
Union,
26+
)
27+
28+
from google.api_core import gapic_v1
29+
from google.api_core import retry as retries
30+
from google.api_core import retry_async as retries_async
31+
32+
try:
33+
OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault, None]
34+
OptionalAsyncRetry = Union[
35+
retries_async.AsyncRetry, gapic_v1.method._MethodDefault, None
36+
]
37+
except AttributeError: # pragma: NO COVER
38+
OptionalRetry = Union[retries.Retry, object, None] # type: ignore
39+
OptionalAsyncRetry = Union[retries_async.AsyncRetry, object, None] # type: ignore
40+
41+
from google.cloud.kms_v1.types import autokey
42+
43+
44+
class ListKeyHandlesPager:
45+
"""A pager for iterating through ``list_key_handles`` requests.
46+
47+
This class thinly wraps an initial
48+
:class:`google.cloud.kms_v1.types.ListKeyHandlesResponse` object, and
49+
provides an ``__iter__`` method to iterate through its
50+
``key_handles`` field.
51+
52+
If there are more pages, the ``__iter__`` method will make additional
53+
``ListKeyHandles`` requests and continue to iterate
54+
through the ``key_handles`` field on the
55+
corresponding responses.
56+
57+
All the usual :class:`google.cloud.kms_v1.types.ListKeyHandlesResponse`
58+
attributes are available on the pager. If multiple requests are made, only
59+
the most recent response is retained, and thus used for attribute lookup.
60+
"""
61+
62+
def __init__(
63+
self,
64+
method: Callable[..., autokey.ListKeyHandlesResponse],
65+
request: autokey.ListKeyHandlesRequest,
66+
response: autokey.ListKeyHandlesResponse,
67+
*,
68+
retry: OptionalRetry = gapic_v1.method.DEFAULT,
69+
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
70+
metadata: Sequence[Tuple[str, str]] = ()
71+
):
72+
"""Instantiate the pager.
73+
74+
Args:
75+
method (Callable): The method that was originally called, and
76+
which instantiated this pager.
77+
request (google.cloud.kms_v1.types.ListKeyHandlesRequest):
78+
The initial request object.
79+
response (google.cloud.kms_v1.types.ListKeyHandlesResponse):
80+
The initial response object.
81+
retry (google.api_core.retry.Retry): Designation of what errors,
82+
if any, should be retried.
83+
timeout (float): The timeout for this request.
84+
metadata (Sequence[Tuple[str, str]]): Strings which should be
85+
sent along with the request as metadata.
86+
"""
87+
self._method = method
88+
self._request = autokey.ListKeyHandlesRequest(request)
89+
self._response = response
90+
self._retry = retry
91+
self._timeout = timeout
92+
self._metadata = metadata
93+
94+
def __getattr__(self, name: str) -> Any:
95+
return getattr(self._response, name)
96+
97+
@property
98+
def pages(self) -> Iterator[autokey.ListKeyHandlesResponse]:
99+
yield self._response
100+
while self._response.next_page_token:
101+
self._request.page_token = self._response.next_page_token
102+
self._response = self._method(
103+
self._request,
104+
retry=self._retry,
105+
timeout=self._timeout,
106+
metadata=self._metadata,
107+
)
108+
yield self._response
109+
110+
def __iter__(self) -> Iterator[autokey.KeyHandle]:
111+
for page in self.pages:
112+
yield from page.key_handles
113+
114+
def __repr__(self) -> str:
115+
return "{0}<{1!r}>".format(self.__class__.__name__, self._response)
116+
117+
118+
class ListKeyHandlesAsyncPager:
119+
"""A pager for iterating through ``list_key_handles`` requests.
120+
121+
This class thinly wraps an initial
122+
:class:`google.cloud.kms_v1.types.ListKeyHandlesResponse` object, and
123+
provides an ``__aiter__`` method to iterate through its
124+
``key_handles`` field.
125+
126+
If there are more pages, the ``__aiter__`` method will make additional
127+
``ListKeyHandles`` requests and continue to iterate
128+
through the ``key_handles`` field on the
129+
corresponding responses.
130+
131+
All the usual :class:`google.cloud.kms_v1.types.ListKeyHandlesResponse`
132+
attributes are available on the pager. If multiple requests are made, only
133+
the most recent response is retained, and thus used for attribute lookup.
134+
"""
135+
136+
def __init__(
137+
self,
138+
method: Callable[..., Awaitable[autokey.ListKeyHandlesResponse]],
139+
request: autokey.ListKeyHandlesRequest,
140+
response: autokey.ListKeyHandlesResponse,
141+
*,
142+
retry: OptionalAsyncRetry = gapic_v1.method.DEFAULT,
143+
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
144+
metadata: Sequence[Tuple[str, str]] = ()
145+
):
146+
"""Instantiates the pager.
147+
148+
Args:
149+
method (Callable): The method that was originally called, and
150+
which instantiated this pager.
151+
request (google.cloud.kms_v1.types.ListKeyHandlesRequest):
152+
The initial request object.
153+
response (google.cloud.kms_v1.types.ListKeyHandlesResponse):
154+
The initial response object.
155+
retry (google.api_core.retry.AsyncRetry): Designation of what errors,
156+
if any, should be retried.
157+
timeout (float): The timeout for this request.
158+
metadata (Sequence[Tuple[str, str]]): Strings which should be
159+
sent along with the request as metadata.
160+
"""
161+
self._method = method
162+
self._request = autokey.ListKeyHandlesRequest(request)
163+
self._response = response
164+
self._retry = retry
165+
self._timeout = timeout
166+
self._metadata = metadata
167+
168+
def __getattr__(self, name: str) -> Any:
169+
return getattr(self._response, name)
170+
171+
@property
172+
async def pages(self) -> AsyncIterator[autokey.ListKeyHandlesResponse]:
173+
yield self._response
174+
while self._response.next_page_token:
175+
self._request.page_token = self._response.next_page_token
176+
self._response = await self._method(
177+
self._request,
178+
retry=self._retry,
179+
timeout=self._timeout,
180+
metadata=self._metadata,
181+
)
182+
yield self._response
183+
184+
def __aiter__(self) -> AsyncIterator[autokey.KeyHandle]:
185+
async def async_generator():
186+
async for page in self.pages:
187+
for response in page.key_handles:
188+
yield response
189+
190+
return async_generator()
191+
192+
def __repr__(self) -> str:
193+
return "{0}<{1!r}>".format(self.__class__.__name__, self._response)

packages/google-cloud-kms/google/cloud/kms_v1/services/autokey/transports/grpc.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@
3434
class AutokeyGrpcTransport(AutokeyTransport):
3535
"""gRPC backend transport for Autokey.
3636
37-
Provides interfaces for using Cloud KMS Autokey to provision new
38-
[CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
37+
Provides interfaces for using `Cloud KMS
38+
Autokey <https://cloud.google.com/kms/help/autokey>`__ to provision
39+
new [CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
3940
Managed Encryption Key (CMEK) use, on-demand. To support certain
4041
client tooling, this feature is modeled around a
4142
[KeyHandle][google.cloud.kms.v1.KeyHandle] resource: creating a

packages/google-cloud-kms/google/cloud/kms_v1/services/autokey/transports/grpc_asyncio.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@
3737
class AutokeyGrpcAsyncIOTransport(AutokeyTransport):
3838
"""gRPC AsyncIO backend transport for Autokey.
3939
40-
Provides interfaces for using Cloud KMS Autokey to provision new
41-
[CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
40+
Provides interfaces for using `Cloud KMS
41+
Autokey <https://cloud.google.com/kms/help/autokey>`__ to provision
42+
new [CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
4243
Managed Encryption Key (CMEK) use, on-demand. To support certain
4344
client tooling, this feature is modeled around a
4445
[KeyHandle][google.cloud.kms.v1.KeyHandle] resource: creating a

packages/google-cloud-kms/google/cloud/kms_v1/services/autokey/transports/rest.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,9 @@ class AutokeyRestStub:
316316
class AutokeyRestTransport(AutokeyTransport):
317317
"""REST backend transport for Autokey.
318318
319-
Provides interfaces for using Cloud KMS Autokey to provision new
320-
[CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
319+
Provides interfaces for using `Cloud KMS
320+
Autokey <https://cloud.google.com/kms/help/autokey>`__ to provision
321+
new [CryptoKeys][google.cloud.kms.v1.CryptoKey], ready for Customer
321322
Managed Encryption Key (CMEK) use, on-demand. To support certain
322323
client tooling, this feature is modeled around a
323324
[KeyHandle][google.cloud.kms.v1.KeyHandle] resource: creating a

0 commit comments

Comments
 (0)