Skip to content

Commit 860059b

Browse files
committed
Merge pull request #1500 from dhermes/happybase-connection-tables
Adding HappyBase Connection.tables().
2 parents 7f4575e + 38fbf2f commit 860059b

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

gcloud/bigtable/happybase/connection.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,32 @@ def table(self, name, use_prefix=True):
234234
if use_prefix:
235235
name = self._table_name(name)
236236
return Table(name, self)
237+
238+
def tables(self):
239+
"""Return a list of table names available to this connection.
240+
241+
.. note::
242+
243+
This lists every table in the cluster owned by this connection,
244+
**not** every table that a given user may have access to.
245+
246+
.. note::
247+
248+
If ``table_prefix`` is set on this connection, only returns the
249+
table names which match that prefix.
250+
251+
:rtype: list
252+
:returns: List of string table names.
253+
"""
254+
low_level_table_instances = self._cluster.list_tables()
255+
table_names = [table_instance.table_id
256+
for table_instance in low_level_table_instances]
257+
258+
# Filter using prefix, and strip prefix from names
259+
if self.table_prefix is not None:
260+
prefix = self._table_name('')
261+
offset = len(prefix)
262+
table_names = [name[offset:] for name in table_names
263+
if name.startswith(prefix)]
264+
265+
return table_names

gcloud/bigtable/happybase/test_connection.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,39 @@ def test_table_factory_with_prefix(self):
278278
def test_table_factory_with_ignored_prefix(self):
279279
self._table_factory_prefix_helper(use_prefix=False)
280280

281+
def test_tables(self):
282+
from gcloud.bigtable.table import Table
283+
284+
table_name1 = 'table-name1'
285+
table_name2 = 'table-name2'
286+
cluster = _Cluster(list_tables_result=[
287+
Table(table_name1, None),
288+
Table(table_name2, None),
289+
])
290+
connection = self._makeOne(autoconnect=False, cluster=cluster)
291+
result = connection.tables()
292+
self.assertEqual(result, [table_name1, table_name2])
293+
294+
def test_tables_with_prefix(self):
295+
from gcloud.bigtable.table import Table
296+
297+
table_prefix = 'prefix'
298+
table_prefix_separator = '<>'
299+
unprefixed_table_name1 = 'table-name1'
300+
301+
table_name1 = (table_prefix + table_prefix_separator +
302+
unprefixed_table_name1)
303+
table_name2 = 'table-name2'
304+
cluster = _Cluster(list_tables_result=[
305+
Table(table_name1, None),
306+
Table(table_name2, None),
307+
])
308+
connection = self._makeOne(
309+
autoconnect=False, cluster=cluster, table_prefix=table_prefix,
310+
table_prefix_separator=table_prefix_separator)
311+
result = connection.tables()
312+
self.assertEqual(result, [unprefixed_table_name1])
313+
281314

282315
class _Client(object):
283316

@@ -316,3 +349,6 @@ def copy(self):
316349
return result
317350
else:
318351
return self
352+
353+
def list_tables(self):
354+
return self.list_tables_result

0 commit comments

Comments
 (0)