예제 #1
0
def check_format_on_ifalias(ifalias):
    """Verify that format on ifalias is correct if it is defined in config"""
    if not ifalias:
        return True
    ifalias_format = CONFIG.get_ifaliasformat()
    if ifalias_format:
        ifalias_format = re.compile(ifalias_format)
        if ifalias_format.match(ifalias):
            return True
        else:
            _logger.error('Wrong format on ifalias: %s', ifalias)
            return False
    else:
        return True
예제 #2
0
파일: views.py 프로젝트: hmpf/nav
def populate_infodict(request, netbox, interfaces):
    """Populate a dictionary used in every http response"""
    allowed_vlans = []
    voice_vlan = None
    readonly = False
    handler = None

    try:
        handler = get_and_populate_livedata(netbox, interfaces)
        allowed_vlans = find_and_populate_allowed_vlans(
            request.account, netbox, interfaces, handler)
        voice_vlan = fetch_voice_vlan_for_netbox(request, handler)
        if voice_vlan:
            if CONFIG.is_cisco_voice_enabled() and is_cisco(netbox):
                set_voice_vlan_attribute_cisco(voice_vlan, interfaces, handler)
            else:
                set_voice_vlan_attribute(voice_vlan, interfaces)
        mark_detained_interfaces(interfaces)
        if CONFIG.is_dot1x_enabled():
            add_dot1x_info(interfaces, handler)
    except NoResponseError:
        readonly = True
        messages.error(
            request,
            "%s did not respond within the set timeouts. Values displayed are from database"
            % netbox.sysname,
        )
        if isinstance(handler, SNMPHandler) and not netbox.read_only:
            messages.error(request, "Read only community not set")
    except ProtocolError:
        readonly = True
        messages.error(
            request,
            "Protocol error when contacting %s. Values displayed are from database"
            % netbox.sysname,
        )
    except DeviceNotConfigurableError as error:
        readonly = True
        messages.error(request, str(error))

    if handler and not handler.is_configurable():
        add_readonly_reason(request, handler)
        readonly = True

    ifaliasformat = CONFIG.get_ifaliasformat()
    aliastemplate = ''
    if ifaliasformat:
        tmpl = get_aliastemplate()
        aliastemplate = tmpl.render({'ifaliasformat': ifaliasformat})

    save_to_database(interfaces)

    auditlog_api_parameters = {
        'object_model': 'interface',
        'object_pks': ','.join([str(i.pk) for i in interfaces]),
        'subsystem': 'portadmin',
    }

    info_dict = get_base_context([(netbox.sysname, )], form=get_form(request))
    info_dict.update({
        'handlertype':
        type(handler).__name__,
        'interfaces':
        interfaces,
        'netbox':
        netbox,
        'voice_vlan':
        voice_vlan,
        'allowed_vlans':
        allowed_vlans,
        'readonly':
        readonly,
        'aliastemplate':
        aliastemplate,
        'trunk_edit':
        CONFIG.get_trunk_edit(),
        'auditlog_api_parameters':
        json.dumps(auditlog_api_parameters),
    })
    return info_dict