Skip to content

Commit a3ff6c7

Browse files
committed
Updating the Resource Manager Overview.
Also making the Connection constructor fail on a service account credential and disabling the Client factories for service accounts.
1 parent 95afe66 commit a3ff6c7

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

docs/resource-manager-api.rst

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,77 @@ With this API, you can do the following:
1010
- Update existing projects
1111
- Delete projects
1212
- Undelete, or recover, projects that you don't want to delete
13+
14+
.. note::
15+
16+
Don't forget to look at the :ref:`Authentication` section below.
17+
It's slightly different from the rest of this library.
18+
19+
Here's a quick example of the full life-cycle:
20+
21+
.. code-block:: python
22+
23+
>>> from gcloud import resource_manager
24+
25+
>>> # List all projects you have access to
26+
>>> client = resource_manager.Client()
27+
>>> for project in client.list_projects():
28+
... print(project)
29+
30+
>>> # Create a new project
31+
>>> new_project = client.project('your-project-id-here',
32+
... name='My new project)
33+
>>> new_project.create()
34+
35+
>>> # Update an existing project
36+
>>> project = client.get_project('my-existing-project')
37+
>>> print(project)
38+
<Project: Existing Project (my-existing-project)>
39+
>>> project.name = 'Modified name'
40+
>>> project.update()
41+
>>> print(project)
42+
<Project: Modified name (my-existing-project)>
43+
44+
>>> # Delete a project
45+
>>> project = client.get_project('my-existing-project')
46+
>>> project.delete()
47+
48+
>>> # Undelete a project
49+
>>> project = client.get_project('my-existing-project')
50+
>>> project.undelete()
51+
52+
.. _Authentication:
53+
54+
Authentication
55+
~~~~~~~~~~~~~~
56+
57+
Unlike the other APIs, the Resource Manager API is focused on managing your
58+
various projects inside Google Cloud Platform. What this means (currently, as
59+
of August 2015) is that you can't use a Service Account to work with some
60+
parts of this API (for example, creating projects).
61+
62+
The reason is actually pretty simple: if your API call is trying to do
63+
something like create a project, what project's Service Account can you use?
64+
Currently none.
65+
66+
This means that for this API you should always use the credentials
67+
provided by the `Google Cloud SDK`_, which you can get by running
68+
``gcloud auth login``.
69+
70+
.. _Google Cloud SDK: http://cloud.google.com/sdk
71+
72+
Once you run that command, ``gcloud-python`` will automatically pick up the
73+
credentials, and you can use the "automatic discovery" feature of the library.
74+
75+
Start by authenticating:
76+
77+
.. code-block:: bash
78+
79+
$ gcloud auth login
80+
81+
And then simply create a client:
82+
83+
.. code-block:: python
84+
85+
>>> from gcloud import resource_manager
86+
>>> client = resource_manager.Client()

gcloud/resource_manager/client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,36 @@ class Client(BaseClient):
4848

4949
_connection_class = Connection
5050

51+
@classmethod
52+
def from_service_account_json(cls, *args, **kwargs):
53+
"""Factory to retrieve JSON credentials while creating client.
54+
55+
The behavior from the parent class is disabled here since the Resource
56+
Manager API can only use credentials from a user account, not a
57+
service account.
58+
59+
:raises: :class:`NotImplementedError <exceptions.NotImplementedError>`
60+
always.
61+
"""
62+
raise NotImplementedError('A service account cannot be used with the '
63+
'Resource Manager API. Only user credentials '
64+
'cab be used.')
65+
66+
@classmethod
67+
def from_service_account_p12(cls, *args, **kwargs):
68+
"""Factory to retrieve P12 credentials while creating client.
69+
70+
The behavior from the parent class is disabled here since the Resource
71+
Manager API can only use credentials from a user account, not a
72+
service account.
73+
74+
:raises: :class:`NotImplementedError <exceptions.NotImplementedError>`
75+
always.
76+
"""
77+
raise NotImplementedError('A service account cannot be used with the '
78+
'Resource Manager API. Only user credentials '
79+
'cab be used.')
80+
5181
def project(self, project_id, name=None, labels=None):
5282
"""Creates a :class:`.Project` bound to the current client.
5383

gcloud/resource_manager/connection.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
"""Create / interact with gcloud.resource_manager connections."""
1616

17+
18+
from oauth2client.client import AssertionCredentials
19+
1720
from gcloud import connection as base_connection
1821

1922

@@ -34,5 +37,11 @@ class Connection(base_connection.JSONConnection):
3437
"""A template for the URL of a particular API call."""
3538

3639
def __init__(self, credentials=None, http=None):
40+
if isinstance(credentials, AssertionCredentials):
41+
message = ('credentials (%r) inherits from '
42+
'AppAssertionCredentials. Only user credentials can be '
43+
'used with the Resource Manager API. '% (credentials,))
44+
raise TypeError(message)
45+
3746
credentials = self._create_scoped_credentials(credentials, SCOPE)
3847
super(Connection, self).__init__(credentials=credentials, http=http)

gcloud/resource_manager/test_client.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ def test_constructor(self):
8888
self.assertEqual(client.connection._credentials, credentials)
8989
self.assertEqual(client.connection._http, http)
9090

91+
def test_from_service_account_json_factory(self):
92+
klass = self._getTargetClass()
93+
with self.assertRaises(NotImplementedError):
94+
klass.from_service_account_json()
95+
96+
def test_from_service_account_p12_factory(self):
97+
klass = self._getTargetClass()
98+
with self.assertRaises(NotImplementedError):
99+
klass.from_service_account_p12()
100+
91101
def test_project_factory(self):
92102
from gcloud.resource_manager.project import Project
93103

gcloud/resource_manager/test_connection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def _getTargetClass(self):
2424
def _makeOne(self, *args, **kw):
2525
return self._getTargetClass()(*args, **kw)
2626

27+
def test_constructor_with_assertion(self):
28+
from oauth2client.client import AssertionCredentials
29+
30+
credentials = AssertionCredentials(None)
31+
with self.assertRaises(TypeError):
32+
self._makeOne(credentials=credentials)
33+
2734
def test_build_api_url_no_extra_query_params(self):
2835
conn = self._makeOne()
2936
URI = '/'.join([

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy