Google Cloud Redis API client library for managing fully managed Redis instances on Google Cloud
—
Complete lifecycle management of Redis instances including creation, configuration updates, deletion, and status monitoring.
List all Redis instances in a specified location with automatic pagination support.
def list_instances(
self,
*,
parent: str,
page_size: Optional[int] = None,
page_token: Optional[str] = None,
**kwargs
) -> pagers.ListInstancesPager:
"""
Lists all Redis instances owned by a project in a given location.
Args:
parent: Required. The resource name of the instance location using the form:
"projects/{project_id}/locations/{location_id}"
page_size: The maximum number of items to return. If not specified,
a default value is chosen by the service.
page_token: The standard list page token.
Returns:
pagers.ListInstancesPager: An iterator of Instance objects.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request failed.
"""Get detailed information about a specific Redis instance.
def get_instance(self, *, name: str, **kwargs) -> cloud_redis.Instance:
"""
Gets the details of a specific Redis instance.
Args:
name: Required. Redis instance resource name using the form:
"projects/{project_id}/locations/{location_id}/instances/{instance_id}"
Returns:
cloud_redis.Instance: The Instance resource.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request failed.
google.api_core.exceptions.NotFound: If the instance doesn't exist.
"""Create a new Redis instance with specified configuration.
def create_instance(
self,
*,
parent: str,
instance_id: str,
instance: cloud_redis.Instance,
**kwargs
) -> operation.Operation:
"""
Creates a Redis instance based on the specified tier and memory size.
Args:
parent: Required. The resource name of the instance location using the form:
"projects/{project_id}/locations/{location_id}"
instance_id: Required. The logical name of the Redis instance in the customer project.
Must be 1-40 characters in length, alphanumeric and dashes only.
instance: Required. A Redis Instance resource to be created.
Returns:
google.api_core.operation.Operation: A long-running operation object.
The result will be an Instance object.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request failed.
google.api_core.exceptions.AlreadyExists: If instance already exists.
"""Update configuration of an existing Redis instance.
def update_instance(
self,
*,
update_mask: field_mask_pb2.FieldMask,
instance: cloud_redis.Instance,
**kwargs
) -> operation.Operation:
"""
Updates the metadata and configuration of a Redis instance.
Args:
update_mask: Required. Mask of fields to update. At least one path must be supplied.
Valid field paths include: display_name, labels, memory_size_gb,
redis_config, replica_count, maintenance_policy, persistence_config.
instance: Required. Update description. Only fields specified in update_mask are updated.
Returns:
google.api_core.operation.Operation: A long-running operation object.
The result will be an Instance object.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request failed.
google.api_core.exceptions.NotFound: If the instance doesn't exist.
"""Delete a Redis instance permanently.
def delete_instance(self, *, name: str, **kwargs) -> operation.Operation:
"""
Deletes a specific Redis instance. Instance stops serving and data is deleted.
Args:
name: Required. Redis instance resource name using the form:
"projects/{project_id}/locations/{location_id}/instances/{instance_id}"
Returns:
google.api_core.operation.Operation: A long-running operation object.
The result will be Empty.
Raises:
google.api_core.exceptions.GoogleAPICallError: If the request failed.
google.api_core.exceptions.NotFound: If the instance doesn't exist.
"""Administrative operations for managing long-running operations and locations.
def list_operations(
self,
*,
name: str,
filter: Optional[str] = None,
page_size: Optional[int] = None,
page_token: Optional[str] = None,
**kwargs
) -> pagers.ListOperationsPager:
"""Lists operations for the Redis service."""
def get_operation(self, *, name: str, **kwargs) -> operations_pb2.Operation:
"""Gets the latest state of a long-running operation."""
def delete_operation(self, *, name: str, **kwargs) -> None:
"""Deletes a long-running operation."""
def cancel_operation(self, *, name: str, **kwargs) -> None:
"""Starts asynchronous cancellation on a long-running operation."""
def get_location(self, *, name: str, **kwargs) -> locations_pb2.Location:
"""Gets information about a location."""
def list_locations(
self,
*,
name: str,
filter: Optional[str] = None,
page_size: Optional[int] = None,
page_token: Optional[str] = None,
**kwargs
) -> pagers.ListLocationsPager:
"""Lists information about supported locations."""class ListInstancesRequest:
parent: str
page_size: int
page_token: str
class GetInstanceRequest:
name: str
class CreateInstanceRequest:
parent: str
instance_id: str
instance: Instance
class UpdateInstanceRequest:
update_mask: FieldMask
instance: Instance
class DeleteInstanceRequest:
name: strclass ListInstancesResponse:
instances: List[Instance]
next_page_token: str
unreachable: List[str]from google.cloud.redis import CloudRedisClient
client = CloudRedisClient()
parent = "projects/my-project/locations/us-central1"
# List all instances
instances = client.list_instances(parent=parent)
for instance in instances:
print(f"Name: {instance.name}")
print(f"State: {instance.state}")
print(f"Host: {instance.host}:{instance.port}")
print(f"Memory: {instance.memory_size_gb}GB")
print("---")
# List with pagination
instances = client.list_instances(parent=parent, page_size=10)
for page in instances.pages:
for instance in page.instances:
print(f"Instance: {instance.display_name}")from google.cloud.redis import CloudRedisClient, Instance
client = CloudRedisClient()
# Define the instance
new_instance = Instance(
display_name="My Redis Cache",
tier=Instance.Tier.STANDARD_HA,
memory_size_gb=2,
redis_version="REDIS_6_X",
authorized_network="projects/my-project/global/networks/default",
connect_mode=Instance.ConnectMode.PRIVATE_SERVICE_ACCESS,
auth_enabled=True,
transit_encryption_mode=Instance.TransitEncryptionMode.SERVER_AUTHENTICATION,
labels={
"environment": "production",
"team": "backend"
}
)
# Create the instance
parent = "projects/my-project/locations/us-central1"
operation = client.create_instance(
parent=parent,
instance_id="my-production-cache",
instance=new_instance
)
print(f"Creating instance: {operation.name}")
# Wait for completion
result = operation.result(timeout=1800) # 30 minutes timeout
print(f"Instance created: {result.name}")
print(f"Host: {result.host}:{result.port}")from google.cloud.redis import CloudRedisClient, Instance
from google.protobuf import field_mask_pb2
client = CloudRedisClient()
# Get the current instance
instance_name = "projects/my-project/locations/us-central1/instances/my-cache"
instance = client.get_instance(name=instance_name)
# Update memory size and labels
instance.memory_size_gb = 4
instance.labels["updated"] = "true"
# Create update mask
update_mask = field_mask_pb2.FieldMask()
update_mask.paths.extend(["memory_size_gb", "labels"])
# Perform the update
operation = client.update_instance(
update_mask=update_mask,
instance=instance
)
result = operation.result(timeout=1800)
print(f"Instance updated: {result.memory_size_gb}GB")from google.cloud.redis import CloudRedisClient
client = CloudRedisClient()
instance_name = "projects/my-project/locations/us-central1/instances/old-cache"
# Delete the instance
operation = client.delete_instance(name=instance_name)
print(f"Deleting instance: {operation.name}")
# Wait for deletion to complete
operation.result(timeout=1800)
print("Instance deleted successfully")from google.cloud.redis import CloudRedisClient
client = CloudRedisClient()
# List recent operations
parent = "projects/my-project/locations/us-central1"
operations = client.list_operations(name=parent)
for operation in operations:
print(f"Operation: {operation.name}")
print(f"Done: {operation.done}")
if operation.done:
if operation.error:
print(f"Error: {operation.error}")
else:
print("Success!")
print("---")
# Get specific operation status
operation_name = "projects/my-project/locations/us-central1/operations/operation-123"
operation = client.get_operation(name=operation_name)
print(f"Operation {operation.name} done: {operation.done}")from google.cloud.redis import CloudRedisClient
client = CloudRedisClient()
# List available locations
parent = "projects/my-project"
locations = client.list_locations(name=parent)
print("Available locations:")
for location in locations:
print(f"Location: {location.name}")
print(f"Display Name: {location.display_name}")
for label_key, label_value in location.labels.items():
print(f" {label_key}: {label_value}")
print("---")
# Get specific location details
location_name = "projects/my-project/locations/us-central1"
location = client.get_location(name=location_name)
print(f"Location: {location.display_name}")
print(f"Labels: {dict(location.labels)}")Install with Tessl CLI
npx tessl i tessl/pypi-google-cloud-redis