Skip to content

Commit

Permalink
Replace credentials with client in make_stub and MetadataTransformer.
Browse files Browse the repository at this point in the history
This is so that we can pass through other configuration directly
via the client.
  • Loading branch information
dhermes committed Jul 28, 2015
1 parent c20f76e commit ac8ac24
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 38 deletions.
2 changes: 1 addition & 1 deletion gcloud_bigtable/_grpc_mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def _grpc_client_test_helper(self, method_name, result_method, request_pb,
('create_scoped', (self._STUB_SCOPES,), {}),
])
factory_args = (
scoped_creds,
client,
stub_factory or getattr(self._MUT, self._STUB_FACTORY_NAME),
stub_host or self._STUB_HOST,
self._STUB_PORT,
Expand Down
20 changes: 10 additions & 10 deletions gcloud_bigtable/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
class MetadataTransformer(object):
"""Callable class to transform metadata for gRPC requests.
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for access tokens
to authorize requests.
:type client: :class:`.client.Client`
:param client: The client that owns the cluster. Provides authorization and
user agent.
"""

def __init__(self, credentials):
self._credentials = credentials
def __init__(self, client):
self._credentials = client.credentials

def __call__(self, ignored_val):
"""Adds authorization header to request metadata."""
Expand Down Expand Up @@ -207,12 +207,12 @@ def get_certs():
return AuthInfo.ROOT_CERTIFICATES


def make_stub(credentials, stub_factory, host, port):
def make_stub(client, stub_factory, host, port):
"""Makes a stub for the an API.
:type credentials: :class:`oauth2client.client.OAuth2Credentials`
:param credentials: The OAuth2 Credentials to use for access tokens
to authorize requests.
:type client: :class:`.client.Client`
:param client: The client that owns the cluster. Provides authorization and
user agent.
:type stub_factory: callable
:param stub_factory: A factory which will create a gRPC stub for
Expand All @@ -228,7 +228,7 @@ def make_stub(credentials, stub_factory, host, port):
:returns: The stub object used to make gRPC requests to the
Data API.
"""
custom_metadata_transformer = MetadataTransformer(credentials)
custom_metadata_transformer = MetadataTransformer(client)
return stub_factory(host, port,
metadata_transformer=custom_metadata_transformer,
secure=True,
Expand Down
4 changes: 2 additions & 2 deletions gcloud_bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ def list_zones(self, timeout_seconds=None):
``OK`` state.
"""
request_pb = messages_pb2.ListZonesRequest(name=self.project_name)
stub = make_stub(self._credentials, CLUSTER_STUB_FACTORY,
stub = make_stub(self, CLUSTER_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down Expand Up @@ -384,7 +384,7 @@ def list_clusters(self, timeout_seconds=None):
zones in the request).
"""
request_pb = messages_pb2.ListClustersRequest(name=self.project_name)
stub = make_stub(self._credentials, CLUSTER_STUB_FACTORY,
stub = make_stub(self, CLUSTER_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down
14 changes: 7 additions & 7 deletions gcloud_bigtable/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ def reload(self, timeout_seconds=None):
cluster.
"""
request_pb = messages_pb2.GetClusterRequest(name=self.name)
stub = make_stub(self.credentials, CLUSTER_STUB_FACTORY,
stub = make_stub(self.client, CLUSTER_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down Expand Up @@ -302,7 +302,7 @@ def operation_finished(self, timeout_seconds=None):
operation_name = ('operations/' + self.name +
'/operations/%d' % (self._operation_id,))
request_pb = operations_pb2.GetOperationRequest(name=operation_name)
stub = make_stub(self.credentials, OPERATIONS_STUB_FACTORY,
stub = make_stub(self.client, OPERATIONS_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down Expand Up @@ -341,7 +341,7 @@ def create(self, timeout_seconds=None):
cluster.
"""
request_pb = _prepare_create_request(self)
stub = make_stub(self.credentials, CLUSTER_STUB_FACTORY,
stub = make_stub(self.client, CLUSTER_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down Expand Up @@ -378,7 +378,7 @@ def update(self, timeout_seconds=None):
display_name=self.display_name,
serve_nodes=self.serve_nodes,
)
stub = make_stub(self.credentials, CLUSTER_STUB_FACTORY,
stub = make_stub(self.client, CLUSTER_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand All @@ -399,7 +399,7 @@ def delete(self, timeout_seconds=None):
cluster.
"""
request_pb = messages_pb2.DeleteClusterRequest(name=self.name)
stub = make_stub(self.credentials, CLUSTER_STUB_FACTORY,
stub = make_stub(self.client, CLUSTER_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand All @@ -416,7 +416,7 @@ def undelete(self, timeout_seconds=None):
cluster.
"""
request_pb = messages_pb2.UndeleteClusterRequest(name=self.name)
stub = make_stub(self.credentials, CLUSTER_STUB_FACTORY,
stub = make_stub(self.client, CLUSTER_STUB_FACTORY,
CLUSTER_ADMIN_HOST, CLUSTER_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand All @@ -442,7 +442,7 @@ def list_tables(self, timeout_seconds=None):
that is not of the expected format.
"""
request_pb = table_messages_pb2.ListTablesRequest(name=self.name)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down
14 changes: 7 additions & 7 deletions gcloud_bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def exists(self, timeout_seconds=None):
exist, an exception will be thrown by the API call.
"""
request_pb = messages_pb2.GetTableRequest(name=self.name)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.cluster.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down Expand Up @@ -272,7 +272,7 @@ def create(self, initial_split_keys=None, timeout_seconds=None):
name=self.cluster.name,
table_id=self.table_id,
)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.cluster.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand All @@ -299,7 +299,7 @@ def rename(self, new_table_id, timeout_seconds=None):
name=self.name,
new_id=new_table_id,
)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.cluster.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand All @@ -315,7 +315,7 @@ def delete(self, timeout_seconds=None):
If not passed, defaults to value set on table.
"""
request_pb = messages_pb2.DeleteTableRequest(name=self.name)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.cluster.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down Expand Up @@ -349,7 +349,7 @@ def create_column_family(self, column_family_id, gc_rule=None,
column_family=column_family,
)

stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.cluster.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down Expand Up @@ -380,7 +380,7 @@ def update_column_family(self, column_family_id, gc_rule=None,
request_kwargs['gc_rule'] = gc_rule.to_pb()
request_pb = data_pb2.ColumnFamily(**request_kwargs)

stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.cluster.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand All @@ -402,7 +402,7 @@ def delete_column_family(self, column_family_id, timeout_seconds=None):
column_family_name = self.name + '/columnFamilies/' + column_family_id
request_pb = messages_pb2.DeleteColumnFamilyRequest(
name=column_family_name)
stub = make_stub(self.credentials, TABLE_STUB_FACTORY,
stub = make_stub(self.cluster.client, TABLE_STUB_FACTORY,
TABLE_ADMIN_HOST, TABLE_ADMIN_PORT)
with stub:
timeout_seconds = timeout_seconds or self.timeout_seconds
Expand Down
39 changes: 28 additions & 11 deletions gcloud_bigtable/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,47 @@ def _makeOne(self, *args, **kwargs):

def test_constructor(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
credentials = _MockWithAttachedMethods()
transformer = self._makeOne(credentials)
self.assertTrue(transformer._credentials is credentials)
self.assertEqual(credentials._called, [])
from gcloud_bigtable.client import Client
from gcloud_bigtable.client import DATA_SCOPE

scoped_creds = object()
credentials = _MockWithAttachedMethods(scoped_creds)
project_id = 'PROJECT_ID'
client = Client(credentials, project_id=project_id)
transformer = self._makeOne(client)
self.assertTrue(transformer._credentials is scoped_creds)
self.assertEqual(credentials._called, [
('create_scoped', ([DATA_SCOPE],), {}),
])

def test___call__(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable._helpers import USER_AGENT
from gcloud_bigtable.client import Client
from gcloud_bigtable.client import DATA_SCOPE

access_token_expected = 'FOOBARBAZ'

class _ReturnVal(object):
access_token = access_token_expected

credentials = _MockWithAttachedMethods(_ReturnVal)
transformer = self._makeOne(credentials)
scoped_creds = _MockWithAttachedMethods(_ReturnVal)
credentials = _MockWithAttachedMethods(scoped_creds)
project_id = 'PROJECT_ID'
client = Client(credentials, project_id=project_id)

transformer = self._makeOne(client)
result = transformer(None)
self.assertEqual(
result,
[
('Authorization', 'Bearer ' + access_token_expected),
('User-agent', USER_AGENT),
])
self.assertEqual(credentials._called, [('get_access_token', (), {})])
self.assertEqual(credentials._called, [
('create_scoped', ([DATA_SCOPE],), {}),
])
self.assertEqual(scoped_creds._called, [('get_access_token', (), {})])


class Test__pb_timestamp_to_datetime(unittest2.TestCase):
Expand Down Expand Up @@ -334,10 +351,10 @@ def test_it(self):
host = 'HOST'
port = 1025
certs = 'FOOBAR'
credentials = _MockWithAttachedMethods()
client = _MockWithAttachedMethods()
with _Monkey(MUT, get_certs=lambda: certs,
MetadataTransformer=transformer):
result = self._callFUT(credentials, custom_factory, host, port)
result = self._callFUT(client, custom_factory, host, port)

self.assertTrue(result is mock_result)
custom_factory.check_called(
Expand All @@ -349,5 +366,5 @@ def test_it(self):
'root_certificates': certs,
}],
)
transformer.check_called(self, [(credentials,)])
self.assertEqual(credentials._called, [])
transformer.check_called(self, [(client,)])
self.assertEqual(client._called, [])
10 changes: 10 additions & 0 deletions gcloud_bigtable/test_cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,16 @@ def test_project_id_getter(self):
cluster = self._makeOne(ZONE, CLUSTER_ID, client)
self.assertEqual(cluster.project_id, PROJECT_ID)

def test_credentials_getter(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable.client import Client

scoped_creds = object()
credentials = _MockWithAttachedMethods(scoped_creds)
client = Client(credentials, project_id=PROJECT_ID)
cluster = self._makeOne(ZONE, CLUSTER_ID, client)
self.assertTrue(cluster.credentials is scoped_creds)

def test_timeout_seconds_getter(self):
from gcloud_bigtable._testing import _MockWithAttachedMethods
from gcloud_bigtable.client import Client
Expand Down

0 comments on commit ac8ac24

Please sign in to comment.