neutron-lib: complete usage of helpers
Add support for:
- get_random_string
- camelize
- round_val
- safe_decode_utf8
Change-Id: I3b5ccbbcfa18aa488b1772f4ae9a591eb494de59
This commit is contained in:
@@ -17,6 +17,7 @@ import signal
|
|||||||
import eventlet
|
import eventlet
|
||||||
import eventlet.event
|
import eventlet.event
|
||||||
import eventlet.queue
|
import eventlet.queue
|
||||||
|
from neutron_lib.utils import helpers
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
|
|
||||||
from neutron._i18n import _, _LE
|
from neutron._i18n import _, _LE
|
||||||
@@ -225,7 +226,7 @@ class AsyncProcess(object):
|
|||||||
def _read(self, stream, queue):
|
def _read(self, stream, queue):
|
||||||
data = stream.readline()
|
data = stream.readline()
|
||||||
if data:
|
if data:
|
||||||
data = common_utils.safe_decode_utf8(data.strip())
|
data = helpers.safe_decode_utf8(data.strip())
|
||||||
queue.put(data)
|
queue.put(data)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import eventlet
|
|||||||
from eventlet.green import subprocess
|
from eventlet.green import subprocess
|
||||||
from eventlet import greenthread
|
from eventlet import greenthread
|
||||||
from neutron_lib import constants
|
from neutron_lib import constants
|
||||||
|
from neutron_lib.utils import helpers
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from oslo_rootwrap import client
|
from oslo_rootwrap import client
|
||||||
@@ -126,8 +127,8 @@ def execute(cmd, process_input=None, addl_env=None,
|
|||||||
_stdout, _stderr = obj.communicate(_process_input)
|
_stdout, _stderr = obj.communicate(_process_input)
|
||||||
returncode = obj.returncode
|
returncode = obj.returncode
|
||||||
obj.stdin.close()
|
obj.stdin.close()
|
||||||
_stdout = utils.safe_decode_utf8(_stdout)
|
_stdout = helpers.safe_decode_utf8(_stdout)
|
||||||
_stderr = utils.safe_decode_utf8(_stderr)
|
_stderr = helpers.safe_decode_utf8(_stderr)
|
||||||
|
|
||||||
extra_ok_codes = extra_ok_codes or []
|
extra_ok_codes = extra_ok_codes or []
|
||||||
if returncode and returncode not in extra_ok_codes:
|
if returncode and returncode not in extra_ok_codes:
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ import os
|
|||||||
|
|
||||||
from eventlet.green import subprocess
|
from eventlet.green import subprocess
|
||||||
from eventlet import greenthread
|
from eventlet import greenthread
|
||||||
|
from neutron_lib.utils import helpers
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from oslo_utils import encodeutils
|
from oslo_utils import encodeutils
|
||||||
|
|
||||||
@@ -57,8 +58,8 @@ def execute(cmd, process_input=None, addl_env=None,
|
|||||||
obj, cmd = create_process(cmd, addl_env=addl_env)
|
obj, cmd = create_process(cmd, addl_env=addl_env)
|
||||||
_stdout, _stderr = obj.communicate(_process_input)
|
_stdout, _stderr = obj.communicate(_process_input)
|
||||||
obj.stdin.close()
|
obj.stdin.close()
|
||||||
_stdout = utils.safe_decode_utf8(_stdout)
|
_stdout = helpers.safe_decode_utf8(_stdout)
|
||||||
_stderr = utils.safe_decode_utf8(_stderr)
|
_stderr = helpers.safe_decode_utf8(_stderr)
|
||||||
|
|
||||||
m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdin: %(stdin)s\n"
|
m = _("\nCommand: %(cmd)s\nExit code: %(code)s\nStdin: %(stdin)s\n"
|
||||||
"Stdout: %(stdout)s\nStderr: %(stderr)s") % \
|
"Stdout: %(stdout)s\nStderr: %(stderr)s") % \
|
||||||
|
|||||||
@@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
"""Utilities and helper functions."""
|
"""Utilities and helper functions."""
|
||||||
|
|
||||||
import decimal
|
|
||||||
import functools
|
import functools
|
||||||
import importlib
|
import importlib
|
||||||
import os
|
import os
|
||||||
@@ -167,11 +166,10 @@ def get_random_mac(base_mac):
|
|||||||
return ':'.join(["%02x" % x for x in mac])
|
return ':'.join(["%02x" % x for x in mac])
|
||||||
|
|
||||||
|
|
||||||
|
@removals.remove(
|
||||||
|
message="Use get_random_string from neutron_lib.utils.helpers")
|
||||||
def get_random_string(length):
|
def get_random_string(length):
|
||||||
"""Get a random hex string of the specified length.
|
return helpers.get_random_string(length)
|
||||||
"""
|
|
||||||
|
|
||||||
return "{0:0{1}x}".format(random.getrandbits(length * 4), length)
|
|
||||||
|
|
||||||
|
|
||||||
def get_dhcp_agent_device_id(network_id, host):
|
def get_dhcp_agent_device_id(network_id, host):
|
||||||
@@ -329,15 +327,16 @@ class DelayedStringRenderer(object):
|
|||||||
return str(self.function(*self.args, **self.kwargs))
|
return str(self.function(*self.args, **self.kwargs))
|
||||||
|
|
||||||
|
|
||||||
|
@removals.remove(
|
||||||
|
message="Use camelize from neutron_lib.utils.helpers")
|
||||||
def camelize(s):
|
def camelize(s):
|
||||||
return ''.join(s.replace('_', ' ').title().split())
|
return helpers.camelize(s)
|
||||||
|
|
||||||
|
|
||||||
|
@removals.remove(
|
||||||
|
message="Use round_val from neutron_lib.utils.helpers")
|
||||||
def round_val(val):
|
def round_val(val):
|
||||||
# we rely on decimal module since it behaves consistently across Python
|
return helpers.round_val(val)
|
||||||
# versions (2.x vs. 3.x)
|
|
||||||
return int(decimal.Decimal(val).quantize(decimal.Decimal('1'),
|
|
||||||
rounding=decimal.ROUND_HALF_UP))
|
|
||||||
|
|
||||||
|
|
||||||
@removals.remove(
|
@removals.remove(
|
||||||
@@ -375,10 +374,10 @@ def load_class_by_alias_or_classname(namespace, name):
|
|||||||
return class_to_load
|
return class_to_load
|
||||||
|
|
||||||
|
|
||||||
|
@removals.remove(
|
||||||
|
message="Use safe_decode_utf8 from neutron_lib.utils.helpers")
|
||||||
def safe_decode_utf8(s):
|
def safe_decode_utf8(s):
|
||||||
if six.PY3 and isinstance(s, bytes):
|
return helpers.safe_decode_utf8(s)
|
||||||
return s.decode('utf-8', 'surrogateescape')
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
def _hex_format(port, mask=0):
|
def _hex_format(port, mask=0):
|
||||||
@@ -857,7 +856,7 @@ def get_related_rand_names(prefixes, max_length=None):
|
|||||||
_("'max_length' must be longer than all prefixes"))
|
_("'max_length' must be longer than all prefixes"))
|
||||||
else:
|
else:
|
||||||
length = 8
|
length = 8
|
||||||
rndchrs = get_random_string(length)
|
rndchrs = helpers.get_random_string(length)
|
||||||
return [p + rndchrs for p in prefixes]
|
return [p + rndchrs for p in prefixes]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -17,13 +17,13 @@ import abc
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from neutron_lib import constants
|
from neutron_lib import constants
|
||||||
|
from neutron_lib.utils import helpers
|
||||||
from oslo_utils import versionutils
|
from oslo_utils import versionutils
|
||||||
from oslo_versionedobjects import base as obj_base
|
from oslo_versionedobjects import base as obj_base
|
||||||
from oslo_versionedobjects import exception
|
from oslo_versionedobjects import exception
|
||||||
from oslo_versionedobjects import fields as obj_fields
|
from oslo_versionedobjects import fields as obj_fields
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from neutron.common import utils
|
|
||||||
from neutron.db import api as db_api
|
from neutron.db import api as db_api
|
||||||
from neutron.db.qos import models as qos_db_model
|
from neutron.db.qos import models as qos_db_model
|
||||||
from neutron.objects import base
|
from neutron.objects import base
|
||||||
@@ -37,7 +37,7 @@ def get_rules(context, qos_policy_id):
|
|||||||
all_rules = []
|
all_rules = []
|
||||||
with db_api.autonested_transaction(context.session):
|
with db_api.autonested_transaction(context.session):
|
||||||
for rule_type in qos_consts.VALID_RULE_TYPES:
|
for rule_type in qos_consts.VALID_RULE_TYPES:
|
||||||
rule_cls_name = 'Qos%sRule' % utils.camelize(rule_type)
|
rule_cls_name = 'Qos%sRule' % helpers.camelize(rule_type)
|
||||||
rule_cls = getattr(sys.modules[__name__], rule_cls_name)
|
rule_cls = getattr(sys.modules[__name__], rule_cls_name)
|
||||||
|
|
||||||
rules = rule_cls.get_objects(context, qos_policy_id=qos_policy_id)
|
rules = rule_cls.get_objects(context, qos_policy_id=qos_policy_id)
|
||||||
|
|||||||
@@ -16,12 +16,12 @@
|
|||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
from neutron_lib.utils import helpers
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
import six
|
import six
|
||||||
|
|
||||||
from neutron._i18n import _, _LE, _LW
|
from neutron._i18n import _, _LE, _LW
|
||||||
from neutron.agent.linux import ip_link_support
|
from neutron.agent.linux import ip_link_support
|
||||||
from neutron.common import utils
|
|
||||||
from neutron.plugins.ml2.drivers.mech_sriov.agent.common \
|
from neutron.plugins.ml2.drivers.mech_sriov.agent.common \
|
||||||
import exceptions as exc
|
import exceptions as exc
|
||||||
from neutron.plugins.ml2.drivers.mech_sriov.agent import pci_lib
|
from neutron.plugins.ml2.drivers.mech_sriov.agent import pci_lib
|
||||||
@@ -183,7 +183,7 @@ class EmbSwitch(object):
|
|||||||
if rate_kbps > 0 and rate_kbps < 1000:
|
if rate_kbps > 0 and rate_kbps < 1000:
|
||||||
rate_mbps = 1
|
rate_mbps = 1
|
||||||
else:
|
else:
|
||||||
rate_mbps = utils.round_val(rate_kbps / 1000.0)
|
rate_mbps = helpers.round_val(rate_kbps / 1000.0)
|
||||||
|
|
||||||
log_dict = {
|
log_dict = {
|
||||||
'rate_mbps': rate_mbps,
|
'rate_mbps': rate_mbps,
|
||||||
|
|||||||
Reference in New Issue
Block a user