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 _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 = "" 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 = 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)
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 = 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)
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)
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 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)
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"] = intf config["access_groups"] = [] acl_v4_config = {} acl_v6_config = {} def common_iter_code(cmd, conf): # Common code for IPV4 and IPV6 config parsing acls = [] re_cmd = cmd + " (\\S+.*)" ip_all = re.findall(re_cmd, conf) for each in ip_all: acl = {} access_grp_config = each.split(" ") acl["name"] = access_grp_config[0] acl["direction"] = access_grp_config[1] acls.append(acl) return acls if "ip" in conf: acls = common_iter_code("ip access-group", conf) acl_v4_config["afi"] = "ipv4" acl_v4_config["acls"] = acls config["access_groups"].append(acl_v4_config) if "ipv6" in conf: acls = common_iter_code("ipv6 traffic-filter", conf) acl_v6_config["afi"] = "ipv6" acl_v6_config["acls"] = acls config["access_groups"].append(acl_v6_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 {} config['name'] = intf config['access_groups'] = [] acl_v4_config = {} acl_v6_config = {} def common_iter_code(cmd, conf): # Common code for IPV4 and IPV6 config parsing acls = [] re_cmd = cmd + ' (\\S+.*)' ip_all = re.findall(re_cmd, conf) for each in ip_all: acl = {} access_grp_config = each.split(' ') acl['name'] = access_grp_config[0] acl['direction'] = access_grp_config[1] acls.append(acl) return acls if 'ip' in conf: acls = common_iter_code('ip access-group', conf) acl_v4_config['afi'] = 'ipv4' acl_v4_config['acls'] = acls config['access_groups'].append(acl_v4_config) if 'ipv6' in conf: acls = common_iter_code('ipv6 traffic-filter', conf) acl_v6_config['afi'] = 'ipv6' acl_v6_config['acls'] = acls config['access_groups'].append(acl_v6_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 {} 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)