Skip to content

Commit

Permalink
Implementing resource_manager.Project.undelete().
Browse files Browse the repository at this point in the history
Also switching default value of reload_data in Project.delete()
to False. This way, the default behavior doesn't incur an
unexpected HTTP request for the user.

This also had in mind that it's much more likely that a user
of Project.undelete() would be much interested in seeing the
new status after the UNDELETE than a typical user deleting
would be.
  • Loading branch information
dhermes committed Aug 25, 2015
1 parent dcb1981 commit f79c049
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 4 deletions.
35 changes: 33 additions & 2 deletions gcloud/resource_manager/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def update(self, client=None):
data=data)
self.set_properties_from_api_repr(resp)

def delete(self, client=None, reload_data=True):
def delete(self, client=None, reload_data=False):
"""API call: delete the project via a ``DELETE`` request.
See:
Expand All @@ -225,11 +225,42 @@ def delete(self, client=None, reload_data=True):
state. If you want to get the updated status,
you'll want this set to :data:`True` as the DELETE
method doesn't send back the updated project.
Default: :data:`True`.
Default: :data:`False`.
"""
client = self._require_client(client)
client.connection.api_request(method='DELETE', path=self.path)

# If the reload flag is set, reload the project.
if reload_data:
self.reload()

def undelete(self, client=None, reload_data=False):
"""API call: undelete the project via a ``POST`` request.
See
https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete
This actually changes the project status (``lifecycleState``) from
``DELETE_REQUESTED`` to ``ACTIVE``.
If the project has already reached a status of ``DELETE_IN_PROGRESS``,
this request will fail and the project cannot be restored.
:type client: :class:`gcloud.resource_manager.client.Client` or
:data:`NoneType <types.NoneType>`
:param client: the client to use. If not passed, falls back to
the client stored on the current project.
:type reload_data: bool
:param reload_data: Whether to reload the project with the latest
state. If you want to get the updated status,
you'll want this set to :data:`True` as the DELETE
method doesn't send back the updated project.
Default: :data:`False`.
"""
client = self._require_client(client)
client.connection.api_request(method='POST',
path=self.path + ':undelete')

# If the reload flag is set, reload the project.
if reload_data:
self.reload()
61 changes: 59 additions & 2 deletions gcloud/resource_manager/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,13 @@ def test_delete_with_reload_data(self):
'lifecycleState': 'ACTIVE',
}
DELETING_PROJECT = PROJECT_RESOURCE.copy()
DELETING_PROJECT['lifecycleState'] = 'DELETE_REQUESTED'
DELETING_PROJECT['lifecycleState'] = NEW_STATE = 'DELETE_REQUESTED'

connection = _Connection(PROJECT_RESOURCE, DELETING_PROJECT)
client = _Client(connection=connection)
project = self._makeOne(PROJECT_ID, client)
project.delete(reload_data=True)
self.assertEqual(project.status, 'DELETE_REQUESTED')
self.assertEqual(project.status, NEW_STATE)

delete_request, get_request = connection._requested
# NOTE: data is not in the request since a DELETE request.
Expand All @@ -258,6 +258,63 @@ def test_delete_with_reload_data(self):
}
self.assertEqual(get_request, expected_get_request)

def test_undelete_without_reload_data(self):
PROJECT_ID = 'project-id'
PROJECT_NUMBER = 123
PROJECT_RESOURCE = {
'projectId': PROJECT_ID,
'projectNumber': PROJECT_NUMBER,
'name': 'Project Name',
'labels': {'env': 'prod'},
'lifecycleState': 'DELETE_REQUESTED',
}
connection = _Connection(PROJECT_RESOURCE)
client = _Client(connection=connection)
project = self._makeOne(PROJECT_ID, client)
project.undelete(reload_data=False)

request, = connection._requested
# NOTE: data is not in the request, undelete doesn't need it.
expected_request = {
'method': 'POST',
'path': project.path + ':undelete',
}
self.assertEqual(request, expected_request)

def test_undelete_with_reload_data(self):
PROJECT_ID = 'project-id'
PROJECT_NUMBER = 123
PROJECT_RESOURCE = {
'projectId': PROJECT_ID,
'projectNumber': PROJECT_NUMBER,
'name': 'Project Name',
'labels': {'env': 'prod'},
'lifecycleState': 'DELETE_REQUESTED',
}
UNDELETED_PROJECT = PROJECT_RESOURCE.copy()
UNDELETED_PROJECT['lifecycleState'] = NEW_STATE = 'ACTIVE'

connection = _Connection(PROJECT_RESOURCE, UNDELETED_PROJECT)
client = _Client(connection=connection)
project = self._makeOne(PROJECT_ID, client)
project.undelete(reload_data=True)
self.assertEqual(project.status, NEW_STATE)

undelete_request, get_request = connection._requested
# NOTE: data is not in the request, undelete doesn't need it.
expected_undelete_request = {
'method': 'POST',
'path': project.path + ':undelete',
}
self.assertEqual(undelete_request, expected_undelete_request)

# NOTE: data is not in the request since a GET request.
expected_get_request = {
'method': 'GET',
'path': project.path,
}
self.assertEqual(get_request, expected_get_request)


class _Connection(object):

Expand Down

0 comments on commit f79c049

Please sign in to comment.