def list_to_dict(self, param):
     if param:
         for _k, val in iteritems(param):
             val["name"] = normalize_interface(val["name"])
             if "ipv4" in val:
                 temp = {}
                 for each in val["ipv4"]:
                     if (each.get("address")
                             and each.get("address") != "dhcp"):
                         temp.update({each["address"]: each})
                     elif each.get("address") == "dhcp":
                         # deprecated attribute
                         temp.update({
                             "dhcp": {
                                 "dhcp": {
                                     "client_id": each.get("dhcp_client"),
                                     "hostname": each.get("dhcp_hostname"),
                                 }
                             }
                         })
                     if not each.get("address"):
                         temp.update({list(each.keys())[0]: each})
                 val["ipv4"] = temp
             if "ipv6" in val:
                 temp = {}
                 for each in val["ipv6"]:
                     if each.get("address"):
                         each["address"] = each["address"].lower()
                         temp.update({each["address"]: each})
                     if not each.get("address"):
                         temp.update({list(each.keys())[0]: each})
                 val["ipv6"] = temp
    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 get_interface_type(intf) == "unknown":
            return {}

        config["name"] = normalize_interface(intf)
        port_priority = utils.parse_conf_arg(conf, "lacp port-priority")
        max_bundle = utils.parse_conf_arg(conf, "lacp max-bundle")
        if port_priority:
            config["port_priority"] = int(port_priority)
        if "lacp fast-switchover" in conf:
            config["fast_switchover"] = True
        if max_bundle:
            config["max_bundle"] = int(max_bundle)

        return utils.remove_empties(config)
Exemple #3
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 = ""
        if match:
            intf = match.group(1)

        if get_interface_type(intf) == "unknown":
            return {}
        if intf.lower().startswith("gi"):
            config["name"] = normalize_interface(intf)
            receive = utils.parse_conf_arg(conf, "Rx:")
            transmit = utils.parse_conf_arg(conf, "Tx:")

            if receive == "enabled":
                config["receive"] = True
            elif receive == "disabled":
                config["receive"] = False
            if transmit == "enabled":
                config["transmit"] = True
            elif transmit == "disabled":
                config["transmit"] = False

        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 = ''
        if match:
            intf = match.group(1)

        if get_interface_type(intf) == 'unknown':
            return {}
        if intf.lower().startswith('gi'):
            config['name'] = normalize_interface(intf)
            receive = utils.parse_conf_arg(conf, 'Rx:')
            transmit = utils.parse_conf_arg(conf, 'Tx:')

            if receive == 'enabled':
                config['receive'] = True
            elif receive == 'disabled':
                config['receive'] = False
            if transmit == 'enabled':
                config['transmit'] = True
            elif transmit == 'disabled':
                config['transmit'] = False

        return utils.remove_empties(config)
Exemple #5
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 get_interface_type(intf) == 'unknown':
            return {}
        # populate the facts from the configuration
        config['name'] = normalize_interface(intf)
        config['description'] = utils.parse_conf_arg(conf, 'description')
        config['speed'] = 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)
Exemple #6
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 get_interface_type(intf) == "unknown":
            return {}
        # populate the facts from the configuration
        config["name"] = normalize_interface(intf)
        config["description"] = utils.parse_conf_arg(conf, "description")
        config["speed"] = 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)
Exemple #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 get_interface_type(intf) == "unknown":
            return {}

        if intf.upper()[:2] in (
                "HU",
                "FO",
                "TW",
                "TE",
                "GI",
                "FA",
                "ET",
                "PO",
        ):
            # populate the facts from the configuration
            config["name"] = normalize_interface(intf)
            has_mode = utils.parse_conf_arg(conf, "switchport mode")
            if has_mode:
                config["mode"] = has_mode
            has_access = utils.parse_conf_arg(conf, "switchport access vlan")
            if has_access:
                config["access"] = {"vlan": int(has_access)}

            has_voice = utils.parse_conf_arg(conf, "switchport voice vlan")
            if has_voice:
                config["voice"] = {"vlan": int(has_voice)}

            trunk = dict()
            trunk["encapsulation"] = utils.parse_conf_arg(
                conf, "switchport trunk encapsulation")
            native_vlan = utils.parse_conf_arg(conf, "native vlan")
            if native_vlan:
                trunk["native_vlan"] = int(native_vlan)
            allowed_vlan = utils.parse_conf_arg(conf, "allowed vlan")
            if allowed_vlan:
                trunk["allowed_vlans"] = allowed_vlan.split(",")
            allowed_vlan_add_all = re.findall("allowed vlan add.*", conf)
            if allowed_vlan_add_all:
                for each in allowed_vlan_add_all:
                    trunk["allowed_vlans"].extend(
                        each.split("allowed vlan add ")[1].split(","))
            pruning_vlan = utils.parse_conf_arg(conf, "pruning vlan")
            if pruning_vlan:
                trunk["pruning_vlans"] = pruning_vlan.split(",")

            config["trunk"] = trunk

        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 get_interface_type(intf) == "unknown":
            return {}
        # populate the facts from the configuration
        config["name"] = normalize_interface(intf)

        ipv4 = []
        ipv4_all = re.findall(r"ip address (\S+.*)", conf)
        for each in ipv4_all:
            each_ipv4 = dict()
            if "secondary" not in each and "dhcp" not in each:
                each_ipv4["address"] = each
            elif "secondary" in each:
                each_ipv4["address"] = each.split(" secondary")[0]
                each_ipv4["secondary"] = True
            elif "dhcp" in each:
                each_ipv4["address"] = "dhcp"
                if "client-id" in each:
                    each_ipv4["dhcp_client"] = int(
                        each.split(" hostname ")[0].split("/")[-1])
                if "hostname" in each:
                    each_ipv4["dhcp_hostname"] = each.split(" hostname ")[-1]
                if "client-id" in each and each_ipv4["dhcp_client"] is None:
                    each_ipv4["dhcp_client"] = int(each.split("/")[-1])
                if "hostname" in each and not each_ipv4["dhcp_hostname"]:
                    each_ipv4["dhcp_hostname"] = each.split(" hostname ")[-1]
            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()
            if "autoconfig" in each:
                each_ipv6["autoconfig"] = True
            if "dhcp" in each:
                each_ipv6["dhcp"] = True
            each_ipv6["address"] = each.lower()
            ipv6.append(each_ipv6)
        config["ipv6"] = ipv6

        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 get_interface_type(intf) == 'unknown':
            return {}
        # populate the facts from the configuration
        config['name'] = normalize_interface(intf)

        ipv4 = []
        ipv4_all = re.findall(r"ip address (\S+.*)", conf)
        for each in ipv4_all:
            each_ipv4 = dict()
            if 'secondary' not in each and 'dhcp' not in each:
                each_ipv4['address'] = each
            elif 'secondary' in each:
                each_ipv4['address'] = each.split(' secondary')[0]
                each_ipv4['secondary'] = True
            elif 'dhcp' in each:
                each_ipv4['address'] = 'dhcp'
                if 'client-id' in each:
                    each_ipv4['dhcp_client'] = int(each.split(' hostname ')[0].split('/')[-1])
                if 'hostname' in each:
                    each_ipv4["dhcp_hostname"] = each.split(' hostname ')[-1]
                if 'client-id' in each and each_ipv4['dhcp_client'] is None:
                    each_ipv4['dhcp_client'] = int(each.split('/')[-1])
                if 'hostname' in each and not each_ipv4["dhcp_hostname"]:
                    each_ipv4["dhcp_hostname"] = each.split(' hostname ')[-1]
            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()
            if 'autoconfig' in each:
                each_ipv6['autoconfig'] = True
            if 'dhcp' in each:
                each_ipv6['dhcp'] = True
            each_ipv6['address'] = each.lower()
            ipv6.append(each_ipv6)
        config['ipv6'] = ipv6

        return utils.remove_empties(config)
    def set_config(self, existing_acl_interfaces_facts):
        """ Collect the configuration from the args passed to the module,
            collect the current configuration (as a dict from facts)
        :rtype: A list
        :returns: the commands necessary to migrate the current configuration
                  to the deisred configuration
        """
        want = self._module.params["config"]
        if want:
            for item in want:
                item["name"] = normalize_interface(item["name"])

        have = existing_acl_interfaces_facts
        resp = self.set_state(want, have)
        return to_list(resp)
 def set_config(self, existing_l3_interfaces_facts):
     """ Collect the configuration from the args passed to the module,
         collect the current configuration (as a dict from facts)
     :rtype: A list
     :returns: the commands necessary to migrate the current configuration
               to the desired configuration
     """
     config = self._module.params.get("config")
     want = []
     if config:
         for each in config:
             each.update({"name": normalize_interface(each["name"])})
             want.append(each)
     have = existing_l3_interfaces_facts
     resp = self.set_state(want, have)
     return to_list(resp)
Exemple #12
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 get_interface_type(intf) == 'unknown':
            return {}

        if intf.upper()[:2] in ('HU', 'FO', 'TW', 'TE', 'GI', 'FA', 'ET',
                                'PO'):
            # populate the facts from the configuration
            config['name'] = normalize_interface(intf)
            has_mode = utils.parse_conf_arg(conf, 'switchport mode')
            if has_mode:
                config['mode'] = has_mode
            has_access = utils.parse_conf_arg(conf, 'switchport access vlan')
            if has_access:
                config["access"] = {"vlan": int(has_access)}

            has_voice = utils.parse_conf_arg(conf, 'switchport voice vlan')
            if has_voice:
                config["voice"] = {"vlan": int(has_voice)}

            trunk = dict()
            trunk["encapsulation"] = utils.parse_conf_arg(
                conf, 'encapsulation')
            native_vlan = utils.parse_conf_arg(conf, 'native vlan')
            if native_vlan:
                trunk["native_vlan"] = int(native_vlan)
            allowed_vlan = utils.parse_conf_arg(conf, 'allowed vlan')
            if allowed_vlan:
                trunk["allowed_vlans"] = allowed_vlan.split(',')
            pruning_vlan = utils.parse_conf_arg(conf, 'pruning vlan')
            if pruning_vlan:
                trunk['pruning_vlans'] = pruning_vlan.split(',')

            config['trunk'] = trunk

        return utils.remove_empties(config)
Exemple #13
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 get_interface_type(intf) == 'unknown':
            return {}
        member_config = {}
        channel_group = utils.parse_conf_arg(conf, 'channel-group')
        if intf.startswith('Gi'):
            config['name'] = intf
            config['members'] = []
            if channel_group:
                channel_group = channel_group.split(' ')
                id = channel_group[0]
                config['name'] = 'Port-channel{0}'.format(str(id))
                if 'mode' in channel_group:
                    mode = channel_group[2]
                    member_config.update({'mode': mode})
                if 'link' in channel_group:
                    link = channel_group[2]
                    member_config.update({'link': link})
            if member_config.get('mode') or member_config.get('link'):
                member_config['member'] = normalize_interface(intf)
                config['members'].append(member_config)

        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 get_interface_type(intf) == "unknown":
            return {}
        member_config = {}
        channel_group = utils.parse_conf_arg(conf, "channel-group")
        if intf.startswith("Gi"):
            config["name"] = intf
            config["members"] = []
            if channel_group:
                channel_group = channel_group.split(" ")
                id = channel_group[0]
                config["name"] = "Port-channel{0}".format(str(id))
                if "mode" in channel_group:
                    mode = channel_group[2]
                    member_config.update({"mode": mode})
                if "link" in channel_group:
                    link = channel_group[2]
                    member_config.update({"link": link})
            if member_config.get("mode") or member_config.get("link"):
                member_config["member"] = normalize_interface(intf)
                config["members"].append(member_config)

        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 get_interface_type(intf) == "unknown":
            return {}

        if intf.upper()[:2] in (
                "HU",
                "FO",
                "FI",
                "TW",
                "TE",
                "GI",
                "FA",
                "ET",
                "PO",
        ):
            # populate the facts from the configuration
            config["name"] = normalize_interface(intf)
            has_mode = utils.parse_conf_arg(conf, "switchport mode")
            if has_mode:
                config["mode"] = has_mode
            has_access = utils.parse_conf_arg(conf, "switchport access vlan")
            if has_access and has_access != "dynamic":
                # switchport access vlan dynamic
                # the above being a valid config is not configurable from CLI
                # being enabled via a VLAN Membership Policy Server it is used
                # to assign switch ports to VLANs dynamically, based on the
                # source MAC address of the device connected to the port
                if len(list(has_access.split(" "))) == 2 and has_access.split(
                        " ")[0] == "name":
                    config["access"] = {"vlan_name": has_access.split(" ")[1]}
                else:
                    config["access"] = {"vlan": int(has_access)}

            has_voice = utils.parse_conf_arg(conf, "switchport voice vlan")
            if has_voice:
                if len(list(has_voice.split(" "))) == 2 and has_voice.split(
                        " ")[0] == "name":
                    config["voice"] = {"vlan_name": has_voice.split(" ")[1]}
                elif type(has_voice) != int:
                    config["voice"] = {"vlan_tag": has_voice}
                else:
                    config["voice"] = {"vlan": int(has_voice)}

            trunk = dict()
            trunk["encapsulation"] = utils.parse_conf_arg(
                conf,
                "switchport trunk encapsulation",
            )
            native_vlan = utils.parse_conf_arg(conf, "native vlan")
            if native_vlan:
                trunk["native_vlan"] = int(native_vlan)
            allowed_vlan = utils.parse_conf_arg(conf, "allowed vlan")
            if allowed_vlan:
                trunk["allowed_vlans"] = allowed_vlan.split(",")
            allowed_vlan_add_all = re.findall("allowed vlan add.*", conf)
            if allowed_vlan_add_all:
                for each in allowed_vlan_add_all:
                    trunk["allowed_vlans"].extend(
                        each.split("allowed vlan add ")[1].split(","), )
            pruning_vlan = utils.parse_conf_arg(conf, "pruning vlan")
            if pruning_vlan:
                trunk["pruning_vlans"] = pruning_vlan.split(",")

            config["trunk"] = trunk

        return utils.remove_empties(config)
Exemple #16
0
 def normalize_interface_names(self, param):
     if param:
         for _k, val in iteritems(param):
             val["name"] = normalize_interface(val["name"])
     return param