Skip to content
Permalink
Browse files
fix!: convert operations pbs into Operation objects when listing oper…
…ations (#186)

Co-authored-by: larkee <larkee@users.noreply.github.com>
  • Loading branch information
larkee and larkee committed Jan 15, 2021
1 parent f1e21ed commit ed7152adc37290c63e59865265f36c593d9b8da3
Showing with 29 additions and 6 deletions.
  1. +17 −2 google/cloud/spanner_v1/instance.py
  2. +12 −4 tests/unit/test_instance.py
@@ -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
@@ -475,7 +476,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.
@@ -503,4 +504,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
)
@@ -697,6 +697,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
@@ -726,7 +727,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),
@@ -738,8 +739,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
@@ -769,7 +772,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),
@@ -783,8 +786,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
@@ -827,7 +832,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),
@@ -839,8 +844,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 (
@@ -888,7 +895,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),
@@ -902,6 +909,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 (

0 comments on commit ed7152a

Please sign in to comment.