Beispiel #1
0
    def save_interfaces(self, db_device, parsed_device_config):

        def _save_acl(db_interface):

            def save(acl_name):
                try:
                    acl = ACL(interface=db_interface, name=acl_name,
                              extended=parsed_device_config['ACLs'][acl_name]['Extended'])
                except KeyError:
                    pass    # access-group enabled on interface but access-list not exist
                else:
                    acl.save()
                    rule_mass = []
                    for nbr, rule_str in enumerate(parsed_device_config['ACLs'][acl_name]['Rules'].split('|')):
                        if rule_str:
                            dic_rule = parse_rule(rule_str)
                        if dic_rule:
                            rule = ACLRule(line_number=nbr, permit=dic_rule['Permit'],
                                           protocol=dic_rule['Protocol'], ip_source=dic_rule['IP_src'],
                                           ip_src_mask=dic_rule['Src_mask'], src_port=dic_rule['Src_port'],
                                           src_operand=dic_rule['Src_operand'], ip_destination=dic_rule['IP_dst'],
                                           ip_dst_mask=dic_rule['Dst_mask'], dst_port=dic_rule['Dst_port'],
                                           dst_operand=dic_rule['Dst_operand'], acl=acl, rule_str=rule_str)
                            rule_mass.append(rule)
                    ACLRule.objects.bulk_create(rule_mass)
            if db_interface.access_group_in:
                acl_name = db_interface.access_group_in
                save(acl_name)
            if db_interface.access_group_out:
                acl_name = db_interface.access_group_out
                save(acl_name)

        for interface_name, interface_options in parsed_device_config['interfaces'].items():
            db_interface = Interface(name=interface_name,
                                     description=interface_options.get('Description'),
                                     ip_address=interface_options.get('IP'),
                                     mode_port=interface_options.get('Mode'),
                                     allowed_vlan=interface_options.get('AllowedVlan'),
                                     trunk_encapsulation=interface_options.get('TEncapsulation'),
                                     access_vlan=interface_options.get('AccessVlan'),
                                     access_group_in=interface_options.get('AccessGroupIn'),
                                     access_group_out=interface_options.get('AccessGroupOut'),
                                     name_if=interface_options.get('Name_If'),
                                     security_level=interface_options.get('SecurityLevel'),
                                     device=db_device)
            if not interface_options.get('Shutdown'):
                db_interface.shutdown = False
            else:
                db_interface.shutdown = True
            if not interface_options.get('noSwitchport'):
                db_interface.no_switchport = False
            else:
                db_interface.no_switchport = True
            db_interface.save()
            if db_interface.access_group_in or db_interface.access_group_out:
                _save_acl(db_interface)
Beispiel #2
0
def api_create_interface(request, *, name, summary, content):
    #只有管理员可以写API
    check_admin(request)
    #name, summary, content不能为空
    if not name or not name.strip():
        raise APIValueError('name', 'name cannot be empty')
    if not summary or not summary.strip():
        raise APIValueError('summart', 'summary cannot be empty')
    if not content or not content.strip():
        raise APIValueError('content', 'content cannot be empty')

    #根据传入的信息,构建一条API数据
    #logging.info("user id --------------id:%s,name:%s,image:%s,summary:%s"%(request.__user__.id, request.__user__.name, request.__user__.image, request.__user__.summary))
    interface = Interface(user_id=request.__user__.id, user_name=request.__user__.name, user_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip())
    #interface = Interface(interface_id=request.__user__.id, interface_name=request.__user__.name, interface_image=request.__user__.image, name=name.strip(), summary=summary.strip(), content=content.strip())
    #保存
    yield from interface.save()
    logging.info("save interface %s"%summary)
    return interface