10000 feat: [google-cloud-compute] Add REST Interceptors which support read… · googleapis/google-cloud-python@6142165 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6142165

Browse files
feat: [google-cloud-compute] Add REST Interceptors which support reading metadata (#13481)
BEGIN_COMMIT_OVERRIDE feat: Add REST Interceptors which support reading metadata feat: Add support for reading selective GAPIC generation methods from service YAML chore: Update gapic-generator-python to v1.22.0 END_COMMIT_OVERRIDE - [ ] Regenerate this pull request now. feat: Add support for reading selective GAPIC generation methods from service YAML chore: Update gapic-generator-python to v1.22.0 PiperOrigin-RevId: 724026024 Source-Link: googleapis/googleapis@ad99638 Source-Link: googleapis/googleapis-gen@e291c4d Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWNvbXB1dGUvLk93bEJvdC55YW1sIiwiaCI6ImUyOTFjNGRkMWQ2NzBlZGExOTk5OGRlNzZmOTY3ZTE2MDNhNDg5OTMifQ== BEGIN_NESTED_COMMIT feat: [google-cloud-compute] Update Compute Engine API to revision 20250119 (#981) Source-Link: googleapis/googleapis@2e899ee Source-Link: googleapis/googleapis-gen@f3e46d9 Copy-Tag: eyJwIjoicGFja2FnZXMvZ29vZ2xlLWNsb3VkLWNvbXB1dGUvLk93bEJvdC55YW1sIiwiaCI6ImYzZTQ2ZDkzY2FiMTZkNzE1NTI4NThkZWEzOTlkMmQxODk2ZjBiNmYifQ== END_NESTED_COMMIT --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: ohmayr <omairnaveed@ymail.com>
1 parent 24b5afe commit 6142165

File tree

301 files changed

+34582
-1561
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

301 files changed

+34582
-1561
lines changed

packages/google-cloud-compute/google/cloud/compute/gapic_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
__version__ = "1.24.0" # {x-release-please-version}
16+
__version__ = "0.0.0" # {x-release-please-version}

packages/google-cloud-compute/google/cloud/compute_v1/gapic_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
__version__ = "1.24.0" # {x-release-please-version}
16+
__version__ = "0.0.0" # {x-release-please-version}

packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
# limitations under the License.
1515
#
1616
from collections import OrderedDict
17+
from http import HTTPStatus
18+
import json
1719
import logging as std_logging
1820
import os
1921
import re
@@ -461,6 +463,33 @@ def _validate_universe_domain(self):
461463
# NOTE (b/349488459): universe validation is disabled until further notice.
462464
return True
463465

466+
def _add_cred_info_for_auth_errors(
467+
self, error: core_exceptions.GoogleAPICallError
468+
) -> None:
469+
"""Adds credential info string to error details for 401/403/404 errors.
470+
471+
Args:
472+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
473+
"""
474+
if error.code not in [
475+
HTTPStatus.UNAUTHORIZED,
476+
HTTPStatus.FORBIDDEN,
477+
HTTPStatus.NOT_FOUND,
478+
]:
479+
return
480+
481+
cred = self._transport._credentials
482+
483+
# get_cred_info is only available in google-auth>=2.35.0
484+
if not hasattr(cred, "get_cred_info"):
485+
return
486+
487+
# ignore the type check since pypy test fails when get_cred_info
488+
# is not available
489+
cred_info = cred.get_cred_info() # type: ignore
490+
if cred_info and hasattr(error._details, "append"):
491+
error._details.append(json.dumps(cred_info))
492+
464493
@property
465494
def api_endpoint(self):
466495
"""Return the API endpoint used by the client instance.

packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/transports/rest.py

Lines changed: 85 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,37 @@ def post_aggregated_list(
118118
) -> compute.AcceleratorTypeAggregatedList:
119119
"""Post-rpc interceptor for aggregated_list
120120
121-
Override in a subclass to manipulate the response
121+
DEPRECATED. Please use the `post_aggregated_list_with_metadata`
122+
interceptor instead.
123+
124+
Override in a subclass to read or manipulate the response
122125
after it is returned by the AcceleratorTypes server but before
123-
it is returned to user code.
126+
it is returned to user code. This `post_aggregated_list` interceptor runs
127+
before the `post_aggregated_list_with_metadata` interceptor.
124128
"""
125129
return response
126130

131+
def post_aggregated_list_with_metadata(
132+
self,
133+
response: compute.AcceleratorTypeAggregatedList,
134+
metadata: Sequence[Tuple[str, Union[str, bytes]]],
135+
) -> Tuple[
136+
compute.AcceleratorTypeAggregatedList, Sequence[Tuple[str, Union[str, bytes]]]
137+
]:
138+
"""Post-rpc interceptor for aggregated_list
139+
140+
Override in a subclass to read or manipulate the response or metadata after it
141+
is returned by the AcceleratorTypes server but before it is returned to user code.
142+
143+
We recommend only using this `post_aggregated_list_with_metadata`
144+
interceptor in new development instead of the `post_aggregated_list` interceptor.
145+
When both interceptors are used, this `post_aggregated_list_with_metadata` interceptor runs after the
146+
`post_aggregated_list` interceptor. The (possibly modified) response returned by
147+
`post_aggregated_list` will be passed to
148+
`post_aggregated_list_with_metadata`.
149+
"""
150+
return response, metadata
151+
127152
def pre_get(
128153
self,
129154
request: compute.GetAcceleratorTypeRequest,
@@ -141,12 +166,35 @@ def pre_get(
141166
def post_get(self, response: compute.AcceleratorType) -> compute.AcceleratorType:
142167
"""Post-rpc interceptor for get
143168
144-
Override in a subclass to manipulate the response
169+
DEPRECATED. Please use the `post_get_with_metadata`
170+
interceptor instead.
171+
172+
Override in a subclass to read or manipulate the response
145173
after it is returned by the AcceleratorTypes server but before
146-
it is returned to user code.
174+
it is returned to user code. This `post_get` interceptor runs
175+
before the `post_get_with_metadata` interceptor.
147176
"""
148177
return response
149178

179+
def post_get_with_metadata(
180+
self,
181+
response: compute.AcceleratorType,
182+
metadata: Sequence[Tuple[str, Union[str, bytes]]],
183+
) -> Tuple[compute.AcceleratorType, Sequence[Tuple[str, Union[str, bytes]]]]:
184+
"""Post-rpc interceptor for get
185+
186+
Override in a subclass to read or manipulate the response or metadata after it
187+
is returned by the AcceleratorTypes server but before it is returned to user code.
188+
189+
We recommend only using this `post_get_with_metadata`
190+
interceptor in new development instead of the `post_get` interceptor.
191+
When both interceptors are used, this `post_get_with_metadata` interceptor runs after the
192+
`post_get` interceptor. The (possibly modified) response returned by
193+
`post_get` will be passed to
194+
`post_get_with_metadata`.
195+
"""
196+
return response, metadata
197+
150198
def pre_list(
151199
self,
152200
request: compute.ListAcceleratorTypesRequest,
@@ -166,12 +214,35 @@ def post_list(
166214
) -> compute.AcceleratorTypeList:
167215
"""Post-rpc interceptor for list
168216
169-
Override in a subclass to manipulate the response
217+
DEPRECATED. Please use the `post_list_with_metadata`
218+
interceptor instead.
219+
220+
Override in a subclass to read or manipulate the response
170221
after it is returned by the AcceleratorTypes server but before
171-
it is returned to user code.
222+
it is returned to user code. This `post_list` interceptor runs
223+
before the `post_list_with_metadata` interceptor.
172224
"""
173225
return response
174226

227+
def post_list_with_metadata(
228+
self,
229+
response: compute.AcceleratorTypeList,
230+
metadata: Sequence[Tuple[str, Union[str, bytes]]],
231+
) -> Tuple[compute.AcceleratorTypeList, Sequence[Tuple[str, Union[str, bytes]]]]:
232+
"""Post-rpc interceptor for list
233+
234+
Override in a subclass to read or manipulate the response or metadata after it
235+
is returned by the AcceleratorTypes server but before it is returned to user code.
236+
237+
We recommend only using this `post_list_with_metadata`
238+
interceptor in new development instead of the `post_list` interceptor.
239+
When both interceptors are used, this `post_list_with_metadata` interceptor runs after the
240+
`post_list` interceptor. The (possibly modified) response returned by
241+
`post_list` will be passed to
242+
`post_list_with_metadata`.
243+
"""
244+
return response, metadata
245+
175246

176247
@dataclasses.dataclass
177248
class AcceleratorTypesRestStub:
@@ -384,6 +455,10 @@ def __call__(
384455
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)
385456

386457
resp = self._interceptor.post_aggregated_list(resp)
458+
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
459+
resp, _ = self._interceptor.post_aggregated_list_with_metadata(
460+
resp, response_metadata
461+
)
387462
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
388463
logging.DEBUG
389464
): # pragma: NO COVER
@@ -538,6 +613,8 @@ def __call__(
538613
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)
539614

540615
resp = self._interceptor.post_get(resp)
616+
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
617+
resp, _ = self._interceptor.post_get_with_metadata(resp, response_metadata)
541618
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
542619
logging.DEBUG
543620
): # pragma: NO COVER
@@ -682,6 +759,8 @@ def __call__(
682759
json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True)
683760

684761
resp = self._interceptor.post_list(resp)
762+
response_metadata = [(k, str(v)) for k, v in response.headers.items()]
763+
resp, _ = self._interceptor.post_list_with_metadata(resp, response_metadata)
685764
if CLIENT_LOGGING_SUPPORTED and _LOGGER.isEnabledFor(
686765
logging.DEBUG
687766
): # pragma: NO COVER

packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#
1616
from collections import OrderedDict
1717
import functools
18+
from http import HTTPStatus
19+
import json
1820
import logging as std_logging
1921
import os
2022
import re
@@ -459,6 +461,33 @@ def _validate_universe_domain(self):
459461
# NOTE (b/349488459): universe validation is disabled until further notice.
460462
return True
461463

464+
def _add_cred_info_for_auth_errors(
465+
self, error: core_exceptions.GoogleAPICallError
466+
) -> None:
467+
"""Adds credential info string to error details for 401/403/404 errors.
468+
469+
Args:
470+
error (google.api_core.exceptions.GoogleAPICallError): The error to add the cred info.
471+
"""
472+
if error.code not in [
473+
HTTPStatus.UNAUTHORIZED,
474+
HTTPStatus.FORBIDDEN,
475+
HTTPStatus.NOT_FOUND,
476+
]:
477+
return
478+
479+
cred = self._transport._credentials
480+
481+
# get_cred_info is only available in google-auth>=2.35.0
482+
if not hasattr(cred, "get_cred_info"):
483+
return
484+
485+
# ignore the type check since pypy test fails when get_cred_info
486+
# is not available
487+
cred_info = cred.get_cred_info() # type: ignore
488+
if cred_info and hasattr(error._details, "append"):
489+
error._details.append(json.dumps(cred_info))
490+
462491
@property
463492
def api_endpoint(self):
464493
"""Return the API endpoint used by the client instance.

0 commit comments

Comments
 (0)
0