Skip to content

Commit 4dabdd2

Browse files
committed
Implementing Bigtable Cluster.operation_finished().
This method is intended to be used to check if a create, update or undelete operation has completed.
1 parent c4ab6de commit 4dabdd2

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

gcloud/bigtable/cluster.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from gcloud.bigtable._generated import bigtable_cluster_data_pb2 as data_pb2
2323
from gcloud.bigtable._generated import (
2424
bigtable_cluster_service_messages_pb2 as messages_pb2)
25+
from gcloud.bigtable._generated import operations_pb2
2526
from gcloud.bigtable.table import Table
2627

2728

@@ -291,6 +292,32 @@ def reload(self):
291292
# cluster ID on the response match the request.
292293
self._update_from_pb(cluster_pb)
293294

295+
def operation_finished(self):
296+
"""Check if the current operation has finished.
297+
298+
:rtype: bool
299+
:returns: A boolean indicating if the current operation has completed.
300+
:raises: :class:`ValueError <exceptions.ValueError>` if there is no
301+
current operation set.
302+
"""
303+
if self._operation_id is None:
304+
raise ValueError('There is no current operation.')
305+
306+
operation_name = ('operations/' + self.name +
307+
'/operations/%d' % (self._operation_id,))
308+
request_pb = operations_pb2.GetOperationRequest(name=operation_name)
309+
# We expact a `._generated.operations_pb2.Operation`.
310+
operation_pb = self._client._operations_stub.GetOperation(
311+
request_pb, self._client.timeout_seconds)
312+
313+
if operation_pb.done:
314+
self._operation_type = None
315+
self._operation_id = None
316+
self._operation_begin = None
317+
return True
318+
else:
319+
return False
320+
294321
def create(self):
295322
"""Create this cluster.
296323

gcloud/bigtable/test_cluster.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,71 @@ def test_reload(self):
231231
self.assertEqual(cluster.serve_nodes, serve_nodes)
232232
self.assertEqual(cluster.display_name, display_name)
233233

234+
def test_operation_finished_without_operation(self):
235+
zone = 'zone'
236+
cluster_id = 'cluster-id'
237+
cluster = self._makeOne(zone, cluster_id, None)
238+
self.assertEqual(cluster._operation_type, None)
239+
with self.assertRaises(ValueError):
240+
cluster.operation_finished()
241+
242+
def _operation_finished_helper(self, done):
243+
from gcloud.bigtable._generated import operations_pb2
244+
from gcloud.bigtable._testing import _FakeStub
245+
246+
project = 'PROJECT'
247+
zone = 'zone'
248+
cluster_id = 'cluster-id'
249+
timeout_seconds = 1
250+
251+
client = _Client(project, timeout_seconds=timeout_seconds)
252+
cluster = self._makeOne(zone, cluster_id, client)
253+
254+
# Patch up the cluster's operation attributes.
255+
cluster._operation_id = op_id = 789
256+
cluster._operation_begin = op_begin = object()
257+
cluster._operation_type = op_type = object()
258+
259+
# Create request_pb
260+
op_name = ('operations/projects/' + project + '/zones/' +
261+
zone + '/clusters/' + cluster_id +
262+
'/operations/%d' % (op_id,))
263+
request_pb = operations_pb2.GetOperationRequest(name=op_name)
264+
265+
# Create response_pb
266+
response_pb = operations_pb2.Operation(done=done)
267+
268+
# Patch the stub used by the API method.
269+
client._operations_stub = stub = _FakeStub(response_pb)
270+
271+
# Create expected_result.
272+
expected_result = done
273+
274+
# Perform the method and check the result.
275+
result = cluster.operation_finished()
276+
277+
self.assertEqual(result, expected_result)
278+
self.assertEqual(stub.method_calls, [(
279+
'GetOperation',
280+
(request_pb, timeout_seconds),
281+
{},
282+
)])
283+
284+
if done:
285+
self.assertEqual(cluster._operation_type, None)
286+
self.assertEqual(cluster._operation_id, None)
287+
self.assertEqual(cluster._operation_begin, None)
288+
else:
289+
self.assertEqual(cluster._operation_type, op_type)
290+
self.assertEqual(cluster._operation_id, op_id)
291+
self.assertEqual(cluster._operation_begin, op_begin)
292+
293+
def test_operation_finished(self):
294+
self._operation_finished_helper(done=True)
295+
296+
def test_operation_finished_not_done(self):
297+
self._operation_finished_helper(done=False)
298+
234299
def test_create(self):
235300
from gcloud._testing import _Monkey
236301
from gcloud.bigtable._generated import (

0 commit comments

Comments
 (0)