From cc633215cffba0081e14fc599c06751c4c1f6511 Mon Sep 17 00:00:00 2001 From: Danny Hermes Date: Wed, 26 Aug 2015 14:24:32 -0700 Subject: [PATCH] Renaming project -> new_project and implementing fetch_project. This is to make the distinction clear between a Resource Manager API request and object creation. --- docs/resource-manager-api.rst | 4 ++-- gcloud/resource_manager/client.py | 20 ++++++++++++++++- gcloud/resource_manager/project.py | 12 +++++----- gcloud/resource_manager/test_client.py | 31 ++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 11 deletions(-) diff --git a/docs/resource-manager-api.rst b/docs/resource-manager-api.rst index a283556babd9c..81de506925bd4 100644 --- a/docs/resource-manager-api.rst +++ b/docs/resource-manager-api.rst @@ -28,8 +28,8 @@ Here's a quick example of the full life-cycle: ... print(project) >>> # Create a new project - >>> new_project = client.project('your-project-id-here', - ... name='My new project) + >>> new_project = client.new_project('your-project-id-here', + ... name='My new project) >>> new_project.create() >>> # Update an existing project diff --git a/gcloud/resource_manager/client.py b/gcloud/resource_manager/client.py index b2b07f1b1dc33..2aa70aa9e7fb7 100644 --- a/gcloud/resource_manager/client.py +++ b/gcloud/resource_manager/client.py @@ -78,7 +78,7 @@ def from_service_account_p12(cls, *args, **kwargs): 'Resource Manager API. Only user ' 'credentials can be used.') - def project(self, project_id, name=None, labels=None): + def new_project(self, project_id, name=None, labels=None): """Creates a :class:`.Project` bound to the current client. Use :meth:`Project.reload() \ @@ -105,6 +105,24 @@ def project(self, project_id, name=None, labels=None): return Project(project_id=project_id, client=self, name=name, labels=labels) + def fetch_project(self, project_id): + """Get an existing project by ID. + + .. note:: + + If the project does not exist, this will raise a + :class:`NotFound ` error. + + :type project_id: str + :param project_id: The ID for this project. + + :rtype: :class:`.Project` + :returns: A :class:`.Project` with metadata fetched from the API. + """ + project = self.new_project(project_id) + project.reload() + return project + def list_projects(self, filter_params=None, page_size=None): """List the projects visible to this client. diff --git a/gcloud/resource_manager/project.py b/gcloud/resource_manager/project.py index f09d03e0c439f..79b9ca80de204 100644 --- a/gcloud/resource_manager/project.py +++ b/gcloud/resource_manager/project.py @@ -24,14 +24,14 @@ class Project(object): .. note:: A :class:`Project` can also be created via - :meth:`Client.project() \ - ` + :meth:`Client.new_project() \ + ` To manage labels on a :class:`Project`:: >>> from gcloud import resource_manager >>> client = resource_manager.Client() - >>> project = client.project('purple-spaceship-123') + >>> project = client.new_project('purple-spaceship-123') >>> project.labels = {'color': 'purple'} >>> project.labels['environment'] = 'production' >>> project.update() @@ -139,9 +139,9 @@ def reload(self, client=None): This method will reload the newest metadata for the project. If you've created a new :class:`Project` instance via - :meth:`Client.project() \ - `, this method will - retrieve project metadata. + :meth:`Client.new_project() \ + `, + this method will retrieve project metadata. .. warning:: diff --git a/gcloud/resource_manager/test_client.py b/gcloud/resource_manager/test_client.py index c2e8b8ab5441a..2ad2873bf33ea 100644 --- a/gcloud/resource_manager/test_client.py +++ b/gcloud/resource_manager/test_client.py @@ -98,7 +98,7 @@ def test_from_service_account_p12_factory(self): with self.assertRaises(NotImplementedError): klass.from_service_account_p12() - def test_project_factory(self): + def test_new_project_factory(self): from gcloud.resource_manager.project import Project credentials = _Credentials() @@ -106,7 +106,7 @@ def test_project_factory(self): project_id = 'project_id' name = object() labels = object() - project = client.project(project_id, name=name, labels=labels) + project = client.new_project(project_id, name=name, labels=labels) self.assertTrue(isinstance(project, Project)) self.assertEqual(project._client, client) @@ -114,6 +114,33 @@ def test_project_factory(self): self.assertEqual(project.name, name) self.assertEqual(project.labels, labels) + def test_fetch_project(self): + from gcloud.resource_manager.project import Project + + project_id = 'project-id' + project_number = 123 + project_name = 'Project Name' + labels = {'env': 'prod'} + project_resource = { + 'projectId': project_id, + 'projectNumber': project_number, + 'name': project_name, + 'labels': labels, + 'lifecycleState': 'ACTIVE', + } + + credentials = _Credentials() + client = self._makeOne(credentials=credentials) + # Patch the connection with one we can easily control. + client.connection = _Connection(project_resource) + + project = client.fetch_project(project_id) + self.assertTrue(isinstance(project, Project)) + self.assertEqual(project._client, client) + self.assertEqual(project.project_id, project_id) + self.assertEqual(project.name, project_name) + self.assertEqual(project.labels, labels) + def test_list_projects_return_type(self): from gcloud.resource_manager.client import _ProjectIterator