Add os-start/os-stop server actions to OSAPI
Implements blueprint start-stop-methods-support-in-os-servers-xapi Change-Id: I7aa241ffdd2ec3a7c9800411268f4adc47960be8
This commit is contained in:
4 changed files with 152 additions and 0 deletions
1
Authors
1
Authors
@@ -143,6 +143,7 @@ Stephanie Reese <reese.sm@gmail.com>
Thierry Carrez <thierry@openstack.org>
Tim Simpson <tim.simpson@rackspace.com>
Todd Willey <todd@ansolabs.com>
Tomoe Sugihara <tomoe@midokura.com>
Tomoya Masuko<masukotm@nttdata.co.jp>
Trey Morris <trey.morris@rackspace.com>
Troy Toman <troy.toman@rackspace.com>
76
nova/api/openstack/compute/contrib/server_start_stop.py
Normal file
76
nova/api/openstack/compute/contrib/server_start_stop.py
Normal file
@@ -0,0 +1,76 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2012 Midokura Japan K.K.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License
import webob
from nova.api.openstack import extensions
from nova.api.openstack import wsgi
from nova import compute
from nova import exception
from nova import log as logging
LOG = logging.getLogger('nova.api.openstack.compute.contrib.server_start_stop')
class ServerStartStopActionController(wsgi.Controller):
def __init__(self, *args, **kwargs):
super(ServerStartStopActionController, self).__init__(*args, **kwargs)
self.compute_api = compute.API()
@wsgi.action('os-start')
def _start_server(self, req, id, body):
"""Start an instance. """
context = req.environ['nova.context']
try:
LOG.debug(_("start instance %r"), id)
instance = self.compute_api.get(context, id)
self.compute_api.start(context, instance)
except exception.ApiError, e:
raise webob.exc.HTTPBadRequest(explanation=e.message)
except exception.NotAuthorized, e:
raise webob.exc.HTTPUnauthorized()
return webob.Response(status_int=202)
@wsgi.action('os-stop')
def _stop_server(self, req, id, body):
"""Stop an instance."""
context = req.environ['nova.context']
try:
LOG.debug(_("stop instance %r"), id)
instance = self.compute_api.get(context, id)
self.compute_api.stop(context, instance)
except exception.ApiError, e:
raise webob.exc.HTTPBadRequest(explanation=e.message)
except exception.NotAuthorized, e:
raise webob.exc.HTTPUnauthorized()
return webob.Response(status_int=202)
class Server_start_stop(extensions.ExtensionDescriptor):
"""Start/Stop instance compute API support"""
name = "ServerStartStop"
namespace = "http://docs.openstack.org/compute/ext/servers/api/v1.1"
updated = "2012年01月23日:00:00+00:00"
def get_controller_extensions(self):
controller = ServerStartStopActionController()
extension = extensions.ControllerExtension(self, 'servers', controller)
return [extension]
@@ -0,0 +1,74 @@
# Copyright (c) 2012 Midokura Japan K.K.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import unittest
import mox
from nova.api.openstack.compute.contrib import server_start_stop
from nova import context
from nova import exception
from nova import compute
from nova import test
from nova.tests.api.openstack import fakes
def fake_compute_api_get(self, context, instance_id):
return {'id': 1, 'uuid': instance_id}
class ServerStartStopTest(test.TestCase):
def setUp(self):
super(ServerStartStopTest, self).setUp()
self.context = context.get_admin_context()
self.controller = server_start_stop.ServerStartStopActionController()
def tearDown(self):
super(ServerStartStopTest, self).tearDown()
def test_start(self):
self.stubs.Set(compute.API, 'get', fake_compute_api_get)
self.mox.StubOutWithMock(compute.API, 'start')
compute.API.start(mox.IgnoreArg(), mox.IgnoreArg())
self.mox.ReplayAll()
req = fakes.HTTPRequest.blank('/v2/fake/servers/test_inst/action')
body = dict(start="")
self.controller._start_server(req, 'test_inst', body)
def test_stop(self):
self.stubs.Set(compute.API, 'get', fake_compute_api_get)
self.mox.StubOutWithMock(compute.API, 'stop')
compute.API.stop(mox.IgnoreArg(), mox.IgnoreArg())
self.mox.ReplayAll()
req = fakes.HTTPRequest.blank('/v2/fake/servers/test_inst/action')
body = dict(stop="")
self.controller._stop_server(req, 'test_inst', body)
def test_start_with_bogus_id(self):
req = fakes.HTTPRequest.blank('/v2/fake/servers/test_inst/action')
body = dict(start="")
self.assertRaises(exception.InstanceNotFound,
self.controller._start_server, req, 'test_inst', body)
def test_stop_with_bogus_id(self):
req = fakes.HTTPRequest.blank('/v2/fake/servers/test_inst/action')
body = dict(start="")
self.assertRaises(exception.InstanceNotFound,
self.controller._stop_server, req, 'test_inst', body)
if __name__ == '__main__':
unittest.main()
@@ -176,6 +176,7 @@ class ExtensionControllerTest(ExtensionTestCase):
"SecurityGroups",
"ServerActionList",
"ServerDiagnostics",
"ServerStartStop",
"SimpleTenantUsage",
"Users",
"VSAs",
Reference in New Issue
Block a user
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.