def get(self, adminname): """ List admin properties """ admins = Admins() result = dict(result=admins.collection.find_one({'name': adminname}, { '_id': 0, 'password': 0, 'token': 0 })) reports = Reports() if result['result'] is not None: result['result']['reportnames'] = reports.list(adminname, unique=True) return result
def get(self, adminname, reportname): """ Updates task state in clients and returns if the task in finished """ parser = RequestParser() parser.add_argument('clientnames', type=str, help='Expected comma seperated hostnames') args = parser.parse_args() if args['clientnames'] is not None: clientnames = form2list(args['clientnames']) else: reports = Reports() clientnames = list( map( lambda x: x['clientname'], reports.collection.find({ 'adminname': adminname, 'name': reportname }))) clients, not_found = init_clients(clientnames, adminname, reportname) if len(clients) == 0: abort(400, message='clientnames: No client found in DB report') return dict( result={x.name: x.finished(task_pool) for x in tqdm(clients)})
def delete(self, adminname, reportname): """ Cancels pending tasks """ global clients parser = RequestParser() parser.add_argument('clientnames', type=str, help='Expected comma seperated hostnames') args = parser.parse_args() if args['clientnames'] is not None: clientnames = form2list(args['clientnames']) else: reports = Reports() clientnames = list( map( lambda x: x['clientname'], reports.collection.find({ 'adminname': adminname, 'name': reportname }))) clients, not_found = init_clients(clientnames, adminname, reportname) if len(clients) == 0: abort(400, message='clientnames: No client found in DB') return dict( result={x.name: x.cancel(task_pool) for x in tqdm(clients)})
def post(self, adminname, reportname): """ Performs a task for specified clients in an ultron """ parser = RequestParser() parser.add_argument('task', type=str, required=True, help='Missing task to be performed') parser.add_argument('kwargs', type=str, help='Expected JSON encoded key-value pairs') parser.add_argument('clientnames', type=str, help='Expected comma seperated hostnames') args = parser.parse_args() task = args['task'] admin = Admin(adminname) if task not in admin.allowed_tasks(): abort( 401, message='task: {}: You are not authorized to perform this task' .format(task)) if args['kwargs'] is not None: try: kwargs = dict(loads(args['kwargs'])) except Exception as e: abort( 400, message='kwargs: {}. Expected JSON encoded key-value pairs' .format(e)) else: kwargs = {} if args['clientnames'] is not None: clientnames = form2list(args['clientnames']) else: reports = Reports() clientnames = list( map( lambda x: x['clientname'], reports.collection.find({ 'adminname': adminname, 'name': reportname }))) clients, not_found = init_clients(clientnames, adminname, reportname) if len(clients) == 0: abort(400, message='clientnames: No client found in DB') return dict(result={ x.name: x.perform(task, task_pool, **kwargs) for x in tqdm(clients) })
def get(self, adminname, reportname, clientname): """ Current state of client """ reports = Reports() found = reports.collection.find_one( { 'clientname': clientname, 'adminname': adminname }, { '_id': 0, '_modelname': 0 }) return dict(result=found)
def post(self, adminname, reportname): """ Starts/loads clients for an ultron """ parser = RequestParser() parser.add_argument('clientnames', type=str, help='Expected comma seperated hostnames') parser.add_argument('props', type=str, help='Expected BSON encoded key-value pairs') parser.add_argument('published', type=int, help='Expected boolean value') args = parser.parse_args() if args['clientnames'] is not None: clientnames = form2list(args['clientnames']) else: reports = Reports() clientnames = list( map( lambda x: x['clientname'], reports.collection.find({ 'adminname': adminname, 'name': reportname }))) clients, not_found = init_clients(clientnames, adminname, reportname) if len(clients) == 0: abort(400, message='clientnames: No client found in DB report') props = {} if args['props'] is not None: try: props = dict(loads(args['props'])) except Exception as e: abort( 400, message='props: {}. Expected BSON encoded key-value pairs'. format(e)) if len(props) > 0 or args['published'] is not None: for c in tqdm(clients): if args['published'] is not None: c.published = bool(args['published']) c.props.update(props) c.save() return dict( results=list(map(lambda x: {x.name: x.dict()}, tqdm(clients))))
def get(self, adminname, reportname): parser = RequestParser() parser.add_argument('clientnames', type=str, help='Expected comma seperated hostnames') parser.add_argument('query', type=str, help='Expected JSON formatted pymongo query') parser.add_argument('projection', type=str, help='Expected JSON formatted pymongo projection') args = parser.parse_args() print('Entered get') if args['clientnames'] is not None: query = {'clientname': {'$in': form2list(args['clientnames'])}} elif args['query'] is not None: try: query = dict(loads(args['query'])) except Exception as e: abort( 400, message='query: {}. Expected JSON encoded pymongo filter'. format(e)) else: query = {} if args['projection'] is not None: try: projection = dict(loads(args['projection'])) except Exception as e: abort( 400, message= 'projection: {}. Expected JSON encoded pymongo projection'. format(e)) else: projection = {} query.update({ 'adminname': adminname, 'name': reportname, 'published': True }) projection.update({'_id': 0, '_modelname': 0, 'published': 0}) reports = Reports() return dict(results=list(reports.collection.find(query, projection)))
def delete(self, adminname, reportname): """ Deletes a report """ reports = Reports() clientnames = list( map( lambda x: x['clientname'], reports.collection.find({ 'adminname': adminname, 'name': reportname }))) clients, not_found = init_clients(clientnames, adminname, reportname) if len(clients) == 0: abort(400, message='clientnames: No client found in DB report') return dict( results=list(map(lambda x: {x.name: x.cleanup()}, tqdm(clients))))