Skip to content

Commit

Permalink
Implementing Bigtable Table.rename().
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Dec 11, 2015
1 parent d6820b9 commit f313ca9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
30 changes: 30 additions & 0 deletions gcloud/bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,36 @@ def create(self, initial_split_keys=None):
# We expect a `._generated.bigtable_table_data_pb2.Table`
client._table_stub.CreateTable(request_pb, client.timeout_seconds)

def rename(self, new_table_id):
"""Rename this table.
.. note::
This cannot be used to move tables between clusters,
zones, or projects.
.. note::
The Bigtable Table Admin API currently returns
``BigtableTableService.RenameTable is not yet implemented``
when this method is used. It's unclear when this method will
actually be supported by the API.
:type new_table_id: str
:param new_table_id: The new name table ID.
"""
request_pb = messages_pb2.RenameTableRequest(
name=self.name,
new_id=new_table_id,
)
client = self._cluster._client
# We expect a `._generated.empty_pb2.Empty`
client._table_stub.RenameTable(request_pb, client.timeout_seconds)

self.table_id = new_table_id

def delete(self):
"""Delete this table."""
request_pb = messages_pb2.DeleteTableRequest(name=self.name)
Expand Down
45 changes: 45 additions & 0 deletions gcloud/bigtable/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,51 @@ def test_create_with_split_keys(self):
initial_split_keys = ['s1', 's2']
self._create_test_helper(initial_split_keys)

def test_rename(self):
from gcloud.bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
from gcloud.bigtable._generated import empty_pb2
from gcloud.bigtable._testing import _FakeStub

project_id = 'project-id'
zone = 'zone'
cluster_id = 'cluster-id'
table_id = 'table-id'
new_table_id = 'new_table_id'
timeout_seconds = 97
self.assertNotEqual(new_table_id, table_id)

client = _Client(timeout_seconds=timeout_seconds)
cluster_name = ('projects/' + project_id + '/zones/' + zone +
'/clusters/' + cluster_id)
cluster = _Cluster(cluster_name, client=client)
table = self._makeOne(table_id, cluster)

# Create request_pb
table_name = cluster_name + '/tables/' + table_id
request_pb = messages_pb2.RenameTableRequest(
name=table_name,
new_id=new_table_id,
)

# Create response_pb
response_pb = empty_pb2.Empty()

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

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

# Perform the method and check the result.
result = table.rename(new_table_id)
self.assertEqual(result, expected_result)
self.assertEqual(stub.method_calls, [(
'RenameTable',
(request_pb, timeout_seconds),
{},
)])

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

0 comments on commit f313ca9

Please sign in to comment.