Skip to content

Commit

Permalink
Add stubs to Bigtable Client class.
Browse files Browse the repository at this point in the history
For now, just adding custom getters which will tell users
whether the Client has been started. Commit to add
methods to actually make the stubs and start the Client will
be forthcoming.
  • Loading branch information
dhermes committed Sep 28, 2015
1 parent f5086c7 commit 8217cb2
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
86 changes: 86 additions & 0 deletions gcloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ def __init__(self, project=None, credentials=None,
self.user_agent = user_agent
self.timeout_seconds = timeout_seconds

# These will be set in start().
self._data_stub = None
self._cluster_stub = None
self._operations_stub = None
self._table_stub = None

@property
def credentials(self):
"""Getter for client's credentials.
Expand All @@ -139,3 +145,83 @@ def credentials(self):
:returns: The credentials stored on the client.
"""
return self._credentials

@property
def project_name(self):
"""Project name to be used with Cluster Admin API.
.. note::
This property will not change if ``project`` does not, but the
return value is not cached.
The project name is of the form
``"projects/{project_id}"``
:rtype: str
:returns: The project name to be used with the Cloud Bigtable Admin
API RPC service.
"""
return 'projects/' + self.project

@property
def data_stub(self):
"""Getter for the gRPC stub used for the Data API.
:rtype: :class:`grpc.early_adopter.implementations._Stub`
:returns: A gRPC stub object.
:raises: :class:`ValueError <exceptions.ValueError>` if the current
client has not been :meth:`start`-ed.
"""
if self._data_stub is None:
raise ValueError('Client has not been started.')
return self._data_stub

@property
def cluster_stub(self):
"""Getter for the gRPC stub used for the Cluster Admin API.
:rtype: :class:`grpc.early_adopter.implementations._Stub`
:returns: A gRPC stub object.
:raises: :class:`ValueError <exceptions.ValueError>` if the current
client is not an admin client or if it has not been
:meth:`start`-ed.
"""
if not self._admin:
raise ValueError('Client is not an admin client.')
if self._cluster_stub is None:
raise ValueError('Client has not been started.')
return self._cluster_stub

@property
def operations_stub(self):
"""Getter for the gRPC stub used for the Operations API.
:rtype: :class:`grpc.early_adopter.implementations._Stub`
:returns: A gRPC stub object.
:raises: :class:`ValueError <exceptions.ValueError>` if the current
client is not an admin client or if it has not been
:meth:`start`-ed.
"""
if not self._admin:
raise ValueError('Client is not an admin client.')
if self._operations_stub is None:
raise ValueError('Client has not been started.')
return self._operations_stub

@property
def table_stub(self):
"""Getter for the gRPC stub used for the Table Admin API.
:rtype: :class:`grpc.early_adopter.implementations._Stub`
:returns: A gRPC stub object.
:raises: :class:`ValueError <exceptions.ValueError>` if the current
client is not an admin client or if it has not been
:meth:`start`-ed.
"""
if not self._admin:
raise ValueError('Client is not an admin client.')
if self._table_stub is None:
raise ValueError('Client has not been started.')
return self._table_stub
98 changes: 98 additions & 0 deletions gcloud/bigtable/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def _constructor_test_helper(self, expected_scopes, creds,
self.assertEqual(client.project, PROJECT)
self.assertEqual(client.timeout_seconds, timeout_seconds)
self.assertEqual(client.user_agent, user_agent)
# Check stubs are set (but null)
self.assertEqual(client._data_stub, None)
self.assertEqual(client._cluster_stub, None)
self.assertEqual(client._operations_stub, None)
self.assertEqual(client._table_stub, None)

def test_constructor_default_scopes(self):
from gcloud.bigtable import client as MUT
Expand Down Expand Up @@ -105,6 +110,99 @@ def test_credentials_getter(self):
client = self._makeOne(project=project, credentials=credentials)
self.assertTrue(client.credentials is credentials)

def test_project_name_property(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials)
project_name = 'projects/' + project
self.assertEqual(client.project_name, project_name)

def test_data_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials)
client._data_stub = object()
self.assertTrue(client.data_stub is client._data_stub)

def test_data_stub_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials)
with self.assertRaises(ValueError):
getattr(client, 'data_stub')

def test_cluster_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
client._cluster_stub = object()
self.assertTrue(client.cluster_stub is client._cluster_stub)

def test_cluster_stub_non_admin_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=False)
with self.assertRaises(ValueError):
getattr(client, 'cluster_stub')

def test_cluster_stub_unset_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
with self.assertRaises(ValueError):
getattr(client, 'cluster_stub')

def test_operations_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
client._operations_stub = object()
self.assertTrue(client.operations_stub is client._operations_stub)

def test_operations_stub_non_admin_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=False)
with self.assertRaises(ValueError):
getattr(client, 'operations_stub')

def test_operations_stub_unset_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
with self.assertRaises(ValueError):
getattr(client, 'operations_stub')

def test_table_stub_getter(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
client._table_stub = object()
self.assertTrue(client.table_stub is client._table_stub)

def test_table_stub_non_admin_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=False)
with self.assertRaises(ValueError):
getattr(client, 'table_stub')

def test_table_stub_unset_failure(self):
credentials = _Credentials()
project = 'PROJECT'
client = self._makeOne(project=project, credentials=credentials,
admin=True)
with self.assertRaises(ValueError):
getattr(client, 'table_stub')


class _Credentials(object):

Expand Down

0 comments on commit 8217cb2

Please sign in to comment.