| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent f44959a commit dfcc90b
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,4 +1,5 @@ | |||
| 1 | 1 | from pkg_resources import get_distribution | |
| 2 | + | ||
| 2 | 3 | import httplib2 | |
| 3 | 4 | ||
| 4 | 5 | ||
@@ -22,11 +23,10 @@ class Connection(object): | |||
| 22 | 23 | """The user agent for gcloud-python requests.""" | |
| 23 | 24 | ||
| 24 | 25 | def __init__(self, credentials=None): | |
| 25 | - """:type credentials: :class:`gcloud.credentials.Credentials` | ||
| 26 | + """ | ||
| 27 | + :type credentials: :class:`oauth2client.client.OAuth2Credentials` | ||
| 26 | 28 | :param credentials: The OAuth2 Credentials to use for this connection. | |
| 27 | - | ||
| 28 | 29 | """ | |
| 29 | - | ||
| 30 | 30 | self._credentials = credentials | |
| 31 | 31 | ||
| 32 | 32 | @property | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,39 +3,36 @@ | |||
| 3 | 3 | from oauth2client import client | |
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | - class Credentials(object): | ||
| 7 | - """An object used to simplify the OAuth2 credentials library. | ||
| 6 | + def get_for_service_account(client_email, private_key_path, scope=None): | ||
| 7 | + """Gets the credentials for a service account. | ||
| 8 | 8 | ||
| 9 | 9 | .. note:: | |
| 10 | - You should not need to use this class directly. | ||
| 10 | + You should not need to use this method directly. | ||
| 11 | 11 | Instead, use the helper methods provided in | |
| 12 | 12 | :func:`gcloud.datastore.__init__.get_connection` | |
| 13 | 13 | and | |
| 14 | 14 | :func:`gcloud.datastore.__init__.get_dataset` | |
| 15 | - which use this class under the hood. | ||
| 16 | - """ | ||
| 15 | + which use this method under the hood. | ||
| 16 | + | ||
| 17 | + :type client_email: string | ||
| 18 | + :param client_email: The e-mail attached to the service account. | ||
| 19 | + | ||
| 20 | + :type private_key_path: string | ||
| 21 | + :param private_key_path: The path to a private key file (this file was | ||
| 22 | + given to you when you created the service | ||
| 23 | + account). | ||
| 17 | 24 | ||
| 18 | - @classmethod | ||
| 19 | - def get_for_service_account(cls, client_email, private_key_path, | ||
| 20 | - scope=None): | ||
| 21 | - """Gets the credentials for a service account. | ||
| 22 | - | ||
| 23 | - :type client_email: string | ||
| 24 | - :param client_email: The e-mail attached to the service account. | ||
| 25 | - | ||
| 26 | - :type private_key_path: string | ||
| 27 | - :param private_key_path: The path to a private key file (this file was | ||
| 28 | - given to you when you created the service | ||
| 29 | - account). | ||
| 30 | - | ||
| 31 | - :type scope: string or tuple of strings | ||
| 32 | - :param scope: The scope against which to authenticate. | ||
| 33 | - (Different services require different scopes, | ||
| 34 | - check the documentation for which scope is required | ||
| 35 | - for the different levels of access | ||
| 36 | - to any particular API.) | ||
| 37 | - """ | ||
| 38 | - return client.SignedJwtAssertionCredentials( | ||
| 39 | - service_account_name=client_email, | ||
| 40 | - private_key=open(private_key_path).read(), | ||
| 41 | - scope=scope) | ||
| 25 | + :type scope: string or tuple of strings | ||
| 26 | + :param scope: The scope against which to authenticate. (Different services | ||
| 27 | + require different scopes, check the documentation for which | ||
| 28 | + scope is required for the different levels of access to any | ||
| 29 | + particular API.) | ||
| 30 | + | ||
| 31 | + :rtype: :class:`oauth2client.client.SignedJwtAssertionCredentials` | ||
| 32 | + :returns: A new SignedJwtAssertionCredentials instance with the | ||
| 33 | + needed service account settings. | ||
| 34 | + """ | ||
| 35 | + return client.SignedJwtAssertionCredentials( | ||
| 36 | + service_account_name=client_email, | ||
| 37 | + private_key=open(private_key_path).read(), | ||
| 38 | + scope=scope) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,12 +61,12 @@ def get_connection(client_email, private_key_path): | |||
| 61 | 61 | :rtype: :class:`gcloud.datastore.connection.Connection` | |
| 62 | 62 | :returns: A connection defined with the proper credentials. | |
| 63 | 63 | """ | |
| 64 | - from gcloud.credentials import Credentials | ||
| 64 | + from gcloud import credentials | ||
| 65 | 65 | from gcloud.datastore.connection import Connection | |
| 66 | 66 | ||
| 67 | - credentials = Credentials.get_for_service_account( | ||
| 67 | + svc_account_credentials = credentials.get_for_service_account( | ||
| 68 | 68 | client_email, private_key_path, scope=SCOPE) | |
| 69 | - return Connection(credentials=credentials) | ||
| 69 | + return Connection(credentials=svc_account_credentials) | ||
| 70 | 70 | ||
| 71 | 71 | ||
| 72 | 72 | def get_dataset(dataset_id, client_email, private_key_path): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,7 +10,7 @@ class Connection(connection.Connection): | |||
| 10 | 10 | This class should understand only the basic types (and protobufs) | |
| 11 | 11 | in method arguments, however should be capable of returning advanced types. | |
| 12 | 12 | ||
| 13 | - :type credentials: :class:`gcloud.credentials.Credentials` | ||
| 13 | + :type credentials: :class:`oauth2client.client.OAuth2Credentials` | ||
| 14 | 14 | :param credentials: The OAuth2 Credentials to use for this connection. | |
| 15 | 15 | """ | |
| 16 | 16 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,12 +62,12 @@ def get_connection(project, client_email, private_key_path): | |||
| 62 | 62 | :returns: A connection defined with the proper credentials. | |
| 63 | 63 | """ | |
| 64 | 64 | ||
| 65 | - from gcloud.credentials import Credentials | ||
| 65 | + from gcloud import credentials | ||
| 66 | 66 | from gcloud.storage.connection import Connection | |
| 67 | 67 | ||
| 68 | - credentials = Credentials.get_for_service_account( | ||
| 68 | + svc_account_credentials = credentials.get_for_service_account( | ||
| 69 | 69 | client_email, private_key_path, scope=SCOPE) | |
| 70 | - return Connection(project=project, credentials=credentials) | ||
| 70 | + return Connection(project=project, credentials=svc_account_credentials) | ||
| 71 | 71 | ||
| 72 | 72 | ||
| 73 | 73 | def get_bucket(bucket_name, project, client_email, private_key_path): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,22 +3,18 @@ | |||
| 3 | 3 | ||
| 4 | 4 | class TestCredentials(unittest2.TestCase): | |
| 5 | 5 | ||
| 6 | - def _getTargetClass(self): | ||
| 7 | - from gcloud.credentials import Credentials | ||
| 8 | - return Credentials | ||
| 9 | - | ||
| 10 | 6 | def test_get_for_service_account_wo_scope(self): | |
| 11 | 7 | from tempfile import NamedTemporaryFile | |
| 12 | 8 | from gcloud import credentials | |
| 13 | 9 | CLIENT_EMAIL = 'phred@example.com' | |
| 14 | 10 | PRIVATE_KEY = 'SEEkR1t' | |
| 15 | - cls = self._getTargetClass() | ||
| 16 | 11 | client = _Client() | |
| 17 | 12 | with _Monkey(credentials, client=client): | |
| 18 | 13 | with NamedTemporaryFile() as f: | |
| 19 | 14 | f.write(PRIVATE_KEY) | |
| 20 | 15 | f.flush() | |
| 21 | - found = cls.get_for_service_account(CLIENT_EMAIL, f.name) | ||
| 16 | + found = credentials.get_for_service_account( | ||
| 17 | + CLIENT_EMAIL, f.name) | ||
| 22 | 18 | self.assertTrue(found is client._signed) | |
| 23 | 19 | self.assertEqual(client._called_with, | |
| 24 | 20 | {'service_account_name': CLIENT_EMAIL, | |
@@ -32,14 +28,13 @@ def test_get_for_service_account_w_scope(self): | |||
| 32 | 28 | CLIENT_EMAIL = 'phred@example.com' | |
| 33 | 29 | PRIVATE_KEY = 'SEEkR1t' | |
| 34 | 30 | SCOPE = 'SCOPE' | |
| 35 | - cls = self._getTargetClass() | ||
| 36 | 31 | client = _Client() | |
| 37 | 32 | with _Monkey(credentials, client=client): | |
| 38 | 33 | with NamedTemporaryFile() as f: | |
| 39 | 34 | f.write(PRIVATE_KEY) | |
| 40 | 35 | f.flush() | |
| 41 | - found = cls.get_for_service_account(CLIENT_EMAIL, f.name, | ||
| 42 | - SCOPE) | ||
| 36 | + found = credentials.get_for_service_account( | ||
| 37 | + CLIENT_EMAIL, f.name, SCOPE) | ||
| 43 | 38 | self.assertTrue(found is client._signed) | |
| 44 | 39 | self.assertEqual(client._called_with, | |
| 45 | 40 | {'service_account_name': CLIENT_EMAIL, | |
| Back | FazBrowse Home | New Git URL |
0 commit comments