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 23, 2015
1 parent 3a26d60 commit d54b9e1
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
20 changes: 20 additions & 0 deletions gcloud/bigtable/column_family.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class ColumnFamily(object):
We can use a :class:`ColumnFamily` to:
* :meth:`update` itself
* :meth:`delete` itself
:type column_family_id: str
Expand Down Expand Up @@ -244,6 +245,25 @@ def __eq__(self, other):
def __ne__(self, other):
return not self.__eq__(other)

def update(self):
"""Update this column family.
.. note::
Only the GC rule can be updated. By changing the column family ID,
you will simply be referring to a different column family.
"""
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`. We ignore it since the only
# data it contains are the GC rule and the column family ID already
# stored on this instance.
client._table_stub.UpdateColumnFamily(request_pb,
client.timeout_seconds)

def delete(self):
"""Delete this column family."""
request_pb = messages_pb2.DeleteColumnFamilyRequest(name=self.name)
Expand Down
56 changes: 56 additions & 0 deletions 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)

def test_delete(self):
from gcloud.bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
Expand Down

0 comments on commit d54b9e1

Please sign in to comment.