neutron_lib.utils package

neutron_lib.utils package

Submodules

neutron_lib.utils.file module

neutron_lib.utils.file.ensure_dir(dir_path)

Ensure a directory with 755 permissions mode.

Parameters:dir_path – The directory path to ensure.
Returns:None.
Raises:OSError – If the underlying call to makedirs raises an OSError other than EEXIST.
neutron_lib.utils.file.replace_file(file_name, data, file_mode=420)

Replaces the contents of file_name with data in a safe manner.

First write to a temp file and then rename. Since POSIX renames are atomic, the file is unlikely to be corrupted by competing writes.

We create the tempfile on the same device to ensure that it can be renamed.

Parameters:
  • file_name – Path to the file to replace.
  • data – The data to write to the file.
  • file_mode – The mode to use for the replaced file.
Returns:

None.

neutron_lib.utils.helpers module

neutron_lib.utils.helpers.camelize(s)

Camelize a str that uses _ as a camelize token.

Parameters:s – The str to camelize that contains a _ at each index where a new camelized word starts.
Returns:The camelized str.
neutron_lib.utils.helpers.compare_elements(a, b)

Compare elements if a and b have same elements.

This method doesn’t consider ordering.

Parameters:
  • a – The first item to compare.
  • b – The second item to compare.
Returns:

True if a and b have the same elements, False otherwise.

neutron_lib.utils.helpers.dict2str(dic)

Build a str representation of a dict.

Parameters:dic – The dict to build a str representation for.
Returns:The dict in str representation that is a k=v command list for each item in dic.
neutron_lib.utils.helpers.dict2tuple(d)

Build a tuple from a dict.

Parameters:d – The dict to coherence into a tuple.
Returns:The dict d in tuple form.
neutron_lib.utils.helpers.diff_list_of_dict(old_list, new_list)

Given 2 lists of dicts, return a tuple containing the diff.

Parameters:
  • old_list – The old list of dicts to diff.
  • new_list – The new list of dicts to diff.
Returns:

A tuple where the first item is a list of the added dicts in the diff and the second item is the removed dicts.

neutron_lib.utils.helpers.get_random_string(length)

Get a random hex string of the specified length.

Parameters:length – The length for the hex string.
Returns:A random hex string of the said length.
neutron_lib.utils.helpers.parse_mappings(mapping_list, unique_values=True, unique_keys=True)

Parse a list of mapping strings into a dictionary.

Parameters:
  • mapping_list – A list of strings of the form ‘<key>:<value>’.
  • unique_values – Values must be unique if True.
  • unique_keys – Keys must be unique if True, else implies that keys and values are not unique.
Returns:

A dict mapping keys to values or to list of values.

Raises:

ValueError – Upon malformed data or duplicate keys.

neutron_lib.utils.helpers.round_val(val)

Round the value.

Parameters:val – The value to round.
Returns:The value rounded using the half round up scheme.
neutron_lib.utils.helpers.safe_decode_utf8(s)

Safe decode a str from UTF.

Parameters:s – The str to decode.
Returns:The decoded str.
neutron_lib.utils.helpers.safe_sort_key(value)

Return value hash or build one for dictionaries.

Parameters:value – The value to build a hash for.
Returns:The value sorted.
neutron_lib.utils.helpers.str2dict(string)

Parse a str representation of a dict into its dict form.

This is the inverse of dict2str()

Parameters:string – The string to parse.
Returns:A dict constructed from the str representation in string.

neutron_lib.utils.host module

neutron_lib.utils.host.cpu_count()

Get the system CPU count.

Returns:The number of CPUs on the system as an int. If there’s an issue fetching the CPU count on the system, a default value of 1 is returned.

neutron_lib.utils.net module

neutron_lib.utils.net.get_hostname()

Get the hostname of the system.

Returns:The hostname of the system.
neutron_lib.utils.net.get_random_mac(base_mac)

Get a random MAC address string of the specified base format.

Any part that is ‘00’ will be randomized

Parameters:base_mac – Base MAC address represented by an array of 6 strings/int
Returns:The MAC address string.
neutron_lib.utils.net.is_port_trusted(port)

Used to determine if port can be trusted not to attack network.

Trust is currently based on the device_owner field starting with ‘network:’ since we restrict who can use that in the default policy.json file.

Parameters:port – The port dict to inspect the ‘device_owner’ for.
Returns:True if the port dict’s ‘device_owner’ value starts with the networking prefix. False otherwise.

neutron_lib.utils.runtime module

neutron_lib.utils.runtime.load_class_by_alias_or_classname(namespace, name)

Load a class using stevedore alias or the class name.

Parameters:
  • namespace – The namespace where the alias is defined.
  • name – The alias or class name of the class to be loaded.
Returns:

Class if it can be loaded.

Raises:

ImportError – if class cannot be loaded.

Module contents

Creative Commons Attribution 3.0 License

Except where otherwise noted, this document is licensed under Creative Commons Attribution 3.0 License. See all OpenStack Legal Documents.