Skip to content

Commit

Permalink
Remove v1 'import_instance' / 'export_instance'; add v1beta1 'failove…
Browse files Browse the repository at this point in the history
…r_instance' (via synth). (#7937)

Also, add routing header to method metadata; add 'docs' session to nox.
  • Loading branch information
yoshi-automation authored and tseaver committed May 17, 2019
1 parent b6252fa commit 6b251fb
Show file tree
Hide file tree
Showing 18 changed files with 839 additions and 1,220 deletions.
301 changes: 104 additions & 197 deletions redis/google/cloud/redis_v1/gapic/cloud_redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import google.api_core.gapic_v1.client_info
import google.api_core.gapic_v1.config
import google.api_core.gapic_v1.method
import google.api_core.gapic_v1.routing_header
import google.api_core.grpc_helpers
import google.api_core.operation
import google.api_core.operations_v1
Expand Down Expand Up @@ -50,16 +51,21 @@ class CloudRedisClient(object):
The ``redis.googleapis.com`` service implements the Google Cloud
Memorystore for Redis API and defines the following resource model for
managing Redis instances: \* The service works with a collection of
cloud projects, named: ``/projects/*`` \* Each project has a collection
of available locations, named: ``/locations/*`` \* Each location has a
collection of Redis instances, named: ``/instances/*`` \* As such, Redis
instances are resources of the form:
``/projects/{project_id}/locations/{location_id}/instances/{instance_id}``
Note that location\_id must be refering to a GCP ``region``; for
example: \*
``projects/redpepper-1290/locations/us-central1/instances/my-redis``
managing Redis instances:
- The service works with a collection of cloud projects, named:
``/projects/*``
- Each project has a collection of available locations, named:
``/locations/*``
- Each location has a collection of Redis instances, named:
``/instances/*``
- As such, Redis instances are resources of the form:
``/projects/{project_id}/locations/{location_id}/instances/{instance_id}``
Note that location\_id must be referring to a GCP ``region``; for
example:
- ``projects/redpepper-1290/locations/us-central1/instances/my-redis``
"""

SERVICE_ADDRESS = "redis.googleapis.com:443"
Expand Down Expand Up @@ -90,22 +96,22 @@ def from_service_account_file(cls, filename, *args, **kwargs):
from_service_account_json = from_service_account_file

@classmethod
def location_path(cls, project, location):
"""Return a fully-qualified location string."""
def instance_path(cls, project, location, instance):
"""Return a fully-qualified instance string."""
return google.api_core.path_template.expand(
"projects/{project}/locations/{location}",
"projects/{project}/locations/{location}/instances/{instance}",
project=project,
location=location,
instance=instance,
)

@classmethod
def instance_path(cls, project, location, instance):
"""Return a fully-qualified instance string."""
def location_path(cls, project, location):
"""Return a fully-qualified location string."""
return google.api_core.path_template.expand(
"projects/{project}/locations/{location}/instances/{instance}",
"projects/{project}/locations/{location}",
project=project,
location=location,
instance=instance,
)

def __init__(
Expand Down Expand Up @@ -219,8 +225,9 @@ def list_instances(
Lists all Redis instances owned by a project in either the specified
location (region) or all locations.
The location should have the following format: \*
``projects/{project_id}/locations/{location_id}``
The location should have the following format:
- ``projects/{project_id}/locations/{location_id}``
If ``location_id`` is specified as ``-`` (wildcard), then all regions
available to the project are queried, and the results are aggregated.
Expand Down Expand Up @@ -291,6 +298,19 @@ def list_instances(
request = cloud_redis_pb2.ListInstancesRequest(
parent=parent, page_size=page_size
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("parent", parent)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

iterator = google.api_core.page_iterator.GRPCIterator(
client=None,
method=functools.partial(
Expand Down Expand Up @@ -360,6 +380,19 @@ def get_instance(
)

request = cloud_redis_pb2.GetInstanceRequest(name=name)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("name", name)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

return self._inner_api_calls["get_instance"](
request, retry=retry, timeout=timeout, metadata=metadata
)
Expand Down Expand Up @@ -460,6 +493,19 @@ def create_instance(
request = cloud_redis_pb2.CreateInstanceRequest(
parent=parent, instance_id=instance_id, instance=instance
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("parent", parent)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

operation = self._inner_api_calls["create_instance"](
request, retry=retry, timeout=timeout, metadata=metadata
)
Expand Down Expand Up @@ -559,6 +605,19 @@ def update_instance(
request = cloud_redis_pb2.UpdateInstanceRequest(
update_mask=update_mask, instance=instance
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("instance.name", instance.name)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

operation = self._inner_api_calls["update_instance"](
request, retry=retry, timeout=timeout, metadata=metadata
)
Expand Down Expand Up @@ -633,191 +692,26 @@ def delete_instance(
)

request = cloud_redis_pb2.DeleteInstanceRequest(name=name)
operation = self._inner_api_calls["delete_instance"](
request, retry=retry, timeout=timeout, metadata=metadata
)
return google.api_core.operation.from_gapic(
operation,
self.transport._operations_client,
empty_pb2.Empty,
metadata_type=cloud_redis_pb2.OperationMetadata,
)

def import_instance(
self,
name,
input_config,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
):
"""
Import a Redis RDB snapshot file from GCS into a Redis instance.
Redis may stop serving during this operation. Instance state will be
IMPORTING for entire operation. When complete, the instance will contain
only data from the imported file.
The returned operation is automatically deleted after a few hours, so
there is no need to call DeleteOperation.
Example:
>>> from google.cloud import redis_v1
>>>
>>> client = redis_v1.CloudRedisClient()
>>>
>>> name = client.instance_path('[PROJECT]', '[LOCATION]', '[INSTANCE]')
>>>
>>> # TODO: Initialize `input_config`:
>>> input_config = {}
>>>
>>> response = client.import_instance(name, input_config)
>>>
>>> def callback(operation_future):
... # Handle result.
... result = operation_future.result()
>>>
>>> response.add_done_callback(callback)
>>>
>>> # Handle metadata.
>>> metadata = response.metadata()
Args:
name (str): Required. Redis instance resource name using the form:
``projects/{project_id}/locations/{location_id}/instances/{instance_id}``
where ``location_id`` refers to a GCP region
input_config (Union[dict, ~google.cloud.redis_v1.types.InputConfig]): Required. Specify data to be imported.
If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.redis_v1.types.InputConfig`
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will not
be retried.
timeout (Optional[float]): The amount of time, in seconds, to wait
for the request to complete. Note that if ``retry`` is
specified, the timeout applies to each individual attempt.
metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
that is provided to the method.
Returns:
A :class:`~google.cloud.redis_v1.types._OperationFuture` instance.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
google.api_core.exceptions.RetryError: If the request failed due
to a retryable error and retry attempts failed.
ValueError: If the parameters are invalid.
"""
# Wrap the transport method to add retry and timeout logic.
if "import_instance" not in self._inner_api_calls:
self._inner_api_calls[
"import_instance"
] = google.api_core.gapic_v1.method.wrap_method(
self.transport.import_instance,
default_retry=self._method_configs["ImportInstance"].retry,
default_timeout=self._method_configs["ImportInstance"].timeout,
client_info=self._client_info,
)

request = cloud_redis_pb2.ImportInstanceRequest(
name=name, input_config=input_config
)
operation = self._inner_api_calls["import_instance"](
request, retry=retry, timeout=timeout, metadata=metadata
)
return google.api_core.operation.from_gapic(
operation,
self.transport._operations_client,
cloud_redis_pb2.Instance,
metadata_type=cloud_redis_pb2.OperationMetadata,
)

def export_instance(
self,
name,
output_config,
retry=google.api_core.gapic_v1.method.DEFAULT,
timeout=google.api_core.gapic_v1.method.DEFAULT,
metadata=None,
):
"""
Export Redis instance data into a Redis RDB format file in GCS.
Redis will continue serving during this operation.
The returned operation is automatically deleted after a few hours, so
there is no need to call DeleteOperation.
Example:
>>> from google.cloud import redis_v1
>>>
>>> client = redis_v1.CloudRedisClient()
>>>
>>> name = client.instance_path('[PROJECT]', '[LOCATION]', '[INSTANCE]')
>>>
>>> # TODO: Initialize `output_config`:
>>> output_config = {}
>>>
>>> response = client.export_instance(name, output_config)
>>>
>>> def callback(operation_future):
... # Handle result.
... result = operation_future.result()
>>>
>>> response.add_done_callback(callback)
>>>
>>> # Handle metadata.
>>> metadata = response.metadata()
Args:
name (str): Required. Redis instance resource name using the form:
``projects/{project_id}/locations/{location_id}/instances/{instance_id}``
where ``location_id`` refers to a GCP region
output_config (Union[dict, ~google.cloud.redis_v1.types.OutputConfig]): Required. Specify data to be exported.
If a dict is provided, it must be of the same form as the protobuf
message :class:`~google.cloud.redis_v1.types.OutputConfig`
retry (Optional[google.api_core.retry.Retry]): A retry object used
to retry requests. If ``None`` is specified, requests will not
be retried.
timeout (Optional[float]): The amount of time, in seconds, to wait
for the request to complete. Note that if ``retry`` is
specified, the timeout applies to each individual attempt.
metadata (Optional[Sequence[Tuple[str, str]]]): Additional metadata
that is provided to the method.
Returns:
A :class:`~google.cloud.redis_v1.types._OperationFuture` instance.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request
failed for any reason.
google.api_core.exceptions.RetryError: If the request failed due
to a retryable error and retry attempts failed.
ValueError: If the parameters are invalid.
"""
# Wrap the transport method to add retry and timeout logic.
if "export_instance" not in self._inner_api_calls:
self._inner_api_calls[
"export_instance"
] = google.api_core.gapic_v1.method.wrap_method(
self.transport.export_instance,
default_retry=self._method_configs["ExportInstance"].retry,
default_timeout=self._method_configs["ExportInstance"].timeout,
client_info=self._client_info,
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("name", name)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

request = cloud_redis_pb2.ExportInstanceRequest(
name=name, output_config=output_config
)
operation = self._inner_api_calls["export_instance"](
operation = self._inner_api_calls["delete_instance"](
request, retry=retry, timeout=timeout, metadata=metadata
)
return google.api_core.operation.from_gapic(
operation,
self.transport._operations_client,
cloud_redis_pb2.Instance,
empty_pb2.Empty,
metadata_type=cloud_redis_pb2.OperationMetadata,
)

Expand Down Expand Up @@ -895,6 +789,19 @@ def failover_instance(
request = cloud_redis_pb2.FailoverInstanceRequest(
name=name, data_protection_mode=data_protection_mode
)
if metadata is None:
metadata = []
metadata = list(metadata)
try:
routing_header = [("name", name)]
except AttributeError:
pass
else:
routing_metadata = google.api_core.gapic_v1.routing_header.to_grpc_metadata(
routing_header
)
metadata.append(routing_metadata)

operation = self._inner_api_calls["failover_instance"](
request, retry=retry, timeout=timeout, metadata=metadata
)
Expand Down
Loading

0 comments on commit 6b251fb

Please sign in to comment.