Esempio n. 1
0
#  Copyright (c) 2020 Ricardo Bartels. All rights reserved.
#
#  netbox-sync.py
#
#  This work is licensed under the terms of the MIT license.
#  For a copy, see file LICENSE.txt included in this
#  repository or visit: <https://opensource.org/licenses/MIT>.

from ipaddress import ip_interface
import asyncio

import aiodns

from module.common.logging import get_logger

log = get_logger()


def normalize_mac_address(mac_address=None):
    """
    normalize a MAC address
        * format letters to upper case
        * add colons if missing

    Parameters
    ----------
    mac_address: str
        MAC address to normalize

    Returns
    -------
Esempio n. 2
0
def instantiate_sources(config_handler=None, inventory=None):
    """
    Instantiate a source handler and add necessary attributes. Also
    validate source handler pre and post initialization.

    Parameters
    ----------
    config_handler: ConfigParser
        a config file handler to read config data from
    inventory: inventory object
        inventory to be passed to source handler

    Returns
    -------
    source handler object: instantiated source handler
    """

    log = get_logger()

    if config_handler is None:
        raise Exception("No config handler defined!")

    if inventory is None:
        raise Exception("No inventory defined!")

    # first validate all available sources
    for possible_source_class in valid_sources:
        validate_source(possible_source_class)

    sources = list()

    # iterate over sources and validate them
    for source_section in config_handler.sections():

        # a source section needs to start with "source/"
        if not source_section.startswith("source/"):
            continue

        # get type of source
        source_type = config_handler.get(source_section, "type", fallback=None)

        if source_type is None:
            log.error(f"Source {source_section} option 'type' is undefined")

        source_class = None
        for possible_source_class in valid_sources:
            validate_source(possible_source_class)
            source_class_type = getattr(possible_source_class, "source_type",
                                        None)
            if source_class_type is None:
                raise AttributeError(
                    "'%s' class attribute 'source_type' not defined." %
                    source_class_type.__name__)

            if source_class_type == source_type:
                source_class = possible_source_class
                break

        if source_class is None:
            log.error(
                f"Unknown source type '{source_type}' defined for '{source_section}'"
            )
            continue

        source_config = get_config(config_handler,
                                   section=source_section,
                                   valid_settings=source_class.settings)

        source_handler = source_class(name=source_section.replace(
            "source/", ""),
                                      inventory=inventory,
                                      settings=source_config)

        validate_source(source_handler, "post")

        # add to list of source handlers
        if source_handler.init_successful is True:
            sources.append(source_handler)

    return sources