From b84d71c6bcca04a47407ea8491911b8584e395bb Mon Sep 17 00:00:00 2001 From: Naveed Massjouni Date: Thu, 29 Sep 2011 14:45:50 -0400 Subject: Updating image progress to be more granular. Before, the image progress had only 2 states, 0 and 100. Now it can be 0, 25, 50 or 100. Change-Id: Ic5de9d4404c8903bc2a6027697905f92374f8af7 --- nova/api/openstack/views/images.py | 22 +++++++++++++++------- nova/tests/api/openstack/test_images.py | 12 ++++++------ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/nova/api/openstack/views/images.py b/nova/api/openstack/views/images.py index 67364b115..8eaf0ca6a 100644 --- a/nova/api/openstack/views/images.py +++ b/nova/api/openstack/views/images.py @@ -37,6 +37,10 @@ class ViewBuilder(object): def _format_status(self, image): """Update the status field to standardize format.""" + + if 'status' not in image: + return + status_mapping = { 'active': 'ACTIVE', 'queued': 'SAVING', @@ -51,6 +55,14 @@ class ViewBuilder(object): except KeyError: image['status'] = 'UNKNOWN' + def _get_progress_for_status(self, status): + progress_map = { + 'queued': 25, + 'saving': 50, + 'active': 100, + } + return progress_map.get(status, 0) + def _build_server(self, image, image_obj): """Indicates that you must use a ViewBuilder subclass.""" raise NotImplementedError() @@ -72,8 +84,8 @@ class ViewBuilder(object): """Return a standardized image structure for display by the API.""" self._format_dates(image_obj) - if "status" in image_obj: - self._format_status(image_obj) + orig_status = image_obj.get('status', '').lower() + self._format_status(image_obj) image = { "id": image_obj.get("id"), @@ -89,11 +101,7 @@ class ViewBuilder(object): "updated": image_obj.get("updated_at"), "status": image_obj.get("status"), }) - - if image["status"].upper() == "ACTIVE": - image["progress"] = 100 - else: - image["progress"] = 0 + image["progress"] = self._get_progress_for_status(orig_status) return image diff --git a/nova/tests/api/openstack/test_images.py b/nova/tests/api/openstack/test_images.py index 05d398478..5e32ccf42 100644 --- a/nova/tests/api/openstack/test_images.py +++ b/nova/tests/api/openstack/test_images.py @@ -133,7 +133,7 @@ class ImagesTest(test.TestCase): "updated": NOW_API_FORMAT, "created": NOW_API_FORMAT, "status": "SAVING", - "progress": 0, + "progress": 25, "minDisk": 0, "minRam": 0, 'server': { @@ -587,7 +587,7 @@ class ImagesTest(test.TestCase): 'updated': NOW_API_FORMAT, 'created': NOW_API_FORMAT, 'status': 'SAVING', - 'progress': 0, + 'progress': 25, }, { 'id': 125, @@ -595,7 +595,7 @@ class ImagesTest(test.TestCase): 'updated': NOW_API_FORMAT, 'created': NOW_API_FORMAT, 'status': 'SAVING', - 'progress': 0, + 'progress': 50, }, { 'id': 126, @@ -685,7 +685,7 @@ class ImagesTest(test.TestCase): 'updated': NOW_API_FORMAT, 'created': NOW_API_FORMAT, 'status': 'SAVING', - 'progress': 0, + 'progress': 25, 'minDisk': 0, 'minRam': 0, 'server': { @@ -723,7 +723,7 @@ class ImagesTest(test.TestCase): 'updated': NOW_API_FORMAT, 'created': NOW_API_FORMAT, 'status': 'SAVING', - 'progress': 0, + 'progress': 50, 'minDisk': 0, 'minRam': 0, 'server': { @@ -978,7 +978,7 @@ class ImagesTest(test.TestCase): 'created': NOW_API_FORMAT, 'status': 'SAVING', 'minDisk': 0, - 'progress': 0, + 'progress': 25, 'minRam': 0, 'server': { 'id': '42', -- cgit