Skip to content

Commit

Permalink
Switch from oauth2client to google-auth (googleapis#2726)
Browse files Browse the repository at this point in the history
* Removes all use of oauth2client from every package and tests.
* Updates core to use google-auth's default credentials, project ID, and scoping logic.
* Updates bigtable to use google-auth's scoping logic.
  • Loading branch information
Jon Wayne Parrott authored Dec 2, 2016
1 parent 92e5119 commit 4608b25
Show file tree
Hide file tree
Showing 40 changed files with 351 additions and 1,398 deletions.
43 changes: 15 additions & 28 deletions bigquery/unit_tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def _make_one(self, *args, **kw):
def test_ctor(self):
from google.cloud.bigquery._http import Connection
PROJECT = 'PROJECT'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
self.assertIsInstance(client._connection, Connection)
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_list_projects_defaults(self):
'friendlyName': 'Two'},
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT_1, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -86,7 +86,7 @@ def test_list_projects_explicit_response_missing_projects_key(self):
PATH = 'projects'
TOKEN = 'TOKEN'
DATA = {}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -128,7 +128,7 @@ def test_list_datasets_defaults(self):
'friendlyName': 'Two'},
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -156,7 +156,7 @@ def test_list_datasets_explicit_response_missing_datasets_key(self):
PATH = 'projects/%s/datasets' % PROJECT
TOKEN = 'TOKEN'
DATA = {}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand All @@ -180,7 +180,7 @@ def test_dataset(self):
from google.cloud.bigquery.dataset import Dataset
PROJECT = 'PROJECT'
DATASET = 'dataset_name'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -190,7 +190,7 @@ def test_dataset(self):

def test_job_from_resource_unknown_type(self):
PROJECT = 'PROJECT'
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
with self.assertRaises(ValueError):
client.job_from_resource({'configuration': {'nonesuch': {}}})
Expand Down Expand Up @@ -304,7 +304,7 @@ def test_list_jobs_defaults(self):
LOAD_DATA,
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -360,7 +360,7 @@ def test_list_jobs_load_job_wo_sourceUris(self):
LOAD_DATA,
]
}
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -388,7 +388,7 @@ def test_list_jobs_explicit_missing(self):
PATH = 'projects/%s/jobs' % PROJECT
DATA = {}
TOKEN = 'TOKEN'
creds = _Credentials()
creds = object()
client = self._make_one(PROJECT, creds)
conn = client._connection = _Connection(DATA)

Expand Down Expand Up @@ -419,7 +419,7 @@ def test_load_table_from_storage(self):
DATASET = 'dataset_name'
DESTINATION = 'destination_table'
SOURCE_URI = 'http://example.com/source.csv'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -438,7 +438,7 @@ def test_copy_table(self):
DATASET = 'dataset_name'
SOURCE = 'source_table'
DESTINATION = 'destination_table'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -458,7 +458,7 @@ def test_extract_table_to_storage(self):
DATASET = 'dataset_name'
SOURCE = 'source_table'
DESTINATION = 'gs://bucket_name/object_name'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
dataset = client.dataset(DATASET)
Expand All @@ -475,7 +475,7 @@ def test_run_async_query_defaults(self):
PROJECT = 'PROJECT'
JOB = 'job_name'
QUERY = 'select count(*) from persons'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
job = client.run_async_query(JOB, QUERY)
Expand Down Expand Up @@ -511,7 +511,7 @@ def test_run_async_w_query_parameters(self):
PROJECT = 'PROJECT'
JOB = 'job_name'
QUERY = 'select count(*) from persons'
creds = _Credentials()
creds = object()
http = object()
client = self._make_one(project=PROJECT, credentials=creds, http=http)
query_parameters = [ScalarQueryParameter('foo', 'INT64', 123)]
Expand Down Expand Up @@ -575,19 +575,6 @@ def test_run_sync_query_w_query_parameters(self):
self.assertEqual(query.query_parameters, query_parameters)


class _Credentials(object):

_scopes = None

@staticmethod
def create_scoped_required():
return True

def create_scoped(self, scope):
self._scopes = scope
return self


class _Connection(object):

def __init__(self, *responses):
Expand Down
17 changes: 9 additions & 8 deletions bigtable/google/cloud/bigtable/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import os

import google.auth.credentials
from google.longrunning import operations_grpc

from google.cloud._helpers import make_insecure_stub
Expand Down Expand Up @@ -201,14 +202,16 @@ def __init__(self, project=None, credentials=None,
else:
scopes.append(DATA_SCOPE)

self._read_only = bool(read_only)

if admin:
scopes.append(ADMIN_SCOPE)

self._admin = bool(admin)
try:
credentials = credentials.create_scoped(scopes)
except AttributeError:
pass

credentials = google.auth.credentials.with_scopes_if_required(
credentials, scopes)

self._credentials = credentials
self.user_agent = user_agent
self.emulator_host = os.getenv(BIGTABLE_EMULATOR)
Expand All @@ -229,12 +232,10 @@ def copy(self):
:rtype: :class:`.Client`
:returns: A copy of the current client.
"""
credentials = self._credentials
copied_creds = credentials.create_scoped(credentials.scopes)
return self.__class__(
self.project,
copied_creds,
READ_ONLY_SCOPE in copied_creds.scopes,
self._credentials,
self._read_only,
self._admin,
self.user_agent,
)
Expand Down
1 change: 1 addition & 0 deletions bigtable/tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ localdeps =
pip install --quiet --upgrade {toxinidir}/../core
deps =
{toxinidir}/../core
mock
pytest
covercmd =
py.test --quiet \
Expand Down
Loading

0 comments on commit 4608b25

Please sign in to comment.