Skip to content

Commit

Permalink
Implementing Cluster.list_tables().
Browse files Browse the repository at this point in the history
Also had to implement Table.__eq__ as a result since we
want to compare Table values.
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent 44f0849 commit 8bfcf4e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
4 changes: 2 additions & 2 deletions gcloud_bigtable/_grpc_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def _grpc_call_helper(self, call_method, method_name, request_obj,

def _grpc_client_test_helper(self, method_name, result_method, request_pb,
response_pb, expected_result, project_id,
stub_factory=None):
stub_factory=None, stub_host=None):
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable._testing import _Monkey
from gcloud_bigtable.client import Client
Expand All @@ -189,7 +189,7 @@ def _grpc_client_test_helper(self, method_name, result_method, request_pb,
factory_args = (
scoped_creds,
stub_factory or getattr(self._MUT, self._STUB_FACTORY_NAME),
self._STUB_HOST,
stub_host or self._STUB_HOST,
self._STUB_PORT,
)
self.assertEqual(mock_make_stub.factory_calls,
Expand Down
36 changes: 36 additions & 0 deletions gcloud_bigtable/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
from gcloud_bigtable._generated import (
bigtable_cluster_service_messages_pb2 as messages_pb2)
from gcloud_bigtable._generated import bigtable_cluster_service_pb2
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as table_messages_pb2)
from gcloud_bigtable._generated import operations_pb2
from gcloud_bigtable._helpers import _parse_pb_any_to_native
from gcloud_bigtable._helpers import _pb_timestamp_to_datetime
from gcloud_bigtable._helpers import _require_pb_property
from gcloud_bigtable.connection import TIMEOUT_SECONDS
from gcloud_bigtable.connection import make_stub
from gcloud_bigtable.table import TABLE_ADMIN_HOST
from gcloud_bigtable.table import TABLE_ADMIN_PORT
from gcloud_bigtable.table import TABLE_STUB_FACTORY
from gcloud_bigtable.table import Table


Expand Down Expand Up @@ -403,3 +408,34 @@ def undelete(self, timeout_seconds=TIMEOUT_SECONDS):
self._operation_type = 'undelete'
self._operation_id, self._operation_begin = _process_operation(
operation_pb2)

def list_tables(self, timeout_seconds=TIMEOUT_SECONDS):
"""List the tables in this cluster.
:type timeout_seconds: integer
:param timeout_seconds: Number of seconds for request time-out.
If not passed, defaults to ``TIMEOUT_SECONDS``.
:rtype: list of :class:`Table`
:returns: The list of tables owned by the cluster.
:raises: :class:`ValueError` if one of the returned tables has a name
that is not of the expected format.
"""
request_pb = table_messages_pb2.ListTablesRequest(name=self.name)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
response = stub.ListTables.async(request_pb, timeout_seconds)
# We expect a `table_messages_pb2.ListTablesResponse`
table_list_pb = response.result()

result = []
for table_pb in table_list_pb.tables:
before, table_id = table_pb.name.split(
self.name + '/tables/', 1)
if before != '':
raise ValueError('Table name %s not of expected format' % (
table_pb.name,))
result.append(self.table(table_id))

return result
9 changes: 9 additions & 0 deletions gcloud_bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ def name(self):
"""
return self.cluster.name + '/tables/' + self.table_id

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
return (other.table_id == self.table_id and
other.cluster == self.cluster)

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

def exists(self, timeout_seconds=TIMEOUT_SECONDS):
"""Reload the metadata for this table.
Expand Down
56 changes: 56 additions & 0 deletions gcloud_bigtable/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,3 +523,59 @@ def result_method(client):
self.assertEqual(CLUSTER_CREATED[0]._operation_id, op_id)
self.assertTrue(CLUSTER_CREATED[0]._operation_begin is op_begin)
mock_process_operation.check_called(self, [(response_pb,)])

def _list_tables_helper(self, table_id, table_name=None):
from gcloud_bigtable._generated import (
bigtable_table_data_pb2 as table_data_pb2)
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as table_messages_pb2)
from gcloud_bigtable import cluster as MUT

# Create request_
cluster_name = ('projects/' + PROJECT_ID + '/zones/' + ZONE +
'/clusters/' + CLUSTER_ID)
request_pb = table_messages_pb2.ListTablesRequest(name=cluster_name)

# Create response_pb
table_name = table_name or (cluster_name + '/tables/' + table_id)
response_pb = table_messages_pb2.ListTablesResponse(
tables=[
table_data_pb2.Table(name=table_name),
],
)

# Create expected_result.
expected_result = [] # We'll add one below.

# We must create the cluster with the client passed in.
TEST_CASE = self
CLUSTER_CREATED = []

def result_method(client):
cluster = TEST_CASE._makeOne(ZONE, CLUSTER_ID, client)
CLUSTER_CREATED.append(cluster)
expected_result.append(cluster.table(table_id))
return cluster.list_tables()

self._grpc_client_test_helper('ListTables', result_method,
request_pb, response_pb, expected_result,
PROJECT_ID,
stub_factory=MUT.TABLE_STUB_FACTORY,
stub_host=MUT.TABLE_ADMIN_HOST)
self.assertEqual(len(CLUSTER_CREATED), 1)

def test_list_tables(self):
table_id = 'table_id'
self._list_tables_helper(table_id)

def test_list_tables_failure_bad_split(self):
with self.assertRaises(ValueError):
self._list_tables_helper(None, table_name='wrong-format')

def test_list_tables_failure_name_bad_before(self):
table_id = 'table_id'
bad_table_name = ('nonempty-section-before' +
'projects/' + PROJECT_ID + '/zones/' + ZONE +
'/clusters/' + CLUSTER_ID + '/tables/' + table_id)
with self.assertRaises(ValueError):
self._list_tables_helper(table_id, table_name=bad_table_name)
25 changes: 25 additions & 0 deletions gcloud_bigtable/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ def test_name_property(self):
expected_name = cluster_name + '/tables/' + TABLE_ID
self.assertEqual(table.name, expected_name)

def test___eq__(self):
table_id = 'table_id'
cluster = object()
table1 = self._makeOne(table_id, cluster)
table2 = self._makeOne(table_id, cluster)
self.assertEqual(table1, table2)

def test___eq__type_differ(self):
table1 = self._makeOne('table_id', None)
table2 = object()
self.assertNotEqual(table1, table2)

def test___ne__same_value(self):
table_id = 'table_id'
cluster = object()
table1 = self._makeOne(table_id, cluster)
table2 = self._makeOne(table_id, cluster)
comparison_val = (table1 != table2)
self.assertFalse(comparison_val)

def test___ne__(self):
table1 = self._makeOne('table_id1', 'cluster1')
table2 = self._makeOne('table_id2', 'cluster2')
self.assertNotEqual(table1, table2)

def test_exists(self):
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
Expand Down

0 comments on commit 8bfcf4e

Please sign in to comment.