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

Commit

Permalink
feat: add common resource helpers; expose client transport; remove se…
Browse files Browse the repository at this point in the history
…nd/recv gRPC limits (#38)
  • Loading branch information
yoshi-automation committed Dec 23, 2020
1 parent add0735 commit f3f1a86
Show file tree
Hide file tree
Showing 21 changed files with 883 additions and 387 deletions.
1 change: 1 addition & 0 deletions docs/redis_v1/types.rst
Expand Up @@ -3,3 +3,4 @@ Types for Google Cloud Redis v1 API

.. automodule:: google.cloud.redis_v1.types
:members:
:show-inheritance:
1 change: 1 addition & 0 deletions docs/redis_v1beta1/types.rst
Expand Up @@ -3,3 +3,4 @@ Types for Google Cloud Redis v1beta1 API

.. automodule:: google.cloud.redis_v1beta1.types
:members:
:show-inheritance:
59 changes: 50 additions & 9 deletions google/cloud/redis_v1/services/cloud_redis/async_client.py
Expand Up @@ -74,9 +74,41 @@ class CloudRedisAsyncClient:
instance_path = staticmethod(CloudRedisClient.instance_path)
parse_instance_path = staticmethod(CloudRedisClient.parse_instance_path)

common_billing_account_path = staticmethod(
CloudRedisClient.common_billing_account_path
)
parse_common_billing_account_path = staticmethod(
CloudRedisClient.parse_common_billing_account_path
)

common_folder_path = staticmethod(CloudRedisClient.common_folder_path)
parse_common_folder_path = staticmethod(CloudRedisClient.parse_common_folder_path)

common_organization_path = staticmethod(CloudRedisClient.common_organization_path)
parse_common_organization_path = staticmethod(
CloudRedisClient.parse_common_organization_path
)

common_project_path = staticmethod(CloudRedisClient.common_project_path)
parse_common_project_path = staticmethod(CloudRedisClient.parse_common_project_path)

common_location_path = staticmethod(CloudRedisClient.common_location_path)
parse_common_location_path = staticmethod(
CloudRedisClient.parse_common_location_path
)

from_service_account_file = CloudRedisClient.from_service_account_file
from_service_account_json = from_service_account_file

@property
def transport(self) -> CloudRedisTransport:
"""Return the transport used by the client instance.
Returns:
CloudRedisTransport: The transport used by the client instance.
"""
return self._client.transport

get_transport_class = functools.partial(
type(CloudRedisClient).get_transport_class, type(CloudRedisClient)
)
Expand Down Expand Up @@ -180,7 +212,8 @@ async def list_instances(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([parent]):
has_flattened_params = builtins.any([parent])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -256,7 +289,8 @@ async def get_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([name]):
has_flattened_params = builtins.any([name])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -365,7 +399,8 @@ async def create_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([parent, instance_id, instance]):
has_flattened_params = builtins.any([parent, instance_id, instance])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -470,7 +505,8 @@ async def update_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([update_mask, instance]):
has_flattened_params = builtins.any([update_mask, instance])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -565,7 +601,8 @@ async def upgrade_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([name, redis_version]):
has_flattened_params = builtins.any([name, redis_version])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -665,7 +702,8 @@ async def import_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([name, input_config]):
has_flattened_params = builtins.any([name, input_config])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -761,7 +799,8 @@ async def export_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([name, output_config]):
has_flattened_params = builtins.any([name, output_config])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -856,7 +895,8 @@ async def failover_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([name, data_protection_mode]):
has_flattened_params = builtins.any([name, data_protection_mode])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down Expand Up @@ -954,7 +994,8 @@ async def delete_instance(
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
if request is not None and builtins.any([name]):
has_flattened_params = builtins.any([name])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
Expand Down
76 changes: 72 additions & 4 deletions google/cloud/redis_v1/services/cloud_redis/client.py
Expand Up @@ -155,6 +155,15 @@ def from_service_account_file(cls, filename: str, *args, **kwargs):

from_service_account_json = from_service_account_file

@property
def transport(self) -> CloudRedisTransport:
"""Return the transport used by the client instance.
Returns:
CloudRedisTransport: The transport used by the client instance.
"""
return self._transport

@staticmethod
def instance_path(project: str, location: str, instance: str,) -> str:
"""Return a fully-qualified instance string."""
Expand All @@ -171,6 +180,65 @@ def parse_instance_path(path: str) -> Dict[str, str]:
)
return m.groupdict() if m else {}

@staticmethod
def common_billing_account_path(billing_account: str,) -> str:
"""Return a fully-qualified billing_account string."""
return "billingAccounts/{billing_account}".format(
billing_account=billing_account,
)

@staticmethod
def parse_common_billing_account_path(path: str) -> Dict[str, str]:
"""Parse a billing_account path into its component segments."""
m = re.match(r"^billingAccounts/(?P<billing_account>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_folder_path(folder: str,) -> str:
"""Return a fully-qualified folder string."""
return "folders/{folder}".format(folder=folder,)

@staticmethod
def parse_common_folder_path(path: str) -> Dict[str, str]:
"""Parse a folder path into its component segments."""
m = re.match(r"^folders/(?P<folder>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_organization_path(organization: str,) -> str:
"""Return a fully-qualified organization string."""
return "organizations/{organization}".format(organization=organization,)

@staticmethod
def parse_common_organization_path(path: str) -> Dict[str, str]:
"""Parse a organization path into its component segments."""
m = re.match(r"^organizations/(?P<organization>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_project_path(project: str,) -> str:
"""Return a fully-qualified project string."""
return "projects/{project}".format(project=project,)

@staticmethod
def parse_common_project_path(path: str) -> Dict[str, str]:
"""Parse a project path into its component segments."""
m = re.match(r"^projects/(?P<project>.+?)$", path)
return m.groupdict() if m else {}

@staticmethod
def common_location_path(project: str, location: str,) -> str:
"""Return a fully-qualified location string."""
return "projects/{project}/locations/{location}".format(
project=project, location=location,
)

@staticmethod
def parse_common_location_path(path: str) -> Dict[str, str]:
"""Parse a location path into its component segments."""
m = re.match(r"^projects/(?P<project>.+?)/locations/(?P<location>.+?)$", path)
return m.groupdict() if m else {}

def __init__(
self,
*,
Expand Down Expand Up @@ -206,10 +274,10 @@ def __init__(
not provided, the default SSL client certificate will be used if
present. If GOOGLE_API_USE_CLIENT_CERTIFICATE is "false" or not
set, no client certificate will be used.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
Raises:
Expand Down
Expand Up @@ -28,7 +28,6 @@
_transport_registry["grpc"] = CloudRedisGrpcTransport
_transport_registry["grpc_asyncio"] = CloudRedisGrpcAsyncIOTransport


__all__ = (
"CloudRedisTransport",
"CloudRedisGrpcTransport",
Expand Down
37 changes: 22 additions & 15 deletions google/cloud/redis_v1/services/cloud_redis/transports/grpc.py
Expand Up @@ -111,10 +111,10 @@ def __init__(
for grpc channel. It is ignored if ``channel`` is provided.
quota_project_id (Optional[str]): An optional project to use for billing
and quota.
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
client_info (google.api_core.gapic_v1.client_info.ClientInfo):
The client info used to send a user-agent string along with
API requests. If ``None``, then default info will be used.
Generally, you only need to set this if you're developing
your own client library.
Raises:
Expand All @@ -123,13 +123,16 @@ def __init__(
google.api_core.exceptions.DuplicateCredentialArgs: If both ``credentials``
and ``credentials_file`` are passed.
"""
self._ssl_channel_credentials = ssl_channel_credentials

if channel:
# Sanity check: Ensure that channel and credentials are not both
# provided.
credentials = False

# If a channel was explicitly provided, set it.
self._grpc_channel = channel
self._ssl_channel_credentials = None
elif api_mtls_endpoint:
warnings.warn(
"api_mtls_endpoint and client_cert_source are deprecated",
Expand Down Expand Up @@ -165,7 +168,12 @@ def __init__(
ssl_credentials=ssl_credentials,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)
self._ssl_channel_credentials = ssl_credentials
else:
host = host if ":" in host else host + ":443"

Expand All @@ -182,9 +190,14 @@ def __init__(
ssl_credentials=ssl_channel_credentials,
scopes=scopes or self.AUTH_SCOPES,
quota_project_id=quota_project_id,
options=[
("grpc.max_send_message_length", -1),
("grpc.max_receive_message_length", -1),
],
)

self._stubs = {} # type: Dict[str, Callable]
self._operations_client = None

# Run the base constructor.
super().__init__(
Expand All @@ -208,7 +221,7 @@ def create_channel(
) -> grpc.Channel:
"""Create and return a gRPC channel object.
Args:
address (Optionsl[str]): The host for the channel to use.
address (Optional[str]): The host for the channel to use.
credentials (Optional[~.Credentials]): The
authorization credentials to attach to requests. These
credentials identify this application to the service. If
Expand Down Expand Up @@ -243,12 +256,8 @@ def create_channel(

@property
def grpc_channel(self) -> grpc.Channel:
"""Create the channel designed to connect to this service.
This property caches on the instance; repeated calls return
the same channel.
"""Return the channel designed to connect to this service.
"""
# Return the channel from cache.
return self._grpc_channel

@property
Expand All @@ -259,13 +268,11 @@ def operations_client(self) -> operations_v1.OperationsClient:
client.
"""
# Sanity check: Only create a new client if we do not already have one.
if "operations_client" not in self.__dict__:
self.__dict__["operations_client"] = operations_v1.OperationsClient(
self.grpc_channel
)
if self._operations_client is None:
self._operations_client = operations_v1.OperationsClient(self.grpc_channel)

# Return the client from cache.
return self.__dict__["operations_client"]
return self._operations_client

@property
def list_instances(
Expand Down

0 comments on commit f3f1a86

Please sign in to comment.