Skip to content

Commit

Permalink
Implementing Bigtable Operation class.
Browse files Browse the repository at this point in the history
Used to provide an object as a response to
Bigtable cluster operations (create, update and
undelete).
  • Loading branch information
dhermes committed Dec 9, 2015
1 parent 32ec056 commit 29d97ee
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
45 changes: 39 additions & 6 deletions gcloud/bigtable/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,39 @@ def _process_operation(operation_pb):
return operation_id, operation_begin


class Operation(object):
"""Representation of a Google API Long-Running Operation.
In particular, these will be the result of operations on
clusters using the Cloud Bigtable API.
:type op_type: str
:param op_type: The type of operation being performed. Expect
``create``, ``update`` or ``undelete``.
:type op_id: int
:param op_id: The ID of the operation.
:type begin: :class:`datetime.datetime`
:param begin: The time when the operation was started.
"""

def __init__(self, op_type, op_id, begin):
self.op_type = op_type
self.op_id = op_id
self.begin = begin

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (other.op_type == self.op_type and
other.op_id == self.op_id and
other.begin == self.begin)

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


class Cluster(object):
"""Representation of a Google Cloud Bigtable Cluster.
Expand Down Expand Up @@ -186,9 +219,6 @@ def __init__(self, zone, cluster_id, client,
self.display_name = display_name or cluster_id
self.serve_nodes = serve_nodes
self._client = client
self._operation_type = None
self._operation_id = None
self._operation_begin = None

def table(self, table_id):
"""Factory to create a table associated with this cluster.
Expand Down Expand Up @@ -309,15 +339,18 @@ def create(self):
cluster.cluster_id = 'i-changed-my-mind'
before calling :meth:`create`.
:rtype: :class:`Operation`
:returns: The long-running operation corresponding to the
create operation.
"""
request_pb = _prepare_create_request(self)
# We expect an `operations_pb2.Operation`.
cluster_pb = self._client._cluster_stub.CreateCluster(
request_pb, self._client.timeout_seconds)

self._operation_type = 'create'
self._operation_id, self._operation_begin = _process_operation(
cluster_pb.current_operation)
op_id, op_begin = _process_operation(cluster_pb.current_operation)
return Operation('create', op_id, op_begin)

def delete(self):
"""Delete this cluster.
Expand Down
57 changes: 52 additions & 5 deletions gcloud/bigtable/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,56 @@
import unittest2


class TestOperation(unittest2.TestCase):

def _getTargetClass(self):
from gcloud.bigtable.cluster import Operation
return Operation

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor(self):
import datetime
op_type = 'fake-op'
op_id = 8915
begin = datetime.datetime(2015, 10, 22, 1, 1)
operation = self._makeOne(op_type, op_id, begin)

self.assertEqual(operation.op_type, op_type)
self.assertEqual(operation.op_id, op_id)
self.assertEqual(operation.begin, begin)

def test___eq__(self):
import datetime
op_type = 'fake-op'
op_id = 8915
begin = datetime.datetime(2015, 10, 22, 1, 1)
operation1 = self._makeOne(op_type, op_id, begin)
operation2 = self._makeOne(op_type, op_id, begin)
self.assertEqual(operation1, operation2)

def test___eq__type_differ(self):
operation1 = self._makeOne('foo', 123, None)
operation2 = object()
self.assertNotEqual(operation1, operation2)

def test___ne__same_value(self):
import datetime
op_type = 'fake-op'
op_id = 8915
begin = datetime.datetime(2015, 10, 22, 1, 1)
operation1 = self._makeOne(op_type, op_id, begin)
operation2 = self._makeOne(op_type, op_id, begin)
comparison_val = (operation1 != operation2)
self.assertFalse(comparison_val)

def test___ne__(self):
operation1 = self._makeOne('foo', 123, None)
operation2 = self._makeOne('bar', 456, None)
self.assertNotEqual(operation1, operation2)


class TestCluster(unittest2.TestCase):

def _getTargetClass(self):
Expand Down Expand Up @@ -263,7 +313,7 @@ def test_create(self):
client._cluster_stub = stub = _FakeStub(response_pb)

# Create expected_result.
expected_result = None # create() has no return value.
expected_result = MUT.Operation('create', op_id, op_begin)

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

def mock_process_operation(operation_pb):
process_operation_called.append(operation_pb)
return (op_id, op_begin)
return op_id, op_begin

# Perform the method and check the result.
with _Monkey(MUT, _prepare_create_request=mock_prep_create_req,
Expand All @@ -289,9 +339,6 @@ def mock_process_operation(operation_pb):
(request_pb, timeout_seconds),
{},
)])
self.assertEqual(cluster._operation_type, 'create')
self.assertEqual(cluster._operation_id, op_id)
self.assertTrue(cluster._operation_begin is op_begin)
self.assertEqual(prep_create_called, [cluster])
self.assertEqual(process_operation_called, [current_op])

Expand Down

0 comments on commit 29d97ee

Please sign in to comment.