Skip to content

Commit f313ca9

Browse files
committed
Implementing Bigtable Table.rename().
1 parent d6820b9 commit f313ca9

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

gcloud/bigtable/table.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,36 @@ def create(self, initial_split_keys=None):
139139
# We expect a `._generated.bigtable_table_data_pb2.Table`
140140
client._table_stub.CreateTable(request_pb, client.timeout_seconds)
141141

142+
def rename(self, new_table_id):
143+
"""Rename this table.
144+
145+
.. note::
146+
147+
This cannot be used to move tables between clusters,
148+
zones, or projects.
149+
150+
.. note::
151+
152+
The Bigtable Table Admin API currently returns
153+
154+
``BigtableTableService.RenameTable is not yet implemented``
155+
156+
when this method is used. It's unclear when this method will
157+
actually be supported by the API.
158+
159+
:type new_table_id: str
160+
:param new_table_id: The new name table ID.
161+
"""
162+
request_pb = messages_pb2.RenameTableRequest(
163+
name=self.name,
164+
new_id=new_table_id,
165+
)
166+
client = self._cluster._client
167+
# We expect a `._generated.empty_pb2.Empty`
168+
client._table_stub.RenameTable(request_pb, client.timeout_seconds)
169+
170+
self.table_id = new_table_id
171+
142172
def delete(self):
143173
"""Delete this table."""
144174
request_pb = messages_pb2.DeleteTableRequest(name=self.name)

gcloud/bigtable/test_table.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,51 @@ def test_create_with_split_keys(self):
143143
initial_split_keys = ['s1', 's2']
144144
self._create_test_helper(initial_split_keys)
145145

146+
def test_rename(self):
147+
from gcloud.bigtable._generated import (
148+
bigtable_table_service_messages_pb2 as messages_pb2)
149+
from gcloud.bigtable._generated import empty_pb2
150+
from gcloud.bigtable._testing import _FakeStub
151+
152+
project_id = 'project-id'
153+
zone = 'zone'
154+
cluster_id = 'cluster-id'
155+
table_id = 'table-id'
156+
new_table_id = 'new_table_id'
157+
timeout_seconds = 97
158+
self.assertNotEqual(new_table_id, table_id)
159+
160+
client = _Client(timeout_seconds=timeout_seconds)
161+
cluster_name = ('projects/' + project_id + '/zones/' + zone +
162+
'/clusters/' + cluster_id)
163+
cluster = _Cluster(cluster_name, client=client)
164+
table = self._makeOne(table_id, cluster)
165+
166+
# Create request_pb
167+
table_name = cluster_name + '/tables/' + table_id
168+
request_pb = messages_pb2.RenameTableRequest(
169+
name=table_name,
170+
new_id=new_table_id,
171+
)
172+
173+
# Create response_pb
174+
response_pb = empty_pb2.Empty()
175+
176+
# Patch the stub used by the API method.
177+
client._table_stub = stub = _FakeStub(response_pb)
178+
179+
# Create expected_result.
180+
expected_result = None # rename() has no return value.
181+
182+
# Perform the method and check the result.
183+
result = table.rename(new_table_id)
184+
self.assertEqual(result, expected_result)
185+
self.assertEqual(stub.method_calls, [(
186+
'RenameTable',
187+
(request_pb, timeout_seconds),
188+
{},
189+
)])
190+
146191
def test_delete(self):
147192
from gcloud.bigtable._generated import (
148193
bigtable_table_service_messages_pb2 as messages_pb2)

0 commit comments

Comments
 (0)