From 735c45070e67a3c080bb706af6639106dfd16cb2 Mon Sep 17 00:00:00 2001 From: Ken'ichi Ohmichi Date: Mon, 9 Mar 2015 00:40:29 +0000 Subject: [PATCH] Add and use raise_feature_not_supported() In HTTPNotImplemented cases, each API passes its original message. The meaning of these messages are almost the same but messages are inconsistent. This patch adds common raise_feature_not_supported() and replaces these messages with the method for consistent message. Partially implements blueprint v2-on-v3-api Change-Id: I23da9bf153cb92944c0f4e76d70ce72a4ff6b16f --- nova/api/openstack/common.py | 9 +++++++-- .../compute/plugins/v3/admin_password.py | 2 +- .../compute/plugins/v3/attach_interfaces.py | 9 +++------ .../compute/plugins/v3/baremetal_nodes.py | 4 ++-- .../compute/plugins/v3/certificates.py | 6 +++++- .../compute/plugins/v3/console_output.py | 4 +--- .../compute/plugins/v3/floating_ip_dns.py | 19 +++++++------------ .../api/openstack/compute/plugins/v3/hosts.py | 11 ++++------- .../compute/plugins/v3/hypervisors.py | 4 ++-- .../openstack/compute/plugins/v3/networks.py | 8 +++----- .../compute/plugins/v3/networks_associate.py | 13 ++++--------- .../compute/plugins/v3/pause_server.py | 7 ++----- .../compute/plugins/v3/remote_consoles.py | 15 ++++----------- .../compute/plugins/v3/server_diagnostics.py | 6 +----- 14 files changed, 46 insertions(+), 71 deletions(-) diff --git a/nova/api/openstack/common.py b/nova/api/openstack/common.py index 673fc4be11d2..63f1c1d1cd53 100644 --- a/nova/api/openstack/common.py +++ b/nova/api/openstack/common.py @@ -536,11 +536,16 @@ def get_instance(compute_api, context, instance_id, expected_attrs=None): raise exc.HTTPNotFound(explanation=e.format_message()) +def raise_feature_not_supported(msg=None): + if msg is None: + msg = _("The requested functionality is not supported.") + raise webob.exc.HTTPNotImplemented(explanation=msg) + + def check_cells_enabled(function): @functools.wraps(function) def inner(*args, **kwargs): if not CONF.cells.enable: - msg = _("Cells is not enabled.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + raise_feature_not_supported() return function(*args, **kwargs) return inner diff --git a/nova/api/openstack/compute/plugins/v3/admin_password.py b/nova/api/openstack/compute/plugins/v3/admin_password.py index e35e956305b4..8c1723cad291 100644 --- a/nova/api/openstack/compute/plugins/v3/admin_password.py +++ b/nova/api/openstack/compute/plugins/v3/admin_password.py @@ -56,7 +56,7 @@ class AdminPasswordController(wsgi.Controller): e, 'changePassword', id) except NotImplementedError: msg = _("Unable to set password on instance") - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported(msg=msg) class AdminPassword(extensions.V3APIExtensionBase): diff --git a/nova/api/openstack/compute/plugins/v3/attach_interfaces.py b/nova/api/openstack/compute/plugins/v3/attach_interfaces.py index f481c937a744..cdcae8e9096a 100644 --- a/nova/api/openstack/compute/plugins/v3/attach_interfaces.py +++ b/nova/api/openstack/compute/plugins/v3/attach_interfaces.py @@ -128,8 +128,7 @@ class InterfaceAttachmentController(wsgi.Controller): exception.NetworkNotFound) as e: raise exc.HTTPNotFound(explanation=e.format_message()) except NotImplementedError: - msg = _("The requested functionality is not supported.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except exception.InterfaceAttachFailed as e: raise webob.exc.HTTPInternalServerError( explanation=e.format_message()) @@ -156,8 +155,7 @@ class InterfaceAttachmentController(wsgi.Controller): except exception.InstanceIsLocked as e: raise exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: - msg = _("The requested functionality is not supported.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except exception.InstanceInvalidState as state_error: common.raise_http_conflict_for_instance_invalid_state(state_error, 'detach_interface', server_id) @@ -175,8 +173,7 @@ class InterfaceAttachmentController(wsgi.Controller): except exception.NotFound as e: raise exc.HTTPNotFound(explanation=e.format_message()) except NotImplementedError: - msg = _("Network driver does not support this function.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() ports = data.get('ports', []) results = [entity_maker(port) for port in ports] diff --git a/nova/api/openstack/compute/plugins/v3/baremetal_nodes.py b/nova/api/openstack/compute/plugins/v3/baremetal_nodes.py index d51678c1793d..9ea5b864d1ed 100644 --- a/nova/api/openstack/compute/plugins/v3/baremetal_nodes.py +++ b/nova/api/openstack/compute/plugins/v3/baremetal_nodes.py @@ -20,6 +20,7 @@ from oslo_config import cfg from oslo_utils import importutils import webob +from nova.api.openstack import common from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova.i18n import _ @@ -59,8 +60,7 @@ CONF.import_opt('compute_driver', 'nova.virt.driver') def _check_ironic_client_enabled(): """Check whether Ironic is installed or not.""" if ironic_client is None: - msg = _("Ironic client unavailable, cannot access Ironic.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() def _get_ironic_client(): diff --git a/nova/api/openstack/compute/plugins/v3/certificates.py b/nova/api/openstack/compute/plugins/v3/certificates.py index 4cd2bc78ea34..6cea0756a54c 100644 --- a/nova/api/openstack/compute/plugins/v3/certificates.py +++ b/nova/api/openstack/compute/plugins/v3/certificates.py @@ -14,6 +14,7 @@ import webob.exc +from nova.api.openstack import common from nova.api.openstack import extensions from nova.api.openstack import wsgi import nova.cert.rpcapi @@ -47,7 +48,10 @@ class CertificatesController(wsgi.Controller): authorize(context, action='show') if id != 'root': msg = _("Only root certificate can be retrieved.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + # TODO(oomichi): This seems a HTTPBadRequest case because of the + # above message. This will be changed with a microversion in the + # future. + common.raise_feature_not_supported(msg=msg) try: cert = self.cert_rpcapi.fetch_ca(context, project_id=context.project_id) diff --git a/nova/api/openstack/compute/plugins/v3/console_output.py b/nova/api/openstack/compute/plugins/v3/console_output.py index 7a5ed845b766..ab93ea18862d 100644 --- a/nova/api/openstack/compute/plugins/v3/console_output.py +++ b/nova/api/openstack/compute/plugins/v3/console_output.py @@ -25,7 +25,6 @@ from nova.api.openstack import wsgi from nova.api import validation from nova import compute from nova import exception -from nova.i18n import _ ALIAS = "os-console-output" authorize = extensions.os_compute_authorizer(ALIAS) @@ -61,8 +60,7 @@ class ConsoleOutputController(wsgi.Controller): except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: - msg = _("Unable to get console log, functionality not implemented") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() # XML output is not correctly escaped, so remove invalid characters # NOTE(cyeoh): We don't support XML output with V2.1, but for diff --git a/nova/api/openstack/compute/plugins/v3/floating_ip_dns.py b/nova/api/openstack/compute/plugins/v3/floating_ip_dns.py index 570dcdadcad2..df65df1aab11 100644 --- a/nova/api/openstack/compute/plugins/v3/floating_ip_dns.py +++ b/nova/api/openstack/compute/plugins/v3/floating_ip_dns.py @@ -17,6 +17,7 @@ import urllib from oslo_utils import netutils import webob +from nova.api.openstack import common from nova.api.openstack.compute.schemas.v3 import floating_ip_dns from nova.api.openstack import extensions from nova.api.openstack import wsgi @@ -95,8 +96,7 @@ class FloatingIPDNSDomainController(wsgi.Controller): try: domains = self.network_api.get_dns_domains(context) except NotImplementedError: - msg = _("Unable to create dns domain") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() domainlist = [_create_domain_entry(domain['domain'], domain.get('scope'), @@ -135,8 +135,7 @@ class FloatingIPDNSDomainController(wsgi.Controller): try: create_dns_domain(context, fqdomain, area) except NotImplementedError: - msg = _("Unable to create dns domain") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() return _translate_domain_entry_view({'domain': fqdomain, 'scope': scope, @@ -154,8 +153,7 @@ class FloatingIPDNSDomainController(wsgi.Controller): try: self.network_api.delete_dns_domain(context, domain) except NotImplementedError: - msg = _("Unable to delete dns domain") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) @@ -189,8 +187,7 @@ class FloatingIPDNSEntryController(wsgi.Controller): id, domain) except NotImplementedError: - msg = _("Unable to get dns domain") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() if not entries: explanation = _("DNS entries not found.") @@ -229,8 +226,7 @@ class FloatingIPDNSEntryController(wsgi.Controller): self.network_api.modify_dns_entry(context, name, address, domain) except NotImplementedError: - msg = _("Unable to update dns domain") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() return _translate_dns_entry_view({'ip': address, 'name': name, @@ -249,8 +245,7 @@ class FloatingIPDNSEntryController(wsgi.Controller): try: self.network_api.delete_dns_entry(context, name, domain) except NotImplementedError: - msg = _("Unable to delete dns domain") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except exception.NotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) diff --git a/nova/api/openstack/compute/plugins/v3/hosts.py b/nova/api/openstack/compute/plugins/v3/hosts.py index d22b5841bd2f..97fee275257a 100644 --- a/nova/api/openstack/compute/plugins/v3/hosts.py +++ b/nova/api/openstack/compute/plugins/v3/hosts.py @@ -19,13 +19,13 @@ from oslo_log import log as logging import six import webob.exc +from nova.api.openstack import common from nova.api.openstack.compute.schemas.v3 import hosts from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova.api import validation from nova import compute from nova import exception -from nova.i18n import _ from nova.i18n import _LI from nova import objects @@ -138,8 +138,7 @@ class HostController(wsgi.Controller): try: result = self.api.set_host_maintenance(context, host_name, mode) except NotImplementedError: - msg = _("Virt driver does not implement host maintenance mode.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except exception.HostNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.ComputeServiceUnavailable as e: @@ -161,8 +160,7 @@ class HostController(wsgi.Controller): result = self.api.set_host_enabled(context, host_name=host_name, enabled=enabled) except NotImplementedError: - msg = _("Virt driver does not implement host disabled status.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except exception.HostNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.ComputeServiceUnavailable as e: @@ -179,8 +177,7 @@ class HostController(wsgi.Controller): result = self.api.host_power_action(context, host_name=host_name, action=action) except NotImplementedError: - msg = _("Virt driver does not implement host power management.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except exception.HostNotFound as e: raise webob.exc.HTTPNotFound(explanation=e.format_message()) except exception.ComputeServiceUnavailable as e: diff --git a/nova/api/openstack/compute/plugins/v3/hypervisors.py b/nova/api/openstack/compute/plugins/v3/hypervisors.py index f70813ab6b0a..a65b64855b32 100644 --- a/nova/api/openstack/compute/plugins/v3/hypervisors.py +++ b/nova/api/openstack/compute/plugins/v3/hypervisors.py @@ -17,6 +17,7 @@ import webob.exc +from nova.api.openstack import common from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova import compute @@ -129,8 +130,7 @@ class HypervisorsController(wsgi.Controller): host = hyp.host uptime = self.host_api.get_host_uptime(context, host) except NotImplementedError: - msg = _("Virt driver does not implement uptime function.") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() service = self.host_api.service_get_by_compute_host(context, host) return dict(hypervisor=self._view_hypervisor(hyp, service, False, diff --git a/nova/api/openstack/compute/plugins/v3/networks.py b/nova/api/openstack/compute/plugins/v3/networks.py index 4ffa5fd5b8d9..b520ee7a4443 100644 --- a/nova/api/openstack/compute/plugins/v3/networks.py +++ b/nova/api/openstack/compute/plugins/v3/networks.py @@ -17,6 +17,7 @@ import netaddr from webob import exc +from nova.api.openstack import common from nova.api.openstack.compute.schemas.v3 import networks as schema from nova.api.openstack import extensions from nova.api.openstack import wsgi @@ -102,9 +103,7 @@ class NetworkController(wsgi.Controller): msg = _("Network not found") raise exc.HTTPNotFound(explanation=msg) except NotImplementedError: - msg = _('Disassociate network is not implemented by the ' - 'configured Network API') - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() @extensions.expected_errors(404) def show(self, req, id): @@ -170,8 +169,7 @@ class NetworkController(wsgi.Controller): self.network_api.add_network_to_project( context, project_id, network_id) except NotImplementedError: - msg = (_("VLAN support must be enabled")) - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() except (exception.NoMoreNetworks, exception.NetworkNotFoundForUUID) as e: raise exc.HTTPBadRequest(explanation=e.format_message()) diff --git a/nova/api/openstack/compute/plugins/v3/networks_associate.py b/nova/api/openstack/compute/plugins/v3/networks_associate.py index 8f215dea26ef..5f9e26dedc54 100644 --- a/nova/api/openstack/compute/plugins/v3/networks_associate.py +++ b/nova/api/openstack/compute/plugins/v3/networks_associate.py @@ -12,6 +12,7 @@ from webob import exc +from nova.api.openstack import common from nova.api.openstack.compute.schemas.v3 import networks_associate from nova.api.openstack import extensions from nova.api.openstack import wsgi @@ -43,9 +44,7 @@ class NetworkAssociateActionController(wsgi.Controller): msg = _("Network not found") raise exc.HTTPNotFound(explanation=msg) except NotImplementedError: - msg = _('Disassociate host is not implemented by the configured ' - 'Network API') - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() @wsgi.action("disassociate_project") @wsgi.response(202) @@ -59,9 +58,7 @@ class NetworkAssociateActionController(wsgi.Controller): msg = _("Network not found") raise exc.HTTPNotFound(explanation=msg) except NotImplementedError: - msg = _('Disassociate project is not implemented by the ' - 'configured Network API') - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() @wsgi.action("associate_host") @wsgi.response(202) @@ -78,9 +75,7 @@ class NetworkAssociateActionController(wsgi.Controller): msg = _("Network not found") raise exc.HTTPNotFound(explanation=msg) except NotImplementedError: - msg = _('Associate host is not implemented by the configured ' - 'Network API') - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() class NetworksAssociate(extensions.V3APIExtensionBase): diff --git a/nova/api/openstack/compute/plugins/v3/pause_server.py b/nova/api/openstack/compute/plugins/v3/pause_server.py index afa171d962fd..c645739bdd9c 100644 --- a/nova/api/openstack/compute/plugins/v3/pause_server.py +++ b/nova/api/openstack/compute/plugins/v3/pause_server.py @@ -20,7 +20,6 @@ from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova import compute from nova import exception -from nova.i18n import _ ALIAS = "os-pause-server" @@ -50,8 +49,7 @@ class PauseServerController(wsgi.Controller): except exception.InstanceNotFound as e: raise exc.HTTPNotFound(explanation=e.format_message()) except NotImplementedError: - msg = _("Virt driver does not implement pause function.") - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() @wsgi.response(202) @extensions.expected_errors((404, 409, 501)) @@ -71,8 +69,7 @@ class PauseServerController(wsgi.Controller): except exception.InstanceNotFound as e: raise exc.HTTPNotFound(explanation=e.format_message()) except NotImplementedError: - msg = _("Virt driver does not implement pause function.") - raise exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() class PauseServer(extensions.V3APIExtensionBase): diff --git a/nova/api/openstack/compute/plugins/v3/remote_consoles.py b/nova/api/openstack/compute/plugins/v3/remote_consoles.py index 25883418bfeb..b0d2cc33cb27 100644 --- a/nova/api/openstack/compute/plugins/v3/remote_consoles.py +++ b/nova/api/openstack/compute/plugins/v3/remote_consoles.py @@ -21,7 +21,6 @@ from nova.api.openstack import wsgi from nova.api import validation from nova import compute from nova import exception -from nova.i18n import _ ALIAS = "os-remote-consoles" @@ -56,8 +55,7 @@ class RemoteConsolesController(wsgi.Controller): except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: - msg = _("Unable to get vnc console, functionality not implemented") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() return {'console': {'type': console_type, 'url': output['url']}} @@ -84,9 +82,7 @@ class RemoteConsolesController(wsgi.Controller): except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: - msg = _("Unable to get spice console, " - "functionality not implemented") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() return {'console': {'type': console_type, 'url': output['url']}} @@ -115,8 +111,7 @@ class RemoteConsolesController(wsgi.Controller): except exception.InstanceNotReady as e: raise webob.exc.HTTPConflict(explanation=e.format_message()) except NotImplementedError: - msg = _("Unable to get rdp console, functionality not implemented") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() return {'console': {'type': console_type, 'url': output['url']}} @@ -145,9 +140,7 @@ class RemoteConsolesController(wsgi.Controller): exception.SocketPortRangeExhaustedException) as e: raise webob.exc.HTTPBadRequest(explanation=e.format_message()) except NotImplementedError: - msg = _("Unable to get serial console, " - "functionality not implemented") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() return {'console': {'type': console_type, 'url': output['url']}} diff --git a/nova/api/openstack/compute/plugins/v3/server_diagnostics.py b/nova/api/openstack/compute/plugins/v3/server_diagnostics.py index fff1c0181e7d..529d61990ebd 100644 --- a/nova/api/openstack/compute/plugins/v3/server_diagnostics.py +++ b/nova/api/openstack/compute/plugins/v3/server_diagnostics.py @@ -13,14 +13,11 @@ # License for the specific language governing permissions and limitations # under the License. -import webob.exc - from nova.api.openstack import common from nova.api.openstack import extensions from nova.api.openstack import wsgi from nova import compute from nova import exception -from nova.i18n import _ ALIAS = "os-server-diagnostics" @@ -49,8 +46,7 @@ class ServerDiagnosticsController(wsgi.Controller): common.raise_http_conflict_for_instance_invalid_state(state_error, 'get_diagnostics', server_id) except NotImplementedError: - msg = _("Unable to get diagnostics, functionality not implemented") - raise webob.exc.HTTPNotImplemented(explanation=msg) + common.raise_feature_not_supported() class ServerDiagnostics(extensions.V3APIExtensionBase):