]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Add set-bootable command
authorHiroyuki Eguchi <h-eguchi@az.jp.nec.com>
Mon, 21 Apr 2014 07:31:16 +0000 (16:31 +0900)
committerHiroyuki Eguchi <h-eguchi@az.jp.nec.com>
Mon, 21 Apr 2014 07:32:16 +0000 (16:32 +0900)
Bootable Status is set to "True" automatically when user
create a volume from a image. But user have to set bootable
status manually when creating a bootable volume manually.

blueprint add-bootable-option

this commit is related to https://review.openstack.org/#/c/83691/

Change-Id: Ib6cdca15950fe86b4fb2a43cfc7338e28260e453

cinder/api/contrib/volume_actions.py
cinder/tests/api/contrib/test_volume_actions.py

index 22cb24eea18da979eba08218ba118ee0fd0473a8..0f8a8b8600ef8dd27dea7480dbc5f01f7bf618aa 100644 (file)
@@ -332,6 +332,38 @@ class VolumeActionsController(wsgi.Controller):
         self.volume_api.retype(context, volume, new_type, policy)
         return webob.Response(status_int=202)
 
+    @wsgi.action('os-set_bootable')
+    def _set_bootable(self, req, id, body):
+        """Update bootable status of a volume."""
+        context = req.environ['cinder.context']
+        try:
+            volume = self.volume_api.get(context, id)
+        except exception.VolumeNotFound as error:
+            raise webob.exc.HTTPNotFound(explanation=error.msg)
+
+        try:
+            bootable = body['os-set_bootable']['bootable']
+        except KeyError:
+            msg = _("Must specify bootable in request.")
+            raise webob.exc.HTTPBadRequest(explanation=msg)
+
+        if isinstance(bootable, basestring):
+            try:
+                bootable = strutils.bool_from_string(bootable,
+                                                     strict=True)
+            except ValueError:
+                msg = _("Bad value for 'bootable'")
+                raise webob.exc.HTTPBadRequest(explanation=msg)
+
+        elif not isinstance(bootable, bool):
+            msg = _("'bootable' not string or bool")
+            raise webob.exc.HTTPBadRequest(explanation=msg)
+
+        update_dict = {'bootable': bootable}
+
+        self.volume_api.update(context, volume, update_dict)
+        return webob.Response(status_int=200)
+
 
 class Volume_actions(extensions.ExtensionDescriptor):
     """Enable volume actions
index f38281010cc623481f910dd3185c9f82a7905c1a..86eb12320843933d0395629bcee2d56efcb2de55 100644 (file)
@@ -287,6 +287,29 @@ class VolumeActionsTest(test.TestCase):
         make_update_readonly_flag_test(self, 11, 400)
         make_update_readonly_flag_test(self, None, 400)
 
+    def test_set_bootable(self):
+
+        def make_set_bootable_test(self, bootable, return_code):
+            body = {"os-set_bootable": {"bootable": bootable}}
+            if bootable is None:
+                body = {"os-set_bootable": {}}
+            req = webob.Request.blank('/v2/fake/volumes/1/action')
+            req.method = "POST"
+            req.body = jsonutils.dumps(body)
+            req.headers["content-type"] = "application/json"
+            res = req.get_response(fakes.wsgi_app())
+            self.assertEqual(res.status_int, return_code)
+
+        make_set_bootable_test(self, True, 200)
+        make_set_bootable_test(self, False, 200)
+        make_set_bootable_test(self, '1', 200)
+        make_set_bootable_test(self, '0', 200)
+        make_set_bootable_test(self, 'true', 200)
+        make_set_bootable_test(self, 'false', 200)
+        make_set_bootable_test(self, 'tt', 400)
+        make_set_bootable_test(self, 11, 400)
+        make_set_bootable_test(self, None, 400)
+
 
 class VolumeRetypeActionsTest(VolumeActionsTest):
     def setUp(self):