Skip to content

Commit

Permalink
Implementing Table.rename().
Browse files Browse the repository at this point in the history
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent 3d40fc0 commit b6f3c54
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 61 deletions.
29 changes: 28 additions & 1 deletion gcloud_bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Table(object):
* Check if it :meth:`exists`
* :meth:`create` itself
* :meth:`rename` itself
* :meth:`delete` itself
:type table_id: string
Expand Down Expand Up @@ -171,8 +172,34 @@ def create(self, initial_split_keys=None, timeout_seconds=TIMEOUT_SECONDS):
# We expect a `._generated.bigtable_table_data_pb2.Table`
response.result()

def rename(self, new_table_id, timeout_seconds=TIMEOUT_SECONDS):
"""Rename this table.
.. note::
This cannot be used to move tables between clusters,
zones, or projects.
:type new_table_id: string
:param new_table_id: The new name table ID.
:type timeout_seconds: integer
:param timeout_seconds: Number of seconds for request time-out.
If not passed, defaults to ``TIMEOUT_SECONDS``.
"""
request_pb = messages_pb2.RenameTableRequest(
name=self.name,
new_id=new_table_id,
)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
response = stub.RenameTable.async(request_pb, timeout_seconds)
# We expect a `._generated.empty_pb2.Empty`
response.result()

def delete(self, timeout_seconds=TIMEOUT_SECONDS):
"""Delete the metadata for this table.
"""Delete this table.
:type timeout_seconds: integer
:param timeout_seconds: Number of seconds for request time-out.
Expand Down
43 changes: 0 additions & 43 deletions gcloud_bigtable/table_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,49 +39,6 @@ class TableConnection(Connection):
SCOPE = 'https://www.googleapis.com/auth/cloud-bigtable.admin'
"""Scope for Table Admin and Cluster Admin API requests."""

def rename_table(self, cluster_name, old_table_id,
new_table_id, timeout_seconds=TIMEOUT_SECONDS):
"""Rename a table.
.. note::
This cannot be used to move tables between clusters,
zones, or projects.
:type cluster_name: string
:param cluster_name: The name of the cluster where the table will be
renamed. Must be of the form
"projects/../zones/../clusters/.."
Since this is a low-level class, we don't check
this, rather we expect callers to pass correctly
formatted data.
:type old_table_id: string
:param old_table_id: The name of the table being renamed.
:type new_table_id: string
:param new_table_id: The new name of the table.
:type timeout_seconds: integer
:param timeout_seconds: Number of seconds for request time-out.
If not passed, defaults to ``TIMEOUT_SECONDS``.
:rtype: :class:`gcloud_bigtable._generated.empty_pb2.Empty`
:returns: The empty response object for the rename table request.
"""
curr_table_name = '%s/tables/%s' % (cluster_name, old_table_id)
request_pb = messages_pb2.RenameTableRequest(
name=curr_table_name,
new_id=new_table_id,
)
result_pb = None
stub = make_stub(self._credentials, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, PORT)
with stub:
response = stub.RenameTable.async(request_pb, timeout_seconds)
result_pb = response.result()

return result_pb

def create_column_family(self, cluster_name, table_id, column_family_id,
timeout_seconds=TIMEOUT_SECONDS):
"""Create a column family in a table.
Expand Down
35 changes: 35 additions & 0 deletions gcloud_bigtable/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,41 @@ 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

new_table_id = 'new_table_id'
self.assertNotEqual(new_table_id, TABLE_ID)

# Create request_pb
table_name = ('projects/' + PROJECT_ID + '/zones/' + ZONE +
'/clusters/' + CLUSTER_ID + '/tables/' + TABLE_ID)
request_pb = messages_pb2.RenameTableRequest(
name=table_name,
new_id=new_table_id,
)

# Create response_pb
response_pb = empty_pb2.Empty()

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

# We must create the cluster with the client passed in
# and then the table with that cluster.
TEST_CASE = self

def result_method(client):
cluster = client.cluster(ZONE, CLUSTER_ID)
table = TEST_CASE._makeOne(TABLE_ID, cluster)
return table.rename(new_table_id)

self._grpc_client_test_helper('RenameTable', result_method,
request_pb, response_pb, expected_result,
PROJECT_ID)

def test_delete(self):
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)
Expand Down
17 changes: 0 additions & 17 deletions gcloud_bigtable/test_table_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,6 @@ def test_constructor(self):
('create_scoped', ((klass.SCOPE,),), {}),
])

def test_rename_table(self):
from gcloud_bigtable._generated import (
bigtable_table_service_messages_pb2 as messages_pb2)

table_name = '%s/tables/%s' % (CLUSTER_NAME, TABLE_ID)
new_table_id = 'NEW_TABLE_ID'
request_obj = messages_pb2.RenameTableRequest(
name=table_name,
new_id=new_table_id,
)

def call_method(connection):
return connection.rename_table(CLUSTER_NAME, TABLE_ID,
new_table_id)

self._grpc_call_helper(call_method, 'RenameTable', request_obj)

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

0 comments on commit b6f3c54

Please sign in to comment.