Skip to content
This repository was archived by the owner on Sep 5, 2023. It is now read-only.

feat: added helper methods for long running operations, IAM, and locations #260

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@

from google.api_core import operation # type: ignore
from google.api_core import operation_async # type: ignore
from google.cloud.location import locations_pb2 # type: ignore
from google.iam.v1 import iam_policy_pb2 # type: ignore
from google.iam.v1 import policy_pb2 # type: ignore
from google.longrunning import operations_pb2
from google.protobuf import duration_pb2 # type: ignore
from google.protobuf import empty_pb2 # type: ignore
from google.protobuf import timestamp_pb2 # type: ignore
Expand Down Expand Up @@ -396,7 +398,7 @@ async def sample_get_function():
google.cloud.functions_v1.types.CloudFunction:
Describes a Cloud Function that
contains user computation executed in
response to an event. It encapsulate
response to an event. It encapsulates
function and triggers configurations.

"""
Expand Down Expand Up @@ -464,7 +466,7 @@ async def create_function(
) -> operation_async.AsyncOperation:
r"""Creates a new function. If a function with the given name
already exists in the specified project, the long running
operation will return ``ALREADY_EXISTS`` error.
operation returns an ``ALREADY_EXISTS`` error.

.. code-block:: python

Expand Down Expand Up @@ -527,7 +529,7 @@ async def sample_create_function():
An object representing a long-running operation.

The result type for the operation will be :class:`google.cloud.functions_v1.types.CloudFunction` Describes a Cloud Function that contains user computation executed in
response to an event. It encapsulate function and
response to an event. It encapsulates function and
triggers configurations.

"""
Expand Down Expand Up @@ -648,7 +650,7 @@ async def sample_update_function():
An object representing a long-running operation.

The result type for the operation will be :class:`google.cloud.functions_v1.types.CloudFunction` Describes a Cloud Function that contains user computation executed in
response to an event. It encapsulate function and
response to an event. It encapsulates function and
triggers configurations.

"""
Expand Down Expand Up @@ -725,8 +727,7 @@ async def delete_function(
) -> operation_async.AsyncOperation:
r"""Deletes a function with the given name from the
specified project. If the given function is used by some
trigger, the trigger will be updated to remove this
function.
trigger, the trigger is updated to remove this function.

.. code-block:: python

Expand Down Expand Up @@ -990,13 +991,13 @@ async def generate_upload_url(
credentials would be used, but that identity does not have
permissions to upload files to the URL.

When making a HTTP PUT request, these two headers need to be
When making an HTTP PUT request, these two headers must be
specified:

- ``content-type: application/zip``
- ``x-goog-content-length-range: 0,104857600``

And this header SHOULD NOT be specified:
And this header must NOT be specified:

- ``Authorization: Bearer YOUR_TOKEN``

Expand Down Expand Up @@ -1076,9 +1077,9 @@ async def generate_download_url(
) -> functions.GenerateDownloadUrlResponse:
r"""Returns a signed URL for downloading deployed
function source code. The URL is only valid for a
limited period and should be used within minutes after
limited period and must be used within minutes after
generation.
For more information about the signed URL usage see:
For more information about the signed URL usage, see:
https://cloud.google.com/storage/docs/access-control/signed-urls

.. code-block:: python
Expand Down Expand Up @@ -1443,8 +1444,8 @@ async def test_iam_permissions(
metadata: Sequence[Tuple[str, str]] = (),
) -> iam_policy_pb2.TestIamPermissionsResponse:
r"""Tests the specified permissions against the IAM access control
policy for a function. If the function does not exist, this will
return an empty set of permissions, not a NOT_FOUND error.
policy for a function. If the function does not exist, this
returns an empty set of permissions, not a NOT_FOUND error.

.. code-block:: python

Expand Down Expand Up @@ -1518,6 +1519,168 @@ async def sample_test_iam_permissions():
# Done; return the response.
return response

async def list_operations(
self,
request: Optional[operations_pb2.ListOperationsRequest] = None,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, str]] = (),
) -> operations_pb2.ListOperationsResponse:
r"""Lists operations that match the specified filter in the request.

Args:
request (:class:`~.operations_pb2.ListOperationsRequest`):
The request object. Request message for
`ListOperations` method.
retry (google.api_core.retry.Retry): Designation of what errors,
if any, should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
~.operations_pb2.ListOperationsResponse:
Response message for ``ListOperations`` method.
"""
# Create or coerce a protobuf request object.
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
request = operations_pb2.ListOperationsRequest(**request)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method.wrap_method(
self._client._transport.list_operations,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
)

# Send the request.
response = await rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)

# Done; return the response.
return response

async def get_operation(
self,
request: Optional[operations_pb2.GetOperationRequest] = None,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, str]] = (),
) -> operations_pb2.Operation:
r"""Gets the latest state of a long-running operation.

Args:
request (:class:`~.operations_pb2.GetOperationRequest`):
The request object. Request message for
`GetOperation` method.
retry (google.api_core.retry.Retry): Designation of what errors,
if any, should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
~.operations_pb2.Operation:
An ``Operation`` object.
"""
# Create or coerce a protobuf request object.
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
request = operations_pb2.GetOperationRequest(**request)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method.wrap_method(
self._client._transport.get_operation,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
)

# Send the request.
response = await rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)

# Done; return the response.
return response

async def list_locations(
self,
request: Optional[locations_pb2.ListLocationsRequest] = None,
*,
retry: OptionalRetry = gapic_v1.method.DEFAULT,
timeout: Union[float, object] = gapic_v1.method.DEFAULT,
metadata: Sequence[Tuple[str, str]] = (),
) -> locations_pb2.ListLocationsResponse:
r"""Lists information about the supported locations for this service.

Args:
request (:class:`~.location_pb2.ListLocationsRequest`):
The request object. Request message for
`ListLocations` method.
retry (google.api_core.retry.Retry): Designation of what errors,
if any, should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
~.location_pb2.ListLocationsResponse:
Response message for ``ListLocations`` method.
"""
# Create or coerce a protobuf request object.
# The request isn't a proto-plus wrapped type,
# so it must be constructed via keyword expansion.
if isinstance(request, dict):
request = locations_pb2.ListLocationsRequest(**request)

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method.wrap_method(
self._client._transport.list_locations,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
)

# Send the request.
response = await rpc(
request,
retry=retry,
timeout=timeout,
metadata=metadata,
)

# Done; return the response.
return response

async def __aenter__(self):
return self

Expand Down
Loading