Fix 'os-start/os-stop' server actions for V2.1 API
'os-start/os-stop' server actions were missed for V2.1 API. This patch converts 'os-start/os-stop' server action for V2.1 API The differences between v2 and v3 are described on the wiki page https://wiki.openstack.org/wiki/NovaAPIv2tov3. Closes-Bug: #1367575 Change-Id: I8b0f27fb639034d368bcc1fc51d20daf9c2cdeb5
This commit is contained in:
		
							
								
								
									
										3
									
								
								doc/v3/api_samples/servers/server-action-start.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								doc/v3/api_samples/servers/server-action-start.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					    "os-start" : null
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										3
									
								
								doc/v3/api_samples/servers/server-action-stop.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								doc/v3/api_samples/servers/server-action-stop.json
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					    "os-stop" : null
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -1018,7 +1018,7 @@ class ServersController(wsgi.Controller):
 | 
				
			|||||||
            raise webob.exc.HTTPNotFound(explanation=e.format_message())
 | 
					            raise webob.exc.HTTPNotFound(explanation=e.format_message())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @extensions.expected_errors((404, 409))
 | 
					    @extensions.expected_errors((404, 409))
 | 
				
			||||||
    @wsgi.action('start')
 | 
					    @wsgi.action('os-start')
 | 
				
			||||||
    def _start_server(self, req, id, body):
 | 
					    def _start_server(self, req, id, body):
 | 
				
			||||||
        """Start an instance."""
 | 
					        """Start an instance."""
 | 
				
			||||||
        context = req.environ['nova.context']
 | 
					        context = req.environ['nova.context']
 | 
				
			||||||
@@ -1033,7 +1033,7 @@ class ServersController(wsgi.Controller):
 | 
				
			|||||||
        return webob.Response(status_int=202)
 | 
					        return webob.Response(status_int=202)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @extensions.expected_errors((404, 409))
 | 
					    @extensions.expected_errors((404, 409))
 | 
				
			||||||
    @wsgi.action('stop')
 | 
					    @wsgi.action('os-stop')
 | 
				
			||||||
    def _stop_server(self, req, id, body):
 | 
					    def _stop_server(self, req, id, body):
 | 
				
			||||||
        """Stop an instance."""
 | 
					        """Stop an instance."""
 | 
				
			||||||
        context = req.environ['nova.context']
 | 
					        context = req.environ['nova.context']
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -15,7 +15,11 @@
 | 
				
			|||||||
import mox
 | 
					import mox
 | 
				
			||||||
import webob
 | 
					import webob
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from nova.api.openstack.compute.contrib import server_start_stop
 | 
					from nova.api.openstack.compute.contrib import server_start_stop \
 | 
				
			||||||
 | 
					    as server_v2
 | 
				
			||||||
 | 
					from nova.api.openstack.compute import plugins
 | 
				
			||||||
 | 
					from nova.api.openstack.compute.plugins.v3 import servers \
 | 
				
			||||||
 | 
					    as server_v21
 | 
				
			||||||
from nova.compute import api as compute_api
 | 
					from nova.compute import api as compute_api
 | 
				
			||||||
from nova import db
 | 
					from nova import db
 | 
				
			||||||
from nova import exception
 | 
					from nova import exception
 | 
				
			||||||
@@ -49,11 +53,18 @@ def fake_start_stop_invalid_state(self, context, instance):
 | 
				
			|||||||
    raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
 | 
					    raise exception.InstanceIsLocked(instance_uuid=instance['uuid'])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ServerStartStopTest(test.TestCase):
 | 
					class ServerStartStopTestV21(test.TestCase):
 | 
				
			||||||
 | 
					    start_policy = "compute:v3:servers:start"
 | 
				
			||||||
 | 
					    stop_policy = "compute:v3:servers:stop"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def setUp(self):
 | 
					    def setUp(self):
 | 
				
			||||||
        super(ServerStartStopTest, self).setUp()
 | 
					        super(ServerStartStopTestV21, self).setUp()
 | 
				
			||||||
        self.controller = server_start_stop.ServerStartStopActionController()
 | 
					        self._setup_controller()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _setup_controller(self):
 | 
				
			||||||
 | 
					        ext_info = plugins.LoadedExtensionInfo()
 | 
				
			||||||
 | 
					        self.controller = server_v21.ServersController(
 | 
				
			||||||
 | 
					                              extension_info=ext_info)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_start(self):
 | 
					    def test_start(self):
 | 
				
			||||||
        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get)
 | 
					        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get)
 | 
				
			||||||
@@ -67,7 +78,7 @@ class ServerStartStopTest(test.TestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def test_start_policy_failed(self):
 | 
					    def test_start_policy_failed(self):
 | 
				
			||||||
        rules = {
 | 
					        rules = {
 | 
				
			||||||
            "compute:start":
 | 
					            self.start_policy:
 | 
				
			||||||
                common_policy.parse_rule("project_id:non_fake")
 | 
					                common_policy.parse_rule("project_id:non_fake")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        policy.set_rules(rules)
 | 
					        policy.set_rules(rules)
 | 
				
			||||||
@@ -77,7 +88,7 @@ class ServerStartStopTest(test.TestCase):
 | 
				
			|||||||
        exc = self.assertRaises(exception.PolicyNotAuthorized,
 | 
					        exc = self.assertRaises(exception.PolicyNotAuthorized,
 | 
				
			||||||
                                self.controller._start_server,
 | 
					                                self.controller._start_server,
 | 
				
			||||||
                                req, 'test_inst', body)
 | 
					                                req, 'test_inst', body)
 | 
				
			||||||
        self.assertIn('compute:start', exc.format_message())
 | 
					        self.assertIn(self.start_policy, exc.format_message())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_start_not_ready(self):
 | 
					    def test_start_not_ready(self):
 | 
				
			||||||
        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get)
 | 
					        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get)
 | 
				
			||||||
@@ -115,7 +126,7 @@ class ServerStartStopTest(test.TestCase):
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    def test_stop_policy_failed(self):
 | 
					    def test_stop_policy_failed(self):
 | 
				
			||||||
        rules = {
 | 
					        rules = {
 | 
				
			||||||
            "compute:stop":
 | 
					            self.stop_policy:
 | 
				
			||||||
                common_policy.parse_rule("project_id:non_fake")
 | 
					                common_policy.parse_rule("project_id:non_fake")
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        policy.set_rules(rules)
 | 
					        policy.set_rules(rules)
 | 
				
			||||||
@@ -125,7 +136,7 @@ class ServerStartStopTest(test.TestCase):
 | 
				
			|||||||
        exc = self.assertRaises(exception.PolicyNotAuthorized,
 | 
					        exc = self.assertRaises(exception.PolicyNotAuthorized,
 | 
				
			||||||
                                self.controller._stop_server,
 | 
					                                self.controller._stop_server,
 | 
				
			||||||
                                req, 'test_inst', body)
 | 
					                                req, 'test_inst', body)
 | 
				
			||||||
        self.assertIn("compute:stop", exc.format_message())
 | 
					        self.assertIn(self.stop_policy, exc.format_message())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test_stop_not_ready(self):
 | 
					    def test_stop_not_ready(self):
 | 
				
			||||||
        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get)
 | 
					        self.stubs.Set(db, 'instance_get_by_uuid', fake_instance_get)
 | 
				
			||||||
@@ -162,3 +173,11 @@ class ServerStartStopTest(test.TestCase):
 | 
				
			|||||||
        body = dict(stop="")
 | 
					        body = dict(stop="")
 | 
				
			||||||
        self.assertRaises(webob.exc.HTTPNotFound,
 | 
					        self.assertRaises(webob.exc.HTTPNotFound,
 | 
				
			||||||
            self.controller._stop_server, req, 'test_inst', body)
 | 
					            self.controller._stop_server, req, 'test_inst', body)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ServerStartStopTestV2(ServerStartStopTestV21):
 | 
				
			||||||
 | 
					    start_policy = "compute:start"
 | 
				
			||||||
 | 
					    stop_policy = "compute:stop"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _setup_controller(self):
 | 
				
			||||||
 | 
					        self.controller = server_v2.ServerStartStopActionController()
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					    "%(action)s" : null
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -0,0 +1,3 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					    "%(action)s" : null
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -158,3 +158,23 @@ class ServersActionsJsonTest(ServersSampleBase):
 | 
				
			|||||||
        uuid = self._post_server()
 | 
					        uuid = self._post_server()
 | 
				
			||||||
        self._test_server_action(uuid, 'create_image',
 | 
					        self._test_server_action(uuid, 'create_image',
 | 
				
			||||||
                                 {'name': 'foo-image'})
 | 
					                                 {'name': 'foo-image'})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					class ServerStartStopJsonTest(ServersSampleBase):
 | 
				
			||||||
 | 
					    sample_dir = 'servers'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def _test_server_action(self, uuid, action, req_tpl):
 | 
				
			||||||
 | 
					        response = self._do_post('servers/%s/action' % uuid,
 | 
				
			||||||
 | 
					                                 req_tpl,
 | 
				
			||||||
 | 
					                                 {'action': action})
 | 
				
			||||||
 | 
					        self.assertEqual(response.status, 202)
 | 
				
			||||||
 | 
					        self.assertEqual(response.read(), "")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_server_start(self):
 | 
				
			||||||
 | 
					        uuid = self._post_server()
 | 
				
			||||||
 | 
					        self._test_server_action(uuid, 'os-stop', 'server-action-stop')
 | 
				
			||||||
 | 
					        self._test_server_action(uuid, 'os-start', 'server-action-start')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_server_stop(self):
 | 
				
			||||||
 | 
					        uuid = self._post_server()
 | 
				
			||||||
 | 
					        self._test_server_action(uuid, 'os-stop', 'server-action-stop')
 | 
				
			||||||
		Reference in New Issue
	
	Block a user