Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: convert operations pbs into Operation objects when listing oper… #186

Merged
merged 1 commit into from Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions google/cloud/spanner_v1/instance.py
Expand Up @@ -14,6 +14,7 @@

"""User friendly container for Cloud Spanner Instance."""

import google.api_core.operation
import re

from google.cloud.spanner_admin_instance_v1 import Instance as InstancePB
Expand Down Expand Up @@ -465,7 +466,7 @@ def list_backup_operations(self, filter_="", page_size=None):
page_iter = self._client.database_admin_api.list_backup_operations(
request=request, metadata=metadata
)
return page_iter
return map(self._item_to_operation, page_iter)

def list_database_operations(self, filter_="", page_size=None):
"""List database operations for the instance.
Expand Down Expand Up @@ -493,4 +494,18 @@ def list_database_operations(self, filter_="", page_size=None):
page_iter = self._client.database_admin_api.list_database_operations(
request=request, metadata=metadata
)
return page_iter
return map(self._item_to_operation, page_iter)

def _item_to_operation(self, operation_pb):
"""Convert an operation protobuf to the native object.
:type operation_pb: :class:`~google.longrunning.operations.Operation`
:param operation_pb: An operation returned from the API.
:rtype: :class:`~google.api_core.operation.Operation`
:returns: The next operation in the page.
"""
operations_client = self._client.database_admin_api.transport.operations_client
metadata_type = _type_string_to_type_pb(operation_pb.metadata.type_url)
response_type = _OPERATION_RESPONSE_TYPES[metadata_type]
return google.api_core.operation.from_gapic(
operation_pb, operations_client, response_type, metadata_type=metadata_type
)
16 changes: 12 additions & 4 deletions tests/unit/test_instance.py
Expand Up @@ -673,6 +673,7 @@ def test_list_backups_w_options(self):
)

def test_list_backup_operations_defaults(self):
from google.api_core.operation import Operation
from google.cloud.spanner_admin_database_v1 import CreateBackupMetadata
from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
from google.cloud.spanner_admin_database_v1 import ListBackupOperationsRequest
Expand Down Expand Up @@ -702,7 +703,7 @@ def test_list_backup_operations_defaults(self):
api._transport.list_backup_operations
] = mock.Mock(return_value=operations_pb)

instance.list_backup_operations()
ops = instance.list_backup_operations()

expected_metadata = (
("google-cloud-resource-prefix", instance.name),
Expand All @@ -714,8 +715,10 @@ def test_list_backup_operations_defaults(self):
retry=mock.ANY,
timeout=mock.ANY,
)
self.assertTrue(all([type(op) == Operation for op in ops]))

def test_list_backup_operations_w_options(self):
from google.api_core.operation import Operation
from google.cloud.spanner_admin_database_v1 import CreateBackupMetadata
from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
from google.cloud.spanner_admin_database_v1 import ListBackupOperationsRequest
Expand Down Expand Up @@ -745,7 +748,7 @@ def test_list_backup_operations_w_options(self):
api._transport.list_backup_operations
] = mock.Mock(return_value=operations_pb)

instance.list_backup_operations(filter_="filter", page_size=10)
ops = instance.list_backup_operations(filter_="filter", page_size=10)

expected_metadata = (
("google-cloud-resource-prefix", instance.name),
Expand All @@ -759,8 +762,10 @@ def test_list_backup_operations_w_options(self):
retry=mock.ANY,
timeout=mock.ANY,
)
self.assertTrue(all([type(op) == Operation for op in ops]))

def test_list_database_operations_defaults(self):
from google.api_core.operation import Operation
from google.cloud.spanner_admin_database_v1 import CreateDatabaseMetadata
from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
from google.cloud.spanner_admin_database_v1 import ListDatabaseOperationsRequest
Expand Down Expand Up @@ -803,7 +808,7 @@ def test_list_database_operations_defaults(self):
api._transport.list_database_operations
] = mock.Mock(return_value=databases_pb)

instance.list_database_operations()
ops = instance.list_database_operations()

expected_metadata = (
("google-cloud-resource-prefix", instance.name),
Expand All @@ -815,8 +820,10 @@ def test_list_database_operations_defaults(self):
retry=mock.ANY,
timeout=mock.ANY,
)
self.assertTrue(all([type(op) == Operation for op in ops]))

def test_list_database_operations_w_options(self):
from google.api_core.operation import Operation
from google.cloud.spanner_admin_database_v1 import DatabaseAdminClient
from google.cloud.spanner_admin_database_v1 import ListDatabaseOperationsRequest
from google.cloud.spanner_admin_database_v1 import (
Expand Down Expand Up @@ -864,7 +871,7 @@ def test_list_database_operations_w_options(self):
api._transport.list_database_operations
] = mock.Mock(return_value=databases_pb)

instance.list_database_operations(filter_="filter", page_size=10)
ops = instance.list_database_operations(filter_="filter", page_size=10)

expected_metadata = (
("google-cloud-resource-prefix", instance.name),
Expand All @@ -878,6 +885,7 @@ def test_list_database_operations_w_options(self):
retry=mock.ANY,
timeout=mock.ANY,
)
self.assertTrue(all([type(op) == Operation for op in ops]))

def test_type_string_to_type_pb_hit(self):
from google.cloud.spanner_admin_database_v1 import (
Expand Down