Skip to content

Commit

Permalink
Adding basic Table class.
Browse files Browse the repository at this point in the history
Also adding a table() factory in Cluster.
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent 79b0749 commit 98a878f
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gcloud_bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ def cluster(self, zone, cluster_id, display_name=None, serve_nodes=3):
Defaults to 3.
:rtype: :class:`Cluster`
:returns: The cluster created owned by this client.
:returns: The cluster owned by this client.
"""
return Cluster(zone, cluster_id, self,
display_name=display_name, serve_nodes=serve_nodes)
Expand Down
16 changes: 14 additions & 2 deletions gcloud_bigtable/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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


_CLUSTER_NAME_RE = re.compile(r'^projects/(?P<project_id>[^/]+)/'
Expand Down Expand Up @@ -184,8 +185,8 @@ def from_pb(cls, cluster_pb, client):
def client(self):
"""Getter for cluster's client.
:rtype: string
:returns: The project ID stored on the client.
:rtype: :class:`.client.Client`
:returns: The client stored on the cluster.
"""
return self._client

Expand Down Expand Up @@ -215,6 +216,17 @@ def name(self):
return (self.client.project_name + '/zones/' + self.zone +
'/clusters/' + self.cluster_id)

def table(self, table_id):
"""Factory to create a table associated with this cluster.
:type table_id: string
:param table_id: The ID of the table.
:rtype: :class:`Table`
:returns: The table owned by this cluster.
"""
return Table(table_id, self)

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
Expand Down
55 changes: 55 additions & 0 deletions gcloud_bigtable/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""User friendly container for Google Cloud Bigtable Table."""


class Table(object):
"""Representation of a Google Cloud Bigtable Table.
:type table_id: string
:param table_id: The ID of the table.
:type cluster: :class:`.cluster.Cluster`
:param cluster: The cluster that owns the table.
"""

def __init__(self, table_id, cluster):
self.table_id = table_id
self._cluster = cluster

@property
def cluster(self):
"""Getter for table's cluster.
:rtype: :class:`.cluster.Cluster`
:returns: The cluster stored on the table.
"""
return self._cluster

@property
def name(self):
"""Table name used in requests.
.. note::
This property will not change if ``table_id`` does not, but the
return value is not cached.
The table name is of the form
"projects/*/zones/*/clusters/*/tables/{table_id}"
:rtype: string
:returns: The table name.
"""
return self.cluster.name + '/tables/' + self.table_id
10 changes: 10 additions & 0 deletions gcloud_bigtable/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ def test_name_property(self):
'/clusters/' + CLUSTER_ID)
self.assertEqual(cluster.name, cluster_name)

def test_table_factory(self):
from gcloud_bigtable.table import Table

cluster = self._makeOne(ZONE, CLUSTER_ID, None)
table_id = 'table_id'
table = cluster.table(table_id)
self.assertTrue(isinstance(table, Table))
self.assertEqual(table.table_id, table_id)
self.assertEqual(table._cluster, cluster)

def test_from_pb_success(self):
from gcloud_bigtable._generated import (
bigtable_cluster_data_pb2 as data_pb2)
Expand Down
53 changes: 53 additions & 0 deletions gcloud_bigtable/test_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright 2015 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import unittest2


TABLE_ID = 'table-id'


class TestTable(unittest2.TestCase):

def _getTargetClass(self):
from gcloud_bigtable.table import Table
return Table

def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)

def test_constructor(self):
cluster = object()
table = self._makeOne(TABLE_ID, cluster)
self.assertEqual(table.table_id, TABLE_ID)
self.assertTrue(table._cluster is cluster)

def test_cluster_getter(self):
cluster = object()
table = self._makeOne(TABLE_ID, cluster)
self.assertTrue(table.cluster is cluster)

def test_name_property(self):
cluster_name = 'cluster_name'
cluster = _Cluster(cluster_name)
table = self._makeOne(TABLE_ID, cluster)
expected_name = cluster_name + '/tables/' + TABLE_ID
self.assertEqual(table.name, expected_name)


class _Cluster(object):

def __init__(self, name):
self.name = name

0 comments on commit 98a878f

Please sign in to comment.