Пример #1
0
def render_trunk_edit(request, interfaceid):
    """Controller for rendering trunk edit view"""

    config = read_config()
    interface = Interface.objects.get(pk=interfaceid)
    agent = get_factory(interface.netbox)
    if request.method == 'POST':
        try:
            handle_trunk_edit(request, agent, interface)
        except SnmpError as error:
            messages.error(request, 'Error editing trunk: %s' % error)
        else:
            messages.success(request, 'Trunk edit successful')

    account = request.account
    netbox = interface.netbox
    check_read_write(netbox, request)
    try:
        vlans = agent.get_netbox_vlans()  # All vlans on this netbox
        native_vlan, trunked_vlans = agent.get_native_and_trunked_vlans(
            interface)
    except SnmpError:
        vlans = native_vlan = trunked_vlans = allowed_vlans = None
        messages.error(request, 'Error getting trunk information')
    else:
        if should_check_access_rights(account):
            allowed_vlans = find_allowed_vlans_for_user_on_netbox(
                account, interface.netbox, agent)
        else:
            allowed_vlans = vlans

    extra_path = [(netbox.sysname,
                   reverse('portadmin-sysname',
                           kwargs={'sysname': netbox.sysname})),
                  ("Trunk %s" % interface, )]

    context = get_base_context(extra_path)
    context.update({
        'interface': interface,
        'available_vlans': vlans,
        'native_vlan': native_vlan,
        'trunked_vlans': trunked_vlans,
        'allowed_vlans': allowed_vlans,
        'trunk_edit': get_trunk_edit(config)
    })

    return render_to_response('portadmin/trunk_edit.html', context,
                              RequestContext(request))
Пример #2
0
def populate_infodict(request, netbox, interfaces):
    """Populate a dictionary used in every http response"""
    allowed_vlans = []
    voice_vlan = None
    readonly = False
    config = read_config()

    try:
        fac = get_and_populate_livedata(netbox, interfaces)
        allowed_vlans = find_and_populate_allowed_vlans(
            request.account, netbox, interfaces, fac)
        voice_vlan = fetch_voice_vlan_for_netbox(request, fac, config)
        if voice_vlan:
            if is_cisco_voice_enabled(config) and is_cisco(netbox):
                set_voice_vlan_attribute_cisco(voice_vlan, interfaces, fac)
            else:
                set_voice_vlan_attribute(voice_vlan, interfaces)
        mark_detained_interfaces(interfaces)
        if is_dot1x_enabled(config):
            add_dot1x_info(interfaces, fac)
    except TimeOutException:
        readonly = True
        messages.error(
            request, "Timeout when contacting %s. Values displayed "
            "are from database" % netbox.sysname)
        if not netbox.read_only:
            messages.error(request, "Read only community not set")
    except SnmpError:
        readonly = True
        messages.error(
            request, "SNMP error when contacting %s. Values "
            "displayed are from database" % netbox.sysname)

    if check_read_write(netbox, request):
        readonly = True

    ifaliasformat = get_ifaliasformat(config)
    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({
        'interfaces':
        interfaces,
        'netbox':
        netbox,
        'voice_vlan':
        voice_vlan,
        'allowed_vlans':
        allowed_vlans,
        'readonly':
        readonly,
        'aliastemplate':
        aliastemplate,
        'trunk_edit':
        get_trunk_edit(config),
        'auditlog_api_parameters':
        json.dumps(auditlog_api_parameters)
    })
    return info_dict