Change v3 admin-password to v2.1

This patch changes v3 admin-password API to v2.1 and backport v3
unit test cases to v2.1.
The differences between v2 and v3 are described on the wiki page
https://wiki.openstack.org/wiki/NovaAPIv2tov3

Partially implements blueprint v2-on-v3-api
Change-Id: I167753eddd188120236c51bade549c907c8a8466
This commit is contained in:
Eli Qiao 2014-08-12 12:40:55 +08:00
parent bcc7cc0c2c
commit 5e6b6b044b
6 changed files with 43 additions and 46 deletions

View File

@ -1,5 +1,5 @@
{ {
"change_password" : { "changePassword" : {
"admin_password" : "foo" "adminPass" : "foo"
} }
} }

View File

@ -34,15 +34,18 @@ class AdminPasswordController(wsgi.Controller):
super(AdminPasswordController, self).__init__(*args, **kwargs) super(AdminPasswordController, self).__init__(*args, **kwargs)
self.compute_api = compute.API() self.compute_api = compute.API()
@wsgi.action('change_password') # TODO(eliqiao): Here should be 204(No content) instead of 202 by v2.1
@wsgi.response(204) # +micorversions because the password has been changed when returning
# a response.
@wsgi.action('changePassword')
@wsgi.response(202)
@extensions.expected_errors((400, 404, 409, 501)) @extensions.expected_errors((400, 404, 409, 501))
@validation.schema(admin_password.change_password) @validation.schema(admin_password.change_password)
def change_password(self, req, id, body): def change_password(self, req, id, body):
context = req.environ['nova.context'] context = req.environ['nova.context']
authorize(context) authorize(context)
password = body['change_password']['admin_password'] password = body['changePassword']['adminPass']
instance = common.get_instance(self.compute_api, context, id, instance = common.get_instance(self.compute_api, context, id,
want_objects=True) want_objects=True)
try: try:
@ -51,7 +54,7 @@ class AdminPasswordController(wsgi.Controller):
raise exc.HTTPConflict(explanation=e.format_message()) raise exc.HTTPConflict(explanation=e.format_message())
except exception.InstanceInvalidState as e: except exception.InstanceInvalidState as e:
raise common.raise_http_conflict_for_instance_invalid_state( raise common.raise_http_conflict_for_instance_invalid_state(
e, 'change_password') e, 'changePassword')
except NotImplementedError: except NotImplementedError:
msg = _("Unable to set password on instance") msg = _("Unable to set password on instance")
raise exc.HTTPNotImplemented(explanation=msg) raise exc.HTTPNotImplemented(explanation=msg)

View File

@ -18,15 +18,15 @@ from nova.api.validation import parameter_types
change_password = { change_password = {
'type': 'object', 'type': 'object',
'properties': { 'properties': {
'change_password': { 'changePassword': {
'type': 'object', 'type': 'object',
'properties': { 'properties': {
'admin_password': parameter_types.admin_password, 'adminPass': parameter_types.admin_password,
}, },
'required': ['admin_password'], 'required': ['adminPass'],
'additionalProperties': False, 'additionalProperties': False,
}, },
}, },
'required': ['change_password'], 'required': ['changePassword'],
'additionalProperties': False, 'additionalProperties': False,
} }

View File

@ -15,7 +15,8 @@
# under the License. # under the License.
import webob import webob
from nova.api.openstack.compute.plugins.v3 import admin_password from nova.api.openstack.compute.plugins.v3 import admin_password \
as admin_password_v21
from nova.compute import api as compute_api from nova.compute import api as compute_api
from nova import exception from nova import exception
from nova.openstack.common import jsonutils from nova.openstack.common import jsonutils
@ -45,18 +46,19 @@ def fake_set_admin_password_not_implemented(self, context, instance,
raise NotImplementedError() raise NotImplementedError()
class AdminPasswordTest(test.NoDBTestCase): class AdminPasswordTestV21(test.NoDBTestCase):
plugin = admin_password_v21
def setUp(self): def setUp(self):
super(AdminPasswordTest, self).setUp() super(AdminPasswordTestV21, self).setUp()
self.stubs.Set(compute_api.API, 'set_admin_password', self.stubs.Set(compute_api.API, 'set_admin_password',
fake_set_admin_password) fake_set_admin_password)
self.stubs.Set(compute_api.API, 'get', fake_get) self.stubs.Set(compute_api.API, 'get', fake_get)
self.app = fakes.wsgi_app_v3(init_only=('servers', self.app = fakes.wsgi_app_v3(init_only=('servers',
admin_password.ALIAS)) self.plugin.ALIAS))
def _make_request(self, url, body): def _make_request(self, body):
req = webob.Request.blank(url) req = webob.Request.blank('/v3/servers/1/action')
req.method = 'POST' req.method = 'POST'
req.body = jsonutils.dumps(body) req.body = jsonutils.dumps(body)
req.content_type = 'application/json' req.content_type = 'application/json'
@ -64,54 +66,46 @@ class AdminPasswordTest(test.NoDBTestCase):
return res return res
def test_change_password(self): def test_change_password(self):
url = '/v3/servers/1/action' body = {'changePassword': {'adminPass': 'test'}}
body = {'change_password': {'admin_password': 'test'}} res = self._make_request(body)
res = self._make_request(url, body) self.assertEqual(res.status_int, 202)
self.assertEqual(res.status_int, 204)
def test_change_password_empty_string(self): def test_change_password_empty_string(self):
url = '/v3/servers/1/action' body = {'changePassword': {'adminPass': ''}}
body = {'change_password': {'admin_password': ''}} res = self._make_request(body)
res = self._make_request(url, body) self.assertEqual(res.status_int, 202)
self.assertEqual(res.status_int, 204)
def test_change_password_with_non_implement(self): def test_change_password_with_non_implement(self):
url = '/v3/servers/1/action' body = {'changePassword': {'adminPass': 'test'}}
body = {'change_password': {'admin_password': 'test'}}
self.stubs.Set(compute_api.API, 'set_admin_password', self.stubs.Set(compute_api.API, 'set_admin_password',
fake_set_admin_password_not_implemented) fake_set_admin_password_not_implemented)
res = self._make_request(url, body) res = self._make_request(body)
self.assertEqual(res.status_int, 501) self.assertEqual(res.status_int, 501)
def test_change_password_with_non_existed_instance(self): def test_change_password_with_non_existed_instance(self):
url = '/v3/servers/1/action' body = {'changePassword': {'adminPass': 'test'}}
body = {'change_password': {'admin_password': 'test'}}
self.stubs.Set(compute_api.API, 'get', fake_get_non_existent) self.stubs.Set(compute_api.API, 'get', fake_get_non_existent)
res = self._make_request(url, body) res = self._make_request(body)
self.assertEqual(res.status_int, 404) self.assertEqual(res.status_int, 404)
def test_change_password_with_non_string_password(self): def test_change_password_with_non_string_password(self):
url = '/v3/servers/1/action' body = {'changePassword': {'adminPass': 1234}}
body = {'change_password': {'admin_password': 1234}} res = self._make_request(body)
res = self._make_request(url, body)
self.assertEqual(res.status_int, 400) self.assertEqual(res.status_int, 400)
def test_change_password_failed(self): def test_change_password_failed(self):
url = '/v3/servers/1/action' body = {'changePassword': {'adminPass': 'test'}}
body = {'change_password': {'admin_password': 'test'}}
self.stubs.Set(compute_api.API, 'set_admin_password', self.stubs.Set(compute_api.API, 'set_admin_password',
fake_set_admin_password_failed) fake_set_admin_password_failed)
res = self._make_request(url, body) res = self._make_request(body)
self.assertEqual(res.status_int, 409) self.assertEqual(res.status_int, 409)
def test_change_password_without_admin_password(self): def test_change_password_without_admin_password(self):
url = '/v3/servers/1/action' body = {'changPassword': {}}
body = {'change_password': {}} res = self._make_request(body)
res = self._make_request(url, body)
self.assertEqual(res.status_int, 400) self.assertEqual(res.status_int, 400)
def test_change_password_none(self): def test_change_password_none(self):
url = '/v3/servers/1/action' body = {'changePassword': None}
body = {'change_password': None} res = self._make_request(body)
res = self._make_request(url, body)
self.assertEqual(res.status_int, 400) self.assertEqual(res.status_int, 400)

View File

@ -1,5 +1,5 @@
{ {
"change_password" : { "changePassword" : {
"admin_password" : "%(password)s" "adminPass" : "%(password)s"
} }
} }

View File

@ -25,5 +25,5 @@ class AdminPasswordJsonTest(test_servers.ServersSampleBase):
response = self._do_post('servers/%s/action' % uuid, response = self._do_post('servers/%s/action' % uuid,
'admin-password-change-password', 'admin-password-change-password',
subs) subs)
self.assertEqual(response.status, 204) self.assertEqual(response.status, 202)
self.assertEqual(response.read(), "") self.assertEqual(response.read(), "")