Skip to content

Commit ce42385

Browse files
author
Jon Wayne Parrott
committed
Update bigtable to use future.operation
1 parent f20faa1 commit ce42385

File tree

5 files changed

+48
-80
lines changed

5 files changed

+48
-80
lines changed

bigtable/google/cloud/bigtable/cluster.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@
2121
instance_pb2 as data_v2_pb2)
2222
from google.cloud.bigtable._generated import (
2323
bigtable_instance_admin_pb2 as messages_v2_pb2)
24-
from google.cloud.operation import Operation
25-
from google.cloud.operation import register_type
26-
24+
from google.cloud.future import operation
2725

2826
_CLUSTER_NAME_RE = re.compile(r'^projects/(?P<project>[^/]+)/'
2927
r'instances/(?P<instance>[^/]+)/clusters/'
@@ -33,9 +31,6 @@
3331
"""Default number of nodes to use when creating a cluster."""
3432

3533

36-
register_type(messages_v2_pb2.UpdateClusterMetadata)
37-
38-
3934
def _prepare_create_request(cluster):
4035
"""Creates a protobuf request for a CreateCluster request.
4136
@@ -49,7 +44,8 @@ def _prepare_create_request(cluster):
4944
parent=cluster._instance.name,
5045
cluster_id=cluster.cluster_id,
5146
cluster=data_v2_pb2.Cluster(
52-
serve_nodes=cluster.serve_nodes,
47+
location=cluster.location,
48+
serve_nodes=cluster.serve_nodes
5349
),
5450
)
5551

@@ -207,15 +203,18 @@ def create(self):
207203
:returns: The long-running operation corresponding to the
208204
create operation.
209205
"""
210-
request_pb = _prepare_create_request(self)
211-
# We expect a `google.longrunning.operations_pb2.Operation`.
212206
client = self._instance._client
207+
208+
# We expect a `google.longrunning.operations_pb2.Operation`.
209+
request_pb = _prepare_create_request(self)
213210
operation_pb = client._instance_stub.CreateCluster(request_pb)
214211

215-
operation = Operation.from_pb(operation_pb, client)
216-
operation.target = self
217-
operation.caller_metadata['request_type'] = 'CreateCluster'
218-
return operation
212+
operation_future = operation.from_grpc(
213+
operation_pb,
214+
client._operations_stub,
215+
data_v2_pb2.Cluster,
216+
metadata_type=messages_v2_pb2.UpdateClusterMetadata)
217+
return operation_future
219218

220219
def update(self):
221220
"""Update this cluster.
@@ -235,18 +234,21 @@ def update(self):
235234
:returns: The long-running operation corresponding to the
236235
update operation.
237236
"""
237+
client = self._instance._client
238+
239+
# We expect a `google.longrunning.operations_pb2.Operation`.
238240
request_pb = data_v2_pb2.Cluster(
239241
name=self.name,
240242
serve_nodes=self.serve_nodes,
241243
)
242-
# We expect a `google.longrunning.operations_pb2.Operation`.
243-
client = self._instance._client
244244
operation_pb = client._instance_stub.UpdateCluster(request_pb)
245245

246-
operation = Operation.from_pb(operation_pb, client)
247-
operation.target = self
248-
operation.caller_metadata['request_type'] = 'UpdateCluster'
249-
return operation
246+
operation_future = operation.from_grpc(
247+
operation_pb,
248+
client._operations_stub,
249+
data_v2_pb2.Cluster,
250+
metadata_type=messages_v2_pb2.UpdateClusterMetadata)
251+
return operation_future
250252

251253
def delete(self):
252254
"""Delete this cluster.

bigtable/google/cloud/bigtable/instance.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,14 @@
2626
from google.cloud.bigtable.cluster import Cluster
2727
from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES
2828
from google.cloud.bigtable.table import Table
29-
from google.cloud.operation import Operation
30-
from google.cloud.operation import register_type
29+
from google.cloud.future import operation
3130

3231

3332
_EXISTING_INSTANCE_LOCATION_ID = 'see-existing-cluster'
3433
_INSTANCE_NAME_RE = re.compile(r'^projects/(?P<project>[^/]+)/'
3534
r'instances/(?P<instance_id>[a-z][-a-z0-9]*)$')
3635

3736

38-
register_type(messages_v2_pb2.CreateInstanceMetadata)
39-
register_type(data_v2_pb2.Instance)
40-
41-
4237
def _prepare_create_request(instance):
4338
"""Creates a protobuf request for a CreateInstance request.
4439
@@ -232,10 +227,12 @@ def create(self):
232227
# We expect a `google.longrunning.operations_pb2.Operation`.
233228
operation_pb = self._client._instance_stub.CreateInstance(request_pb)
234229

235-
operation = Operation.from_pb(operation_pb, self._client)
236-
operation.target = self
237-
operation.caller_metadata['request_type'] = 'CreateInstance'
238-
return operation
230+
operation_future = operation.from_grpc(
231+
operation_pb,
232+
self._client._operations_stub,
233+
data_v2_pb2.Instance,
234+
metadata_type=messages_v2_pb2.CreateInstanceMetadata)
235+
return operation_future
239236

240237
def update(self):
241238
"""Update this instance.

bigtable/tests/system.py

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
from google.cloud.environment_vars import BIGTABLE_EMULATOR
3333

3434
from test_utils.retry import RetryErrors
35-
from test_utils.retry import RetryResult
3635
from test_utils.system import EmulatorCreds
3736
from test_utils.system import unique_resource_id
3837

@@ -65,27 +64,6 @@ class Config(object):
6564
IN_EMULATOR = False
6665

6766

68-
def _wait_until_complete(operation, max_attempts=5):
69-
"""Wait until an operation has completed.
70-
71-
:type operation: :class:`google.cloud.operation.Operation`
72-
:param operation: Operation that has not completed.
73-
74-
:type max_attempts: int
75-
:param max_attempts: (Optional) The maximum number of times to check if
76-
the operation has completed. Defaults to 5.
77-
78-
:rtype: bool
79-
:returns: Boolean indicating if the operation is complete.
80-
"""
81-
82-
def _operation_complete(result):
83-
return result
84-
85-
retry = RetryResult(_operation_complete, max_tries=max_attempts)
86-
return retry(operation.poll)()
87-
88-
8967
def _retry_on_unavailable(exc):
9068
"""Retry only errors whose status code is 'UNAVAILABLE'."""
9169
from grpc import StatusCode
@@ -117,8 +95,7 @@ def setUpModule():
11795

11896
# After listing, create the test instance.
11997
created_op = Config.INSTANCE.create()
120-
if not _wait_until_complete(created_op):
121-
raise RuntimeError('Instance creation exceed 5 seconds.')
98+
created_op.result(timeout=10)
12299

123100

124101
def tearDownModule():
@@ -166,7 +143,7 @@ def test_create_instance(self):
166143
self.instances_to_delete.append(instance)
167144

168145
# We want to make sure the operation completes.
169-
self.assertTrue(_wait_until_complete(operation))
146+
operation.result(timeout=10)
170147

171148
# Create a new instance instance and make sure it is the same.
172149
instance_alt = Config.CLIENT.instance(ALT_INSTANCE_ID, LOCATION_ID)

bigtable/tests/unit/test_cluster.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import unittest
1717

18+
import mock
19+
1820

1921
class TestCluster(unittest.TestCase):
2022

@@ -232,7 +234,7 @@ def test_reload(self):
232234

233235
def test_create(self):
234236
from google.longrunning import operations_pb2
235-
from google.cloud.operation import Operation
237+
from google.cloud.future import operation
236238
from google.cloud.bigtable._generated import (
237239
bigtable_instance_admin_pb2 as messages_v2_pb2)
238240
from tests.unit._testing import _FakeStub
@@ -256,13 +258,9 @@ def test_create(self):
256258
# Perform the method and check the result.
257259
result = cluster.create()
258260

259-
self.assertIsInstance(result, Operation)
260-
self.assertEqual(result.name, OP_NAME)
261-
self.assertIs(result.target, cluster)
262-
self.assertIs(result.client, client)
261+
self.assertIsInstance(result, operation.Operation)
262+
self.assertEqual(result.operation.name, OP_NAME)
263263
self.assertIsNone(result.metadata)
264-
self.assertEqual(result.caller_metadata,
265-
{'request_type': 'CreateCluster'})
266264

267265
self.assertEqual(len(stub.method_calls), 1)
268266
api_name, args, kwargs = stub.method_calls[0]
@@ -278,7 +276,7 @@ def test_create(self):
278276
def test_update(self):
279277
import datetime
280278
from google.longrunning import operations_pb2
281-
from google.cloud.operation import Operation
279+
from google.cloud.future import operation
282280
from google.protobuf.any_pb2 import Any
283281
from google.cloud._helpers import _datetime_to_pb_timestamp
284282
from google.cloud.bigtable._generated import (
@@ -324,15 +322,11 @@ def test_update(self):
324322

325323
result = cluster.update()
326324

327-
self.assertIsInstance(result, Operation)
328-
self.assertEqual(result.name, OP_NAME)
329-
self.assertIs(result.target, cluster)
330-
self.assertIs(result.client, client)
325+
self.assertIsInstance(result, operation.Operation)
326+
self.assertEqual(result.operation.name, OP_NAME)
331327
self.assertIsInstance(result.metadata,
332328
messages_v2_pb2.UpdateClusterMetadata)
333329
self.assertEqual(result.metadata.request_time, NOW_PB)
334-
self.assertEqual(result.caller_metadata,
335-
{'request_type': 'UpdateCluster'})
336330

337331
self.assertEqual(len(stub.method_calls), 1)
338332
api_name, args, kwargs = stub.method_calls[0]
@@ -446,6 +440,7 @@ class _Client(object):
446440
def __init__(self, project):
447441
self.project = project
448442
self.project_name = 'projects/' + self.project
443+
self._operations_stub = mock.sentinel.operations_stub
449444

450445
def __eq__(self, other):
451446
return (other.project == self.project and

bigtable/tests/unit/test_instance.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import unittest
1717

18+
import mock
19+
1820

1921
class TestInstance(unittest.TestCase):
2022

@@ -236,7 +238,7 @@ def test_create(self):
236238
bigtable_instance_admin_pb2 as messages_v2_pb2)
237239
from google.cloud._helpers import _datetime_to_pb_timestamp
238240
from tests.unit._testing import _FakeStub
239-
from google.cloud.operation import Operation
241+
from google.cloud.future import operation
240242
from google.cloud.bigtable.cluster import DEFAULT_SERVE_NODES
241243

242244
NOW = datetime.datetime.utcnow()
@@ -263,15 +265,11 @@ def test_create(self):
263265
# Perform the method and check the result.
264266
result = instance.create()
265267

266-
self.assertIsInstance(result, Operation)
267-
self.assertEqual(result.name, self.OP_NAME)
268-
self.assertIs(result.target, instance)
269-
self.assertIs(result.client, client)
268+
self.assertIsInstance(result, operation.Operation)
269+
self.assertEqual(result.operation.name, self.OP_NAME)
270270
self.assertIsInstance(result.metadata,
271271
messages_v2_pb2.CreateInstanceMetadata)
272272
self.assertEqual(result.metadata.request_time, NOW_PB)
273-
self.assertEqual(result.caller_metadata,
274-
{'request_type': 'CreateInstance'})
275273

276274
self.assertEqual(len(stub.method_calls), 1)
277275
api_name, args, kwargs = stub.method_calls[0]
@@ -291,7 +289,7 @@ def test_create_w_explicit_serve_nodes(self):
291289
from google.cloud.bigtable._generated import (
292290
bigtable_instance_admin_pb2 as messages_v2_pb2)
293291
from tests.unit._testing import _FakeStub
294-
from google.cloud.operation import Operation
292+
from google.cloud.future import operation
295293

296294
SERVE_NODES = 5
297295

@@ -308,10 +306,8 @@ def test_create_w_explicit_serve_nodes(self):
308306
# Perform the method and check the result.
309307
result = instance.create()
310308

311-
self.assertIsInstance(result, Operation)
312-
self.assertEqual(result.name, self.OP_NAME)
313-
self.assertIs(result.target, instance)
314-
self.assertIs(result.client, client)
309+
self.assertIsInstance(result, operation.Operation)
310+
self.assertEqual(result.operation.name, self.OP_NAME)
315311

316312
self.assertEqual(len(stub.method_calls), 1)
317313
api_name, args, kwargs = stub.method_calls[0]
@@ -582,6 +578,7 @@ class _Client(object):
582578
def __init__(self, project):
583579
self.project = project
584580
self.project_name = 'projects/' + self.project
581+
self._operations_stub = mock.sentinel.operations_stub
585582

586583
def copy(self):
587584
from copy import deepcopy

0 commit comments

Comments
 (0)