8000 Implementing Bigtable Operation.finished(). · googleapis/google-cloud-python@39c5b24 · GitHub
[go: up one dir, main page]

Skip to content

Commit 39c5b24

Browse files
committed
Implementing Bigtable Operation.finished().
This method is intended to be used to check if a create, update or undelete operation has completed.
1 parent 6819b4d commit 39c5b24

File tree

2 files changed

+111
-8
lines changed

2 files changed

+111
-8
lines changed

gcloud/bigtable/cluster.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
bigtable_cluster_service_messages_pb2 as messages_pb2)
2525
from gcloud.bigtable._generated import (
2626
bigtable_table_service_messages_pb2 as table_messages_pb2)
27+
from gcloud.bigtable._generated import operations_pb2
2728
from gcloud.bigtable.table import Table
2829

2930

@@ -170,23 +171,54 @@ class Operation(object):
170171
171172
:type begin: :class:`datetime.datetime`
172173
:param begin: The time when the operation was started.
174+
175+
:type cluster: :class:`Cluster`
176+
:param cluster: The cluster that created the operation.
173177
"""
174178

175-
def __init__(self, op_type, op_id, begin):
179+
def __init__(self, op_type, op_id, begin, cluster=None):
176180
self.op_type = op_type
177181
self.op_id = op_id
178182
self.begin = begin
183+
self._cluster = cluster
184+
self._complete = False
179185

180186
def __eq__(self, other):
181187
if not isinstance(other, self.__class__):
182188
return False
183189
return (other.op_type == self.op_type and
184190
other.op_id == self.op_id and
185-
other.begin == self.begin)
191+
other.begin == self.begin and
192+
other._cluster == self._cluster and
193+
other._complete == self._complete)
186194

187195
def __ne__(self, other):
188196
return not self.__eq__(other)
189197

198+
def finished(self):
199+
"""Check if the operation has finished.
200+
201+
:rtype: bool
202+
:returns: A boolean indicating if the current operation has completed.
203+
:raises: :class:`ValueError <exceptions.ValueError>` if the operation
204+
has already completed.
205+
"""
206+
if self._complete:
207+
raise ValueError('The operation has completed.')
208+
209+
operation_name = ('operations/' + self._cluster.name +
210+
'/operations/%d' % (self.op_id,))
211+
request_pb = operations_pb2.GetOperationRequest(name=operation_name)
212+
# We expact a `._generated.operations_pb2.Operation`.
213+
operation_pb = self._cluster._client._operations_stub.GetOperation(
214+
request_pb, self._cluster._client.timeout_seconds)
215+
216+
if operation_pb.done:
217+
self._complete = True
218+
return True
219+
else:
220+
return False
221+
190222

191223
class Cluster(object):
192224
"""Representation of a Google Cloud Bigtable Cluster.

gcloud/bigtable/test_cluster.py

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,34 @@ def _getTargetClass(self):
2525
def _makeOne(self, *args, **kwargs):
2626
return self._getTargetClass()(*args, **kwargs)
2727

28-
def test_constructor(self):
28+
def _constructor_test_helper(self, cluster=None):
2929
import datetime
3030
op_type = 'fake-op'
3131
op_id = 8915
3232
begin = datetime.datetime(2015, 10, 22, 1, 1)
33-
operation = self._makeOne(op_type, op_id, begin)
33+
operation = self._makeOne(op_type, op_id, begin, cluster=cluster)
3434

3535
self.assertEqual(operation.op_type, op_type)
3636
self.assertEqual(operation.op_id, op_id)
3737
self.assertEqual(operation.begin, begin)
38+
self.assertEqual(operation._cluster, cluster)
39+
self.assertFalse(operation._complete)
40+
41+
def test_constructor_defaults(self):
42+
self._constructor_test_helper()
43+
44+
def test_constructor_explicit_cluster(self):
45+
cluster = object()
46+
self._constructor_test_helper(cluster=cluster)
3847

3948
def test___eq__(self):
4049
import datetime
4150
op_type = 'fake-op'
4251
op_id = 8915
4352
begin = datetime.datetime(2015, 10, 22, 1, 1)
44-
operation1 = self._makeOne(op_type, op_id, begin)
45-
operation2 = self._makeOne(op_type, op_id, begin)
53+
cluster = object()
54+
operation1 = self._makeOne(op_type, op_id, begin, cluster=cluster)
55+
operation2 = self._makeOne(op_type, op_id, begin, cluster=cluster)
4656
self.assertEqual(operation1, operation2)
4757

4858
def test___eq__type_differ(self):
@@ -55,8 +65,9 @@ def test___ne__same_value(self):
5565
op_type = 'fake-op'
5666
op_id = 8915
5767
begin = datetime.datetime(2015, 10, 22, 1, 1)
58-
operation1 = self._makeOne(op_type, op_id, begin)
59-
operation2 = self._makeOne(op_type, op_id, begin)
68+
cluster = object()
69+
operation1 = self._makeOne(op_type, op_id, begin, cluster=cluster)
70+
operation2 = self._makeOne(op_type, op_id, begin, cluster=cluster)
6071
comparison_val = (operation1 != operation2)
6172
self.assertFalse(comparison_val)
6273

@@ -65,6 +76,66 @@ def test___ne__(self):
6576
operation2 = self._makeOne('bar', 456, None)
6677
self.assertNotEqual(operation1, operation2)
6778

79+
def test_finished_without_operation(self):
80+
operation = self._makeOne(None, None, None)
81+
operation._complete = True
82+
with self.assertRaises(ValueError):
83+
operation.finished()
84+
85+
def _finished_helper(self, done):
86+
import datetime
87+
from gcloud.bigtable._generated import operations_pb2
88+
from gcloud.bigtable._testing import _FakeStub
89+
from gcloud.bigtable.cluster import Cluster
90+
91+
project = 'PROJECT'
92+
zone = 'zone'
93+
cluster_id = 'cluster-id'
94+
op_type = 'fake-op'
95+
op_id = 789
96+
begin = datetime.datetime(2015, 10, 22, 1, 1)
97+
timeout_seconds = 1
98+
99+
client = _Client(project, timeout_seconds=timeout_seconds)
100+
cluster = Cluster(zone, cluster_id, client)
101+
operation = self._makeOne(op_type, op_id, begin, cluster=cluster)
102+
103+
# Create request_pb
104+
op_name = ('operations/projects/' + project + '/zones/' +
105+
zone + '/clusters/' + cluster_id +
106+
'/operations/%d' % (op_id,))
107+
request_pb = operations_pb2.GetOperationRequest(name=op_name)
108+
109+
# Create response_pb
110+
response_pb = operations_pb2.Operation(done=done)
111+
112+
# Patch the stub used by the API method.
113+
client._operations_stub = stub = _FakeStub(response_pb)
114+
115+
# Create expected_result.
116+
expected_result = done
117+
118+
# Perform the method and check the result.
119+
result = operation.finished()
120+
121+
self.assertEqual(result, expected_result)
122+
self.assertEqual(stub.method_calls, [(
123+
'GetOperation',
124+
(request_pb, timeout_seconds),
125+
{},
126+
)])
127+
128+
if done:
129+
self.assertTrue(operation._complete)
130+
else:
131+
self.assertFalse(operation._complete)
132+
133+
def test_finished(self):
134+
self._finished_helper(done=True)
135+
136+
def test_finished_not_done(self):
137+
self._finished_helper(done=False)
138+
68139

69140
class TestCluster(unittest2.TestCase):
70141

0 commit comments

Comments
 (0)
0