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:
parent
bcc7cc0c2c
commit
5e6b6b044b
@ -1,5 +1,5 @@
|
||||
{
|
||||
"change_password" : {
|
||||
"admin_password" : "foo"
|
||||
"changePassword" : {
|
||||
"adminPass" : "foo"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -34,15 +34,18 @@ class AdminPasswordController(wsgi.Controller):
|
||||
super(AdminPasswordController, self).__init__(*args, **kwargs)
|
||||
self.compute_api = compute.API()
|
||||
|
||||
@wsgi.action('change_password')
|
||||
@wsgi.response(204)
|
||||
# TODO(eliqiao): Here should be 204(No content) instead of 202 by v2.1
|
||||
# +micorversions because the password has been changed when returning
|
||||
# a response.
|
||||
@wsgi.action('changePassword')
|
||||
@wsgi.response(202)
|
||||
@extensions.expected_errors((400, 404, 409, 501))
|
||||
@validation.schema(admin_password.change_password)
|
||||
def change_password(self, req, id, body):
|
||||
context = req.environ['nova.context']
|
||||
authorize(context)
|
||||
|
||||
password = body['change_password']['admin_password']
|
||||
password = body['changePassword']['adminPass']
|
||||
instance = common.get_instance(self.compute_api, context, id,
|
||||
want_objects=True)
|
||||
try:
|
||||
@ -51,7 +54,7 @@ class AdminPasswordController(wsgi.Controller):
|
||||
raise exc.HTTPConflict(explanation=e.format_message())
|
||||
except exception.InstanceInvalidState as e:
|
||||
raise common.raise_http_conflict_for_instance_invalid_state(
|
||||
e, 'change_password')
|
||||
e, 'changePassword')
|
||||
except NotImplementedError:
|
||||
msg = _("Unable to set password on instance")
|
||||
raise exc.HTTPNotImplemented(explanation=msg)
|
||||
|
@ -18,15 +18,15 @@ from nova.api.validation import parameter_types
|
||||
change_password = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'change_password': {
|
||||
'changePassword': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'admin_password': parameter_types.admin_password,
|
||||
'adminPass': parameter_types.admin_password,
|
||||
},
|
||||
'required': ['admin_password'],
|
||||
'required': ['adminPass'],
|
||||
'additionalProperties': False,
|
||||
},
|
||||
},
|
||||
'required': ['change_password'],
|
||||
'required': ['changePassword'],
|
||||
'additionalProperties': False,
|
||||
}
|
||||
|
@ -15,7 +15,8 @@
|
||||
# under the License.
|
||||
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 import exception
|
||||
from nova.openstack.common import jsonutils
|
||||
@ -45,18 +46,19 @@ def fake_set_admin_password_not_implemented(self, context, instance,
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class AdminPasswordTest(test.NoDBTestCase):
|
||||
class AdminPasswordTestV21(test.NoDBTestCase):
|
||||
plugin = admin_password_v21
|
||||
|
||||
def setUp(self):
|
||||
super(AdminPasswordTest, self).setUp()
|
||||
super(AdminPasswordTestV21, self).setUp()
|
||||
self.stubs.Set(compute_api.API, 'set_admin_password',
|
||||
fake_set_admin_password)
|
||||
self.stubs.Set(compute_api.API, 'get', fake_get)
|
||||
self.app = fakes.wsgi_app_v3(init_only=('servers',
|
||||
admin_password.ALIAS))
|
||||
self.plugin.ALIAS))
|
||||
|
||||
def _make_request(self, url, body):
|
||||
req = webob.Request.blank(url)
|
||||
def _make_request(self, body):
|
||||
req = webob.Request.blank('/v3/servers/1/action')
|
||||
req.method = 'POST'
|
||||
req.body = jsonutils.dumps(body)
|
||||
req.content_type = 'application/json'
|
||||
@ -64,54 +66,46 @@ class AdminPasswordTest(test.NoDBTestCase):
|
||||
return res
|
||||
|
||||
def test_change_password(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': {'admin_password': 'test'}}
|
||||
res = self._make_request(url, body)
|
||||
self.assertEqual(res.status_int, 204)
|
||||
body = {'changePassword': {'adminPass': 'test'}}
|
||||
res = self._make_request(body)
|
||||
self.assertEqual(res.status_int, 202)
|
||||
|
||||
def test_change_password_empty_string(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': {'admin_password': ''}}
|
||||
res = self._make_request(url, body)
|
||||
self.assertEqual(res.status_int, 204)
|
||||
body = {'changePassword': {'adminPass': ''}}
|
||||
res = self._make_request(body)
|
||||
self.assertEqual(res.status_int, 202)
|
||||
|
||||
def test_change_password_with_non_implement(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': {'admin_password': 'test'}}
|
||||
body = {'changePassword': {'adminPass': 'test'}}
|
||||
self.stubs.Set(compute_api.API, 'set_admin_password',
|
||||
fake_set_admin_password_not_implemented)
|
||||
res = self._make_request(url, body)
|
||||
res = self._make_request(body)
|
||||
self.assertEqual(res.status_int, 501)
|
||||
|
||||
def test_change_password_with_non_existed_instance(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': {'admin_password': 'test'}}
|
||||
body = {'changePassword': {'adminPass': 'test'}}
|
||||
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)
|
||||
|
||||
def test_change_password_with_non_string_password(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': {'admin_password': 1234}}
|
||||
res = self._make_request(url, body)
|
||||
body = {'changePassword': {'adminPass': 1234}}
|
||||
res = self._make_request(body)
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_change_password_failed(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': {'admin_password': 'test'}}
|
||||
body = {'changePassword': {'adminPass': 'test'}}
|
||||
self.stubs.Set(compute_api.API, 'set_admin_password',
|
||||
fake_set_admin_password_failed)
|
||||
res = self._make_request(url, body)
|
||||
res = self._make_request(body)
|
||||
self.assertEqual(res.status_int, 409)
|
||||
|
||||
def test_change_password_without_admin_password(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': {}}
|
||||
res = self._make_request(url, body)
|
||||
body = {'changPassword': {}}
|
||||
res = self._make_request(body)
|
||||
self.assertEqual(res.status_int, 400)
|
||||
|
||||
def test_change_password_none(self):
|
||||
url = '/v3/servers/1/action'
|
||||
body = {'change_password': None}
|
||||
res = self._make_request(url, body)
|
||||
body = {'changePassword': None}
|
||||
res = self._make_request(body)
|
||||
self.assertEqual(res.status_int, 400)
|
@ -1,5 +1,5 @@
|
||||
{
|
||||
"change_password" : {
|
||||
"admin_password" : "%(password)s"
|
||||
"changePassword" : {
|
||||
"adminPass" : "%(password)s"
|
||||
}
|
||||
}
|
||||
|
@ -25,5 +25,5 @@ class AdminPasswordJsonTest(test_servers.ServersSampleBase):
|
||||
response = self._do_post('servers/%s/action' % uuid,
|
||||
'admin-password-change-password',
|
||||
subs)
|
||||
self.assertEqual(response.status, 204)
|
||||
self.assertEqual(response.status, 202)
|
||||
self.assertEqual(response.read(), "")
|
||||
|
Loading…
Reference in New Issue
Block a user