diff --git a/gcloud/bigtable/client.py b/gcloud/bigtable/client.py index 6d32554aa24e9..6ebe07698b068 100644 --- a/gcloud/bigtable/client.py +++ b/gcloud/bigtable/client.py @@ -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. @@ -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 ` 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 ` 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 ` 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 ` 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 diff --git a/gcloud/bigtable/test_client.py b/gcloud/bigtable/test_client.py index 8755048b06a89..2f1e1c3be1dec 100644 --- a/gcloud/bigtable/test_client.py +++ b/gcloud/bigtable/test_client.py @@ -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 @@ -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):