Skip to content

Commit

Permalink
Implementing Bigtable ColumnFamily.update().
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Dec 22, 2015
1 parent 9e2b07d commit 888cd25
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 1 deletion.
25 changes: 25 additions & 0 deletions gcloud/bigtable/column_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ def to_pb(self):
class ColumnFamily(object):
"""Representation of a Google Cloud Bigtable Column Family.
We can use a :class:`ColumnFamily` to:
* :meth:`update` itself
:type column_family_id: str
:param column_family_id: The ID of the column family. Must be of the
form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.
Expand Down Expand Up @@ -238,6 +242,27 @@ def __eq__(self, other):
def __ne__(self, other):
return not self.__eq__(other)

def update(self):
"""Update this column family.
.. note::
The Bigtable Table Admin API currently returns
``BigtableTableService.UpdateColumnFamily is not yet implemented``
when this method is used. It's unclear when this method will
actually be supported by the API.
"""
request_kwargs = {'name': self.name}
if self.gc_rule is not None:
request_kwargs['gc_rule'] = self.gc_rule.to_pb()
request_pb = data_pb2.ColumnFamily(**request_kwargs)
client = self._table._cluster._client
# We expect a `.data_pb2.ColumnFamily`
client._table_stub.UpdateColumnFamily(request_pb,
client.timeout_seconds)


def _gc_rule_from_pb(gc_rule_pb):
"""Convert a protobuf GC rule to a native object.
Expand Down
71 changes: 70 additions & 1 deletion gcloud/bigtable/test_column_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,62 @@ def test___ne__(self):
column_family2 = self._makeOne('column_family_id2', None)
self.assertNotEqual(column_family1, column_family2)

def _update_test_helper(self, gc_rule=None):
from gcloud.bigtable._generated import (
bigtable_table_data_pb2 as data_pb2)
from gcloud.bigtable._testing import _FakeStub

project_id = 'project-id'
zone = 'zone'
cluster_id = 'cluster-id'
table_id = 'table-id'
column_family_id = 'column-family-id'
timeout_seconds = 28
table_name = ('projects/' + project_id + '/zones/' + zone +
'/clusters/' + cluster_id + '/tables/' + table_id)
column_family_name = table_name + '/columnFamilies/' + column_family_id

client = _Client(timeout_seconds=timeout_seconds)
table = _Table(table_name, client=client)
column_family = self._makeOne(column_family_id, table, gc_rule=gc_rule)

# Create request_pb
if gc_rule is None:
request_pb = data_pb2.ColumnFamily(name=column_family_name)
else:
request_pb = data_pb2.ColumnFamily(
name=column_family_name,
gc_rule=gc_rule.to_pb(),
)

# Create response_pb
response_pb = data_pb2.ColumnFamily()

# Patch the stub used by the API method.
client._table_stub = stub = _FakeStub(response_pb)

# Create expected_result.
expected_result = None # update() has no return value.

# Perform the method and check the result.
self.assertEqual(stub.results, (response_pb,))
result = column_family.update()
self.assertEqual(stub.results, ())
self.assertEqual(result, expected_result)
self.assertEqual(stub.method_calls, [(
'UpdateColumnFamily',
(request_pb, timeout_seconds),
{},
)])

def test_update(self):
self._update_test_helper(gc_rule=None)

def test_update_with_gc_rule(self):
from gcloud.bigtable.column_family import MaxVersionsGCRule
gc_rule = MaxVersionsGCRule(1337)
self._update_test_helper(gc_rule=gc_rule)


class Test__gc_rule_from_pb(unittest2.TestCase):

Expand Down Expand Up @@ -471,7 +527,20 @@ def WhichOneof(cls, name):
self.assertEqual(MockProto.names, ['rule'])


class _Cluster(object):

def __init__(self, client=None):
self._client = client


class _Client(object):

def __init__(self, timeout_seconds=None):
self.timeout_seconds = timeout_seconds


class _Table(object):

def __init__(self, name):
def __init__(self, name, client=None):
self.name = name
self._cluster = _Cluster(client)

0 comments on commit 888cd25

Please sign in to comment.