Wrap public strings with i18n

Currently the public error messages/logs are plain ASCII strings.
This patch converts them to their i18n version.

Change-Id: I7486eb1ffbf05f39abf7125a37435278aebc79ee
Closes-Bug: #1525053
This commit is contained in:
reedip 2016-01-12 10:29:39 +09:00
parent cf43dd58b0
commit a35f137a58
3 changed files with 66 additions and 47 deletions

View File

@ -14,11 +14,19 @@
import oslo_i18n import oslo_i18n
_translators = oslo_i18n.TranslatorFactory(domain='neutron') DOMAIN = "kuryr"
_translators = oslo_i18n.TranslatorFactory(domain=DOMAIN)
# The primary translation function using the well-known name "_" # The primary translation function using the well-known name "_"
_ = _translators.primary _ = _translators.primary
# The contextual translation function using the name "_C"
_C = _translators.contextual_form
# The plural translation function using the name "_P"
_P = _translators.plural_form
# Translators for log levels. # Translators for log levels.
# #
# The abbreviated names are meant to reflect the usual use of a short # The abbreviated names are meant to reflect the usual use of a short
@ -28,3 +36,7 @@ _LI = _translators.log_info
_LW = _translators.log_warning _LW = _translators.log_warning
_LE = _translators.log_error _LE = _translators.log_error
_LC = _translators.log_critical _LC = _translators.log_critical
def get_available_languages():
return oslo_i18n.get_available_languages(DOMAIN)

View File

@ -28,6 +28,7 @@ from kuryr import binding
from kuryr.common import config from kuryr.common import config
from kuryr.common import constants from kuryr.common import constants
from kuryr.common import exceptions from kuryr.common import exceptions
from kuryr._i18n import _LE, _LI, _LW
from kuryr import schemata from kuryr import schemata
from kuryr import utils from kuryr import utils
@ -137,8 +138,8 @@ def _cache_default_subnetpool_ids(app):
for subnetpool in subnetpools['subnetpools']: for subnetpool in subnetpools['subnetpools']:
default_subnetpool_id_set.add(subnetpool['id']) default_subnetpool_id_set.add(subnetpool['id'])
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during retrieving the default " app.logger.error(_LE("Error happened during retrieving the default"
"subnet pools.".format(ex)) " subnet pools.").format(ex))
app.DEFAULT_POOL_IDS = frozenset(default_subnetpool_id_set) app.DEFAULT_POOL_IDS = frozenset(default_subnetpool_id_set)
@ -228,8 +229,8 @@ def _create_port(endpoint_id, neutron_network_id, interface_mac, fixed_ips):
try: try:
rcvd_port = app.neutron.create_port({'port': port}) rcvd_port = app.neutron.create_port({'port': port})
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during creating a " app.logger.error(_LE("Error happend during creating a"
"Neutron port: {0}".format(ex)) " Neutron port: {0}").format(ex))
raise raise
return rcvd_port['port'] return rcvd_port['port']
@ -244,8 +245,8 @@ def _update_port(port, endpoint_id):
'device_owner': constants.DEVICE_OWNER, 'device_owner': constants.DEVICE_OWNER,
'device_id': endpoint_id}}) 'device_id': endpoint_id}})
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during creating a " app.logger.error(_LE("Error happend during creating a "
"Neutron port: {0}".format(ex)) "Neutron port: {0}").format(ex))
raise raise
return response_port['port'] return response_port['port']
@ -425,8 +426,8 @@ def network_driver_create_network():
https://github.com/docker/libnetwork/blob/master/docs/remote.md#create-network # noqa https://github.com/docker/libnetwork/blob/master/docs/remote.md#create-network # noqa
""" """
json_data = flask.request.get_json(force=True) json_data = flask.request.get_json(force=True)
app.logger.debug("Received JSON data {0} for /NetworkDriver.CreateNetwork" app.logger.debug("Received JSON data {0} for"
.format(json_data)) " /NetworkDriver.CreateNetwork".format(json_data))
jsonschema.validate(json_data, schemata.NETWORK_CREATE_SCHEMA) jsonschema.validate(json_data, schemata.NETWORK_CREATE_SCHEMA)
neutron_network_name = json_data['NetworkID'] neutron_network_name = json_data['NetworkID']
@ -441,7 +442,8 @@ def network_driver_create_network():
network = app.neutron.create_network( network = app.neutron.create_network(
{'network': {'name': neutron_network_name, "admin_state_up": True}}) {'network': {'name': neutron_network_name, "admin_state_up": True}})
app.logger.info("Created a new network with name {0} successfully: {1}" app.logger.info(_LI("Created a new network with name {0}"
" successfully: {1}")
.format(neutron_network_name, network)) .format(neutron_network_name, network))
cidr = netaddr.IPNetwork(pool_cidr) cidr = netaddr.IPNetwork(pool_cidr)
@ -481,16 +483,16 @@ def network_driver_delete_network():
https://github.com/docker/libnetwork/blob/master/docs/remote.md#delete-network # noqa https://github.com/docker/libnetwork/blob/master/docs/remote.md#delete-network # noqa
""" """
json_data = flask.request.get_json(force=True) json_data = flask.request.get_json(force=True)
app.logger.debug("Received JSON data {0} for /NetworkDriver.DeleteNetwork" app.logger.debug("Received JSON data {0} for"
.format(json_data)) " /NetworkDriver.DeleteNetwork".format(json_data))
jsonschema.validate(json_data, schemata.NETWORK_DELETE_SCHEMA) jsonschema.validate(json_data, schemata.NETWORK_DELETE_SCHEMA)
neutron_network_name = json_data['NetworkID'] neutron_network_name = json_data['NetworkID']
try: try:
filtered_networks = _get_networks_by_attrs(name=neutron_network_name) filtered_networks = _get_networks_by_attrs(name=neutron_network_name)
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during listing " app.logger.error(_LE("Error happened during listing "
"Neutron networks: {0}".format(ex)) "Neutron networks: {0}").format(ex))
raise raise
# We assume Neutron's Network names are not conflicted in Kuryr because # We assume Neutron's Network names are not conflicted in Kuryr because
# they are Docker IDs, 256 bits hashed values, which are rarely conflicted. # they are Docker IDs, 256 bits hashed values, which are rarely conflicted.
@ -521,22 +523,22 @@ def network_driver_delete_network():
# and continue to proceed. # and continue to proceed.
app.neutron.delete_subnet(subnet['id']) app.neutron.delete_subnet(subnet['id'])
except n_exceptions.Conflict as ex: except n_exceptions.Conflict as ex:
app.logger.error("Subnet, {0}, is in use. " app.logger.error(_LE(
"Network cant be deleted." "Subnet, {0}, is in use. Network cant be deleted.").format(
.format(subnet['id'])) subnet['id']))
raise raise
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during deleting a " app.logger.error(_LE("Error happened during deleting a "
"Neutron subnets: {0}".format(ex)) "Neutron subnets: {0}").format(ex))
raise raise
try: try:
app.neutron.delete_network(neutron_network_id) app.neutron.delete_network(neutron_network_id)
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during deleting a " app.logger.error(_LE("Error happened during deleting a "
"Neutron network: {0}".format(ex)) "Neutron network: {0}").format(ex))
raise raise
app.logger.info("Deleted the network with ID {0} successfully" app.logger.info(_LI("Deleted the network with ID {0} successfully")
.format(neutron_network_id)) .format(neutron_network_id))
return flask.jsonify(constants.SCHEMA['SUCCESS']) return flask.jsonify(constants.SCHEMA['SUCCESS'])
@ -576,7 +578,8 @@ def network_driver_create_endpoint():
https://github.com/docker/libnetwork/blob/master/docs/remote.md#create-endpoint # noqa https://github.com/docker/libnetwork/blob/master/docs/remote.md#create-endpoint # noqa
""" """
json_data = flask.request.get_json(force=True) json_data = flask.request.get_json(force=True)
app.logger.debug("Received JSON data {0} for /NetworkDriver.CreateEndpoint" app.logger.debug("Received JSON data {0} for "
"/NetworkDriver.CreateEndpoint"
.format(json_data)) .format(json_data))
jsonschema.validate(json_data, schemata.ENDPOINT_CREATE_SCHEMA) jsonschema.validate(json_data, schemata.ENDPOINT_CREATE_SCHEMA)
@ -629,8 +632,8 @@ def network_driver_delete_endpoint():
https://github.com/docker/libnetwork/blob/master/docs/remote.md#delete-endpoint # noqa https://github.com/docker/libnetwork/blob/master/docs/remote.md#delete-endpoint # noqa
""" """
json_data = flask.request.get_json(force=True) json_data = flask.request.get_json(force=True)
app.logger.debug("Received JSON data {0} for /NetworkDriver.DeleteEndpoint" app.logger.debug("Received JSON data {0} for"
.format(json_data)) " /NetworkDriver.DeleteEndpoint".format(json_data))
jsonschema.validate(json_data, schemata.ENDPOINT_DELETE_SCHEMA) jsonschema.validate(json_data, schemata.ENDPOINT_DELETE_SCHEMA)
return flask.jsonify(constants.SCHEMA['SUCCESS']) return flask.jsonify(constants.SCHEMA['SUCCESS'])
@ -708,12 +711,13 @@ def network_driver_join():
app.logger.error(stderr) app.logger.error(stderr)
except exceptions.VethCreationFailure as ex: except exceptions.VethCreationFailure as ex:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
app.logger.error('Preparing the veth pair was failed: {0}.' app.logger.error(_LE('Preparing the veth '
'pair was failed: {0}.')
.format(ex)) .format(ex))
except processutils.ProcessExecutionError: except processutils.ProcessExecutionError:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
app.logger.error( app.logger.error(_LE(
'Could not bind the Neutron port to the veth endpoint.') 'Could not bind the Neutron port to the veth endpoint.'))
join_response = { join_response = {
"InterfaceName": { "InterfaceName": {
@ -757,7 +761,8 @@ def network_driver_leave():
} }
""" """
json_data = flask.request.get_json(force=True) json_data = flask.request.get_json(force=True)
app.logger.debug("Received JSON data {0} for /NetworkDriver.DeleteEndpoint" app.logger.debug("Received JSON data {0} for"
" /NetworkDriver.DeleteEndpoint"
.format(json_data)) .format(json_data))
jsonschema.validate(json_data, schemata.LEAVE_SCHEMA) jsonschema.validate(json_data, schemata.LEAVE_SCHEMA)
neutron_network_name = json_data['NetworkID'] neutron_network_name = json_data['NetworkID']
@ -785,12 +790,12 @@ def network_driver_leave():
app.logger.error(stderr) app.logger.error(stderr)
except processutils.ProcessExecutionError: except processutils.ProcessExecutionError:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
app.logger.error( app.logger.error(_LE(
'Could not unbind the Neutron port from the veth ' 'Could not unbind the Neutron port from the veth '
'endpoint.') 'endpoint.'))
except exceptions.VethDeletionFailure: except exceptions.VethDeletionFailure:
with excutils.save_and_reraise_exception(): with excutils.save_and_reraise_exception():
app.logger.error('Cleaning the veth pair up was failed.') app.logger.error(_LE('Cleaning the veth pair up was failed.'))
return flask.jsonify(constants.SCHEMA['SUCCESS']) return flask.jsonify(constants.SCHEMA['SUCCESS'])
@ -851,7 +856,7 @@ def ipam_request_pool():
pool_id = '' pool_id = ''
subnet_cidr = '' subnet_cidr = ''
if requested_pool: if requested_pool:
app.logger.info("Creating subnetpool with the given pool CIDR") app.logger.info(_LI("Creating subnetpool with the given pool CIDR"))
if requested_subpool: if requested_subpool:
cidr = netaddr.IPNetwork(requested_subpool) cidr = netaddr.IPNetwork(requested_subpool)
else: else:
@ -883,12 +888,12 @@ def ipam_request_pool():
pool_id = pool['id'] pool_id = pool['id']
prefixes = pool['prefixes'] prefixes = pool['prefixes']
if len(prefixes) > 1: if len(prefixes) > 1:
app.logger.warning("More than one prefixes present. " app.logger.warning(_LW("More than one prefixes present. "
"Picking first one.") "Picking first one."))
cidr = netaddr.IPNetwork(prefixes[0]) cidr = netaddr.IPNetwork(prefixes[0])
subnet_cidr = _get_subnet_cidr_using_cidr(cidr) subnet_cidr = _get_subnet_cidr_using_cidr(cidr)
else: else:
app.logger.error("Default neutron pools not found") app.logger.error(_LE("Default neutron pools not found."))
req_pool_res = {'PoolID': pool_id, req_pool_res = {'PoolID': pool_id,
'Pool': subnet_cidr} 'Pool': subnet_cidr}
return flask.jsonify(req_pool_res) return flask.jsonify(req_pool_res)
@ -932,8 +937,8 @@ def ipam_request_address():
pool = pools[0] pool = pools[0]
prefixes = pool['prefixes'] prefixes = pool['prefixes']
if len(prefixes) > 1: if len(prefixes) > 1:
app.logger.warning("More than one prefixes present. Picking " app.logger.warning(_LW("More than one prefixes present. Picking "
"first one.") "first one."))
for prefix in prefixes: for prefix in prefixes:
cidr = netaddr.IPNetwork(prefix) cidr = netaddr.IPNetwork(prefix)
@ -969,8 +974,8 @@ def ipam_request_address():
allocated_address = '/'.join( allocated_address = '/'.join(
[allocated_address, str(cidr.prefixlen)]) [allocated_address, str(cidr.prefixlen)])
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during ip allocation on" app.logger.error(_LE("Error happend during ip allocation on"
"Neutron side: {0}".format(ex)) "Neutron side: {0}").format(ex))
raise raise
else: else:
# Auxiliary address or gw_address is received at network creation time. # Auxiliary address or gw_address is received at network creation time.
@ -1010,11 +1015,11 @@ def ipam_release_pool():
try: try:
app.neutron.delete_subnetpool(pool_id) app.neutron.delete_subnetpool(pool_id)
except n_exceptions.Conflict as ex: except n_exceptions.Conflict as ex:
app.logger.info("The subnetpool with ID {0} is still in use." app.logger.info(_LI("The subnetpool with ID {0} is still in use."
" It can't be deleted for now.".format(pool_id)) " It can't be deleted for now.").format(pool_id))
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened during deleting a " app.logger.error(_LE("Error happend during deleting a "
"Neutron subnetpool: {0}".format(ex)) "Neutron subnetpool: {0}").format(ex))
raise raise
return flask.jsonify(constants.SCHEMA['SUCCESS']) return flask.jsonify(constants.SCHEMA['SUCCESS'])
@ -1080,8 +1085,8 @@ def ipam_release_address():
for port in filtered_ports: for port in filtered_ports:
app.neutron.delete_port(port['id']) app.neutron.delete_port(port['id'])
except n_exceptions.NeutronClientException as ex: except n_exceptions.NeutronClientException as ex:
app.logger.error("Error happened while fetching and deleting port, " app.logger.error(_LE("Error happend while fetching and deleting port, "
"{0}".format(ex)) "{0}").format(ex))
raise raise
return flask.jsonify(constants.SCHEMA['SUCCESS']) return flask.jsonify(constants.SCHEMA['SUCCESS'])

View File

@ -18,6 +18,7 @@ import traceback
import flask import flask
import jsonschema import jsonschema
from neutronclient.common import exceptions as n_exceptions from neutronclient.common import exceptions as n_exceptions
from neutronclient.neutron import client from neutronclient.neutron import client
from neutronclient.v2_0 import client as client_v2 from neutronclient.v2_0 import client as client_v2
@ -25,6 +26,7 @@ from oslo_concurrency import processutils
from oslo_config import cfg from oslo_config import cfg
from werkzeug import exceptions as w_exceptions from werkzeug import exceptions as w_exceptions
from kuryr._i18n import _LE
from kuryr.common import exceptions from kuryr.common import exceptions
DOCKER_NETNS_BASE = '/var/run/docker/netns' DOCKER_NETNS_BASE = '/var/run/docker/netns'
@ -67,7 +69,7 @@ def make_json_app(import_name, **kwargs):
@app.errorhandler(jsonschema.ValidationError) @app.errorhandler(jsonschema.ValidationError)
@app.errorhandler(processutils.ProcessExecutionError) @app.errorhandler(processutils.ProcessExecutionError)
def make_json_error(ex): def make_json_error(ex):
app.logger.error("Unexpected error happened: {0}".format(ex)) app.logger.error(_LE("Unexpected error happened: {0}").format(ex))
traceback.print_exc(file=sys.stderr) traceback.print_exc(file=sys.stderr)
response = flask.jsonify({"Err": str(ex)}) response = flask.jsonify({"Err": str(ex)})
response.status_code = w_exceptions.InternalServerError.code response.status_code = w_exceptions.InternalServerError.code