Example #1
0
    def _clear_config(self, want, have):
        # Delete the interface config based on the want and have config
        commands = []

        if want.get('name'):
            interface_type = get_interface_type(want['name'])
            interface = 'interface ' + want['name']
        else:
            interface_type = get_interface_type(have['name'])
            interface = 'interface ' + have['name']

        if have.get('description'
                    ) and want.get('description') != have.get('description'):
            remove_command_from_config_list(interface, 'description', commands)
        if not have.get(
                'enabled') and want.get('enabled') != have.get('enabled'):
            # if enable is False set enable as True which is the default behavior
            remove_command_from_config_list(interface, 'shutdown', commands)

        if interface_type.lower() == 'gigabitethernet':
            if have.get('speed') and have.get('speed') != 'auto' and want.get(
                    'speed') != have.get('speed'):
                remove_command_from_config_list(interface, 'speed', commands)
            if have.get(
                    'duplex') and have.get('duplex') != 'auto' and want.get(
                        'duplex') != have.get('duplex'):
                remove_command_from_config_list(interface, 'duplex', commands)
            if have.get('mtu') and want.get('mtu') != have.get('mtu'):
                remove_command_from_config_list(interface, 'mtu', commands)

        return commands
Example #2
0
    def _clear_config(self, want, have):
        # Delete the interface config based on the want and have config
        commands = []

        if want.get("name"):
            interface_type = get_interface_type(want["name"])
            interface = "interface " + want["name"]
        else:
            interface_type = get_interface_type(have["name"])
            interface = "interface " + have["name"]

        if have.get("description"
                    ) and want.get("description") != have.get("description"):
            remove_command_from_config_list(interface, "description", commands)
        if not have.get(
                "enabled") and want.get("enabled") != have.get("enabled"):
            # if enable is False set enable as True which is the default behavior
            remove_command_from_config_list(interface, "shutdown", commands)

        if interface_type.lower() == "gigabitethernet":
            if (have.get("speed") and have.get("speed") != "auto"
                    and want.get("speed") != have.get("speed")):
                remove_command_from_config_list(interface, "speed", commands)
            if (have.get("duplex") and have.get("duplex") != "auto"
                    and want.get("duplex") != have.get("duplex")):
                remove_command_from_config_list(interface, "duplex", commands)
            if have.get("mtu") and want.get("mtu") != have.get("mtu"):
                remove_command_from_config_list(interface, "mtu", commands)

        return commands
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """

        config = deepcopy(spec)
        match = re.search(r"^(\S+)", conf)

        intf = match.group(1)
        if match.group(1).lower() == "preconfigure":
            match = re.search(r"^(\S+) (.*)", conf)
            if match:
                intf = match.group(2)

        if get_interface_type(intf) == "unknown":
            return {}
        # populate the facts from the configuration
        config["name"] = intf
        config["description"] = utils.parse_conf_arg(conf, "description")
        if utils.parse_conf_arg(conf, "speed"):
            config["speed"] = int(utils.parse_conf_arg(conf, "speed"))
        if utils.parse_conf_arg(conf, "mtu"):
            config["mtu"] = int(utils.parse_conf_arg(conf, "mtu"))
        config["duplex"] = utils.parse_conf_arg(conf, "duplex")
        enabled = utils.parse_conf_cmd_arg(conf, "shutdown", False)
        config["enabled"] = enabled if enabled is not None else True

        return utils.remove_empties(config)
Example #4
0
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """

        config = deepcopy(spec)
        match = re.search(r'^(\S+)', conf)

        intf = match.group(1)
        if match.group(1).lower() == "preconfigure":
            match = re.search(r'^(\S+) (.*)', conf)
            if match:
                intf = match.group(2)

        if get_interface_type(intf) == 'unknown':
            return {}
        # populate the facts from the configuration
        config['name'] = intf
        config['description'] = utils.parse_conf_arg(conf, 'description')
        if utils.parse_conf_arg(conf, 'speed'):
            config['speed'] = int(utils.parse_conf_arg(conf, 'speed'))
        if utils.parse_conf_arg(conf, 'mtu'):
            config['mtu'] = int(utils.parse_conf_arg(conf, 'mtu'))
        config['duplex'] = utils.parse_conf_arg(conf, 'duplex')
        enabled = utils.parse_conf_cmd_arg(conf, 'shutdown', False)
        config['enabled'] = enabled if enabled is not None else True

        return utils.remove_empties(config)
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        config = deepcopy(spec)
        match = re.search(r"^(\S+)", conf)

        intf = match.group(1)

        if match.group(1).lower() == "preconfigure":
            match = re.search(r"^(\S+) (.*)", conf)
            if match:
                intf = match.group(2)

        if get_interface_type(intf) == "unknown":
            return {}

        if intf.lower().startswith("gi"):
            config["name"] = intf

            # populate the facts from the configuration
            native_vlan = re.search(r"dot1q native vlan (\d+)", conf)
            if native_vlan:
                config["native_vlan"] = int(native_vlan.group(1))

            dot1q = utils.parse_conf_arg(conf, "encapsulation dot1q")
            config["q_vlan"] = []
            if dot1q:
                config["q_vlan"].append(int(dot1q.split(" ")[0]))
                if len(dot1q.split(" ")) > 1:
                    config["q_vlan"].append(int(dot1q.split(" ")[2]))

            if utils.parse_conf_cmd_arg(conf, "l2transport", True):
                config["l2transport"] = True
            if utils.parse_conf_arg(conf, "propagate"):
                config["propagate"] = True
            config["l2protocol"] = []

            cdp = utils.parse_conf_arg(conf, "l2protocol cdp")
            pvst = utils.parse_conf_arg(conf, "l2protocol pvst")
            stp = utils.parse_conf_arg(conf, "l2protocol stp")
            vtp = utils.parse_conf_arg(conf, "l2protocol vtp")
            if cdp:
                config["l2protocol"].append({"cdp": cdp})
            if pvst:
                config["l2protocol"].append({"pvst": pvst})
            if stp:
                config["l2protocol"].append({"stp": stp})
            if vtp:
                config["l2protocol"].append({"vtp": vtp})

            return utils.remove_empties(config)
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        config = deepcopy(spec)
        match = re.search(r'^(\S+)', conf)

        intf = match.group(1)

        if match.group(1).lower() == "preconfigure":
            match = re.search(r'^(\S+) (.*)', conf)
            if match:
                intf = match.group(2)

        if get_interface_type(intf) == 'unknown':
            return {}

        if intf.lower().startswith('gi'):
            config['name'] = intf

            # populate the facts from the configuration
            native_vlan = re.search(r"dot1q native vlan (\d+)", conf)
            if native_vlan:
                config["native_vlan"] = int(native_vlan.group(1))

            dot1q = utils.parse_conf_arg(conf, 'encapsulation dot1q')
            config['q_vlan'] = []
            if dot1q:
                config['q_vlan'].append(int(dot1q.split(' ')[0]))
                if len(dot1q.split(' ')) > 1:
                    config['q_vlan'].append(int(dot1q.split(' ')[2]))

            if utils.parse_conf_cmd_arg(conf, 'l2transport', True):
                config['l2transport'] = True
            if utils.parse_conf_arg(conf, 'propagate'):
                config['propagate'] = True
            config['l2protocol'] = []

            cdp = utils.parse_conf_arg(conf, 'l2protocol cdp')
            pvst = utils.parse_conf_arg(conf, 'l2protocol pvst')
            stp = utils.parse_conf_arg(conf, 'l2protocol stp')
            vtp = utils.parse_conf_arg(conf, 'l2protocol vtp')
            if cdp:
                config['l2protocol'].append({'cdp': cdp})
            if pvst:
                config['l2protocol'].append({'pvst': pvst})
            if stp:
                config['l2protocol'].append({'stp': stp})
            if vtp:
                config['l2protocol'].append({'vtp': vtp})

            return utils.remove_empties(config)
Example #7
0
    def render_config(self, spec, conf):
        """
        Render config as dictionary structure and delete keys from spec for null values
        :param spec: The facts tree, generated from the argspec
        :param conf: The configuration
        :rtype: dictionary
        :returns: The generated config
        """
        config = deepcopy(spec)
        match = re.search(r"^(\S+)", conf)

        intf = match.group(1)
        if match.group(1).lower() == "preconfigure":
            match = re.search(r"^(\S+) (.*)", conf)
            if match:
                intf = match.group(2)

        if get_interface_type(intf) == "unknown":
            return {}

        # populate the facts from the configuration
        config["name"] = intf

        # Get the configured IPV4 details
        ipv4 = []
        ipv4_all = re.findall(r"ipv4 address (\S+.*)", conf)
        for each in ipv4_all:
            each_ipv4 = dict()
            if "secondary" in each:
                each_ipv4["address"] = each.split(" secondary")[0]
                each_ipv4["secondary"] = True
            else:
                each_ipv4["address"] = each
            ipv4.append(each_ipv4)
            config["ipv4"] = ipv4

        # Get the configured IPV6 details
        ipv6 = []
        ipv6_all = re.findall(r"ipv6 address (\S+)", conf)
        for each in ipv6_all:
            each_ipv6 = dict()
            each_ipv6["address"] = each
            ipv6.append(each_ipv6)
            config["ipv6"] = ipv6

        return utils.remove_empties(config)