diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster/__init__.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster/__init__.py index d88d0b9dc375..035c70c004a0 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster/__init__.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster/__init__.py @@ -26,27 +26,35 @@ ) from google.cloud.redis_cluster_v1.types.cloud_redis_cluster import ( AuthorizationMode, + CertificateAuthority, Cluster, + ClusterPersistenceConfig, CreateClusterRequest, DeleteClusterRequest, DiscoveryEndpoint, + GetClusterCertificateAuthorityRequest, GetClusterRequest, ListClustersRequest, ListClustersResponse, + NodeType, OperationMetadata, PscConfig, PscConnection, TransitEncryptionMode, UpdateClusterRequest, + ZoneDistributionConfig, ) __all__ = ( "CloudRedisClusterClient", "CloudRedisClusterAsyncClient", + "CertificateAuthority", "Cluster", + "ClusterPersistenceConfig", "CreateClusterRequest", "DeleteClusterRequest", "DiscoveryEndpoint", + "GetClusterCertificateAuthorityRequest", "GetClusterRequest", "ListClustersRequest", "ListClustersResponse", @@ -54,6 +62,8 @@ "PscConfig", "PscConnection", "UpdateClusterRequest", + "ZoneDistributionConfig", "AuthorizationMode", + "NodeType", "TransitEncryptionMode", ) diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/__init__.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/__init__.py index 929c2d593c14..6122779a1997 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/__init__.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/__init__.py @@ -24,34 +24,44 @@ ) from .types.cloud_redis_cluster import ( AuthorizationMode, + CertificateAuthority, Cluster, + ClusterPersistenceConfig, CreateClusterRequest, DeleteClusterRequest, DiscoveryEndpoint, + GetClusterCertificateAuthorityRequest, GetClusterRequest, ListClustersRequest, ListClustersResponse, + NodeType, OperationMetadata, PscConfig, PscConnection, TransitEncryptionMode, UpdateClusterRequest, + ZoneDistributionConfig, ) __all__ = ( "CloudRedisClusterAsyncClient", "AuthorizationMode", + "CertificateAuthority", "CloudRedisClusterClient", "Cluster", + "ClusterPersistenceConfig", "CreateClusterRequest", "DeleteClusterRequest", "DiscoveryEndpoint", + "GetClusterCertificateAuthorityRequest", "GetClusterRequest", "ListClustersRequest", "ListClustersResponse", + "NodeType", "OperationMetadata", "PscConfig", "PscConnection", "TransitEncryptionMode", "UpdateClusterRequest", + "ZoneDistributionConfig", ) diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/gapic_metadata.json b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/gapic_metadata.json index bfb2ea820e75..c134ce3dedb6 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/gapic_metadata.json +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/gapic_metadata.json @@ -25,6 +25,11 @@ "get_cluster" ] }, + "GetClusterCertificateAuthority": { + "methods": [ + "get_cluster_certificate_authority" + ] + }, "ListClusters": { "methods": [ "list_clusters" @@ -55,6 +60,11 @@ "get_cluster" ] }, + "GetClusterCertificateAuthority": { + "methods": [ + "get_cluster_certificate_authority" + ] + }, "ListClusters": { "methods": [ "list_clusters" @@ -85,6 +95,11 @@ "get_cluster" ] }, + "GetClusterCertificateAuthority": { + "methods": [ + "get_cluster_certificate_authority" + ] + }, "ListClusters": { "methods": [ "list_clusters" diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/async_client.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/async_client.py index d09f07c1566a..39126efb1d99 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/async_client.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/async_client.py @@ -100,6 +100,12 @@ class CloudRedisClusterAsyncClient: _DEFAULT_ENDPOINT_TEMPLATE = CloudRedisClusterClient._DEFAULT_ENDPOINT_TEMPLATE _DEFAULT_UNIVERSE = CloudRedisClusterClient._DEFAULT_UNIVERSE + certificate_authority_path = staticmethod( + CloudRedisClusterClient.certificate_authority_path + ) + parse_certificate_authority_path = staticmethod( + CloudRedisClusterClient.parse_certificate_authority_path + ) cluster_path = staticmethod(CloudRedisClusterClient.cluster_path) parse_cluster_path = staticmethod(CloudRedisClusterClient.parse_cluster_path) common_billing_account_path = staticmethod( @@ -969,6 +975,117 @@ async def sample_create_cluster(): # Done; return the response. return response + async def get_cluster_certificate_authority( + self, + request: Optional[ + Union[cloud_redis_cluster.GetClusterCertificateAuthorityRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> cloud_redis_cluster.CertificateAuthority: + r"""Gets the details of certificate authority information + for Redis cluster. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import redis_cluster_v1 + + async def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1.CloudRedisClusterAsyncClient() + + # Initialize request argument(s) + request = redis_cluster_v1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = await client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.redis_cluster_v1.types.GetClusterCertificateAuthorityRequest, dict]]): + The request object. Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + name (:class:`str`): + Required. Redis cluster certificate authority resource + name using the form: + ``projects/{project_id}/locations/{location_id}/clusters/{cluster_id}/certificateAuthority`` + where ``location_id`` refers to a GCP region. + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): 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: + google.cloud.redis_cluster_v1.types.CertificateAuthority: + Redis cluster certificate authority + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, cloud_redis_cluster.GetClusterCertificateAuthorityRequest + ): + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.get_cluster_certificate_authority + ] + + # 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),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + async def list_operations( self, request: Optional[operations_pb2.ListOperationsRequest] = None, diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/client.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/client.py index 4145237dcd72..e0dcd55a8ed8 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/client.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/client.py @@ -220,6 +220,28 @@ def transport(self) -> CloudRedisClusterTransport: """ return self._transport + @staticmethod + def certificate_authority_path( + project: str, + location: str, + cluster: str, + ) -> str: + """Returns a fully-qualified certificate_authority string.""" + return "projects/{project}/locations/{location}/clusters/{cluster}/certificateAuthority".format( + project=project, + location=location, + cluster=cluster, + ) + + @staticmethod + def parse_certificate_authority_path(path: str) -> Dict[str, str]: + """Parses a certificate_authority path into its component segments.""" + m = re.match( + r"^projects/(?P.+?)/locations/(?P.+?)/clusters/(?P.+?)/certificateAuthority$", + path, + ) + return m.groupdict() if m else {} + @staticmethod def cluster_path( project: str, @@ -1375,6 +1397,116 @@ def sample_create_cluster(): # Done; return the response. return response + def get_cluster_certificate_authority( + self, + request: Optional[ + Union[cloud_redis_cluster.GetClusterCertificateAuthorityRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> cloud_redis_cluster.CertificateAuthority: + r"""Gets the details of certificate authority information + for Redis cluster. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import redis_cluster_v1 + + def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1.CloudRedisClusterClient() + + # Initialize request argument(s) + request = redis_cluster_v1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.redis_cluster_v1.types.GetClusterCertificateAuthorityRequest, dict]): + The request object. Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + name (str): + Required. Redis cluster certificate authority resource + name using the form: + ``projects/{project_id}/locations/{location_id}/clusters/{cluster_id}/certificateAuthority`` + where ``location_id`` refers to a GCP region. + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + 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: + google.cloud.redis_cluster_v1.types.CertificateAuthority: + Redis cluster certificate authority + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, cloud_redis_cluster.GetClusterCertificateAuthorityRequest + ): + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.get_cluster_certificate_authority + ] + + # 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),)), + ) + + # Validate the universe domain. + self._validate_universe_domain() + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + def __enter__(self) -> "CloudRedisClusterClient": return self diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/base.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/base.py index 534e48b609fb..0f1e7f17cc1a 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/base.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/base.py @@ -153,6 +153,11 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), + self.get_cluster_certificate_authority: gapic_v1.method.wrap_method( + self.get_cluster_certificate_authority, + default_timeout=600.0, + client_info=client_info, + ), } def close(self): @@ -217,6 +222,18 @@ def create_cluster( ]: raise NotImplementedError() + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + Union[ + cloud_redis_cluster.CertificateAuthority, + Awaitable[cloud_redis_cluster.CertificateAuthority], + ], + ]: + raise NotImplementedError() + @property def list_operations( self, diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc.py index ae7000cede07..dfda013dbd0d 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc.py @@ -434,6 +434,39 @@ def create_cluster( ) return self._stubs["create_cluster"] + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + cloud_redis_cluster.CertificateAuthority, + ]: + r"""Return a callable for the get cluster certificate + authority method over gRPC. + + Gets the details of certificate authority information + for Redis cluster. + + Returns: + Callable[[~.GetClusterCertificateAuthorityRequest], + ~.CertificateAuthority]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_cluster_certificate_authority" not in self._stubs: + self._stubs[ + "get_cluster_certificate_authority" + ] = self.grpc_channel.unary_unary( + "/google.cloud.redis.cluster.v1.CloudRedisCluster/GetClusterCertificateAuthority", + request_serializer=cloud_redis_cluster.GetClusterCertificateAuthorityRequest.serialize, + response_deserializer=cloud_redis_cluster.CertificateAuthority.deserialize, + ) + return self._stubs["get_cluster_certificate_authority"] + def close(self): self.grpc_channel.close() diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc_asyncio.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc_asyncio.py index 5fc7418b7bd2..dcc783f975fb 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc_asyncio.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/grpc_asyncio.py @@ -448,6 +448,39 @@ def create_cluster( ) return self._stubs["create_cluster"] + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + Awaitable[cloud_redis_cluster.CertificateAuthority], + ]: + r"""Return a callable for the get cluster certificate + authority method over gRPC. + + Gets the details of certificate authority information + for Redis cluster. + + Returns: + Callable[[~.GetClusterCertificateAuthorityRequest], + Awaitable[~.CertificateAuthority]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_cluster_certificate_authority" not in self._stubs: + self._stubs[ + "get_cluster_certificate_authority" + ] = self.grpc_channel.unary_unary( + "/google.cloud.redis.cluster.v1.CloudRedisCluster/GetClusterCertificateAuthority", + request_serializer=cloud_redis_cluster.GetClusterCertificateAuthorityRequest.serialize, + response_deserializer=cloud_redis_cluster.CertificateAuthority.deserialize, + ) + return self._stubs["get_cluster_certificate_authority"] + def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { @@ -476,6 +509,11 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), + self.get_cluster_certificate_authority: gapic_v1.method_async.wrap_method( + self.get_cluster_certificate_authority, + default_timeout=600.0, + client_info=client_info, + ), } def close(self): diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/rest.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/rest.py index 68b357f9bdd4..b280f43ab323 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/rest.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/services/cloud_redis_cluster/transports/rest.py @@ -96,6 +96,14 @@ def post_get_cluster(self, response): logging.log(f"Received response: {response}") return response + def pre_get_cluster_certificate_authority(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_cluster_certificate_authority(self, response): + logging.log(f"Received response: {response}") + return response + def pre_list_clusters(self, request, metadata): logging.log(f"Received request: {request}") return request, metadata @@ -187,6 +195,32 @@ def post_get_cluster( """ return response + def pre_get_cluster_certificate_authority( + self, + request: cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[ + cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + Sequence[Tuple[str, str]], + ]: + """Pre-rpc interceptor for get_cluster_certificate_authority + + Override in a subclass to manipulate the request or metadata + before they are sent to the CloudRedisCluster server. + """ + return request, metadata + + def post_get_cluster_certificate_authority( + self, response: cloud_redis_cluster.CertificateAuthority + ) -> cloud_redis_cluster.CertificateAuthority: + """Post-rpc interceptor for get_cluster_certificate_authority + + Override in a subclass to manipulate the response + after it is returned by the CloudRedisCluster server but before + it is returned to user code. + """ + return response + def pre_list_clusters( self, request: cloud_redis_cluster.ListClustersRequest, @@ -810,6 +844,97 @@ def __call__( resp = self._interceptor.post_get_cluster(resp) return resp + class _GetClusterCertificateAuthority(CloudRedisClusterRestStub): + def __hash__(self): + return hash("GetClusterCertificateAuthority") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> cloud_redis_cluster.CertificateAuthority: + r"""Call the get cluster certificate + authority method over HTTP. + + Args: + request (~.cloud_redis_cluster.GetClusterCertificateAuthorityRequest): + The request object. Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + 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: + ~.cloud_redis_cluster.CertificateAuthority: + Redis cluster certificate authority + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/clusters/*/certificateAuthority}", + }, + ] + request, metadata = self._interceptor.pre_get_cluster_certificate_authority( + request, metadata + ) + pb_request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest.pb( + request + ) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = cloud_redis_cluster.CertificateAuthority() + pb_resp = cloud_redis_cluster.CertificateAuthority.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_cluster_certificate_authority(resp) + return resp + class _ListClusters(CloudRedisClusterRestStub): def __hash__(self): return hash("ListClusters") @@ -1014,6 +1139,17 @@ def get_cluster( # In C++ this would require a dynamic_cast return self._GetCluster(self._session, self._host, self._interceptor) # type: ignore + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + cloud_redis_cluster.CertificateAuthority, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetClusterCertificateAuthority(self._session, self._host, self._interceptor) # type: ignore + @property def list_clusters( self, diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/__init__.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/__init__.py index 6b145cb8610b..b7bd90233770 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/__init__.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/__init__.py @@ -15,25 +15,33 @@ # from .cloud_redis_cluster import ( AuthorizationMode, + CertificateAuthority, Cluster, + ClusterPersistenceConfig, CreateClusterRequest, DeleteClusterRequest, DiscoveryEndpoint, + GetClusterCertificateAuthorityRequest, GetClusterRequest, ListClustersRequest, ListClustersResponse, + NodeType, OperationMetadata, PscConfig, PscConnection, TransitEncryptionMode, UpdateClusterRequest, + ZoneDistributionConfig, ) __all__ = ( + "CertificateAuthority", "Cluster", + "ClusterPersistenceConfig", "CreateClusterRequest", "DeleteClusterRequest", "DiscoveryEndpoint", + "GetClusterCertificateAuthorityRequest", "GetClusterRequest", "ListClustersRequest", "ListClustersResponse", @@ -41,6 +49,8 @@ "PscConfig", "PscConnection", "UpdateClusterRequest", + "ZoneDistributionConfig", "AuthorizationMode", + "NodeType", "TransitEncryptionMode", ) diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/cloud_redis_cluster.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/cloud_redis_cluster.py index 075f7649ebfc..9b7b1155cf1d 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/cloud_redis_cluster.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1/types/cloud_redis_cluster.py @@ -25,6 +25,7 @@ package="google.cloud.redis.cluster.v1", manifest={ "AuthorizationMode", + "NodeType", "TransitEncryptionMode", "CreateClusterRequest", "ListClustersRequest", @@ -32,11 +33,15 @@ "UpdateClusterRequest", "GetClusterRequest", "DeleteClusterRequest", + "GetClusterCertificateAuthorityRequest", "Cluster", "PscConfig", "DiscoveryEndpoint", "PscConnection", "OperationMetadata", + "CertificateAuthority", + "ClusterPersistenceConfig", + "ZoneDistributionConfig", }, ) @@ -57,6 +62,28 @@ class AuthorizationMode(proto.Enum): AUTH_MODE_DISABLED = 2 +class NodeType(proto.Enum): + r"""NodeType of a redis cluster node, + + Values: + NODE_TYPE_UNSPECIFIED (0): + No description available. + REDIS_SHARED_CORE_NANO (1): + Redis shared core nano node_type. + REDIS_HIGHMEM_MEDIUM (2): + Redis highmem medium node_type. + REDIS_HIGHMEM_XLARGE (3): + Redis highmem xlarge node_type. + REDIS_STANDARD_SMALL (4): + Redis standard small node_type. + """ + NODE_TYPE_UNSPECIFIED = 0 + REDIS_SHARED_CORE_NANO = 1 + REDIS_HIGHMEM_MEDIUM = 2 + REDIS_HIGHMEM_XLARGE = 3 + REDIS_STANDARD_SMALL = 4 + + class TransitEncryptionMode(proto.Enum): r"""Available mode of in-transit encryption. @@ -270,6 +297,24 @@ class DeleteClusterRequest(proto.Message): ) +class GetClusterCertificateAuthorityRequest(proto.Message): + r"""Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + + Attributes: + name (str): + Required. Redis cluster certificate authority resource name + using the form: + ``projects/{project_id}/locations/{location_id}/clusters/{cluster_id}/certificateAuthority`` + where ``location_id`` refers to a GCP region. + """ + + name: str = proto.Field( + proto.STRING, + number=1, + ) + + class Cluster(proto.Message): r"""A cluster instance. @@ -305,7 +350,7 @@ class Cluster(proto.Message): disabled for the cluster. size_gb (int): Output only. Redis memory size in GB for the - entire cluster. + entire cluster rounded up to the next integer. This field is a member of `oneof`_ ``_size_gb``. shard_count (int): @@ -330,6 +375,30 @@ class Cluster(proto.Message): state_info (google.cloud.redis_cluster_v1.types.Cluster.StateInfo): Output only. Additional information about the current state of the cluster. + node_type (google.cloud.redis_cluster_v1.types.NodeType): + Optional. The type of a redis node in the + cluster. NodeType determines the underlying + machine-type of a redis node. + persistence_config (google.cloud.redis_cluster_v1.types.ClusterPersistenceConfig): + Optional. Persistence config (RDB, AOF) for + the cluster. + redis_configs (MutableMapping[str, str]): + Optional. Key/Value pairs of customer + overrides for mutable Redis Configs + precise_size_gb (float): + Output only. Precise value of redis memory + size in GB for the entire cluster. + + This field is a member of `oneof`_ ``_precise_size_gb``. + zone_distribution_config (google.cloud.redis_cluster_v1.types.ZoneDistributionConfig): + Optional. This config will be used to + determine how the customer wants us to + distribute cluster resources within the region. + deletion_protection_enabled (bool): + Optional. The delete operation will fail when + the value is set to true. + + This field is a member of `oneof`_ ``_deletion_protection_enabled``. """ class State(proto.Enum): @@ -466,6 +535,36 @@ class UpdateInfo(proto.Message): number=18, message=StateInfo, ) + node_type: "NodeType" = proto.Field( + proto.ENUM, + number=19, + enum="NodeType", + ) + persistence_config: "ClusterPersistenceConfig" = proto.Field( + proto.MESSAGE, + number=20, + message="ClusterPersistenceConfig", + ) + redis_configs: MutableMapping[str, str] = proto.MapField( + proto.STRING, + proto.STRING, + number=21, + ) + precise_size_gb: float = proto.Field( + proto.DOUBLE, + number=22, + optional=True, + ) + zone_distribution_config: "ZoneDistributionConfig" = proto.Field( + proto.MESSAGE, + number=23, + message="ZoneDistributionConfig", + ) + deletion_protection_enabled: bool = proto.Field( + proto.BOOL, + number=25, + optional=True, + ) class PscConfig(proto.Message): @@ -625,4 +724,242 @@ class OperationMetadata(proto.Message): ) +class CertificateAuthority(proto.Message): + r"""Redis cluster certificate authority + + .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields + + Attributes: + managed_server_ca (google.cloud.redis_cluster_v1.types.CertificateAuthority.ManagedCertificateAuthority): + + This field is a member of `oneof`_ ``server_ca``. + name (str): + Identifier. Unique name of the resource in this scope + including project, location and cluster using the form: + ``projects/{project}/locations/{location}/clusters/{cluster}/certificateAuthority`` + """ + + class ManagedCertificateAuthority(proto.Message): + r""" + + Attributes: + ca_certs (MutableSequence[google.cloud.redis_cluster_v1.types.CertificateAuthority.ManagedCertificateAuthority.CertChain]): + The PEM encoded CA certificate chains for + redis managed server authentication + """ + + class CertChain(proto.Message): + r""" + + Attributes: + certificates (MutableSequence[str]): + The certificates that form the CA chain, from + leaf to root order. + """ + + certificates: MutableSequence[str] = proto.RepeatedField( + proto.STRING, + number=1, + ) + + ca_certs: MutableSequence[ + "CertificateAuthority.ManagedCertificateAuthority.CertChain" + ] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message="CertificateAuthority.ManagedCertificateAuthority.CertChain", + ) + + managed_server_ca: ManagedCertificateAuthority = proto.Field( + proto.MESSAGE, + number=1, + oneof="server_ca", + message=ManagedCertificateAuthority, + ) + name: str = proto.Field( + proto.STRING, + number=2, + ) + + +class ClusterPersistenceConfig(proto.Message): + r"""Configuration of the persistence functionality. + + Attributes: + mode (google.cloud.redis_cluster_v1.types.ClusterPersistenceConfig.PersistenceMode): + Optional. The mode of persistence. + rdb_config (google.cloud.redis_cluster_v1.types.ClusterPersistenceConfig.RDBConfig): + Optional. RDB configuration. This field will + be ignored if mode is not RDB. + aof_config (google.cloud.redis_cluster_v1.types.ClusterPersistenceConfig.AOFConfig): + Optional. AOF configuration. This field will + be ignored if mode is not AOF. + """ + + class PersistenceMode(proto.Enum): + r"""Available persistence modes. + + Values: + PERSISTENCE_MODE_UNSPECIFIED (0): + Not set. + DISABLED (1): + Persistence is disabled, and any snapshot + data is deleted. + RDB (2): + RDB based persistence is enabled. + AOF (3): + AOF based persistence is enabled. + """ + PERSISTENCE_MODE_UNSPECIFIED = 0 + DISABLED = 1 + RDB = 2 + AOF = 3 + + class RDBConfig(proto.Message): + r"""Configuration of the RDB based persistence. + + Attributes: + rdb_snapshot_period (google.cloud.redis_cluster_v1.types.ClusterPersistenceConfig.RDBConfig.SnapshotPeriod): + Optional. Period between RDB snapshots. + rdb_snapshot_start_time (google.protobuf.timestamp_pb2.Timestamp): + Optional. The time that the first snapshot + was/will be attempted, and to which future + snapshots will be aligned. If not provided, the + current time will be used. + """ + + class SnapshotPeriod(proto.Enum): + r"""Available snapshot periods. + + Values: + SNAPSHOT_PERIOD_UNSPECIFIED (0): + Not set. + ONE_HOUR (1): + One hour. + SIX_HOURS (2): + Six hours. + TWELVE_HOURS (3): + Twelve hours. + TWENTY_FOUR_HOURS (4): + Twenty four hours. + """ + SNAPSHOT_PERIOD_UNSPECIFIED = 0 + ONE_HOUR = 1 + SIX_HOURS = 2 + TWELVE_HOURS = 3 + TWENTY_FOUR_HOURS = 4 + + rdb_snapshot_period: "ClusterPersistenceConfig.RDBConfig.SnapshotPeriod" = ( + proto.Field( + proto.ENUM, + number=1, + enum="ClusterPersistenceConfig.RDBConfig.SnapshotPeriod", + ) + ) + rdb_snapshot_start_time: timestamp_pb2.Timestamp = proto.Field( + proto.MESSAGE, + number=2, + message=timestamp_pb2.Timestamp, + ) + + class AOFConfig(proto.Message): + r"""Configuration of the AOF based persistence. + + Attributes: + append_fsync (google.cloud.redis_cluster_v1.types.ClusterPersistenceConfig.AOFConfig.AppendFsync): + Optional. fsync configuration. + """ + + class AppendFsync(proto.Enum): + r"""Available fsync modes. + + Values: + APPEND_FSYNC_UNSPECIFIED (0): + Not set. Default: EVERYSEC + NO (1): + Never fsync. Normally Linux will flush data + every 30 seconds with this configuration, but + it's up to the kernel's exact tuning. + EVERYSEC (2): + fsync every second. Fast enough, and you may + lose 1 second of data if there is a disaster + ALWAYS (3): + fsync every time new commands are appended to + the AOF. It has the best data loss protection at + the cost of performance + """ + APPEND_FSYNC_UNSPECIFIED = 0 + NO = 1 + EVERYSEC = 2 + ALWAYS = 3 + + append_fsync: "ClusterPersistenceConfig.AOFConfig.AppendFsync" = proto.Field( + proto.ENUM, + number=1, + enum="ClusterPersistenceConfig.AOFConfig.AppendFsync", + ) + + mode: PersistenceMode = proto.Field( + proto.ENUM, + number=1, + enum=PersistenceMode, + ) + rdb_config: RDBConfig = proto.Field( + proto.MESSAGE, + number=2, + message=RDBConfig, + ) + aof_config: AOFConfig = proto.Field( + proto.MESSAGE, + number=3, + message=AOFConfig, + ) + + +class ZoneDistributionConfig(proto.Message): + r"""Zone distribution config for allocation of cluster resources. + + Attributes: + mode (google.cloud.redis_cluster_v1.types.ZoneDistributionConfig.ZoneDistributionMode): + Optional. The mode of zone distribution. Defaults to + MULTI_ZONE, when not specified. + zone (str): + Optional. When SINGLE ZONE distribution is selected, zone + field would be used to allocate all resources in that zone. + This is not applicable to MULTI_ZONE, and would be ignored + for MULTI_ZONE clusters. + """ + + class ZoneDistributionMode(proto.Enum): + r"""Defines various modes of zone distribution. + Currently supports two modes, can be expanded in future to + support more types of distribution modes. + design doc: go/same-zone-cluster + + Values: + ZONE_DISTRIBUTION_MODE_UNSPECIFIED (0): + Not Set. Default: MULTI_ZONE + MULTI_ZONE (1): + Distribute all resources across 3 zones + picked at random, within the region. + SINGLE_ZONE (2): + Distribute all resources in a single zone. + The zone field must be specified, when this mode + is selected. + """ + ZONE_DISTRIBUTION_MODE_UNSPECIFIED = 0 + MULTI_ZONE = 1 + SINGLE_ZONE = 2 + + mode: ZoneDistributionMode = proto.Field( + proto.ENUM, + number=1, + enum=ZoneDistributionMode, + ) + zone: str = proto.Field( + proto.STRING, + number=2, + ) + + __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/__init__.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/__init__.py index 70363e520fad..57add71f7bec 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/__init__.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/__init__.py @@ -24,34 +24,44 @@ ) from .types.cloud_redis_cluster import ( AuthorizationMode, + CertificateAuthority, Cluster, + ClusterPersistenceConfig, CreateClusterRequest, DeleteClusterRequest, DiscoveryEndpoint, + GetClusterCertificateAuthorityRequest, GetClusterRequest, ListClustersRequest, ListClustersResponse, + NodeType, OperationMetadata, PscConfig, PscConnection, TransitEncryptionMode, UpdateClusterRequest, + ZoneDistributionConfig, ) __all__ = ( "CloudRedisClusterAsyncClient", "AuthorizationMode", + "CertificateAuthority", "CloudRedisClusterClient", "Cluster", + "ClusterPersistenceConfig", "CreateClusterRequest", "DeleteClusterRequest", "DiscoveryEndpoint", + "GetClusterCertificateAuthorityRequest", "GetClusterRequest", "ListClustersRequest", "ListClustersResponse", + "NodeType", "OperationMetadata", "PscConfig", "PscConnection", "TransitEncryptionMode", "UpdateClusterRequest", + "ZoneDistributionConfig", ) diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/gapic_metadata.json b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/gapic_metadata.json index 673d281018ed..57715b8b0180 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/gapic_metadata.json +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/gapic_metadata.json @@ -25,6 +25,11 @@ "get_cluster" ] }, + "GetClusterCertificateAuthority": { + "methods": [ + "get_cluster_certificate_authority" + ] + }, "ListClusters": { "methods": [ "list_clusters" @@ -55,6 +60,11 @@ "get_cluster" ] }, + "GetClusterCertificateAuthority": { + "methods": [ + "get_cluster_certificate_authority" + ] + }, "ListClusters": { "methods": [ "list_clusters" @@ -85,6 +95,11 @@ "get_cluster" ] }, + "GetClusterCertificateAuthority": { + "methods": [ + "get_cluster_certificate_authority" + ] + }, "ListClusters": { "methods": [ "list_clusters" diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/async_client.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/async_client.py index d550bc94fc30..0df6afc949c2 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/async_client.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/async_client.py @@ -100,6 +100,12 @@ class CloudRedisClusterAsyncClient: _DEFAULT_ENDPOINT_TEMPLATE = CloudRedisClusterClient._DEFAULT_ENDPOINT_TEMPLATE _DEFAULT_UNIVERSE = CloudRedisClusterClient._DEFAULT_UNIVERSE + certificate_authority_path = staticmethod( + CloudRedisClusterClient.certificate_authority_path + ) + parse_certificate_authority_path = staticmethod( + CloudRedisClusterClient.parse_certificate_authority_path + ) cluster_path = staticmethod(CloudRedisClusterClient.cluster_path) parse_cluster_path = staticmethod(CloudRedisClusterClient.parse_cluster_path) common_billing_account_path = staticmethod( @@ -969,6 +975,117 @@ async def sample_create_cluster(): # Done; return the response. return response + async def get_cluster_certificate_authority( + self, + request: Optional[ + Union[cloud_redis_cluster.GetClusterCertificateAuthorityRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> cloud_redis_cluster.CertificateAuthority: + r"""Gets the details of certificate authority information + for Redis cluster. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import redis_cluster_v1beta1 + + async def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1beta1.CloudRedisClusterAsyncClient() + + # Initialize request argument(s) + request = redis_cluster_v1beta1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = await client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + + Args: + request (Optional[Union[google.cloud.redis_cluster_v1beta1.types.GetClusterCertificateAuthorityRequest, dict]]): + The request object. Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + name (:class:`str`): + Required. Redis cluster certificate authority resource + name using the form: + ``projects/{project_id}/locations/{location_id}/clusters/{cluster_id}/certificateAuthority`` + where ``location_id`` refers to a GCP region. + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + retry (google.api_core.retry_async.AsyncRetry): 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: + google.cloud.redis_cluster_v1beta1.types.CertificateAuthority: + Redis cluster certificate authority + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, cloud_redis_cluster.GetClusterCertificateAuthorityRequest + ): + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest(request) + + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._client._transport._wrapped_methods[ + self._client._transport.get_cluster_certificate_authority + ] + + # 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),)), + ) + + # Validate the universe domain. + self._client._validate_universe_domain() + + # Send the request. + response = await rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + async def list_operations( self, request: Optional[operations_pb2.ListOperationsRequest] = None, diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/client.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/client.py index 62d2caba35f4..a0edca844b7a 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/client.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/client.py @@ -220,6 +220,28 @@ def transport(self) -> CloudRedisClusterTransport: """ return self._transport + @staticmethod + def certificate_authority_path( + project: str, + location: str, + cluster: str, + ) -> str: + """Returns a fully-qualified certificate_authority string.""" + return "projects/{project}/locations/{location}/clusters/{cluster}/certificateAuthority".format( + project=project, + location=location, + cluster=cluster, + ) + + @staticmethod + def parse_certificate_authority_path(path: str) -> Dict[str, str]: + """Parses a certificate_authority path into its component segments.""" + m = re.match( + r"^projects/(?P.+?)/locations/(?P.+?)/clusters/(?P.+?)/certificateAuthority$", + path, + ) + return m.groupdict() if m else {} + @staticmethod def cluster_path( project: str, @@ -1375,6 +1397,116 @@ def sample_create_cluster(): # Done; return the response. return response + def get_cluster_certificate_authority( + self, + request: Optional[ + Union[cloud_redis_cluster.GetClusterCertificateAuthorityRequest, dict] + ] = None, + *, + name: Optional[str] = None, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Union[float, object] = gapic_v1.method.DEFAULT, + metadata: Sequence[Tuple[str, str]] = (), + ) -> cloud_redis_cluster.CertificateAuthority: + r"""Gets the details of certificate authority information + for Redis cluster. + + .. code-block:: python + + # This snippet has been automatically generated and should be regarded as a + # code template only. + # It will require modifications to work: + # - It may require correct/in-range values for request initialization. + # - It may require specifying regional endpoints when creating the service + # client as shown in: + # https://googleapis.dev/python/google-api-core/latest/client_options.html + from google.cloud import redis_cluster_v1beta1 + + def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1beta1.CloudRedisClusterClient() + + # Initialize request argument(s) + request = redis_cluster_v1beta1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + + Args: + request (Union[google.cloud.redis_cluster_v1beta1.types.GetClusterCertificateAuthorityRequest, dict]): + The request object. Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + name (str): + Required. Redis cluster certificate authority resource + name using the form: + ``projects/{project_id}/locations/{location_id}/clusters/{cluster_id}/certificateAuthority`` + where ``location_id`` refers to a GCP region. + + This corresponds to the ``name`` field + on the ``request`` instance; if ``request`` is provided, this + should not be set. + 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: + google.cloud.redis_cluster_v1beta1.types.CertificateAuthority: + Redis cluster certificate authority + """ + # Create or coerce a protobuf request object. + # - Quick check: If we got a request object, we should *not* have + # gotten any keyword arguments that map to the request. + has_flattened_params = 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." + ) + + # - Use the request object if provided (there's no risk of modifying the input as + # there are no flattened fields), or create one. + if not isinstance( + request, cloud_redis_cluster.GetClusterCertificateAuthorityRequest + ): + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest(request) + # If we have keyword arguments corresponding to fields on the + # request, apply these. + if name is not None: + request.name = name + + # Wrap the RPC method; this adds retry and timeout information, + # and friendly error handling. + rpc = self._transport._wrapped_methods[ + self._transport.get_cluster_certificate_authority + ] + + # 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),)), + ) + + # Validate the universe domain. + self._validate_universe_domain() + + # Send the request. + response = rpc( + request, + retry=retry, + timeout=timeout, + metadata=metadata, + ) + + # Done; return the response. + return response + def __enter__(self) -> "CloudRedisClusterClient": return self diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/base.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/base.py index 0f53895e948e..5e9b6b07334e 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/base.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/base.py @@ -153,6 +153,11 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), + self.get_cluster_certificate_authority: gapic_v1.method.wrap_method( + self.get_cluster_certificate_authority, + default_timeout=600.0, + client_info=client_info, + ), } def close(self): @@ -217,6 +222,18 @@ def create_cluster( ]: raise NotImplementedError() + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + Union[ + cloud_redis_cluster.CertificateAuthority, + Awaitable[cloud_redis_cluster.CertificateAuthority], + ], + ]: + raise NotImplementedError() + @property def list_operations( self, diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc.py index 1962178cdfb1..1172034d147e 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc.py @@ -434,6 +434,39 @@ def create_cluster( ) return self._stubs["create_cluster"] + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + cloud_redis_cluster.CertificateAuthority, + ]: + r"""Return a callable for the get cluster certificate + authority method over gRPC. + + Gets the details of certificate authority information + for Redis cluster. + + Returns: + Callable[[~.GetClusterCertificateAuthorityRequest], + ~.CertificateAuthority]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_cluster_certificate_authority" not in self._stubs: + self._stubs[ + "get_cluster_certificate_authority" + ] = self.grpc_channel.unary_unary( + "/google.cloud.redis.cluster.v1beta1.CloudRedisCluster/GetClusterCertificateAuthority", + request_serializer=cloud_redis_cluster.GetClusterCertificateAuthorityRequest.serialize, + response_deserializer=cloud_redis_cluster.CertificateAuthority.deserialize, + ) + return self._stubs["get_cluster_certificate_authority"] + def close(self): self.grpc_channel.close() diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc_asyncio.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc_asyncio.py index f1e95ba4b184..4d75303454ef 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc_asyncio.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/grpc_asyncio.py @@ -448,6 +448,39 @@ def create_cluster( ) return self._stubs["create_cluster"] + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + Awaitable[cloud_redis_cluster.CertificateAuthority], + ]: + r"""Return a callable for the get cluster certificate + authority method over gRPC. + + Gets the details of certificate authority information + for Redis cluster. + + Returns: + Callable[[~.GetClusterCertificateAuthorityRequest], + Awaitable[~.CertificateAuthority]]: + A function that, when called, will call the underlying RPC + on the server. + """ + # Generate a "stub function" on-the-fly which will actually make + # the request. + # gRPC handles serialization and deserialization, so we just need + # to pass in the functions for each. + if "get_cluster_certificate_authority" not in self._stubs: + self._stubs[ + "get_cluster_certificate_authority" + ] = self.grpc_channel.unary_unary( + "/google.cloud.redis.cluster.v1beta1.CloudRedisCluster/GetClusterCertificateAuthority", + request_serializer=cloud_redis_cluster.GetClusterCertificateAuthorityRequest.serialize, + response_deserializer=cloud_redis_cluster.CertificateAuthority.deserialize, + ) + return self._stubs["get_cluster_certificate_authority"] + def _prep_wrapped_messages(self, client_info): """Precompute the wrapped methods, overriding the base class method to use async wrappers.""" self._wrapped_methods = { @@ -476,6 +509,11 @@ def _prep_wrapped_messages(self, client_info): default_timeout=600.0, client_info=client_info, ), + self.get_cluster_certificate_authority: gapic_v1.method_async.wrap_method( + self.get_cluster_certificate_authority, + default_timeout=600.0, + client_info=client_info, + ), } def close(self): diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/rest.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/rest.py index 418694c46220..1ee6e010f804 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/rest.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/services/cloud_redis_cluster/transports/rest.py @@ -96,6 +96,14 @@ def post_get_cluster(self, response): logging.log(f"Received response: {response}") return response + def pre_get_cluster_certificate_authority(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_cluster_certificate_authority(self, response): + logging.log(f"Received response: {response}") + return response + def pre_list_clusters(self, request, metadata): logging.log(f"Received request: {request}") return request, metadata @@ -187,6 +195,32 @@ def post_get_cluster( """ return response + def pre_get_cluster_certificate_authority( + self, + request: cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[ + cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + Sequence[Tuple[str, str]], + ]: + """Pre-rpc interceptor for get_cluster_certificate_authority + + Override in a subclass to manipulate the request or metadata + before they are sent to the CloudRedisCluster server. + """ + return request, metadata + + def post_get_cluster_certificate_authority( + self, response: cloud_redis_cluster.CertificateAuthority + ) -> cloud_redis_cluster.CertificateAuthority: + """Post-rpc interceptor for get_cluster_certificate_authority + + Override in a subclass to manipulate the response + after it is returned by the CloudRedisCluster server but before + it is returned to user code. + """ + return response + def pre_list_clusters( self, request: cloud_redis_cluster.ListClustersRequest, @@ -810,6 +844,97 @@ def __call__( resp = self._interceptor.post_get_cluster(resp) return resp + class _GetClusterCertificateAuthority(CloudRedisClusterRestStub): + def __hash__(self): + return hash("GetClusterCertificateAuthority") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, Any] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> cloud_redis_cluster.CertificateAuthority: + r"""Call the get cluster certificate + authority method over HTTP. + + Args: + request (~.cloud_redis_cluster.GetClusterCertificateAuthorityRequest): + The request object. Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + 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: + ~.cloud_redis_cluster.CertificateAuthority: + Redis cluster certificate authority + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1beta1/{name=projects/*/locations/*/clusters/*/certificateAuthority}", + }, + ] + request, metadata = self._interceptor.pre_get_cluster_certificate_authority( + request, metadata + ) + pb_request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest.pb( + request + ) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception + # subclass. + if response.status_code >= 400: + raise core_exceptions.from_http_response(response) + + # Return the response + resp = cloud_redis_cluster.CertificateAuthority() + pb_resp = cloud_redis_cluster.CertificateAuthority.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_cluster_certificate_authority(resp) + return resp + class _ListClusters(CloudRedisClusterRestStub): def __hash__(self): return hash("ListClusters") @@ -1014,6 +1139,17 @@ def get_cluster( # In C++ this would require a dynamic_cast return self._GetCluster(self._session, self._host, self._interceptor) # type: ignore + @property + def get_cluster_certificate_authority( + self, + ) -> Callable[ + [cloud_redis_cluster.GetClusterCertificateAuthorityRequest], + cloud_redis_cluster.CertificateAuthority, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetClusterCertificateAuthority(self._session, self._host, self._interceptor) # type: ignore + @property def list_clusters( self, diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/__init__.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/__init__.py index 6b145cb8610b..b7bd90233770 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/__init__.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/__init__.py @@ -15,25 +15,33 @@ # from .cloud_redis_cluster import ( AuthorizationMode, + CertificateAuthority, Cluster, + ClusterPersistenceConfig, CreateClusterRequest, DeleteClusterRequest, DiscoveryEndpoint, + GetClusterCertificateAuthorityRequest, GetClusterRequest, ListClustersRequest, ListClustersResponse, + NodeType, OperationMetadata, PscConfig, PscConnection, TransitEncryptionMode, UpdateClusterRequest, + ZoneDistributionConfig, ) __all__ = ( + "CertificateAuthority", "Cluster", + "ClusterPersistenceConfig", "CreateClusterRequest", "DeleteClusterRequest", "DiscoveryEndpoint", + "GetClusterCertificateAuthorityRequest", "GetClusterRequest", "ListClustersRequest", "ListClustersResponse", @@ -41,6 +49,8 @@ "PscConfig", "PscConnection", "UpdateClusterRequest", + "ZoneDistributionConfig", "AuthorizationMode", + "NodeType", "TransitEncryptionMode", ) diff --git a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/cloud_redis_cluster.py b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/cloud_redis_cluster.py index d5847f4c8bcf..bb33c98a38fe 100644 --- a/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/cloud_redis_cluster.py +++ b/packages/google-cloud-redis-cluster/google/cloud/redis_cluster_v1beta1/types/cloud_redis_cluster.py @@ -25,6 +25,7 @@ package="google.cloud.redis.cluster.v1beta1", manifest={ "AuthorizationMode", + "NodeType", "TransitEncryptionMode", "CreateClusterRequest", "ListClustersRequest", @@ -32,11 +33,15 @@ "UpdateClusterRequest", "GetClusterRequest", "DeleteClusterRequest", + "GetClusterCertificateAuthorityRequest", "Cluster", "PscConfig", "DiscoveryEndpoint", "PscConnection", "OperationMetadata", + "CertificateAuthority", + "ClusterPersistenceConfig", + "ZoneDistributionConfig", }, ) @@ -57,6 +62,28 @@ class AuthorizationMode(proto.Enum): AUTH_MODE_DISABLED = 2 +class NodeType(proto.Enum): + r"""NodeType of a redis cluster node, + + Values: + NODE_TYPE_UNSPECIFIED (0): + No description available. + REDIS_SHARED_CORE_NANO (1): + Redis shared core nano node_type. + REDIS_HIGHMEM_MEDIUM (2): + Redis highmem medium node_type. + REDIS_HIGHMEM_XLARGE (3): + Redis highmem xlarge node_type. + REDIS_STANDARD_SMALL (4): + Redis standard small node_type. + """ + NODE_TYPE_UNSPECIFIED = 0 + REDIS_SHARED_CORE_NANO = 1 + REDIS_HIGHMEM_MEDIUM = 2 + REDIS_HIGHMEM_XLARGE = 3 + REDIS_STANDARD_SMALL = 4 + + class TransitEncryptionMode(proto.Enum): r"""Available mode of in-transit encryption. @@ -270,6 +297,24 @@ class DeleteClusterRequest(proto.Message): ) +class GetClusterCertificateAuthorityRequest(proto.Message): + r"""Request for + [GetClusterCertificateAuthorityRequest][CloudRedis.GetClusterCertificateAuthorityRequest]. + + Attributes: + name (str): + Required. Redis cluster certificate authority resource name + using the form: + ``projects/{project_id}/locations/{location_id}/clusters/{cluster_id}/certificateAuthority`` + where ``location_id`` refers to a GCP region. + """ + + name: str = proto.Field( + proto.STRING, + number=1, + ) + + class Cluster(proto.Message): r"""A cluster instance. @@ -305,7 +350,7 @@ class Cluster(proto.Message): disabled for the cluster. size_gb (int): Output only. Redis memory size in GB for the - entire cluster. + entire cluster rounded up to the next integer. This field is a member of `oneof`_ ``_size_gb``. shard_count (int): @@ -330,6 +375,30 @@ class Cluster(proto.Message): state_info (google.cloud.redis_cluster_v1beta1.types.Cluster.StateInfo): Output only. Additional information about the current state of the cluster. + node_type (google.cloud.redis_cluster_v1beta1.types.NodeType): + Optional. The type of a redis node in the + cluster. NodeType determines the underlying + machine-type of a redis node. + persistence_config (google.cloud.redis_cluster_v1beta1.types.ClusterPersistenceConfig): + Optional. Persistence config (RDB, AOF) for + the cluster. + redis_configs (MutableMapping[str, str]): + Optional. Key/Value pairs of customer + overrides for mutable Redis Configs + precise_size_gb (float): + Output only. Precise value of redis memory + size in GB for the entire cluster. + + This field is a member of `oneof`_ ``_precise_size_gb``. + zone_distribution_config (google.cloud.redis_cluster_v1beta1.types.ZoneDistributionConfig): + Optional. This config will be used to + determine how the customer wants us to + distribute cluster resources within the region. + deletion_protection_enabled (bool): + Optional. The delete operation will fail when + the value is set to true. + + This field is a member of `oneof`_ ``_deletion_protection_enabled``. """ class State(proto.Enum): @@ -466,6 +535,36 @@ class UpdateInfo(proto.Message): number=18, message=StateInfo, ) + node_type: "NodeType" = proto.Field( + proto.ENUM, + number=19, + enum="NodeType", + ) + persistence_config: "ClusterPersistenceConfig" = proto.Field( + proto.MESSAGE, + number=20, + message="ClusterPersistenceConfig", + ) + redis_configs: MutableMapping[str, str] = proto.MapField( + proto.STRING, + proto.STRING, + number=21, + ) + precise_size_gb: float = proto.Field( + proto.DOUBLE, + number=22, + optional=True, + ) + zone_distribution_config: "ZoneDistributionConfig" = proto.Field( + proto.MESSAGE, + number=23, + message="ZoneDistributionConfig", + ) + deletion_protection_enabled: bool = proto.Field( + proto.BOOL, + number=25, + optional=True, + ) class PscConfig(proto.Message): @@ -625,4 +724,242 @@ class OperationMetadata(proto.Message): ) +class CertificateAuthority(proto.Message): + r"""Redis cluster certificate authority + + .. _oneof: https://proto-plus-python.readthedocs.io/en/stable/fields.html#oneofs-mutually-exclusive-fields + + Attributes: + managed_server_ca (google.cloud.redis_cluster_v1beta1.types.CertificateAuthority.ManagedCertificateAuthority): + + This field is a member of `oneof`_ ``server_ca``. + name (str): + Identifier. Unique name of the resource in this scope + including project, location and cluster using the form: + ``projects/{project}/locations/{location}/clusters/{cluster}/certificateAuthority`` + """ + + class ManagedCertificateAuthority(proto.Message): + r""" + + Attributes: + ca_certs (MutableSequence[google.cloud.redis_cluster_v1beta1.types.CertificateAuthority.ManagedCertificateAuthority.CertChain]): + The PEM encoded CA certificate chains for + redis managed server authentication + """ + + class CertChain(proto.Message): + r""" + + Attributes: + certificates (MutableSequence[str]): + The certificates that form the CA chain, from + leaf to root order. + """ + + certificates: MutableSequence[str] = proto.RepeatedField( + proto.STRING, + number=1, + ) + + ca_certs: MutableSequence[ + "CertificateAuthority.ManagedCertificateAuthority.CertChain" + ] = proto.RepeatedField( + proto.MESSAGE, + number=1, + message="CertificateAuthority.ManagedCertificateAuthority.CertChain", + ) + + managed_server_ca: ManagedCertificateAuthority = proto.Field( + proto.MESSAGE, + number=1, + oneof="server_ca", + message=ManagedCertificateAuthority, + ) + name: str = proto.Field( + proto.STRING, + number=2, + ) + + +class ClusterPersistenceConfig(proto.Message): + r"""Configuration of the persistence functionality. + + Attributes: + mode (google.cloud.redis_cluster_v1beta1.types.ClusterPersistenceConfig.PersistenceMode): + Optional. The mode of persistence. + rdb_config (google.cloud.redis_cluster_v1beta1.types.ClusterPersistenceConfig.RDBConfig): + Optional. RDB configuration. This field will + be ignored if mode is not RDB. + aof_config (google.cloud.redis_cluster_v1beta1.types.ClusterPersistenceConfig.AOFConfig): + Optional. AOF configuration. This field will + be ignored if mode is not AOF. + """ + + class PersistenceMode(proto.Enum): + r"""Available persistence modes. + + Values: + PERSISTENCE_MODE_UNSPECIFIED (0): + Not set. + DISABLED (1): + Persistence is disabled, and any snapshot + data is deleted. + RDB (2): + RDB based persistence is enabled. + AOF (3): + AOF based persistence is enabled. + """ + PERSISTENCE_MODE_UNSPECIFIED = 0 + DISABLED = 1 + RDB = 2 + AOF = 3 + + class RDBConfig(proto.Message): + r"""Configuration of the RDB based persistence. + + Attributes: + rdb_snapshot_period (google.cloud.redis_cluster_v1beta1.types.ClusterPersistenceConfig.RDBConfig.SnapshotPeriod): + Optional. Period between RDB snapshots. + rdb_snapshot_start_time (google.protobuf.timestamp_pb2.Timestamp): + Optional. The time that the first snapshot + was/will be attempted, and to which future + snapshots will be aligned. If not provided, the + current time will be used. + """ + + class SnapshotPeriod(proto.Enum): + r"""Available snapshot periods. + + Values: + SNAPSHOT_PERIOD_UNSPECIFIED (0): + Not set. + ONE_HOUR (1): + One hour. + SIX_HOURS (2): + Six hours. + TWELVE_HOURS (3): + Twelve hours. + TWENTY_FOUR_HOURS (4): + Twenty four hours. + """ + SNAPSHOT_PERIOD_UNSPECIFIED = 0 + ONE_HOUR = 1 + SIX_HOURS = 2 + TWELVE_HOURS = 3 + TWENTY_FOUR_HOURS = 4 + + rdb_snapshot_period: "ClusterPersistenceConfig.RDBConfig.SnapshotPeriod" = ( + proto.Field( + proto.ENUM, + number=1, + enum="ClusterPersistenceConfig.RDBConfig.SnapshotPeriod", + ) + ) + rdb_snapshot_start_time: timestamp_pb2.Timestamp = proto.Field( + proto.MESSAGE, + number=2, + message=timestamp_pb2.Timestamp, + ) + + class AOFConfig(proto.Message): + r"""Configuration of the AOF based persistence. + + Attributes: + append_fsync (google.cloud.redis_cluster_v1beta1.types.ClusterPersistenceConfig.AOFConfig.AppendFsync): + Optional. fsync configuration. + """ + + class AppendFsync(proto.Enum): + r"""Available fsync modes. + + Values: + APPEND_FSYNC_UNSPECIFIED (0): + Not set. Default: EVERYSEC + NO (1): + Never fsync. Normally Linux will flush data + every 30 seconds with this configuration, but + it's up to the kernel's exact tuning. + EVERYSEC (2): + fsync every second. Fast enough, and you may + lose 1 second of data if there is a disaster + ALWAYS (3): + fsync every time new commands are appended to + the AOF. It has the best data loss protection at + the cost of performance + """ + APPEND_FSYNC_UNSPECIFIED = 0 + NO = 1 + EVERYSEC = 2 + ALWAYS = 3 + + append_fsync: "ClusterPersistenceConfig.AOFConfig.AppendFsync" = proto.Field( + proto.ENUM, + number=1, + enum="ClusterPersistenceConfig.AOFConfig.AppendFsync", + ) + + mode: PersistenceMode = proto.Field( + proto.ENUM, + number=1, + enum=PersistenceMode, + ) + rdb_config: RDBConfig = proto.Field( + proto.MESSAGE, + number=2, + message=RDBConfig, + ) + aof_config: AOFConfig = proto.Field( + proto.MESSAGE, + number=3, + message=AOFConfig, + ) + + +class ZoneDistributionConfig(proto.Message): + r"""Zone distribution config for allocation of cluster resources. + + Attributes: + mode (google.cloud.redis_cluster_v1beta1.types.ZoneDistributionConfig.ZoneDistributionMode): + Optional. The mode of zone distribution. Defaults to + MULTI_ZONE, when not specified. + zone (str): + Optional. When SINGLE ZONE distribution is selected, zone + field would be used to allocate all resources in that zone. + This is not applicable to MULTI_ZONE, and would be ignored + for MULTI_ZONE clusters. + """ + + class ZoneDistributionMode(proto.Enum): + r"""Defines various modes of zone distribution. + Currently supports two modes, can be expanded in future to + support more types of distribution modes. + design doc: go/same-zone-cluster + + Values: + ZONE_DISTRIBUTION_MODE_UNSPECIFIED (0): + Not Set. Default: MULTI_ZONE + MULTI_ZONE (1): + Distribute all resources across 3 zones + picked at random, within the region. + SINGLE_ZONE (2): + Distribute all resources in a single zone. + The zone field must be specified, when this mode + is selected. + """ + ZONE_DISTRIBUTION_MODE_UNSPECIFIED = 0 + MULTI_ZONE = 1 + SINGLE_ZONE = 2 + + mode: ZoneDistributionMode = proto.Field( + proto.ENUM, + number=1, + enum=ZoneDistributionMode, + ) + zone: str = proto.Field( + proto.STRING, + number=2, + ) + + __all__ = tuple(sorted(__protobuf__.manifest)) diff --git a/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py new file mode 100644 index 000000000000..17d24731b57e --- /dev/null +++ b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetClusterCertificateAuthority +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-redis-cluster + + +# [START redis_v1_generated_CloudRedisCluster_GetClusterCertificateAuthority_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import redis_cluster_v1 + + +async def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1.CloudRedisClusterAsyncClient() + + # Initialize request argument(s) + request = redis_cluster_v1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = await client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + +# [END redis_v1_generated_CloudRedisCluster_GetClusterCertificateAuthority_async] diff --git a/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py new file mode 100644 index 000000000000..e374fc6a5282 --- /dev/null +++ b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetClusterCertificateAuthority +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-redis-cluster + + +# [START redis_v1_generated_CloudRedisCluster_GetClusterCertificateAuthority_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import redis_cluster_v1 + + +def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1.CloudRedisClusterClient() + + # Initialize request argument(s) + request = redis_cluster_v1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + +# [END redis_v1_generated_CloudRedisCluster_GetClusterCertificateAuthority_sync] diff --git a/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py new file mode 100644 index 000000000000..14ad74faa0ee --- /dev/null +++ b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetClusterCertificateAuthority +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-redis-cluster + + +# [START redis_v1beta1_generated_CloudRedisCluster_GetClusterCertificateAuthority_async] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import redis_cluster_v1beta1 + + +async def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1beta1.CloudRedisClusterAsyncClient() + + # Initialize request argument(s) + request = redis_cluster_v1beta1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = await client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + +# [END redis_v1beta1_generated_CloudRedisCluster_GetClusterCertificateAuthority_async] diff --git a/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py new file mode 100644 index 000000000000..8c998f848da0 --- /dev/null +++ b/packages/google-cloud-redis-cluster/samples/generated_samples/redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py @@ -0,0 +1,52 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generated code. DO NOT EDIT! +# +# Snippet for GetClusterCertificateAuthority +# NOTE: This snippet has been automatically generated for illustrative purposes only. +# It may require modifications to work in your environment. + +# To install the latest published package dependency, execute the following: +# python3 -m pip install google-cloud-redis-cluster + + +# [START redis_v1beta1_generated_CloudRedisCluster_GetClusterCertificateAuthority_sync] +# This snippet has been automatically generated and should be regarded as a +# code template only. +# It will require modifications to work: +# - It may require correct/in-range values for request initialization. +# - It may require specifying regional endpoints when creating the service +# client as shown in: +# https://googleapis.dev/python/google-api-core/latest/client_options.html +from google.cloud import redis_cluster_v1beta1 + + +def sample_get_cluster_certificate_authority(): + # Create a client + client = redis_cluster_v1beta1.CloudRedisClusterClient() + + # Initialize request argument(s) + request = redis_cluster_v1beta1.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Make the request + response = client.get_cluster_certificate_authority(request=request) + + # Handle the response + print(response) + +# [END redis_v1beta1_generated_CloudRedisCluster_GetClusterCertificateAuthority_sync] diff --git a/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1.json b/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1.json index 8abe75407a19..b69528fcb001 100644 --- a/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1.json +++ b/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1.json @@ -349,6 +349,167 @@ ], "title": "redis_v1_generated_cloud_redis_cluster_delete_cluster_sync.py" }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.redis_cluster_v1.CloudRedisClusterAsyncClient", + "shortName": "CloudRedisClusterAsyncClient" + }, + "fullName": "google.cloud.redis_cluster_v1.CloudRedisClusterAsyncClient.get_cluster_certificate_authority", + "method": { + "fullName": "google.cloud.redis.cluster.v1.CloudRedisCluster.GetClusterCertificateAuthority", + "service": { + "fullName": "google.cloud.redis.cluster.v1.CloudRedisCluster", + "shortName": "CloudRedisCluster" + }, + "shortName": "GetClusterCertificateAuthority" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.redis_cluster_v1.types.GetClusterCertificateAuthorityRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.redis_cluster_v1.types.CertificateAuthority", + "shortName": "get_cluster_certificate_authority" + }, + "description": "Sample for GetClusterCertificateAuthority", + "file": "redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "redis_v1_generated_CloudRedisCluster_GetClusterCertificateAuthority_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.redis_cluster_v1.CloudRedisClusterClient", + "shortName": "CloudRedisClusterClient" + }, + "fullName": "google.cloud.redis_cluster_v1.CloudRedisClusterClient.get_cluster_certificate_authority", + "method": { + "fullName": "google.cloud.redis.cluster.v1.CloudRedisCluster.GetClusterCertificateAuthority", + "service": { + "fullName": "google.cloud.redis.cluster.v1.CloudRedisCluster", + "shortName": "CloudRedisCluster" + }, + "shortName": "GetClusterCertificateAuthority" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.redis_cluster_v1.types.GetClusterCertificateAuthorityRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.redis_cluster_v1.types.CertificateAuthority", + "shortName": "get_cluster_certificate_authority" + }, + "description": "Sample for GetClusterCertificateAuthority", + "file": "redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "redis_v1_generated_CloudRedisCluster_GetClusterCertificateAuthority_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "redis_v1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py" + }, { "canonical": true, "clientMethod": { diff --git a/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1beta1.json b/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1beta1.json index 534bbe2d1802..95ce3f58686e 100644 --- a/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1beta1.json +++ b/packages/google-cloud-redis-cluster/samples/generated_samples/snippet_metadata_google.cloud.redis.cluster.v1beta1.json @@ -349,6 +349,167 @@ ], "title": "redis_v1beta1_generated_cloud_redis_cluster_delete_cluster_sync.py" }, + { + "canonical": true, + "clientMethod": { + "async": true, + "client": { + "fullName": "google.cloud.redis_cluster_v1beta1.CloudRedisClusterAsyncClient", + "shortName": "CloudRedisClusterAsyncClient" + }, + "fullName": "google.cloud.redis_cluster_v1beta1.CloudRedisClusterAsyncClient.get_cluster_certificate_authority", + "method": { + "fullName": "google.cloud.redis.cluster.v1beta1.CloudRedisCluster.GetClusterCertificateAuthority", + "service": { + "fullName": "google.cloud.redis.cluster.v1beta1.CloudRedisCluster", + "shortName": "CloudRedisCluster" + }, + "shortName": "GetClusterCertificateAuthority" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.redis_cluster_v1beta1.types.GetClusterCertificateAuthorityRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.redis_cluster_v1beta1.types.CertificateAuthority", + "shortName": "get_cluster_certificate_authority" + }, + "description": "Sample for GetClusterCertificateAuthority", + "file": "redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "redis_v1beta1_generated_CloudRedisCluster_GetClusterCertificateAuthority_async", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_async.py" + }, + { + "canonical": true, + "clientMethod": { + "client": { + "fullName": "google.cloud.redis_cluster_v1beta1.CloudRedisClusterClient", + "shortName": "CloudRedisClusterClient" + }, + "fullName": "google.cloud.redis_cluster_v1beta1.CloudRedisClusterClient.get_cluster_certificate_authority", + "method": { + "fullName": "google.cloud.redis.cluster.v1beta1.CloudRedisCluster.GetClusterCertificateAuthority", + "service": { + "fullName": "google.cloud.redis.cluster.v1beta1.CloudRedisCluster", + "shortName": "CloudRedisCluster" + }, + "shortName": "GetClusterCertificateAuthority" + }, + "parameters": [ + { + "name": "request", + "type": "google.cloud.redis_cluster_v1beta1.types.GetClusterCertificateAuthorityRequest" + }, + { + "name": "name", + "type": "str" + }, + { + "name": "retry", + "type": "google.api_core.retry.Retry" + }, + { + "name": "timeout", + "type": "float" + }, + { + "name": "metadata", + "type": "Sequence[Tuple[str, str]" + } + ], + "resultType": "google.cloud.redis_cluster_v1beta1.types.CertificateAuthority", + "shortName": "get_cluster_certificate_authority" + }, + "description": "Sample for GetClusterCertificateAuthority", + "file": "redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py", + "language": "PYTHON", + "origin": "API_DEFINITION", + "regionTag": "redis_v1beta1_generated_CloudRedisCluster_GetClusterCertificateAuthority_sync", + "segments": [ + { + "end": 51, + "start": 27, + "type": "FULL" + }, + { + "end": 51, + "start": 27, + "type": "SHORT" + }, + { + "end": 40, + "start": 38, + "type": "CLIENT_INITIALIZATION" + }, + { + "end": 45, + "start": 41, + "type": "REQUEST_INITIALIZATION" + }, + { + "end": 48, + "start": 46, + "type": "REQUEST_EXECUTION" + }, + { + "end": 52, + "start": 49, + "type": "RESPONSE_HANDLING" + } + ], + "title": "redis_v1beta1_generated_cloud_redis_cluster_get_cluster_certificate_authority_sync.py" + }, { "canonical": true, "clientMethod": { diff --git a/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1_keywords.py b/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1_keywords.py index d7116f5cea62..287e2046204c 100644 --- a/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1_keywords.py +++ b/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1_keywords.py @@ -42,6 +42,7 @@ class redis_clusterCallTransformer(cst.CSTTransformer): 'create_cluster': ('parent', 'cluster_id', 'cluster', 'request_id', ), 'delete_cluster': ('name', 'request_id', ), 'get_cluster': ('name', ), + 'get_cluster_certificate_authority': ('name', ), 'list_clusters': ('parent', 'page_size', 'page_token', ), 'update_cluster': ('update_mask', 'cluster', 'request_id', ), } diff --git a/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1beta1_keywords.py b/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1beta1_keywords.py index d7116f5cea62..287e2046204c 100644 --- a/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1beta1_keywords.py +++ b/packages/google-cloud-redis-cluster/scripts/fixup_redis_cluster_v1beta1_keywords.py @@ -42,6 +42,7 @@ class redis_clusterCallTransformer(cst.CSTTransformer): 'create_cluster': ('parent', 'cluster_id', 'cluster', 'request_id', ), 'delete_cluster': ('name', 'request_id', ), 'get_cluster': ('name', ), + 'get_cluster_certificate_authority': ('name', ), 'list_clusters': ('parent', 'page_size', 'page_token', ), 'update_cluster': ('update_mask', 'cluster', 'request_id', ), } diff --git a/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1/test_cloud_redis_cluster.py b/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1/test_cloud_redis_cluster.py index 50ed11a09afe..a12c5b80831f 100644 --- a/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1/test_cloud_redis_cluster.py +++ b/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1/test_cloud_redis_cluster.py @@ -1765,6 +1765,9 @@ def test_get_cluster(request_type, transport: str = "grpc"): transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, size_gb=739, shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) response = client.get_cluster(request) @@ -1790,6 +1793,9 @@ def test_get_cluster(request_type, transport: str = "grpc"): ) assert response.size_gb == 739 assert response.shard_count == 1178 + assert response.node_type == cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO + assert math.isclose(response.precise_size_gb, 0.15810000000000002, rel_tol=1e-6) + assert response.deletion_protection_enabled is True def test_get_cluster_empty_call(): @@ -1896,6 +1902,9 @@ async def test_get_cluster_empty_call_async(): transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, size_gb=739, shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) ) response = await client.get_cluster() @@ -1976,6 +1985,9 @@ async def test_get_cluster_async( transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, size_gb=739, shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) ) response = await client.get_cluster(request) @@ -2002,6 +2014,9 @@ async def test_get_cluster_async( ) assert response.size_gb == 739 assert response.shard_count == 1178 + assert response.node_type == cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO + assert math.isclose(response.precise_size_gb, 0.15810000000000002, rel_tol=1e-6) + assert response.deletion_protection_enabled is True @pytest.mark.asyncio @@ -3298,6 +3313,399 @@ async def test_create_cluster_flattened_error_async(): ) +@pytest.mark.parametrize( + "request_type", + [ + cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + dict, + ], +) +def test_get_cluster_certificate_authority(request_type, transport: str = "grpc"): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) + response = client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, cloud_redis_cluster.CertificateAuthority) + assert response.name == "name_value" + + +def test_get_cluster_certificate_authority_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_cluster_certificate_authority() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + +def test_get_cluster_certificate_authority_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_cluster_certificate_authority(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == cloud_redis_cluster.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + +def test_get_cluster_certificate_authority_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_cluster_certificate_authority + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_cluster_certificate_authority + ] = mock_rpc + request = {} + client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_cluster_certificate_authority(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_empty_call_async(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) + ) + response = await client.get_cluster_certificate_authority() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_cluster_certificate_authority + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + class AwaitableMock(mock.AsyncMock): + def __await__(self): + self.await_count += 1 + return iter([]) + + mock_object = AwaitableMock() + client._client._transport._wrapped_methods[ + client._client._transport.get_cluster_certificate_authority + ] = mock_object + + request = {} + await client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_object.call_count == 1 + + await client.get_cluster_certificate_authority(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_object.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_async( + transport: str = "grpc_asyncio", + request_type=cloud_redis_cluster.GetClusterCertificateAuthorityRequest, +): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) + ) + response = await client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, cloud_redis_cluster.CertificateAuthority) + assert response.name == "name_value" + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_async_from_dict(): + await test_get_cluster_certificate_authority_async(request_type=dict) + + +def test_get_cluster_certificate_authority_field_headers(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value = cloud_redis_cluster.CertificateAuthority() + client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_field_headers_async(): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority() + ) + await client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_cluster_certificate_authority_flattened(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = cloud_redis_cluster.CertificateAuthority() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_cluster_certificate_authority( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +def test_get_cluster_certificate_authority_flattened_error(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_cluster_certificate_authority( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest(), + name="name_value", + ) + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_flattened_async(): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = cloud_redis_cluster.CertificateAuthority() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_cluster_certificate_authority( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_flattened_error_async(): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_cluster_certificate_authority( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest(), + name="name_value", + ) + + @pytest.mark.parametrize( "request_type", [ @@ -3305,42 +3713,442 @@ async def test_create_cluster_flattened_error_async(): dict, ], ) -def test_list_clusters_rest(request_type): +def test_list_clusters_rest(request_type): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = cloud_redis_cluster.ListClustersResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_clusters(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListClustersPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_clusters_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_clusters in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.list_clusters] = mock_rpc + + request = {} + client.list_clusters(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_clusters(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_clusters_rest_required_fields( + request_type=cloud_redis_cluster.ListClustersRequest, +): + transport_class = transports.CloudRedisClusterRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_clusters._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_clusters._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = cloud_redis_cluster.ListClustersResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_clusters(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_clusters_rest_unset_required_fields(): + transport = transports.CloudRedisClusterRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_clusters._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_clusters_rest_interceptors(null_interceptor): + transport = transports.CloudRedisClusterRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.CloudRedisClusterRestInterceptor(), + ) + client = CloudRedisClusterClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.CloudRedisClusterRestInterceptor, "post_list_clusters" + ) as post, mock.patch.object( + transports.CloudRedisClusterRestInterceptor, "pre_list_clusters" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = cloud_redis_cluster.ListClustersRequest.pb( + cloud_redis_cluster.ListClustersRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = cloud_redis_cluster.ListClustersResponse.to_json( + cloud_redis_cluster.ListClustersResponse() + ) + + request = cloud_redis_cluster.ListClustersRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = cloud_redis_cluster.ListClustersResponse() + + client.list_clusters( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_clusters_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.ListClustersRequest +): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_clusters(request) + + +def test_list_clusters_rest_flattened(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = cloud_redis_cluster.ListClustersResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_clusters(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/clusters" % client.transport._host, + args[1], + ) + + +def test_list_clusters_rest_flattened_error(transport: str = "rest"): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_clusters( + cloud_redis_cluster.ListClustersRequest(), + parent="parent_value", + ) + + +def test_list_clusters_rest_pager(transport: str = "rest"): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + cloud_redis_cluster.ListClustersResponse( + clusters=[ + cloud_redis_cluster.Cluster(), + cloud_redis_cluster.Cluster(), + cloud_redis_cluster.Cluster(), + ], + next_page_token="abc", + ), + cloud_redis_cluster.ListClustersResponse( + clusters=[], + next_page_token="def", + ), + cloud_redis_cluster.ListClustersResponse( + clusters=[ + cloud_redis_cluster.Cluster(), + ], + next_page_token="ghi", + ), + cloud_redis_cluster.ListClustersResponse( + clusters=[ + cloud_redis_cluster.Cluster(), + cloud_redis_cluster.Cluster(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + cloud_redis_cluster.ListClustersResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_clusters(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, cloud_redis_cluster.Cluster) for i in results) + + pages = list(client.list_clusters(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + cloud_redis_cluster.GetClusterRequest, + dict, + ], +) +def test_get_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.ListClustersResponse( - next_page_token="next_page_token_value", - unreachable=["unreachable_value"], + return_value = cloud_redis_cluster.Cluster( + name="name_value", + state=cloud_redis_cluster.Cluster.State.CREATING, + uid="uid_value", + replica_count=1384, + authorization_mode=cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH, + transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, + size_gb=739, + shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 # Convert return value to protobuf type - return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_clusters(request) + response = client.get_cluster(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListClustersPager) - assert response.next_page_token == "next_page_token_value" - assert response.unreachable == ["unreachable_value"] + assert isinstance(response, cloud_redis_cluster.Cluster) + assert response.name == "name_value" + assert response.state == cloud_redis_cluster.Cluster.State.CREATING + assert response.uid == "uid_value" + assert response.replica_count == 1384 + assert ( + response.authorization_mode + == cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH + ) + assert ( + response.transit_encryption_mode + == cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED + ) + assert response.size_gb == 739 + assert response.shard_count == 1178 + assert response.node_type == cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO + assert math.isclose(response.precise_size_gb, 0.15810000000000002, rel_tol=1e-6) + assert response.deletion_protection_enabled is True -def test_list_clusters_rest_use_cached_wrapped_rpc(): +def test_get_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -3354,35 +4162,35 @@ def test_list_clusters_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.list_clusters in client._transport._wrapped_methods + assert client._transport.get_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.list_clusters] = mock_rpc + client._transport._wrapped_methods[client._transport.get_cluster] = mock_rpc request = {} - client.list_clusters(request) + client.get_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_clusters(request) + client.get_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_clusters_rest_required_fields( - request_type=cloud_redis_cluster.ListClustersRequest, +def test_get_cluster_rest_required_fields( + request_type=cloud_redis_cluster.GetClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["parent"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -3393,28 +4201,21 @@ def test_list_clusters_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_clusters._get_unset_required_fields(jsonified_request) + ).get_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["parent"] = "parent_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_clusters._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "page_size", - "page_token", - ) - ) + ).get_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3423,7 +4224,7 @@ def test_list_clusters_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.ListClustersResponse() + return_value = cloud_redis_cluster.Cluster() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -3444,38 +4245,30 @@ def test_list_clusters_rest_required_fields( response_value.status_code = 200 # Convert return value to protobuf type - return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_clusters(request) + response = client.get_cluster(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_list_clusters_rest_unset_required_fields(): +def test_get_cluster_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.list_clusters._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "pageSize", - "pageToken", - ) - ) - & set(("parent",)) - ) + unset_fields = transport.get_cluster._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_clusters_rest_interceptors(null_interceptor): +def test_get_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -3488,14 +4281,14 @@ def test_list_clusters_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_list_clusters" + transports.CloudRedisClusterRestInterceptor, "post_get_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_list_clusters" + transports.CloudRedisClusterRestInterceptor, "pre_get_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.ListClustersRequest.pb( - cloud_redis_cluster.ListClustersRequest() + pb_message = cloud_redis_cluster.GetClusterRequest.pb( + cloud_redis_cluster.GetClusterRequest() ) transcode.return_value = { "method": "post", @@ -3507,19 +4300,19 @@ def test_list_clusters_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = cloud_redis_cluster.ListClustersResponse.to_json( - cloud_redis_cluster.ListClustersResponse() + req.return_value._content = cloud_redis_cluster.Cluster.to_json( + cloud_redis_cluster.Cluster() ) - request = cloud_redis_cluster.ListClustersRequest() + request = cloud_redis_cluster.GetClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = cloud_redis_cluster.ListClustersResponse() + post.return_value = cloud_redis_cluster.Cluster() - client.list_clusters( + client.get_cluster( request, metadata=[ ("key", "val"), @@ -3531,8 +4324,8 @@ def test_list_clusters_rest_interceptors(null_interceptor): post.assert_called_once() -def test_list_clusters_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.ListClustersRequest +def test_get_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.GetClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3540,7 +4333,7 @@ def test_list_clusters_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -3552,10 +4345,10 @@ def test_list_clusters_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.list_clusters(request) + client.get_cluster(request) -def test_list_clusters_rest_flattened(): +def test_get_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -3564,14 +4357,14 @@ def test_list_clusters_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.ListClustersResponse() + return_value = cloud_redis_cluster.Cluster() # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} # get truthy value for each flattened field mock_args = dict( - parent="parent_value", + name="name_value", ) mock_args.update(sample_request) @@ -3579,24 +4372,24 @@ def test_list_clusters_rest_flattened(): response_value = Response() response_value.status_code = 200 # Convert return value to protobuf type - return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.list_clusters(**mock_args) + client.get_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/clusters" % client.transport._host, + "%s/v1/{name=projects/*/locations/*/clusters/*}" % client.transport._host, args[1], ) -def test_list_clusters_rest_flattened_error(transport: str = "rest"): +def test_get_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -3605,136 +4398,160 @@ def test_list_clusters_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_clusters( - cloud_redis_cluster.ListClustersRequest(), - parent="parent_value", + client.get_cluster( + cloud_redis_cluster.GetClusterRequest(), + name="name_value", ) -def test_list_clusters_rest_pager(transport: str = "rest"): +def test_get_cluster_rest_error(): client = CloudRedisClusterClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - cloud_redis_cluster.ListClustersResponse( - clusters=[ - cloud_redis_cluster.Cluster(), - cloud_redis_cluster.Cluster(), - cloud_redis_cluster.Cluster(), - ], - next_page_token="abc", - ), - cloud_redis_cluster.ListClustersResponse( - clusters=[], - next_page_token="def", - ), - cloud_redis_cluster.ListClustersResponse( - clusters=[ - cloud_redis_cluster.Cluster(), - ], - next_page_token="ghi", - ), - cloud_redis_cluster.ListClustersResponse( - clusters=[ - cloud_redis_cluster.Cluster(), - cloud_redis_cluster.Cluster(), - ], - ), - ) - # Two responses for two calls - response = response + response - - # Wrap the values into proper Response objs - response = tuple( - cloud_redis_cluster.ListClustersResponse.to_json(x) for x in response - ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} - - pager = client.list_clusters(request=sample_request) - - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, cloud_redis_cluster.Cluster) for i in results) - - pages = list(client.list_clusters(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token - @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.GetClusterRequest, + cloud_redis_cluster.UpdateClusterRequest, dict, ], ) -def test_get_cluster_rest(request_type): +def test_update_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = { + "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} + } + request_init["cluster"] = { + "name": "projects/sample1/locations/sample2/clusters/sample3", + "create_time": {"seconds": 751, "nanos": 543}, + "state": 1, + "uid": "uid_value", + "replica_count": 1384, + "authorization_mode": 1, + "transit_encryption_mode": 1, + "size_gb": 739, + "shard_count": 1178, + "psc_configs": [{"network": "network_value"}], + "discovery_endpoints": [ + {"address": "address_value", "port": 453, "psc_config": {}} + ], + "psc_connections": [ + { + "psc_connection_id": "psc_connection_id_value", + "address": "address_value", + "forwarding_rule": "forwarding_rule_value", + "project_id": "project_id_value", + "network": "network_value", + } + ], + "state_info": { + "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} + }, + "node_type": 1, + "persistence_config": { + "mode": 1, + "rdb_config": {"rdb_snapshot_period": 1, "rdb_snapshot_start_time": {}}, + "aof_config": {"append_fsync": 1}, + }, + "redis_configs": {}, + "precise_size_gb": 0.15810000000000002, + "zone_distribution_config": {"mode": 1, "zone": "zone_value"}, + "deletion_protection_enabled": True, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = cloud_redis_cluster.UpdateClusterRequest.meta.fields["cluster"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["cluster"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["cluster"][field])): + del request_init["cluster"][field][i][subfield] + else: + del request_init["cluster"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.Cluster( - name="name_value", - state=cloud_redis_cluster.Cluster.State.CREATING, - uid="uid_value", - replica_count=1384, - authorization_mode=cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH, - transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, - size_gb=739, - shard_count=1178, - ) + return_value = operations_pb2.Operation(name="operations/spam") # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type - return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_cluster(request) + response = client.update_cluster(request) # Establish that the response is the type that we expect. - assert isinstance(response, cloud_redis_cluster.Cluster) - assert response.name == "name_value" - assert response.state == cloud_redis_cluster.Cluster.State.CREATING - assert response.uid == "uid_value" - assert response.replica_count == 1384 - assert ( - response.authorization_mode - == cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH - ) - assert ( - response.transit_encryption_mode - == cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED - ) - assert response.size_gb == 739 - assert response.shard_count == 1178 + assert response.operation.name == "operations/spam" -def test_get_cluster_rest_use_cached_wrapped_rpc(): +def test_update_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -3748,35 +4565,38 @@ def test_get_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.get_cluster in client._transport._wrapped_methods + assert client._transport.update_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.get_cluster] = mock_rpc + client._transport._wrapped_methods[client._transport.update_cluster] = mock_rpc request = {} - client.get_cluster(request) + client.update_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.get_cluster(request) + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.update_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_get_cluster_rest_required_fields( - request_type=cloud_redis_cluster.GetClusterRequest, +def test_update_cluster_rest_required_fields( + request_type=cloud_redis_cluster.UpdateClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -3787,21 +4607,24 @@ def test_get_cluster_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_cluster._get_unset_required_fields(jsonified_request) + ).update_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["name"] = "name_value" - unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_cluster._get_unset_required_fields(jsonified_request) + ).update_cluster._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "update_mask", + ) + ) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3810,7 +4633,7 @@ def test_get_cluster_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.Cluster() + return_value = operations_pb2.Operation(name="operations/spam") # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -3822,39 +4645,50 @@ def test_get_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "get", + "method": "patch", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_cluster(request) + response = client.update_cluster(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_get_cluster_rest_unset_required_fields(): +def test_update_cluster_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.get_cluster._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.update_cluster._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "updateMask", + ) + ) + & set( + ( + "updateMask", + "cluster", + ) + ) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_cluster_rest_interceptors(null_interceptor): +def test_update_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -3867,14 +4701,16 @@ def test_get_cluster_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_get_cluster" + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.CloudRedisClusterRestInterceptor, "post_update_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_get_cluster" + transports.CloudRedisClusterRestInterceptor, "pre_update_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.GetClusterRequest.pb( - cloud_redis_cluster.GetClusterRequest() + pb_message = cloud_redis_cluster.UpdateClusterRequest.pb( + cloud_redis_cluster.UpdateClusterRequest() ) transcode.return_value = { "method": "post", @@ -3886,19 +4722,19 @@ def test_get_cluster_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = cloud_redis_cluster.Cluster.to_json( - cloud_redis_cluster.Cluster() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() ) - request = cloud_redis_cluster.GetClusterRequest() + request = cloud_redis_cluster.UpdateClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = cloud_redis_cluster.Cluster() + post.return_value = operations_pb2.Operation() - client.get_cluster( + client.update_cluster( request, metadata=[ ("key", "val"), @@ -3910,8 +4746,8 @@ def test_get_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_get_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.GetClusterRequest +def test_update_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.UpdateClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3919,7 +4755,9 @@ def test_get_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = { + "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -3931,10 +4769,10 @@ def test_get_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.get_cluster(request) + client.update_cluster(request) -def test_get_cluster_rest_flattened(): +def test_update_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -3943,39 +4781,41 @@ def test_get_cluster_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.Cluster() + return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + sample_request = { + "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} + } # get truthy value for each flattened field mock_args = dict( - name="name_value", + cluster=cloud_redis_cluster.Cluster(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type - return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.get_cluster(**mock_args) + client.update_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/clusters/*}" % client.transport._host, + "%s/v1/{cluster.name=projects/*/locations/*/clusters/*}" + % client.transport._host, args[1], ) -def test_get_cluster_rest_flattened_error(transport: str = "rest"): +def test_update_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -3984,13 +4824,14 @@ def test_get_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_cluster( - cloud_redis_cluster.GetClusterRequest(), - name="name_value", + client.update_cluster( + cloud_redis_cluster.UpdateClusterRequest(), + cluster=cloud_redis_cluster.Cluster(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_get_cluster_rest_error(): +def test_update_cluster_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -3999,114 +4840,18 @@ def test_get_cluster_rest_error(): @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.UpdateClusterRequest, + cloud_redis_cluster.DeleteClusterRequest, dict, ], ) -def test_update_cluster_rest(request_type): +def test_delete_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = { - "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} - } - request_init["cluster"] = { - "name": "projects/sample1/locations/sample2/clusters/sample3", - "create_time": {"seconds": 751, "nanos": 543}, - "state": 1, - "uid": "uid_value", - "replica_count": 1384, - "authorization_mode": 1, - "transit_encryption_mode": 1, - "size_gb": 739, - "shard_count": 1178, - "psc_configs": [{"network": "network_value"}], - "discovery_endpoints": [ - {"address": "address_value", "port": 453, "psc_config": {}} - ], - "psc_connections": [ - { - "psc_connection_id": "psc_connection_id_value", - "address": "address_value", - "forwarding_rule": "forwarding_rule_value", - "project_id": "project_id_value", - "network": "network_value", - } - ], - "state_info": { - "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} - }, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 - - # Determine if the message type is proto-plus or protobuf - test_field = cloud_redis_cluster.UpdateClusterRequest.meta.fields["cluster"] - - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] - - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["cluster"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["cluster"][field])): - del request_init["cluster"][field][i][subfield] - else: - del request_init["cluster"][field][subfield] + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -4121,13 +4866,13 @@ def get_message_fields(field): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.update_cluster(request) + response = client.delete_cluster(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_update_cluster_rest_use_cached_wrapped_rpc(): +def test_delete_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -4141,17 +4886,17 @@ def test_update_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.update_cluster in client._transport._wrapped_methods + assert client._transport.delete_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.update_cluster] = mock_rpc + client._transport._wrapped_methods[client._transport.delete_cluster] = mock_rpc request = {} - client.update_cluster(request) + client.delete_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 @@ -4160,19 +4905,20 @@ def test_update_cluster_rest_use_cached_wrapped_rpc(): # subsequent calls should use the cached wrapper wrapper_fn.reset_mock() - client.update_cluster(request) + client.delete_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_update_cluster_rest_required_fields( - request_type=cloud_redis_cluster.UpdateClusterRequest, +def test_delete_cluster_rest_required_fields( + request_type=cloud_redis_cluster.DeleteClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -4183,24 +4929,23 @@ def test_update_cluster_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).update_cluster._get_unset_required_fields(jsonified_request) + ).delete_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present + jsonified_request["name"] = "name_value" + unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).update_cluster._get_unset_required_fields(jsonified_request) + ).delete_cluster._get_unset_required_fields(jsonified_request) # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "request_id", - "update_mask", - ) - ) + assert not set(unset_fields) - set(("request_id",)) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4221,10 +4966,9 @@ def test_update_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "patch", + "method": "delete", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() @@ -4234,37 +4978,24 @@ def test_update_cluster_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.update_cluster(request) + response = client.delete_cluster(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_update_cluster_rest_unset_required_fields(): +def test_delete_cluster_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.update_cluster._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "requestId", - "updateMask", - ) - ) - & set( - ( - "updateMask", - "cluster", - ) - ) - ) - + unset_fields = transport.delete_cluster._get_unset_required_fields({}) + assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_cluster_rest_interceptors(null_interceptor): +def test_delete_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -4279,14 +5010,14 @@ def test_update_cluster_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_update_cluster" + transports.CloudRedisClusterRestInterceptor, "post_delete_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_update_cluster" + transports.CloudRedisClusterRestInterceptor, "pre_delete_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.UpdateClusterRequest.pb( - cloud_redis_cluster.UpdateClusterRequest() + pb_message = cloud_redis_cluster.DeleteClusterRequest.pb( + cloud_redis_cluster.DeleteClusterRequest() ) transcode.return_value = { "method": "post", @@ -4302,7 +5033,7 @@ def test_update_cluster_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = cloud_redis_cluster.UpdateClusterRequest() + request = cloud_redis_cluster.DeleteClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -4310,7 +5041,7 @@ def test_update_cluster_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.update_cluster( + client.delete_cluster( request, metadata=[ ("key", "val"), @@ -4322,8 +5053,8 @@ def test_update_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_update_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.UpdateClusterRequest +def test_delete_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.DeleteClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4331,9 +5062,7 @@ def test_update_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = { - "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} - } + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -4345,10 +5074,10 @@ def test_update_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.update_cluster(request) + client.delete_cluster(request) -def test_update_cluster_rest_flattened(): +def test_delete_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -4360,14 +5089,11 @@ def test_update_cluster_rest_flattened(): return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method - sample_request = { - "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} - } + sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} # get truthy value for each flattened field mock_args = dict( - cluster=cloud_redis_cluster.Cluster(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + name="name_value", ) mock_args.update(sample_request) @@ -4378,20 +5104,19 @@ def test_update_cluster_rest_flattened(): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.update_cluster(**mock_args) + client.delete_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{cluster.name=projects/*/locations/*/clusters/*}" - % client.transport._host, + "%s/v1/{name=projects/*/locations/*/clusters/*}" % client.transport._host, args[1], ) -def test_update_cluster_rest_flattened_error(transport: str = "rest"): +def test_delete_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -4400,14 +5125,13 @@ def test_update_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_cluster( - cloud_redis_cluster.UpdateClusterRequest(), - cluster=cloud_redis_cluster.Cluster(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + client.delete_cluster( + cloud_redis_cluster.DeleteClusterRequest(), + name="name_value", ) -def test_update_cluster_rest_error(): +def test_delete_cluster_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -4416,18 +5140,122 @@ def test_update_cluster_rest_error(): @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.DeleteClusterRequest, + cloud_redis_cluster.CreateClusterRequest, dict, ], ) -def test_delete_cluster_rest(request_type): +def test_create_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["cluster"] = { + "name": "name_value", + "create_time": {"seconds": 751, "nanos": 543}, + "state": 1, + "uid": "uid_value", + "replica_count": 1384, + "authorization_mode": 1, + "transit_encryption_mode": 1, + "size_gb": 739, + "shard_count": 1178, + "psc_configs": [{"network": "network_value"}], + "discovery_endpoints": [ + {"address": "address_value", "port": 453, "psc_config": {}} + ], + "psc_connections": [ + { + "psc_connection_id": "psc_connection_id_value", + "address": "address_value", + "forwarding_rule": "forwarding_rule_value", + "project_id": "project_id_value", + "network": "network_value", + } + ], + "state_info": { + "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} + }, + "node_type": 1, + "persistence_config": { + "mode": 1, + "rdb_config": {"rdb_snapshot_period": 1, "rdb_snapshot_start_time": {}}, + "aof_config": {"append_fsync": 1}, + }, + "redis_configs": {}, + "precise_size_gb": 0.15810000000000002, + "zone_distribution_config": {"mode": 1, "zone": "zone_value"}, + "deletion_protection_enabled": True, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = cloud_redis_cluster.CreateClusterRequest.meta.fields["cluster"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["cluster"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["cluster"][field])): + del request_init["cluster"][field][i][subfield] + else: + del request_init["cluster"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -4442,13 +5270,13 @@ def test_delete_cluster_rest(request_type): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_cluster(request) + response = client.create_cluster(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_delete_cluster_rest_use_cached_wrapped_rpc(): +def test_create_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -4462,17 +5290,17 @@ def test_delete_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.delete_cluster in client._transport._wrapped_methods + assert client._transport.create_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.delete_cluster] = mock_rpc + client._transport._wrapped_methods[client._transport.create_cluster] = mock_rpc request = {} - client.delete_cluster(request) + client.create_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 @@ -4481,20 +5309,21 @@ def test_delete_cluster_rest_use_cached_wrapped_rpc(): # subsequent calls should use the cached wrapper wrapper_fn.reset_mock() - client.delete_cluster(request) + client.create_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_delete_cluster_rest_required_fields( - request_type=cloud_redis_cluster.DeleteClusterRequest, +def test_create_cluster_rest_required_fields( + request_type=cloud_redis_cluster.CreateClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["name"] = "" + request_init["parent"] = "" + request_init["cluster_id"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -4502,26 +5331,37 @@ def test_delete_cluster_rest_required_fields( ) # verify fields with default values are dropped + assert "clusterId" not in jsonified_request unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_cluster._get_unset_required_fields(jsonified_request) + ).create_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present + assert "clusterId" in jsonified_request + assert jsonified_request["clusterId"] == request_init["cluster_id"] - jsonified_request["name"] = "name_value" + jsonified_request["parent"] = "parent_value" + jsonified_request["clusterId"] = "cluster_id_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_cluster._get_unset_required_fields(jsonified_request) + ).create_cluster._get_unset_required_fields(jsonified_request) # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("request_id",)) + assert not set(unset_fields) - set( + ( + "cluster_id", + "request_id", + ) + ) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "clusterId" in jsonified_request + assert jsonified_request["clusterId"] == "cluster_id_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4542,9 +5382,10 @@ def test_delete_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "delete", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() @@ -4554,24 +5395,44 @@ def test_delete_cluster_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_cluster(request) + response = client.create_cluster(request) - expected_params = [("$alt", "json;enum-encoding=int")] + expected_params = [ + ( + "clusterId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_delete_cluster_rest_unset_required_fields(): +def test_create_cluster_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.delete_cluster._get_unset_required_fields({}) - assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + unset_fields = transport.create_cluster._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "clusterId", + "requestId", + ) + ) + & set( + ( + "parent", + "clusterId", + "cluster", + ) + ) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_cluster_rest_interceptors(null_interceptor): +def test_create_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -4586,14 +5447,14 @@ def test_delete_cluster_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_delete_cluster" + transports.CloudRedisClusterRestInterceptor, "post_create_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_delete_cluster" + transports.CloudRedisClusterRestInterceptor, "pre_create_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.DeleteClusterRequest.pb( - cloud_redis_cluster.DeleteClusterRequest() + pb_message = cloud_redis_cluster.CreateClusterRequest.pb( + cloud_redis_cluster.CreateClusterRequest() ) transcode.return_value = { "method": "post", @@ -4609,7 +5470,7 @@ def test_delete_cluster_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = cloud_redis_cluster.DeleteClusterRequest() + request = cloud_redis_cluster.CreateClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -4617,7 +5478,7 @@ def test_delete_cluster_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.delete_cluster( + client.create_cluster( request, metadata=[ ("key", "val"), @@ -4629,8 +5490,8 @@ def test_delete_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_delete_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.DeleteClusterRequest +def test_create_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.CreateClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4638,7 +5499,7 @@ def test_delete_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -4650,10 +5511,10 @@ def test_delete_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.delete_cluster(request) + client.create_cluster(request) -def test_delete_cluster_rest_flattened(): +def test_create_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -4665,11 +5526,13 @@ def test_delete_cluster_rest_flattened(): return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + sample_request = {"parent": "projects/sample1/locations/sample2"} # get truthy value for each flattened field mock_args = dict( - name="name_value", + parent="parent_value", + cluster=cloud_redis_cluster.Cluster(name="name_value"), + cluster_id="cluster_id_value", ) mock_args.update(sample_request) @@ -4680,19 +5543,19 @@ def test_delete_cluster_rest_flattened(): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.delete_cluster(**mock_args) + client.create_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{name=projects/*/locations/*/clusters/*}" % client.transport._host, + "%s/v1/{parent=projects/*/locations/*}/clusters" % client.transport._host, args[1], ) -def test_delete_cluster_rest_flattened_error(transport: str = "rest"): +def test_create_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -4701,13 +5564,15 @@ def test_delete_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_cluster( - cloud_redis_cluster.DeleteClusterRequest(), - name="name_value", + client.create_cluster( + cloud_redis_cluster.CreateClusterRequest(), + parent="parent_value", + cluster=cloud_redis_cluster.Cluster(name="name_value"), + cluster_id="cluster_id_value", ) -def test_delete_cluster_rest_error(): +def test_create_cluster_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -4716,133 +5581,46 @@ def test_delete_cluster_rest_error(): @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.CreateClusterRequest, + cloud_redis_cluster.GetClusterCertificateAuthorityRequest, dict, ], ) -def test_create_cluster_rest(request_type): +def test_get_cluster_certificate_authority_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["cluster"] = { - "name": "name_value", - "create_time": {"seconds": 751, "nanos": 543}, - "state": 1, - "uid": "uid_value", - "replica_count": 1384, - "authorization_mode": 1, - "transit_encryption_mode": 1, - "size_gb": 739, - "shard_count": 1178, - "psc_configs": [{"network": "network_value"}], - "discovery_endpoints": [ - {"address": "address_value", "port": 453, "psc_config": {}} - ], - "psc_connections": [ - { - "psc_connection_id": "psc_connection_id_value", - "address": "address_value", - "forwarding_rule": "forwarding_rule_value", - "project_id": "project_id_value", - "network": "network_value", - } - ], - "state_info": { - "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} - }, + request_init = { + "name": "projects/sample1/locations/sample2/clusters/sample3/certificateAuthority" } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 - - # Determine if the message type is proto-plus or protobuf - test_field = cloud_redis_cluster.CreateClusterRequest.meta.fields["cluster"] - - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] - - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["cluster"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["cluster"][field])): - del request_init["cluster"][field][i][subfield] - else: - del request_init["cluster"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.CertificateAuthority.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.create_cluster(request) + response = client.get_cluster_certificate_authority(request) # Establish that the response is the type that we expect. - assert response.operation.name == "operations/spam" + assert isinstance(response, cloud_redis_cluster.CertificateAuthority) + assert response.name == "name_value" -def test_create_cluster_rest_use_cached_wrapped_rpc(): +def test_get_cluster_certificate_authority_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -4856,40 +5634,40 @@ def test_create_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.create_cluster in client._transport._wrapped_methods + assert ( + client._transport.get_cluster_certificate_authority + in client._transport._wrapped_methods + ) # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.create_cluster] = mock_rpc + client._transport._wrapped_methods[ + client._transport.get_cluster_certificate_authority + ] = mock_rpc request = {} - client.create_cluster(request) + client.get_cluster_certificate_authority(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper - wrapper_fn.reset_mock() - - client.create_cluster(request) + client.get_cluster_certificate_authority(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_create_cluster_rest_required_fields( - request_type=cloud_redis_cluster.CreateClusterRequest, +def test_get_cluster_certificate_authority_rest_required_fields( + request_type=cloud_redis_cluster.GetClusterCertificateAuthorityRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["parent"] = "" - request_init["cluster_id"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -4897,37 +5675,24 @@ def test_create_cluster_rest_required_fields( ) # verify fields with default values are dropped - assert "clusterId" not in jsonified_request unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).create_cluster._get_unset_required_fields(jsonified_request) + ).get_cluster_certificate_authority._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - assert "clusterId" in jsonified_request - assert jsonified_request["clusterId"] == request_init["cluster_id"] - jsonified_request["parent"] = "parent_value" - jsonified_request["clusterId"] = "cluster_id_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).create_cluster._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "cluster_id", - "request_id", - ) - ) + ).get_cluster_certificate_authority._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" - assert "clusterId" in jsonified_request - assert jsonified_request["clusterId"] == "cluster_id_value" + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4936,7 +5701,7 @@ def test_create_cluster_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = cloud_redis_cluster.CertificateAuthority() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -4948,57 +5713,41 @@ def test_create_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "get", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = cloud_redis_cluster.CertificateAuthority.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.create_cluster(request) + response = client.get_cluster_certificate_authority(request) - expected_params = [ - ( - "clusterId", - "", - ), - ("$alt", "json;enum-encoding=int"), - ] + expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_create_cluster_rest_unset_required_fields(): +def test_get_cluster_certificate_authority_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.create_cluster._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "clusterId", - "requestId", - ) - ) - & set( - ( - "parent", - "clusterId", - "cluster", - ) - ) + unset_fields = ( + transport.get_cluster_certificate_authority._get_unset_required_fields({}) ) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_cluster_rest_interceptors(null_interceptor): +def test_get_cluster_certificate_authority_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -5011,16 +5760,16 @@ def test_create_cluster_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_create_cluster" + transports.CloudRedisClusterRestInterceptor, + "post_get_cluster_certificate_authority", ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_create_cluster" + transports.CloudRedisClusterRestInterceptor, + "pre_get_cluster_certificate_authority", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.CreateClusterRequest.pb( - cloud_redis_cluster.CreateClusterRequest() + pb_message = cloud_redis_cluster.GetClusterCertificateAuthorityRequest.pb( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest() ) transcode.return_value = { "method": "post", @@ -5032,19 +5781,19 @@ def test_create_cluster_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = json_format.MessageToJson( - operations_pb2.Operation() + req.return_value._content = cloud_redis_cluster.CertificateAuthority.to_json( + cloud_redis_cluster.CertificateAuthority() ) - request = cloud_redis_cluster.CreateClusterRequest() + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() + post.return_value = cloud_redis_cluster.CertificateAuthority() - client.create_cluster( + client.get_cluster_certificate_authority( request, metadata=[ ("key", "val"), @@ -5056,8 +5805,9 @@ def test_create_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_create_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.CreateClusterRequest +def test_get_cluster_certificate_authority_rest_bad_request( + transport: str = "rest", + request_type=cloud_redis_cluster.GetClusterCertificateAuthorityRequest, ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -5065,7 +5815,9 @@ def test_create_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "name": "projects/sample1/locations/sample2/clusters/sample3/certificateAuthority" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -5077,10 +5829,10 @@ def test_create_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.create_cluster(request) + client.get_cluster_certificate_authority(request) -def test_create_cluster_rest_flattened(): +def test_get_cluster_certificate_authority_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -5089,39 +5841,44 @@ def test_create_cluster_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = cloud_redis_cluster.CertificateAuthority() # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = { + "name": "projects/sample1/locations/sample2/clusters/sample3/certificateAuthority" + } # get truthy value for each flattened field mock_args = dict( - parent="parent_value", - cluster=cloud_redis_cluster.Cluster(name="name_value"), - cluster_id="cluster_id_value", + name="name_value", ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.CertificateAuthority.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.create_cluster(**mock_args) + client.get_cluster_certificate_authority(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1/{parent=projects/*/locations/*}/clusters" % client.transport._host, + "%s/v1/{name=projects/*/locations/*/clusters/*/certificateAuthority}" + % client.transport._host, args[1], ) -def test_create_cluster_rest_flattened_error(transport: str = "rest"): +def test_get_cluster_certificate_authority_rest_flattened_error( + transport: str = "rest", +): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -5130,15 +5887,13 @@ def test_create_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_cluster( - cloud_redis_cluster.CreateClusterRequest(), - parent="parent_value", - cluster=cloud_redis_cluster.Cluster(name="name_value"), - cluster_id="cluster_id_value", + client.get_cluster_certificate_authority( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest(), + name="name_value", ) -def test_create_cluster_rest_error(): +def test_get_cluster_certificate_authority_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -5288,6 +6043,7 @@ def test_cloud_redis_cluster_base_transport(): "update_cluster", "delete_cluster", "create_cluster", + "get_cluster_certificate_authority", "get_location", "list_locations", "get_operation", @@ -5591,6 +6347,9 @@ def test_cloud_redis_cluster_client_transport_session_collision(transport_name): session1 = client1.transport.create_cluster._session session2 = client2.transport.create_cluster._session assert session1 != session2 + session1 = client1.transport.get_cluster_certificate_authority._session + session2 = client2.transport.get_cluster_certificate_authority._session + assert session1 != session2 def test_cloud_redis_cluster_grpc_transport_channel(): @@ -5753,25 +6512,53 @@ def test_cloud_redis_cluster_grpc_lro_async_client(): assert transport.operations_client is transport.operations_client -def test_cluster_path(): +def test_certificate_authority_path(): project = "squid" location = "clam" cluster = "whelk" - expected = "projects/{project}/locations/{location}/clusters/{cluster}".format( + expected = "projects/{project}/locations/{location}/clusters/{cluster}/certificateAuthority".format( project=project, location=location, cluster=cluster, ) - actual = CloudRedisClusterClient.cluster_path(project, location, cluster) + actual = CloudRedisClusterClient.certificate_authority_path( + project, location, cluster + ) assert expected == actual -def test_parse_cluster_path(): +def test_parse_certificate_authority_path(): expected = { "project": "octopus", "location": "oyster", "cluster": "nudibranch", } + path = CloudRedisClusterClient.certificate_authority_path(**expected) + + # Check that the path construction is reversible. + actual = CloudRedisClusterClient.parse_certificate_authority_path(path) + assert expected == actual + + +def test_cluster_path(): + project = "cuttlefish" + location = "mussel" + cluster = "winkle" + expected = "projects/{project}/locations/{location}/clusters/{cluster}".format( + project=project, + location=location, + cluster=cluster, + ) + actual = CloudRedisClusterClient.cluster_path(project, location, cluster) + assert expected == actual + + +def test_parse_cluster_path(): + expected = { + "project": "nautilus", + "location": "scallop", + "cluster": "abalone", + } path = CloudRedisClusterClient.cluster_path(**expected) # Check that the path construction is reversible. @@ -5780,7 +6567,7 @@ def test_parse_cluster_path(): def test_common_billing_account_path(): - billing_account = "cuttlefish" + billing_account = "squid" expected = "billingAccounts/{billing_account}".format( billing_account=billing_account, ) @@ -5790,7 +6577,7 @@ def test_common_billing_account_path(): def test_parse_common_billing_account_path(): expected = { - "billing_account": "mussel", + "billing_account": "clam", } path = CloudRedisClusterClient.common_billing_account_path(**expected) @@ -5800,7 +6587,7 @@ def test_parse_common_billing_account_path(): def test_common_folder_path(): - folder = "winkle" + folder = "whelk" expected = "folders/{folder}".format( folder=folder, ) @@ -5810,7 +6597,7 @@ def test_common_folder_path(): def test_parse_common_folder_path(): expected = { - "folder": "nautilus", + "folder": "octopus", } path = CloudRedisClusterClient.common_folder_path(**expected) @@ -5820,7 +6607,7 @@ def test_parse_common_folder_path(): def test_common_organization_path(): - organization = "scallop" + organization = "oyster" expected = "organizations/{organization}".format( organization=organization, ) @@ -5830,7 +6617,7 @@ def test_common_organization_path(): def test_parse_common_organization_path(): expected = { - "organization": "abalone", + "organization": "nudibranch", } path = CloudRedisClusterClient.common_organization_path(**expected) @@ -5840,7 +6627,7 @@ def test_parse_common_organization_path(): def test_common_project_path(): - project = "squid" + project = "cuttlefish" expected = "projects/{project}".format( project=project, ) @@ -5850,7 +6637,7 @@ def test_common_project_path(): def test_parse_common_project_path(): expected = { - "project": "clam", + "project": "mussel", } path = CloudRedisClusterClient.common_project_path(**expected) @@ -5860,8 +6647,8 @@ def test_parse_common_project_path(): def test_common_location_path(): - project = "whelk" - location = "octopus" + project = "winkle" + location = "nautilus" expected = "projects/{project}/locations/{location}".format( project=project, location=location, @@ -5872,8 +6659,8 @@ def test_common_location_path(): def test_parse_common_location_path(): expected = { - "project": "oyster", - "location": "nudibranch", + "project": "scallop", + "location": "abalone", } path = CloudRedisClusterClient.common_location_path(**expected) diff --git a/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1beta1/test_cloud_redis_cluster.py b/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1beta1/test_cloud_redis_cluster.py index 01f8f781e6ff..8ed0407f51da 100644 --- a/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1beta1/test_cloud_redis_cluster.py +++ b/packages/google-cloud-redis-cluster/tests/unit/gapic/redis_cluster_v1beta1/test_cloud_redis_cluster.py @@ -1765,6 +1765,9 @@ def test_get_cluster(request_type, transport: str = "grpc"): transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, size_gb=739, shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) response = client.get_cluster(request) @@ -1790,6 +1793,9 @@ def test_get_cluster(request_type, transport: str = "grpc"): ) assert response.size_gb == 739 assert response.shard_count == 1178 + assert response.node_type == cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO + assert math.isclose(response.precise_size_gb, 0.15810000000000002, rel_tol=1e-6) + assert response.deletion_protection_enabled is True def test_get_cluster_empty_call(): @@ -1896,6 +1902,9 @@ async def test_get_cluster_empty_call_async(): transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, size_gb=739, shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) ) response = await client.get_cluster() @@ -1976,6 +1985,9 @@ async def test_get_cluster_async( transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, size_gb=739, shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) ) response = await client.get_cluster(request) @@ -2002,6 +2014,9 @@ async def test_get_cluster_async( ) assert response.size_gb == 739 assert response.shard_count == 1178 + assert response.node_type == cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO + assert math.isclose(response.precise_size_gb, 0.15810000000000002, rel_tol=1e-6) + assert response.deletion_protection_enabled is True @pytest.mark.asyncio @@ -3298,6 +3313,399 @@ async def test_create_cluster_flattened_error_async(): ) +@pytest.mark.parametrize( + "request_type", + [ + cloud_redis_cluster.GetClusterCertificateAuthorityRequest, + dict, + ], +) +def test_get_cluster_certificate_authority(request_type, transport: str = "grpc"): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) + response = client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, cloud_redis_cluster.CertificateAuthority) + assert response.name == "name_value" + + +def test_get_cluster_certificate_authority_empty_call(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_cluster_certificate_authority() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + +def test_get_cluster_certificate_authority_non_empty_request_with_auto_populated_field(): + # This test is a coverage failsafe to make sure that UUID4 fields are + # automatically populated, according to AIP-4235, with non-empty requests. + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Populate all string fields in the request which are not UUID4 + # since we want to check that UUID4 are populated automatically + # if they meet the requirements of AIP 4235. + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client.get_cluster_certificate_authority(request=request) + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == cloud_redis_cluster.GetClusterCertificateAuthorityRequest( + name="name_value", + ) + + +def test_get_cluster_certificate_authority_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._transport.get_cluster_certificate_authority + in client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[ + client._transport.get_cluster_certificate_authority + ] = mock_rpc + request = {} + client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.get_cluster_certificate_authority(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_empty_call_async(): + # This test is a coverage failsafe to make sure that totally empty calls, + # i.e. request == None and no flattened fields passed, work. + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="grpc_asyncio", + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) + ) + response = await client.get_cluster_certificate_authority() + call.assert_called() + _, args, _ = call.mock_calls[0] + assert args[0] == cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_async_use_cached_wrapped_rpc( + transport: str = "grpc_asyncio", +): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method_async.wrap_method") as wrapper_fn: + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert ( + client._client._transport.get_cluster_certificate_authority + in client._client._transport._wrapped_methods + ) + + # Replace cached wrapped function with mock + class AwaitableMock(mock.AsyncMock): + def __await__(self): + self.await_count += 1 + return iter([]) + + mock_object = AwaitableMock() + client._client._transport._wrapped_methods[ + client._client._transport.get_cluster_certificate_authority + ] = mock_object + + request = {} + await client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_object.call_count == 1 + + await client.get_cluster_certificate_authority(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_object.call_count == 2 + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_async( + transport: str = "grpc_asyncio", + request_type=cloud_redis_cluster.GetClusterCertificateAuthorityRequest, +): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Everything is optional in proto3 as far as the runtime is concerned, + # and we are mocking out the actual API, so just send an empty request. + request = request_type() + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) + ) + response = await client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + assert args[0] == request + + # Establish that the response is the type that we expect. + assert isinstance(response, cloud_redis_cluster.CertificateAuthority) + assert response.name == "name_value" + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_async_from_dict(): + await test_get_cluster_certificate_authority_async(request_type=dict) + + +def test_get_cluster_certificate_authority_field_headers(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value = cloud_redis_cluster.CertificateAuthority() + client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_field_headers_async(): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Any value that is part of the HTTP/1.1 URI should be sent as + # a field header. Set these to a non-empty value. + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() + + request.name = "name_value" + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority() + ) + await client.get_cluster_certificate_authority(request) + + # Establish that the underlying gRPC stub method was called. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + assert args[0] == request + + # Establish that the field header was sent. + _, _, kw = call.mock_calls[0] + assert ( + "x-goog-request-params", + "name=name_value", + ) in kw["metadata"] + + +def test_get_cluster_certificate_authority_flattened(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = cloud_redis_cluster.CertificateAuthority() + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + client.get_cluster_certificate_authority( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) == 1 + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +def test_get_cluster_certificate_authority_flattened_error(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_cluster_certificate_authority( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest(), + name="name_value", + ) + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_flattened_async(): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Mock the actual call within the gRPC stub, and fake the request. + with mock.patch.object( + type(client.transport.get_cluster_certificate_authority), "__call__" + ) as call: + # Designate an appropriate return value for the call. + call.return_value = cloud_redis_cluster.CertificateAuthority() + + call.return_value = grpc_helpers_async.FakeUnaryUnaryCall( + cloud_redis_cluster.CertificateAuthority() + ) + # Call the method with a truthy value for each flattened field, + # using the keyword arguments to the method. + response = await client.get_cluster_certificate_authority( + name="name_value", + ) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(call.mock_calls) + _, args, _ = call.mock_calls[0] + arg = args[0].name + mock_val = "name_value" + assert arg == mock_val + + +@pytest.mark.asyncio +async def test_get_cluster_certificate_authority_flattened_error_async(): + client = CloudRedisClusterAsyncClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + await client.get_cluster_certificate_authority( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest(), + name="name_value", + ) + + @pytest.mark.parametrize( "request_type", [ @@ -3305,42 +3713,443 @@ async def test_create_cluster_flattened_error_async(): dict, ], ) -def test_list_clusters_rest(request_type): +def test_list_clusters_rest(request_type): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = cloud_redis_cluster.ListClustersResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_clusters(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListClustersPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_clusters_rest_use_cached_wrapped_rpc(): + # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, + # instead of constructing them on each call + with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Should wrap all calls on client creation + assert wrapper_fn.call_count > 0 + wrapper_fn.reset_mock() + + # Ensure method has been cached + assert client._transport.list_clusters in client._transport._wrapped_methods + + # Replace cached wrapped function with mock + mock_rpc = mock.Mock() + mock_rpc.return_value.name = ( + "foo" # operation_request.operation in compute client(s) expect a string. + ) + client._transport._wrapped_methods[client._transport.list_clusters] = mock_rpc + + request = {} + client.list_clusters(request) + + # Establish that the underlying gRPC stub method was called. + assert mock_rpc.call_count == 1 + + client.list_clusters(request) + + # Establish that a new wrapper was not created for this call + assert wrapper_fn.call_count == 0 + assert mock_rpc.call_count == 2 + + +def test_list_clusters_rest_required_fields( + request_type=cloud_redis_cluster.ListClustersRequest, +): + transport_class = transports.CloudRedisClusterRestTransport + + request_init = {} + request_init["parent"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson(pb_request, use_integers_for_enums=False) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_clusters._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["parent"] = "parent_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_clusters._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = cloud_redis_cluster.ListClustersResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_clusters(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_clusters_rest_unset_required_fields(): + transport = transports.CloudRedisClusterRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_clusters._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "pageSize", + "pageToken", + ) + ) + & set(("parent",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_clusters_rest_interceptors(null_interceptor): + transport = transports.CloudRedisClusterRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.CloudRedisClusterRestInterceptor(), + ) + client = CloudRedisClusterClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.CloudRedisClusterRestInterceptor, "post_list_clusters" + ) as post, mock.patch.object( + transports.CloudRedisClusterRestInterceptor, "pre_list_clusters" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = cloud_redis_cluster.ListClustersRequest.pb( + cloud_redis_cluster.ListClustersRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = cloud_redis_cluster.ListClustersResponse.to_json( + cloud_redis_cluster.ListClustersResponse() + ) + + request = cloud_redis_cluster.ListClustersRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = cloud_redis_cluster.ListClustersResponse() + + client.list_clusters( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_clusters_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.ListClustersRequest +): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_clusters(request) + + +def test_list_clusters_rest_flattened(): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = cloud_redis_cluster.ListClustersResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_clusters(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1beta1/{parent=projects/*/locations/*}/clusters" + % client.transport._host, + args[1], + ) + + +def test_list_clusters_rest_flattened_error(transport: str = "rest"): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_clusters( + cloud_redis_cluster.ListClustersRequest(), + parent="parent_value", + ) + + +def test_list_clusters_rest_pager(transport: str = "rest"): + client = CloudRedisClusterClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + cloud_redis_cluster.ListClustersResponse( + clusters=[ + cloud_redis_cluster.Cluster(), + cloud_redis_cluster.Cluster(), + cloud_redis_cluster.Cluster(), + ], + next_page_token="abc", + ), + cloud_redis_cluster.ListClustersResponse( + clusters=[], + next_page_token="def", + ), + cloud_redis_cluster.ListClustersResponse( + clusters=[ + cloud_redis_cluster.Cluster(), + ], + next_page_token="ghi", + ), + cloud_redis_cluster.ListClustersResponse( + clusters=[ + cloud_redis_cluster.Cluster(), + cloud_redis_cluster.Cluster(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + cloud_redis_cluster.ListClustersResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_clusters(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, cloud_redis_cluster.Cluster) for i in results) + + pages = list(client.list_clusters(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + cloud_redis_cluster.GetClusterRequest, + dict, + ], +) +def test_get_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.ListClustersResponse( - next_page_token="next_page_token_value", - unreachable=["unreachable_value"], + return_value = cloud_redis_cluster.Cluster( + name="name_value", + state=cloud_redis_cluster.Cluster.State.CREATING, + uid="uid_value", + replica_count=1384, + authorization_mode=cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH, + transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, + size_gb=739, + shard_count=1178, + node_type=cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO, + precise_size_gb=0.15810000000000002, + deletion_protection_enabled=True, ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 # Convert return value to protobuf type - return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_clusters(request) + response = client.get_cluster(request) # Establish that the response is the type that we expect. - assert isinstance(response, pagers.ListClustersPager) - assert response.next_page_token == "next_page_token_value" - assert response.unreachable == ["unreachable_value"] + assert isinstance(response, cloud_redis_cluster.Cluster) + assert response.name == "name_value" + assert response.state == cloud_redis_cluster.Cluster.State.CREATING + assert response.uid == "uid_value" + assert response.replica_count == 1384 + assert ( + response.authorization_mode + == cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH + ) + assert ( + response.transit_encryption_mode + == cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED + ) + assert response.size_gb == 739 + assert response.shard_count == 1178 + assert response.node_type == cloud_redis_cluster.NodeType.REDIS_SHARED_CORE_NANO + assert math.isclose(response.precise_size_gb, 0.15810000000000002, rel_tol=1e-6) + assert response.deletion_protection_enabled is True -def test_list_clusters_rest_use_cached_wrapped_rpc(): +def test_get_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -3354,35 +4163,35 @@ def test_list_clusters_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.list_clusters in client._transport._wrapped_methods + assert client._transport.get_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.list_clusters] = mock_rpc + client._transport._wrapped_methods[client._transport.get_cluster] = mock_rpc request = {} - client.list_clusters(request) + client.get_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.list_clusters(request) + client.get_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_list_clusters_rest_required_fields( - request_type=cloud_redis_cluster.ListClustersRequest, +def test_get_cluster_rest_required_fields( + request_type=cloud_redis_cluster.GetClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["parent"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -3393,28 +4202,21 @@ def test_list_clusters_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_clusters._get_unset_required_fields(jsonified_request) + ).get_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["parent"] = "parent_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).list_clusters._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "page_size", - "page_token", - ) - ) + ).get_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3423,7 +4225,7 @@ def test_list_clusters_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.ListClustersResponse() + return_value = cloud_redis_cluster.Cluster() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -3444,38 +4246,30 @@ def test_list_clusters_rest_required_fields( response_value.status_code = 200 # Convert return value to protobuf type - return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.list_clusters(request) + response = client.get_cluster(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_list_clusters_rest_unset_required_fields(): +def test_get_cluster_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.list_clusters._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "pageSize", - "pageToken", - ) - ) - & set(("parent",)) - ) + unset_fields = transport.get_cluster._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_list_clusters_rest_interceptors(null_interceptor): +def test_get_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -3488,14 +4282,14 @@ def test_list_clusters_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_list_clusters" + transports.CloudRedisClusterRestInterceptor, "post_get_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_list_clusters" + transports.CloudRedisClusterRestInterceptor, "pre_get_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.ListClustersRequest.pb( - cloud_redis_cluster.ListClustersRequest() + pb_message = cloud_redis_cluster.GetClusterRequest.pb( + cloud_redis_cluster.GetClusterRequest() ) transcode.return_value = { "method": "post", @@ -3507,19 +4301,19 @@ def test_list_clusters_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = cloud_redis_cluster.ListClustersResponse.to_json( - cloud_redis_cluster.ListClustersResponse() + req.return_value._content = cloud_redis_cluster.Cluster.to_json( + cloud_redis_cluster.Cluster() ) - request = cloud_redis_cluster.ListClustersRequest() + request = cloud_redis_cluster.GetClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = cloud_redis_cluster.ListClustersResponse() + post.return_value = cloud_redis_cluster.Cluster() - client.list_clusters( + client.get_cluster( request, metadata=[ ("key", "val"), @@ -3531,8 +4325,8 @@ def test_list_clusters_rest_interceptors(null_interceptor): post.assert_called_once() -def test_list_clusters_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.ListClustersRequest +def test_get_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.GetClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3540,7 +4334,7 @@ def test_list_clusters_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -3552,10 +4346,10 @@ def test_list_clusters_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.list_clusters(request) + client.get_cluster(request) -def test_list_clusters_rest_flattened(): +def test_get_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -3564,14 +4358,14 @@ def test_list_clusters_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.ListClustersResponse() + return_value = cloud_redis_cluster.Cluster() # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} # get truthy value for each flattened field mock_args = dict( - parent="parent_value", + name="name_value", ) mock_args.update(sample_request) @@ -3579,25 +4373,25 @@ def test_list_clusters_rest_flattened(): response_value = Response() response_value.status_code = 200 # Convert return value to protobuf type - return_value = cloud_redis_cluster.ListClustersResponse.pb(return_value) + return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.list_clusters(**mock_args) + client.get_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1beta1/{parent=projects/*/locations/*}/clusters" + "%s/v1beta1/{name=projects/*/locations/*/clusters/*}" % client.transport._host, args[1], ) -def test_list_clusters_rest_flattened_error(transport: str = "rest"): +def test_get_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -3606,136 +4400,160 @@ def test_list_clusters_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.list_clusters( - cloud_redis_cluster.ListClustersRequest(), - parent="parent_value", + client.get_cluster( + cloud_redis_cluster.GetClusterRequest(), + name="name_value", ) -def test_list_clusters_rest_pager(transport: str = "rest"): +def test_get_cluster_rest_error(): client = CloudRedisClusterClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) - # Mock the http request call within the method and fake a response. - with mock.patch.object(Session, "request") as req: - # TODO(kbandes): remove this mock unless there's a good reason for it. - # with mock.patch.object(path_template, 'transcode') as transcode: - # Set the response as a series of pages - response = ( - cloud_redis_cluster.ListClustersResponse( - clusters=[ - cloud_redis_cluster.Cluster(), - cloud_redis_cluster.Cluster(), - cloud_redis_cluster.Cluster(), - ], - next_page_token="abc", - ), - cloud_redis_cluster.ListClustersResponse( - clusters=[], - next_page_token="def", - ), - cloud_redis_cluster.ListClustersResponse( - clusters=[ - cloud_redis_cluster.Cluster(), - ], - next_page_token="ghi", - ), - cloud_redis_cluster.ListClustersResponse( - clusters=[ - cloud_redis_cluster.Cluster(), - cloud_redis_cluster.Cluster(), - ], - ), - ) - # Two responses for two calls - response = response + response - - # Wrap the values into proper Response objs - response = tuple( - cloud_redis_cluster.ListClustersResponse.to_json(x) for x in response - ) - return_values = tuple(Response() for i in response) - for return_val, response_val in zip(return_values, response): - return_val._content = response_val.encode("UTF-8") - return_val.status_code = 200 - req.side_effect = return_values - - sample_request = {"parent": "projects/sample1/locations/sample2"} - - pager = client.list_clusters(request=sample_request) - - results = list(pager) - assert len(results) == 6 - assert all(isinstance(i, cloud_redis_cluster.Cluster) for i in results) - - pages = list(client.list_clusters(request=sample_request).pages) - for page_, token in zip(pages, ["abc", "def", "ghi", ""]): - assert page_.raw_page.next_page_token == token - @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.GetClusterRequest, + cloud_redis_cluster.UpdateClusterRequest, dict, ], ) -def test_get_cluster_rest(request_type): +def test_update_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = { + "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} + } + request_init["cluster"] = { + "name": "projects/sample1/locations/sample2/clusters/sample3", + "create_time": {"seconds": 751, "nanos": 543}, + "state": 1, + "uid": "uid_value", + "replica_count": 1384, + "authorization_mode": 1, + "transit_encryption_mode": 1, + "size_gb": 739, + "shard_count": 1178, + "psc_configs": [{"network": "network_value"}], + "discovery_endpoints": [ + {"address": "address_value", "port": 453, "psc_config": {}} + ], + "psc_connections": [ + { + "psc_connection_id": "psc_connection_id_value", + "address": "address_value", + "forwarding_rule": "forwarding_rule_value", + "project_id": "project_id_value", + "network": "network_value", + } + ], + "state_info": { + "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} + }, + "node_type": 1, + "persistence_config": { + "mode": 1, + "rdb_config": {"rdb_snapshot_period": 1, "rdb_snapshot_start_time": {}}, + "aof_config": {"append_fsync": 1}, + }, + "redis_configs": {}, + "precise_size_gb": 0.15810000000000002, + "zone_distribution_config": {"mode": 1, "zone": "zone_value"}, + "deletion_protection_enabled": True, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = cloud_redis_cluster.UpdateClusterRequest.meta.fields["cluster"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["cluster"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["cluster"][field])): + del request_init["cluster"][field][i][subfield] + else: + del request_init["cluster"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.Cluster( - name="name_value", - state=cloud_redis_cluster.Cluster.State.CREATING, - uid="uid_value", - replica_count=1384, - authorization_mode=cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH, - transit_encryption_mode=cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED, - size_gb=739, - shard_count=1178, - ) + return_value = operations_pb2.Operation(name="operations/spam") # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type - return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_cluster(request) + response = client.update_cluster(request) # Establish that the response is the type that we expect. - assert isinstance(response, cloud_redis_cluster.Cluster) - assert response.name == "name_value" - assert response.state == cloud_redis_cluster.Cluster.State.CREATING - assert response.uid == "uid_value" - assert response.replica_count == 1384 - assert ( - response.authorization_mode - == cloud_redis_cluster.AuthorizationMode.AUTH_MODE_IAM_AUTH - ) - assert ( - response.transit_encryption_mode - == cloud_redis_cluster.TransitEncryptionMode.TRANSIT_ENCRYPTION_MODE_DISABLED - ) - assert response.size_gb == 739 - assert response.shard_count == 1178 + assert response.operation.name == "operations/spam" -def test_get_cluster_rest_use_cached_wrapped_rpc(): +def test_update_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -3749,35 +4567,38 @@ def test_get_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.get_cluster in client._transport._wrapped_methods + assert client._transport.update_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.get_cluster] = mock_rpc + client._transport._wrapped_methods[client._transport.update_cluster] = mock_rpc request = {} - client.get_cluster(request) + client.update_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - client.get_cluster(request) + # Operation methods build a cached wrapper on first rpc call + # subsequent calls should use the cached wrapper + wrapper_fn.reset_mock() + + client.update_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_get_cluster_rest_required_fields( - request_type=cloud_redis_cluster.GetClusterRequest, +def test_update_cluster_rest_required_fields( + request_type=cloud_redis_cluster.UpdateClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -3788,21 +4609,24 @@ def test_get_cluster_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_cluster._get_unset_required_fields(jsonified_request) + ).update_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - jsonified_request["name"] = "name_value" - unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).get_cluster._get_unset_required_fields(jsonified_request) + ).update_cluster._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "update_mask", + ) + ) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3811,7 +4635,7 @@ def test_get_cluster_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.Cluster() + return_value = operations_pb2.Operation(name="operations/spam") # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -3823,39 +4647,50 @@ def test_get_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "get", + "method": "patch", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 - - # Convert return value to protobuf type - return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.get_cluster(request) + response = client.update_cluster(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_get_cluster_rest_unset_required_fields(): +def test_update_cluster_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.get_cluster._get_unset_required_fields({}) - assert set(unset_fields) == (set(()) & set(("name",))) + unset_fields = transport.update_cluster._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "updateMask", + ) + ) + & set( + ( + "updateMask", + "cluster", + ) + ) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_get_cluster_rest_interceptors(null_interceptor): +def test_update_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -3868,14 +4703,16 @@ def test_get_cluster_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_get_cluster" + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.CloudRedisClusterRestInterceptor, "post_update_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_get_cluster" + transports.CloudRedisClusterRestInterceptor, "pre_update_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.GetClusterRequest.pb( - cloud_redis_cluster.GetClusterRequest() + pb_message = cloud_redis_cluster.UpdateClusterRequest.pb( + cloud_redis_cluster.UpdateClusterRequest() ) transcode.return_value = { "method": "post", @@ -3887,19 +4724,19 @@ def test_get_cluster_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = cloud_redis_cluster.Cluster.to_json( - cloud_redis_cluster.Cluster() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() ) - request = cloud_redis_cluster.GetClusterRequest() + request = cloud_redis_cluster.UpdateClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = cloud_redis_cluster.Cluster() + post.return_value = operations_pb2.Operation() - client.get_cluster( + client.update_cluster( request, metadata=[ ("key", "val"), @@ -3911,8 +4748,8 @@ def test_get_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_get_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.GetClusterRequest +def test_update_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.UpdateClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -3920,7 +4757,9 @@ def test_get_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = { + "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -3932,10 +4771,10 @@ def test_get_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.get_cluster(request) + client.update_cluster(request) -def test_get_cluster_rest_flattened(): +def test_update_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -3944,40 +4783,41 @@ def test_get_cluster_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = cloud_redis_cluster.Cluster() + return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + sample_request = { + "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} + } # get truthy value for each flattened field mock_args = dict( - name="name_value", + cluster=cloud_redis_cluster.Cluster(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 - # Convert return value to protobuf type - return_value = cloud_redis_cluster.Cluster.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.get_cluster(**mock_args) + client.update_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1beta1/{name=projects/*/locations/*/clusters/*}" + "%s/v1beta1/{cluster.name=projects/*/locations/*/clusters/*}" % client.transport._host, args[1], ) -def test_get_cluster_rest_flattened_error(transport: str = "rest"): +def test_update_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -3986,13 +4826,14 @@ def test_get_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.get_cluster( - cloud_redis_cluster.GetClusterRequest(), - name="name_value", + client.update_cluster( + cloud_redis_cluster.UpdateClusterRequest(), + cluster=cloud_redis_cluster.Cluster(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), ) -def test_get_cluster_rest_error(): +def test_update_cluster_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -4001,114 +4842,18 @@ def test_get_cluster_rest_error(): @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.UpdateClusterRequest, + cloud_redis_cluster.DeleteClusterRequest, dict, ], ) -def test_update_cluster_rest(request_type): +def test_delete_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = { - "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} - } - request_init["cluster"] = { - "name": "projects/sample1/locations/sample2/clusters/sample3", - "create_time": {"seconds": 751, "nanos": 543}, - "state": 1, - "uid": "uid_value", - "replica_count": 1384, - "authorization_mode": 1, - "transit_encryption_mode": 1, - "size_gb": 739, - "shard_count": 1178, - "psc_configs": [{"network": "network_value"}], - "discovery_endpoints": [ - {"address": "address_value", "port": 453, "psc_config": {}} - ], - "psc_connections": [ - { - "psc_connection_id": "psc_connection_id_value", - "address": "address_value", - "forwarding_rule": "forwarding_rule_value", - "project_id": "project_id_value", - "network": "network_value", - } - ], - "state_info": { - "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} - }, - } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 - - # Determine if the message type is proto-plus or protobuf - test_field = cloud_redis_cluster.UpdateClusterRequest.meta.fields["cluster"] - - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] - - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["cluster"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["cluster"][field])): - del request_init["cluster"][field][i][subfield] - else: - del request_init["cluster"][field][subfield] + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -4123,13 +4868,13 @@ def get_message_fields(field): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.update_cluster(request) + response = client.delete_cluster(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_update_cluster_rest_use_cached_wrapped_rpc(): +def test_delete_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -4143,17 +4888,17 @@ def test_update_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.update_cluster in client._transport._wrapped_methods + assert client._transport.delete_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.update_cluster] = mock_rpc + client._transport._wrapped_methods[client._transport.delete_cluster] = mock_rpc request = {} - client.update_cluster(request) + client.delete_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 @@ -4162,19 +4907,20 @@ def test_update_cluster_rest_use_cached_wrapped_rpc(): # subsequent calls should use the cached wrapper wrapper_fn.reset_mock() - client.update_cluster(request) + client.delete_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_update_cluster_rest_required_fields( - request_type=cloud_redis_cluster.UpdateClusterRequest, +def test_delete_cluster_rest_required_fields( + request_type=cloud_redis_cluster.DeleteClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -4185,24 +4931,23 @@ def test_update_cluster_rest_required_fields( unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).update_cluster._get_unset_required_fields(jsonified_request) + ).delete_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present + jsonified_request["name"] = "name_value" + unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).update_cluster._get_unset_required_fields(jsonified_request) + ).delete_cluster._get_unset_required_fields(jsonified_request) # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "request_id", - "update_mask", - ) - ) + assert not set(unset_fields) - set(("request_id",)) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4223,10 +4968,9 @@ def test_update_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "patch", + "method": "delete", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() @@ -4236,37 +4980,24 @@ def test_update_cluster_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.update_cluster(request) + response = client.delete_cluster(request) expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_update_cluster_rest_unset_required_fields(): - transport = transports.CloudRedisClusterRestTransport( - credentials=ga_credentials.AnonymousCredentials - ) - - unset_fields = transport.update_cluster._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "requestId", - "updateMask", - ) - ) - & set( - ( - "updateMask", - "cluster", - ) - ) +def test_delete_cluster_rest_unset_required_fields(): + transport = transports.CloudRedisClusterRestTransport( + credentials=ga_credentials.AnonymousCredentials ) + unset_fields = transport.delete_cluster._get_unset_required_fields({}) + assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_update_cluster_rest_interceptors(null_interceptor): +def test_delete_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -4281,14 +5012,14 @@ def test_update_cluster_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_update_cluster" + transports.CloudRedisClusterRestInterceptor, "post_delete_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_update_cluster" + transports.CloudRedisClusterRestInterceptor, "pre_delete_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.UpdateClusterRequest.pb( - cloud_redis_cluster.UpdateClusterRequest() + pb_message = cloud_redis_cluster.DeleteClusterRequest.pb( + cloud_redis_cluster.DeleteClusterRequest() ) transcode.return_value = { "method": "post", @@ -4304,7 +5035,7 @@ def test_update_cluster_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = cloud_redis_cluster.UpdateClusterRequest() + request = cloud_redis_cluster.DeleteClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -4312,7 +5043,7 @@ def test_update_cluster_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.update_cluster( + client.delete_cluster( request, metadata=[ ("key", "val"), @@ -4324,8 +5055,8 @@ def test_update_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_update_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.UpdateClusterRequest +def test_delete_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.DeleteClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4333,9 +5064,7 @@ def test_update_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = { - "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} - } + request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -4347,10 +5076,10 @@ def test_update_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.update_cluster(request) + client.delete_cluster(request) -def test_update_cluster_rest_flattened(): +def test_delete_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -4362,14 +5091,11 @@ def test_update_cluster_rest_flattened(): return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method - sample_request = { - "cluster": {"name": "projects/sample1/locations/sample2/clusters/sample3"} - } + sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} # get truthy value for each flattened field mock_args = dict( - cluster=cloud_redis_cluster.Cluster(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + name="name_value", ) mock_args.update(sample_request) @@ -4380,20 +5106,20 @@ def test_update_cluster_rest_flattened(): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.update_cluster(**mock_args) + client.delete_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1beta1/{cluster.name=projects/*/locations/*/clusters/*}" + "%s/v1beta1/{name=projects/*/locations/*/clusters/*}" % client.transport._host, args[1], ) -def test_update_cluster_rest_flattened_error(transport: str = "rest"): +def test_delete_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -4402,14 +5128,13 @@ def test_update_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.update_cluster( - cloud_redis_cluster.UpdateClusterRequest(), - cluster=cloud_redis_cluster.Cluster(name="name_value"), - update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + client.delete_cluster( + cloud_redis_cluster.DeleteClusterRequest(), + name="name_value", ) -def test_update_cluster_rest_error(): +def test_delete_cluster_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -4418,18 +5143,122 @@ def test_update_cluster_rest_error(): @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.DeleteClusterRequest, + cloud_redis_cluster.CreateClusterRequest, dict, ], ) -def test_delete_cluster_rest(request_type): +def test_create_cluster_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["cluster"] = { + "name": "name_value", + "create_time": {"seconds": 751, "nanos": 543}, + "state": 1, + "uid": "uid_value", + "replica_count": 1384, + "authorization_mode": 1, + "transit_encryption_mode": 1, + "size_gb": 739, + "shard_count": 1178, + "psc_configs": [{"network": "network_value"}], + "discovery_endpoints": [ + {"address": "address_value", "port": 453, "psc_config": {}} + ], + "psc_connections": [ + { + "psc_connection_id": "psc_connection_id_value", + "address": "address_value", + "forwarding_rule": "forwarding_rule_value", + "project_id": "project_id_value", + "network": "network_value", + } + ], + "state_info": { + "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} + }, + "node_type": 1, + "persistence_config": { + "mode": 1, + "rdb_config": {"rdb_snapshot_period": 1, "rdb_snapshot_start_time": {}}, + "aof_config": {"append_fsync": 1}, + }, + "redis_configs": {}, + "precise_size_gb": 0.15810000000000002, + "zone_distribution_config": {"mode": 1, "zone": "zone_value"}, + "deletion_protection_enabled": True, + } + # The version of a generated dependency at test runtime may differ from the version used during generation. + # Delete any fields which are not present in the current runtime dependency + # See https://github.com/googleapis/gapic-generator-python/issues/1748 + + # Determine if the message type is proto-plus or protobuf + test_field = cloud_redis_cluster.CreateClusterRequest.meta.fields["cluster"] + + def get_message_fields(field): + # Given a field which is a message (composite type), return a list with + # all the fields of the message. + # If the field is not a composite type, return an empty list. + message_fields = [] + + if hasattr(field, "message") and field.message: + is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") + + if is_field_type_proto_plus_type: + message_fields = field.message.meta.fields.values() + # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types + else: # pragma: NO COVER + message_fields = field.message.DESCRIPTOR.fields + return message_fields + + runtime_nested_fields = [ + (field.name, nested_field.name) + for field in get_message_fields(test_field) + for nested_field in get_message_fields(field) + ] + + subfields_not_in_runtime = [] + + # For each item in the sample request, create a list of sub fields which are not present at runtime + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for field, value in request_init["cluster"].items(): # pragma: NO COVER + result = None + is_repeated = False + # For repeated fields + if isinstance(value, list) and len(value): + is_repeated = True + result = value[0] + # For fields where the type is another message + if isinstance(value, dict): + result = value + + if result and hasattr(result, "keys"): + for subfield in result.keys(): + if (field, subfield) not in runtime_nested_fields: + subfields_not_in_runtime.append( + { + "field": field, + "subfield": subfield, + "is_repeated": is_repeated, + } + ) + + # Remove fields from the sample request which are not present in the runtime version of the dependency + # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime + for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER + field = subfield_to_delete.get("field") + field_repeated = subfield_to_delete.get("is_repeated") + subfield = subfield_to_delete.get("subfield") + if subfield: + if field_repeated: + for i in range(0, len(request_init["cluster"][field])): + del request_init["cluster"][field][i][subfield] + else: + del request_init["cluster"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. @@ -4444,13 +5273,13 @@ def test_delete_cluster_rest(request_type): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_cluster(request) + response = client.create_cluster(request) # Establish that the response is the type that we expect. assert response.operation.name == "operations/spam" -def test_delete_cluster_rest_use_cached_wrapped_rpc(): +def test_create_cluster_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -4464,17 +5293,17 @@ def test_delete_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.delete_cluster in client._transport._wrapped_methods + assert client._transport.create_cluster in client._transport._wrapped_methods # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.delete_cluster] = mock_rpc + client._transport._wrapped_methods[client._transport.create_cluster] = mock_rpc request = {} - client.delete_cluster(request) + client.create_cluster(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 @@ -4483,20 +5312,21 @@ def test_delete_cluster_rest_use_cached_wrapped_rpc(): # subsequent calls should use the cached wrapper wrapper_fn.reset_mock() - client.delete_cluster(request) + client.create_cluster(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_delete_cluster_rest_required_fields( - request_type=cloud_redis_cluster.DeleteClusterRequest, +def test_create_cluster_rest_required_fields( + request_type=cloud_redis_cluster.CreateClusterRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["name"] = "" + request_init["parent"] = "" + request_init["cluster_id"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -4504,26 +5334,37 @@ def test_delete_cluster_rest_required_fields( ) # verify fields with default values are dropped + assert "clusterId" not in jsonified_request unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_cluster._get_unset_required_fields(jsonified_request) + ).create_cluster._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present + assert "clusterId" in jsonified_request + assert jsonified_request["clusterId"] == request_init["cluster_id"] - jsonified_request["name"] = "name_value" + jsonified_request["parent"] = "parent_value" + jsonified_request["clusterId"] = "cluster_id_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).delete_cluster._get_unset_required_fields(jsonified_request) + ).create_cluster._get_unset_required_fields(jsonified_request) # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set(("request_id",)) + assert not set(unset_fields) - set( + ( + "cluster_id", + "request_id", + ) + ) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "name" in jsonified_request - assert jsonified_request["name"] == "name_value" + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "clusterId" in jsonified_request + assert jsonified_request["clusterId"] == "cluster_id_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4544,9 +5385,10 @@ def test_delete_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "delete", + "method": "post", "query_params": pb_request, } + transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() @@ -4556,24 +5398,44 @@ def test_delete_cluster_rest_required_fields( response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.delete_cluster(request) + response = client.create_cluster(request) - expected_params = [("$alt", "json;enum-encoding=int")] + expected_params = [ + ( + "clusterId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_delete_cluster_rest_unset_required_fields(): +def test_create_cluster_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.delete_cluster._get_unset_required_fields({}) - assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + unset_fields = transport.create_cluster._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "clusterId", + "requestId", + ) + ) + & set( + ( + "parent", + "clusterId", + "cluster", + ) + ) + ) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_delete_cluster_rest_interceptors(null_interceptor): +def test_create_cluster_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -4588,14 +5450,14 @@ def test_delete_cluster_rest_interceptors(null_interceptor): ) as transcode, mock.patch.object( operation.Operation, "_set_result_from_operation" ), mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_delete_cluster" + transports.CloudRedisClusterRestInterceptor, "post_create_cluster" ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_delete_cluster" + transports.CloudRedisClusterRestInterceptor, "pre_create_cluster" ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.DeleteClusterRequest.pb( - cloud_redis_cluster.DeleteClusterRequest() + pb_message = cloud_redis_cluster.CreateClusterRequest.pb( + cloud_redis_cluster.CreateClusterRequest() ) transcode.return_value = { "method": "post", @@ -4611,7 +5473,7 @@ def test_delete_cluster_rest_interceptors(null_interceptor): operations_pb2.Operation() ) - request = cloud_redis_cluster.DeleteClusterRequest() + request = cloud_redis_cluster.CreateClusterRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), @@ -4619,7 +5481,7 @@ def test_delete_cluster_rest_interceptors(null_interceptor): pre.return_value = request, metadata post.return_value = operations_pb2.Operation() - client.delete_cluster( + client.create_cluster( request, metadata=[ ("key", "val"), @@ -4631,8 +5493,8 @@ def test_delete_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_delete_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.DeleteClusterRequest +def test_create_cluster_rest_bad_request( + transport: str = "rest", request_type=cloud_redis_cluster.CreateClusterRequest ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4640,7 +5502,7 @@ def test_delete_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + request_init = {"parent": "projects/sample1/locations/sample2"} request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -4652,10 +5514,10 @@ def test_delete_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.delete_cluster(request) + client.create_cluster(request) -def test_delete_cluster_rest_flattened(): +def test_create_cluster_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -4667,11 +5529,13 @@ def test_delete_cluster_rest_flattened(): return_value = operations_pb2.Operation(name="operations/spam") # get arguments that satisfy an http rule for this method - sample_request = {"name": "projects/sample1/locations/sample2/clusters/sample3"} + sample_request = {"parent": "projects/sample1/locations/sample2"} # get truthy value for each flattened field mock_args = dict( - name="name_value", + parent="parent_value", + cluster=cloud_redis_cluster.Cluster(name="name_value"), + cluster_id="cluster_id_value", ) mock_args.update(sample_request) @@ -4682,20 +5546,20 @@ def test_delete_cluster_rest_flattened(): response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.delete_cluster(**mock_args) + client.create_cluster(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1beta1/{name=projects/*/locations/*/clusters/*}" + "%s/v1beta1/{parent=projects/*/locations/*}/clusters" % client.transport._host, args[1], ) -def test_delete_cluster_rest_flattened_error(transport: str = "rest"): +def test_create_cluster_rest_flattened_error(transport: str = "rest"): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -4704,13 +5568,15 @@ def test_delete_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.delete_cluster( - cloud_redis_cluster.DeleteClusterRequest(), - name="name_value", + client.create_cluster( + cloud_redis_cluster.CreateClusterRequest(), + parent="parent_value", + cluster=cloud_redis_cluster.Cluster(name="name_value"), + cluster_id="cluster_id_value", ) -def test_delete_cluster_rest_error(): +def test_create_cluster_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -4719,133 +5585,46 @@ def test_delete_cluster_rest_error(): @pytest.mark.parametrize( "request_type", [ - cloud_redis_cluster.CreateClusterRequest, + cloud_redis_cluster.GetClusterCertificateAuthorityRequest, dict, ], ) -def test_create_cluster_rest(request_type): +def test_get_cluster_certificate_authority_rest(request_type): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} - request_init["cluster"] = { - "name": "name_value", - "create_time": {"seconds": 751, "nanos": 543}, - "state": 1, - "uid": "uid_value", - "replica_count": 1384, - "authorization_mode": 1, - "transit_encryption_mode": 1, - "size_gb": 739, - "shard_count": 1178, - "psc_configs": [{"network": "network_value"}], - "discovery_endpoints": [ - {"address": "address_value", "port": 453, "psc_config": {}} - ], - "psc_connections": [ - { - "psc_connection_id": "psc_connection_id_value", - "address": "address_value", - "forwarding_rule": "forwarding_rule_value", - "project_id": "project_id_value", - "network": "network_value", - } - ], - "state_info": { - "update_info": {"target_shard_count": 1920, "target_replica_count": 2126} - }, + request_init = { + "name": "projects/sample1/locations/sample2/clusters/sample3/certificateAuthority" } - # The version of a generated dependency at test runtime may differ from the version used during generation. - # Delete any fields which are not present in the current runtime dependency - # See https://github.com/googleapis/gapic-generator-python/issues/1748 - - # Determine if the message type is proto-plus or protobuf - test_field = cloud_redis_cluster.CreateClusterRequest.meta.fields["cluster"] - - def get_message_fields(field): - # Given a field which is a message (composite type), return a list with - # all the fields of the message. - # If the field is not a composite type, return an empty list. - message_fields = [] - - if hasattr(field, "message") and field.message: - is_field_type_proto_plus_type = not hasattr(field.message, "DESCRIPTOR") - - if is_field_type_proto_plus_type: - message_fields = field.message.meta.fields.values() - # Add `# pragma: NO COVER` because there may not be any `*_pb2` field types - else: # pragma: NO COVER - message_fields = field.message.DESCRIPTOR.fields - return message_fields - - runtime_nested_fields = [ - (field.name, nested_field.name) - for field in get_message_fields(test_field) - for nested_field in get_message_fields(field) - ] - - subfields_not_in_runtime = [] - - # For each item in the sample request, create a list of sub fields which are not present at runtime - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for field, value in request_init["cluster"].items(): # pragma: NO COVER - result = None - is_repeated = False - # For repeated fields - if isinstance(value, list) and len(value): - is_repeated = True - result = value[0] - # For fields where the type is another message - if isinstance(value, dict): - result = value - - if result and hasattr(result, "keys"): - for subfield in result.keys(): - if (field, subfield) not in runtime_nested_fields: - subfields_not_in_runtime.append( - { - "field": field, - "subfield": subfield, - "is_repeated": is_repeated, - } - ) - - # Remove fields from the sample request which are not present in the runtime version of the dependency - # Add `# pragma: NO COVER` because this test code will not run if all subfields are present at runtime - for subfield_to_delete in subfields_not_in_runtime: # pragma: NO COVER - field = subfield_to_delete.get("field") - field_repeated = subfield_to_delete.get("is_repeated") - subfield = subfield_to_delete.get("subfield") - if subfield: - if field_repeated: - for i in range(0, len(request_init["cluster"][field])): - del request_init["cluster"][field][i][subfield] - else: - del request_init["cluster"][field][subfield] request = request_type(**request_init) # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = cloud_redis_cluster.CertificateAuthority( + name="name_value", + ) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.CertificateAuthority.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.create_cluster(request) + response = client.get_cluster_certificate_authority(request) # Establish that the response is the type that we expect. - assert response.operation.name == "operations/spam" + assert isinstance(response, cloud_redis_cluster.CertificateAuthority) + assert response.name == "name_value" -def test_create_cluster_rest_use_cached_wrapped_rpc(): +def test_get_cluster_certificate_authority_rest_use_cached_wrapped_rpc(): # Clients should use _prep_wrapped_messages to create cached wrapped rpcs, # instead of constructing them on each call with mock.patch("google.api_core.gapic_v1.method.wrap_method") as wrapper_fn: @@ -4859,40 +5638,40 @@ def test_create_cluster_rest_use_cached_wrapped_rpc(): wrapper_fn.reset_mock() # Ensure method has been cached - assert client._transport.create_cluster in client._transport._wrapped_methods + assert ( + client._transport.get_cluster_certificate_authority + in client._transport._wrapped_methods + ) # Replace cached wrapped function with mock mock_rpc = mock.Mock() mock_rpc.return_value.name = ( "foo" # operation_request.operation in compute client(s) expect a string. ) - client._transport._wrapped_methods[client._transport.create_cluster] = mock_rpc + client._transport._wrapped_methods[ + client._transport.get_cluster_certificate_authority + ] = mock_rpc request = {} - client.create_cluster(request) + client.get_cluster_certificate_authority(request) # Establish that the underlying gRPC stub method was called. assert mock_rpc.call_count == 1 - # Operation methods build a cached wrapper on first rpc call - # subsequent calls should use the cached wrapper - wrapper_fn.reset_mock() - - client.create_cluster(request) + client.get_cluster_certificate_authority(request) # Establish that a new wrapper was not created for this call assert wrapper_fn.call_count == 0 assert mock_rpc.call_count == 2 -def test_create_cluster_rest_required_fields( - request_type=cloud_redis_cluster.CreateClusterRequest, +def test_get_cluster_certificate_authority_rest_required_fields( + request_type=cloud_redis_cluster.GetClusterCertificateAuthorityRequest, ): transport_class = transports.CloudRedisClusterRestTransport request_init = {} - request_init["parent"] = "" - request_init["cluster_id"] = "" + request_init["name"] = "" request = request_type(**request_init) pb_request = request_type.pb(request) jsonified_request = json.loads( @@ -4900,37 +5679,24 @@ def test_create_cluster_rest_required_fields( ) # verify fields with default values are dropped - assert "clusterId" not in jsonified_request unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).create_cluster._get_unset_required_fields(jsonified_request) + ).get_cluster_certificate_authority._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with default values are now present - assert "clusterId" in jsonified_request - assert jsonified_request["clusterId"] == request_init["cluster_id"] - jsonified_request["parent"] = "parent_value" - jsonified_request["clusterId"] = "cluster_id_value" + jsonified_request["name"] = "name_value" unset_fields = transport_class( credentials=ga_credentials.AnonymousCredentials() - ).create_cluster._get_unset_required_fields(jsonified_request) - # Check that path parameters and body parameters are not mixing in. - assert not set(unset_fields) - set( - ( - "cluster_id", - "request_id", - ) - ) + ).get_cluster_certificate_authority._get_unset_required_fields(jsonified_request) jsonified_request.update(unset_fields) # verify required fields with non-default values are left alone - assert "parent" in jsonified_request - assert jsonified_request["parent"] == "parent_value" - assert "clusterId" in jsonified_request - assert jsonified_request["clusterId"] == "cluster_id_value" + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -4939,7 +5705,7 @@ def test_create_cluster_rest_required_fields( request = request_type(**request_init) # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = cloud_redis_cluster.CertificateAuthority() # Mock the http request call within the method and fake a response. with mock.patch.object(Session, "request") as req: # We need to mock transcode() because providing default values @@ -4951,57 +5717,41 @@ def test_create_cluster_rest_required_fields( pb_request = request_type.pb(request) transcode_result = { "uri": "v1/sample_method", - "method": "post", + "method": "get", "query_params": pb_request, } - transcode_result["body"] = pb_request transcode.return_value = transcode_result response_value = Response() response_value.status_code = 200 + + # Convert return value to protobuf type + return_value = cloud_redis_cluster.CertificateAuthority.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - response = client.create_cluster(request) + response = client.get_cluster_certificate_authority(request) - expected_params = [ - ( - "clusterId", - "", - ), - ("$alt", "json;enum-encoding=int"), - ] + expected_params = [("$alt", "json;enum-encoding=int")] actual_params = req.call_args.kwargs["params"] assert expected_params == actual_params -def test_create_cluster_rest_unset_required_fields(): +def test_get_cluster_certificate_authority_rest_unset_required_fields(): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials ) - unset_fields = transport.create_cluster._get_unset_required_fields({}) - assert set(unset_fields) == ( - set( - ( - "clusterId", - "requestId", - ) - ) - & set( - ( - "parent", - "clusterId", - "cluster", - ) - ) + unset_fields = ( + transport.get_cluster_certificate_authority._get_unset_required_fields({}) ) + assert set(unset_fields) == (set(()) & set(("name",))) @pytest.mark.parametrize("null_interceptor", [True, False]) -def test_create_cluster_rest_interceptors(null_interceptor): +def test_get_cluster_certificate_authority_rest_interceptors(null_interceptor): transport = transports.CloudRedisClusterRestTransport( credentials=ga_credentials.AnonymousCredentials(), interceptor=None @@ -5014,16 +5764,16 @@ def test_create_cluster_rest_interceptors(null_interceptor): ) as req, mock.patch.object( path_template, "transcode" ) as transcode, mock.patch.object( - operation.Operation, "_set_result_from_operation" - ), mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "post_create_cluster" + transports.CloudRedisClusterRestInterceptor, + "post_get_cluster_certificate_authority", ) as post, mock.patch.object( - transports.CloudRedisClusterRestInterceptor, "pre_create_cluster" + transports.CloudRedisClusterRestInterceptor, + "pre_get_cluster_certificate_authority", ) as pre: pre.assert_not_called() post.assert_not_called() - pb_message = cloud_redis_cluster.CreateClusterRequest.pb( - cloud_redis_cluster.CreateClusterRequest() + pb_message = cloud_redis_cluster.GetClusterCertificateAuthorityRequest.pb( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest() ) transcode.return_value = { "method": "post", @@ -5035,19 +5785,19 @@ def test_create_cluster_rest_interceptors(null_interceptor): req.return_value = Response() req.return_value.status_code = 200 req.return_value.request = PreparedRequest() - req.return_value._content = json_format.MessageToJson( - operations_pb2.Operation() + req.return_value._content = cloud_redis_cluster.CertificateAuthority.to_json( + cloud_redis_cluster.CertificateAuthority() ) - request = cloud_redis_cluster.CreateClusterRequest() + request = cloud_redis_cluster.GetClusterCertificateAuthorityRequest() metadata = [ ("key", "val"), ("cephalopod", "squid"), ] pre.return_value = request, metadata - post.return_value = operations_pb2.Operation() + post.return_value = cloud_redis_cluster.CertificateAuthority() - client.create_cluster( + client.get_cluster_certificate_authority( request, metadata=[ ("key", "val"), @@ -5059,8 +5809,9 @@ def test_create_cluster_rest_interceptors(null_interceptor): post.assert_called_once() -def test_create_cluster_rest_bad_request( - transport: str = "rest", request_type=cloud_redis_cluster.CreateClusterRequest +def test_get_cluster_certificate_authority_rest_bad_request( + transport: str = "rest", + request_type=cloud_redis_cluster.GetClusterCertificateAuthorityRequest, ): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), @@ -5068,7 +5819,9 @@ def test_create_cluster_rest_bad_request( ) # send a request that will satisfy transcoding - request_init = {"parent": "projects/sample1/locations/sample2"} + request_init = { + "name": "projects/sample1/locations/sample2/clusters/sample3/certificateAuthority" + } request = request_type(**request_init) # Mock the http request call within the method and fake a BadRequest error. @@ -5080,10 +5833,10 @@ def test_create_cluster_rest_bad_request( response_value.status_code = 400 response_value.request = Request() req.return_value = response_value - client.create_cluster(request) + client.get_cluster_certificate_authority(request) -def test_create_cluster_rest_flattened(): +def test_get_cluster_certificate_authority_rest_flattened(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest", @@ -5092,40 +5845,44 @@ def test_create_cluster_rest_flattened(): # Mock the http request call within the method and fake a response. with mock.patch.object(type(client.transport._session), "request") as req: # Designate an appropriate value for the returned response. - return_value = operations_pb2.Operation(name="operations/spam") + return_value = cloud_redis_cluster.CertificateAuthority() # get arguments that satisfy an http rule for this method - sample_request = {"parent": "projects/sample1/locations/sample2"} + sample_request = { + "name": "projects/sample1/locations/sample2/clusters/sample3/certificateAuthority" + } # get truthy value for each flattened field mock_args = dict( - parent="parent_value", - cluster=cloud_redis_cluster.Cluster(name="name_value"), - cluster_id="cluster_id_value", + name="name_value", ) mock_args.update(sample_request) # Wrap the value into a proper Response obj response_value = Response() response_value.status_code = 200 + # Convert return value to protobuf type + return_value = cloud_redis_cluster.CertificateAuthority.pb(return_value) json_return_value = json_format.MessageToJson(return_value) response_value._content = json_return_value.encode("UTF-8") req.return_value = response_value - client.create_cluster(**mock_args) + client.get_cluster_certificate_authority(**mock_args) # Establish that the underlying call was made with the expected # request object values. assert len(req.mock_calls) == 1 _, args, _ = req.mock_calls[0] assert path_template.validate( - "%s/v1beta1/{parent=projects/*/locations/*}/clusters" + "%s/v1beta1/{name=projects/*/locations/*/clusters/*/certificateAuthority}" % client.transport._host, args[1], ) -def test_create_cluster_rest_flattened_error(transport: str = "rest"): +def test_get_cluster_certificate_authority_rest_flattened_error( + transport: str = "rest", +): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport=transport, @@ -5134,15 +5891,13 @@ def test_create_cluster_rest_flattened_error(transport: str = "rest"): # Attempting to call a method with both a request object and flattened # fields is an error. with pytest.raises(ValueError): - client.create_cluster( - cloud_redis_cluster.CreateClusterRequest(), - parent="parent_value", - cluster=cloud_redis_cluster.Cluster(name="name_value"), - cluster_id="cluster_id_value", + client.get_cluster_certificate_authority( + cloud_redis_cluster.GetClusterCertificateAuthorityRequest(), + name="name_value", ) -def test_create_cluster_rest_error(): +def test_get_cluster_certificate_authority_rest_error(): client = CloudRedisClusterClient( credentials=ga_credentials.AnonymousCredentials(), transport="rest" ) @@ -5292,6 +6047,7 @@ def test_cloud_redis_cluster_base_transport(): "update_cluster", "delete_cluster", "create_cluster", + "get_cluster_certificate_authority", "get_location", "list_locations", "get_operation", @@ -5595,6 +6351,9 @@ def test_cloud_redis_cluster_client_transport_session_collision(transport_name): session1 = client1.transport.create_cluster._session session2 = client2.transport.create_cluster._session assert session1 != session2 + session1 = client1.transport.get_cluster_certificate_authority._session + session2 = client2.transport.get_cluster_certificate_authority._session + assert session1 != session2 def test_cloud_redis_cluster_grpc_transport_channel(): @@ -5757,25 +6516,53 @@ def test_cloud_redis_cluster_grpc_lro_async_client(): assert transport.operations_client is transport.operations_client -def test_cluster_path(): +def test_certificate_authority_path(): project = "squid" location = "clam" cluster = "whelk" - expected = "projects/{project}/locations/{location}/clusters/{cluster}".format( + expected = "projects/{project}/locations/{location}/clusters/{cluster}/certificateAuthority".format( project=project, location=location, cluster=cluster, ) - actual = CloudRedisClusterClient.cluster_path(project, location, cluster) + actual = CloudRedisClusterClient.certificate_authority_path( + project, location, cluster + ) assert expected == actual -def test_parse_cluster_path(): +def test_parse_certificate_authority_path(): expected = { "project": "octopus", "location": "oyster", "cluster": "nudibranch", } + path = CloudRedisClusterClient.certificate_authority_path(**expected) + + # Check that the path construction is reversible. + actual = CloudRedisClusterClient.parse_certificate_authority_path(path) + assert expected == actual + + +def test_cluster_path(): + project = "cuttlefish" + location = "mussel" + cluster = "winkle" + expected = "projects/{project}/locations/{location}/clusters/{cluster}".format( + project=project, + location=location, + cluster=cluster, + ) + actual = CloudRedisClusterClient.cluster_path(project, location, cluster) + assert expected == actual + + +def test_parse_cluster_path(): + expected = { + "project": "nautilus", + "location": "scallop", + "cluster": "abalone", + } path = CloudRedisClusterClient.cluster_path(**expected) # Check that the path construction is reversible. @@ -5784,7 +6571,7 @@ def test_parse_cluster_path(): def test_common_billing_account_path(): - billing_account = "cuttlefish" + billing_account = "squid" expected = "billingAccounts/{billing_account}".format( billing_account=billing_account, ) @@ -5794,7 +6581,7 @@ def test_common_billing_account_path(): def test_parse_common_billing_account_path(): expected = { - "billing_account": "mussel", + "billing_account": "clam", } path = CloudRedisClusterClient.common_billing_account_path(**expected) @@ -5804,7 +6591,7 @@ def test_parse_common_billing_account_path(): def test_common_folder_path(): - folder = "winkle" + folder = "whelk" expected = "folders/{folder}".format( folder=folder, ) @@ -5814,7 +6601,7 @@ def test_common_folder_path(): def test_parse_common_folder_path(): expected = { - "folder": "nautilus", + "folder": "octopus", } path = CloudRedisClusterClient.common_folder_path(**expected) @@ -5824,7 +6611,7 @@ def test_parse_common_folder_path(): def test_common_organization_path(): - organization = "scallop" + organization = "oyster" expected = "organizations/{organization}".format( organization=organization, ) @@ -5834,7 +6621,7 @@ def test_common_organization_path(): def test_parse_common_organization_path(): expected = { - "organization": "abalone", + "organization": "nudibranch", } path = CloudRedisClusterClient.common_organization_path(**expected) @@ -5844,7 +6631,7 @@ def test_parse_common_organization_path(): def test_common_project_path(): - project = "squid" + project = "cuttlefish" expected = "projects/{project}".format( project=project, ) @@ -5854,7 +6641,7 @@ def test_common_project_path(): def test_parse_common_project_path(): expected = { - "project": "clam", + "project": "mussel", } path = CloudRedisClusterClient.common_project_path(**expected) @@ -5864,8 +6651,8 @@ def test_parse_common_project_path(): def test_common_location_path(): - project = "whelk" - location = "octopus" + project = "winkle" + location = "nautilus" expected = "projects/{project}/locations/{location}".format( project=project, location=location, @@ -5876,8 +6663,8 @@ def test_common_location_path(): def test_parse_common_location_path(): expected = { - "project": "oyster", - "location": "nudibranch", + "project": "scallop", + "location": "abalone", } path = CloudRedisClusterClient.common_location_path(**expected)