Commit 9a79cd84 authored by janis.streib's avatar janis.streib 🦉
Browse files

UPD: (broken): macfinder: don't use session

parent fb957165
from . import macfinder
from net_suite.model import *
from net_suite import app
from net_suite.views import api_netdb_required, get_db_conn, db
from net_suite.views import api_netdb_required, get_db_conn, db, get_current_mgr
import requests
from flask import request, jsonify
import datetime
from flask import request, jsonify, abort
import re
import collections
from . import OUI_DB
......@@ -18,7 +16,6 @@ class MacFinderJob(DBObject):
self.mac = format_mac(mac)
self.vendor = OUI_DB.get(self.mac[:8], None)
self.vlan = vlan
self.started = datetime.datetime.utcnow()
self.bcd = bcd
def poll(self, session, db, connection):
......@@ -36,14 +33,14 @@ class MacFinderJob(DBObject):
return self.status
@macfinder.route('/create_job', methods=['post'])
@macfinder.route('/job', methods=['post'])
@api_netdb_required
def create_job():
if not len(format_mac(request.json['mac'])) == 17:
return jsonify({'success': False, 'error': 'invalid_mac'}), 400
vlan_valid = False
vlans = VLAN.get_by_bcd(db, get_db_conn(), request.json['bcd'])
user = request.environ['beaker.session']['login']
user = get_current_mgr()
for vlan in vlans:
if not user.has_permission('nm.macfinder_global'):
if user.has_bcd(vlan.bcd):
......@@ -53,9 +50,6 @@ def create_job():
vlan_valid = True
if not vlan_valid:
return jsonify({'success': False, 'error': 'invalid_vlan'}), 404
s = request.environ['beaker.session']
if 'macfinder_jobs' not in s:
s['macfinder_jobs'] = dict()
created_jobs = []
for vlan in vlans:
resp = requests.post(app.config.get('MACFINDER_SERVER_URL') + '/new_task',
......@@ -66,35 +60,23 @@ def create_job():
return jsonify({'success': False, 'error': 'internal_error'}), 500
job_id = resp.json()['job_id']
created_jobs.append(job_id)
job = MacFinderJob(job_id, request.json['mac'], vlan.id, request.json['bcd'])
s['macfinder_jobs'][job_id] = job
s.save()
return jsonify({'success': True, 'job_ids': created_jobs})
@macfinder.route('/jobs')
@api_netdb_required
def jobs():
s = request.environ['beaker.session']
session = requests.Session()
for id, job in s.get('macfinder_jobs', dict()).items():
job.poll(session, db, get_db_conn())
return jsonify({'jobs': collections.OrderedDict(sorted(s.get('macfinder_jobs', dict()).items(), reverse=True,
key=lambda x: x[1].started))})
@macfinder.route('/poll/<job_id>')
@macfinder.route('/job/<job_id>')
@api_netdb_required
def poll(job_id):
s = request.environ['beaker.session']
session = requests.session()
return jsonify(s['macfinder_jobs'][job_id].poll(session, db, get_db_conn()))
return jsonify(MacFinderJob(job_id=job_id).poll(session, db, get_db_conn()))
@macfinder.route('/oui_lookup')
def oui_lookup():
mac = format_mac(request.args['mac'])
return jsonify({'mac': mac, 'vendor': OUI_DB.get(mac[:8], None)})
vendor = OUI_DB.get(mac[:8], None)
if vendor is None:
return abort(404)
return jsonify({'mac': mac, 'vendor': vendor})
def format_mac(mac):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment