Skip to content

Commit b11e8fd

Browse files
committed
Implementing Bigtable Operation class.
Used to provide an object as a response to Bigtable cluster operations (create, update and undelete).
1 parent 32ec056 commit b11e8fd

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

gcloud/bigtable/cluster.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,38 @@ def _process_operation(operation_pb):
155155
return operation_id, operation_begin
156156

157157

158+
class Operation(object):
159+
"""Representation of a Google API Long-Running Operation.
160+
161+
In particular, the
162+
163+
:type op_type: str
164+
:param op_type: The type of operation being performed. Expect
165+
``create``, ``update`` or ``undelete``.
166+
167+
:type op_id: int
168+
:param op_id: The ID of the operation.
169+
170+
:type begin: :class:`datetime.datetime`
171+
:param begin: The time when the operation was started.
172+
"""
173+
174+
def __init__(self, op_type, op_id, begin):
175+
self.op_type = op_type
176+
self.op_id = op_id
177+
self.begin = begin
178+
179+
def __eq__(self, other):
180+
if not isinstance(other, self.__class__):
181+
return False
182+
return (other.op_type == self.op_type and
183+
other.op_id == self.op_id and
184+
other.begin == self.begin)
185+
186+
def __ne__(self, other):
187+
return not self.__eq__(other)
188+
189+
158190
class Cluster(object):
159191
"""Representation of a Google Cloud Bigtable Cluster.
160192
@@ -186,9 +218,6 @@ def __init__(self, zone, cluster_id, client,
186218
self.display_name = display_name or cluster_id
187219
self.serve_nodes = serve_nodes
188220
self._client = client
189-
self._operation_type = None
190-
self._operation_id = None
191-
self._operation_begin = None
192221

193222
def table(self, table_id):
194223
"""Factory to create a table associated with this cluster.
@@ -309,15 +338,18 @@ def create(self):
309338
cluster.cluster_id = 'i-changed-my-mind'
310339
311340
before calling :meth:`create`.
341+
342+
:rtype: :class:`Operation`
343+
:returns: The long-running operation corresponding to the
344+
create operation.
312345
"""
313346
request_pb = _prepare_create_request(self)
314347
# We expect an `operations_pb2.Operation`.
315348
cluster_pb = self._client._cluster_stub.CreateCluster(
316349
request_pb, self._client.timeout_seconds)
317350

318-
self._operation_type = 'create'
319-
self._operation_id, self._operation_begin = _process_operation(
320-
cluster_pb.current_operation)
351+
op_id, op_begin = _process_operation(cluster_pb.current_operation)
352+
return Operation('create', op_id, op_begin)
321353

322354
def delete(self):
323355
"""Delete this cluster.

gcloud/bigtable/test_cluster.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,56 @@
1616
import unittest2
1717

1818

19+
class TestOperation(unittest2.TestCase):
20+
21+
def _getTargetClass(self):
22+
from gcloud.bigtable.cluster import Operation
23+
return Operation
24+
25+
def _makeOne(self, *args, **kwargs):
26+
return self._getTargetClass()(*args, **kwargs)
27+
28+
def test_constructor(self):
29+
import datetime
30+
op_type = 'fake-op'
31+
op_id = 8915
32+
begin = datetime.datetime(2015, 10, 22, 1, 1)
33+
operation = self._makeOne(op_type, op_id, begin)
34+
35+
self.assertEqual(operation.op_type, op_type)
36+
self.assertEqual(operation.op_id, op_id)
37+
self.assertEqual(operation.begin, begin)
38+
39+
def test___eq__(self):
40+
import datetime
41+
op_type = 'fake-op'
42+
op_id = 8915
43+
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)
46+
self.assertEqual(operation1, operation2)
47+
48+
def test___eq__type_differ(self):
49+
operation1 = self._makeOne('foo', 123, None)
50+
operation2 = object()
51+
self.assertNotEqual(operation1, operation2)
52+
53+
def test___ne__same_value(self):
54+
import datetime
55+
op_type = 'fake-op'
56+
op_id = 8915
57+
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)
60+
comparison_val = (operation1 != operation2)
61+
self.assertFalse(comparison_val)
62+
63+
def test___ne__(self):
64+
operation1 = self._makeOne('foo', 123, None)
65+
operation2 = self._makeOne('bar', 456, None)
66+
self.assertNotEqual(operation1, operation2)
67+
68+
1969
class TestCluster(unittest2.TestCase):
2070

2171
def _getTargetClass(self):
@@ -263,7 +313,7 @@ def test_create(self):
263313
client._cluster_stub = stub = _FakeStub(response_pb)
264314

265315
# Create expected_result.
266-
expected_result = None # create() has no return value.
316+
expected_result = MUT.Operation('create', op_id, op_begin)
267317

268318
# Create the mocks.
269319
prep_create_called = []
@@ -276,7 +326,7 @@ def mock_prep_create_req(cluster):
276326

277327
def mock_process_operation(operation_pb):
278328
process_operation_called.append(operation_pb)
279-
return (op_id, op_begin)
329+
return op_id, op_begin
280330

281331
# Perform the method and check the result.
282332
with _Monkey(MUT, _prepare_create_request=mock_prep_create_req,
@@ -289,9 +339,6 @@ def mock_process_operation(operation_pb):
289339
(request_pb, timeout_seconds),
290340
{},
291341
)])
292-
self.assertEqual(cluster._operation_type, 'create')
293-
self.assertEqual(cluster._operation_id, op_id)
294-
self.assertTrue(cluster._operation_begin is op_begin)
295342
self.assertEqual(prep_create_called, [cluster])
296343
self.assertEqual(process_operation_called, [current_op])
297344

0 commit comments

Comments
 (0)