def post(self): if request.json: parent_id = request.json.get('parentId') name = request.json.get('snapshotName', '') if not parent_id: return application.make_fail_response('parent_id is required!') context.application.data_manager.import_snapshot(parent_id, name) elif request.files: parent_id = request.form.get('parent_id') if request.form else '' if not parent_id: return application.make_fail_response('parent_id is required!') stream = request.files['file'] if not stream: return application.make_fail_response('file is required!') filename = stream.filename or str(uuid.uuid4()) path = application._cm.ROOT / 'snapshot' / filename if path.suffix != '.lb': return application.make_fail_response( f'Unknown file type `.{path.suffix}`, `.lb` is required!') stream.save(str(path)) application.snapshot_import_uri = f'file://{str(path)}' name = path.stem context.application.data_manager.import_snapshot(parent_id, name, path=path) return application.make_ok_response()
def delete(self): label_id = request.json.get('id') if not label_id: return application.make_fail_response('Label id is required!') if not label_id in application.labels.label_map: return application.make_fail_response(f'Label {label_id} not found!') application.labels.delete_label(label_id) return context.make_ok_response()
def put(self): label = request.json.get('label') label_id = label.get('id') if not label_id: return application.make_fail_response('Label id is required!') if not label_id in application.labels.label_map: return application.make_fail_response(f'Label {label.get("name")} not found!') application.labels.update_label(label) return context.make_ok_response()
def post(self): label = request.json.get('label') required_key = ['name'] missed_required_key = [key for key in required_key if not label.get(key)] if missed_required_key: return application.make_fail_response(f'Label {" ".join(missed_required_key)} is required!') label_id = application.labels._get_label_name_md5(label) if label_id in application.labels.label_map: return application.make_fail_response(f'Label {label["name"]} existed!') application.labels.create_label(label) return context.make_ok_response()
def issue(): issue_req = request.json template_info = issue_req['template'] template = template_loader.get_template(template_info['path']) issue_data = issue_req['issue'] attachments = issue_req['attachments'] snapshots = issue_req['snapshots'] all_bugit_message = '' # Export Snapshot for snapshot in snapshots: attachments.append(attachment.export_snapshot(snapshot)) # Set bugit script context context = {'issue': issue_data, 'attachments': attachments} # Set submit actions submit_action_functions = template.submit() # Do submit for action_function in submit_action_functions: try: bug_url = action_function(context) if bug_url: all_bugit_message += bug_url except Exception as e: if e.__class__.__name__ == 'BugitFormInputError': return application.make_fail_response(str(e)) error_message = traceback.format_exc() trace = "<br/>".join(error_message.split("\n")) lyrebird.report({ 'action': 'bugit.submit', 'bugit': { 'status': 'failure', 'data': issue_data, 'error': error_message } }) return application.make_fail_response( f'Script function "{action_function.__name__}" crash.<br/>{trace}' ) lyrebird.report({ 'action': 'bugit.submit', 'bugit': { 'status': 'success', 'data': issue_data } }) #Delete Snapshot attachment.remove_attach() return application.make_ok_response( message=f'Create issue success! {all_bugit_message}')
def put(self, checker_id): _checker = application.checkers.get(checker_id) if not _checker: return application.make_fail_response(f'Checker {checker_id} not found') if 'status' not in request.json: return application.make_fail_response(f'Checker {checker_id} status not found') _checker_status = request.json.get('status') if _checker_status: application.checkers[checker_id].activate() else: application.checkers[checker_id].deactivate() return application.make_ok_response()
def post(self): if not request.json.get('snapshot'): return application.make_fail_response( 'Missing required argument: snapshot') if not request.json.get('events'): return application.make_fail_response( 'Missing required argument: events') group_id, file_ = context.application.data_manager.export_from_local( request.json) req = send_file(file_) req.headers.add('SnapshotId', group_id) return req
def template(): if request.method == 'GET': templates = template_loader.template_list() selected_index = None last_selected_template = cache.get_selected_template() for index, template in enumerate(templates): if template['path'] == last_selected_template: selected_index = index return application.make_ok_response(selected_index=selected_index, templates=templates) elif request.method == 'POST': ''' Get template detail by path And save lastest_selected_template ''' if request.json and 'path' in request.json: template_path = request.json['path'] template = template_loader.get_template(template_path) # load from cache template_key = md5(template_path.encode()).hexdigest() template_detail = cache.get(template_key) # load from template if inspect.getargspec(template.form).args: template_detail = template.form({'cache': template_detail}) else: template_detail = template.form() cache.selected_template(template_path) return jsonify(template_detail) else: return application.make_fail_response( 'Need path argument for getting template detail')
def on_app_error(error): _logger.error( f'[mocker server exception]: {error.__class__.__name__} {error}\n{traceback.format_exc()}' ) return application.make_fail_response( f'{error.__class__.__name__} {error}\n{traceback.format_exc()}' )
def post(self, checker_id): _checker = application.checkers.get(checker_id) if not _checker: return application.make_fail_response(f'Checker {checker_id} not found') _checker_data = request.json.get('content') if not _checker_data: return application.make_fail_response(f'Checker {checker_id} data not found') origin_checker_detail = _checker.read() try: _checker.write(_checker_data) except Exception as e: _checker.write(origin_checker_detail) return application.make_fail_response(f'Save checker {checker_id} failure: {str(e)}') return application.make_ok_response()
def get(self, group_id=None, label=None): if group_id: _group = context.application.data_manager.id_map.get(group_id) if not _group: return context.make_fail_response('Not found group') return application.make_ok_response(data=_group) root = context.application.data_manager.root if not label: return application.make_ok_response(data=root) # update mock data tree with label groups_set = set() label_list = label.split('+') for label_id in label_list: label = application.labels.label_map.get(label_id) if not label: return application.make_fail_response(f'Label {label_id} is not found!') if not groups_set: groups_set = set(label['groups']) else: groups_set = groups_set & set(label['groups']) data_map = context.application.data_manager.make_data_map_by_group(groups_set) return application.make_ok_response(data=data_map)
def get(self, snapshot_id): link = context.application.data_manager.snapshot_import_cache.get( snapshot_id, {}).get('link') if not link: application.make_fail_response( f'No import snapshot {snapshot_id} link found!') path = context.application.data_manager.read_snapshot_from_link(link) context.application.data_manager.snapshot_import_cache[snapshot_id][ 'path'] = path detail, output_path = context.application.data_manager.get_snapshot_file_detail( path) context.application.data_manager._remove_file([output_path]) detail.pop('children') return application.make_ok_response(data=detail)
def post(self): parent_node = request.json.get("parentNode") snapshot_name = request.json.get('snapshotName', '') if "id" not in parent_node: return application.make_fail_response(msg="object has no attribute : parentNode.id") context.application.data_manager.import_snapshot(parent_node["id"], snapshot_name) return application.make_ok_response()
def put(self, mode=None): if context.Mode.contains(mode): context.application.work_mode = mode return application.make_ok_response() else: return application.make_fail_response( f'Unknown record mode: {mode}')
def ui_cache(template_key): if request.method == 'GET': data = cache.get_draft_list(template_key) return application.make_ok_response(data=data) elif request.method == 'POST': template_path = request.json.get('template_path') if not template_path: return application.make_fail_response( 'Missing required argument: template_path') cache_name = request.json.get('cache_name') if not cache_name: return application.make_fail_response( 'Missing required argument: cache_name') template_detail = request.json.get('data') if not template_detail: return application.make_fail_response( 'Missing required argument: data') for field in template_detail: if 'extraMsg' in field: del field['extraMsg'] draft_name = cache.get_filename(template_path, cache_name) cache.put(draft_name, template_detail) cache.put(f'{draft_name}{cache.DRAFT_INFO_FILE_SUFFIX}', {'cache_name': cache_name}) cache.selected_template(template_path, cache_name) return application.make_ok_response() elif request.method == 'DELETE': template_path = request.json.get('template_path') if not template_path: return application.make_fail_response( 'Missing required argument: template_path') cache_name = request.json.get('cache_name') if not cache_name: return application.make_fail_response( 'Missing required argument: cache_name') draft_name = cache.get_filename(template_path, cache_name) cache.delete(draft_name) cache.delete(f'{draft_name}{cache.DRAFT_INFO_FILE_SUFFIX}') return application.make_ok_response()
def get(self, id): for item in context.application.cache.items(): if item['id'] == id: # Import decoder for decoding the requested content display_item = {} application.encoders_decoders.decoder_handler(item, output=display_item) return application.make_ok_response(data=display_item) return application.make_fail_response(f'Request {id} not found')
def patch(self): update_conf = request.get_json() if update_conf is None or not isinstance(update_conf, dict): return application.make_fail_response( 'Request body must be a JSONObject!') if not update_conf: logger.warning( 'This request cannot modify config, request body is empty.') return application.make_ok_response() try: application._cm.override_config_field(update_conf) return application.make_ok_response() except ConfigException as e: return application.make_fail_response(str(e))
def post(self): if request.files: stream = request.files['file'] if not stream: return application.make_fail_response('Missing required file') parent_id = request.form.get('parent_id') if request.form else None if not parent_id: return application.make_fail_response( 'Missing required argument: parent_id') filename = stream.filename or str(uuid.uuid4()) path = context.application.data_manager.snapshot_workspace / filename stream.save(str(path)) group_id = context.application.data_manager.import_from_file( parent_id, path) return application.make_ok_response(group_id=group_id) elif request.json: parent_id = request.json.get('parentId') name = request.json.get('snapshotName', '') if not parent_id: return application.make_fail_response( 'Missing required argument: parent_id') snapshot_id = request.json.get('snapshotId') snapshot_info = context.application.data_manager.snapshot_import_cache.get( snapshot_id) if not snapshot_info: return application.make_fail_response( f'Snapshot cache {snapshot_id} not found!') path = snapshot_info.get('path') if not Path(path).exists(): link = snapshot_info.get('link') path = context.application.data_manager.read_snapshot_from_link( link) context.application.data_manager.snapshot_import_cache[ snapshot_id]['path'] = path group_id = context.application.data_manager.import_from_file( parent_id, path, name=name) return application.make_ok_response(group_id=group_id) return application.make_fail_response('No import snapshot info found!')
def put(self): template = request.json["templateName"] # check template valid if template in list(config.indexes_by_name.keys()): # reset bandwidth config.bandwidth = config.indexes_by_name[template] logger.debug(f'Modified bandwidth: {config.bandwidth}') return application.make_ok_response(bandwidth=config.bandwidth) else: return application.make_fail_response(msg=f'invalid template ! request data is {template}')
def get(self, checker_id=None): if not checker_id: sorted_checker_groups = application.server['checker'].get_sorted_checker_groups() return application.make_ok_response(data=sorted_checker_groups) _checker = application.checkers.get(checker_id) if not _checker: return application.make_fail_response(f'Checker {checker_id} not found') content = _checker.read() return application.make_ok_response(data=content)
def template(): if request.method == 'GET': ''' Get all template list, all draft list, last selected template and last selected draft ''' draft_version = cache.check_draft_version() if draft_version < cache.DRAFT_VERSION_V_1_8_0: logger.info('Updating draft into v1.8.0 from v1.7.0') cache.update_selected_template() cache.update_all_draft_file() templates = template_loader.template_list() last_selected_template = cache.get_selected_template() selected_template_index = None for index, template in enumerate(templates): if template['path'] == last_selected_template: selected_template_index = index break if not selected_template_index: return application.make_ok_response(templates=templates) template_key = cache.get_filename(last_selected_template) drafts = cache.get_draft_list(template_key) last_selected_cache = cache.get_selected_cache() return application.make_ok_response( selected_template_index=selected_template_index, templates=templates, selected_cache=last_selected_cache, drafts=drafts) elif request.method == 'POST': ''' Get template detail by path And save lastest_selected_template ''' if not request.json or not request.json.get('path'): return application.make_fail_response( 'Missing required argument: path') template_path = request.json['path'] draft_name = request.json.get('cache_name', '') draft_filename = cache.get_filename(template_path, draft_name) template_detail = cache.get(draft_filename) template = template_loader.get_template(template_path) if inspect.getargspec(template.form).args: template_detail = template.form({'cache': template_detail}) else: template_detail = template.form() cache.selected_template(template_path, draft_name) return jsonify(template_detail)
def put(self): selected_filter_name = request.json.get('name') if not selected_filter_name: context.application.selected_filter = None return application.make_ok_response() for f in context.application.filters: if f['name'] == selected_filter_name: context.application.selected_filter = f return application.make_ok_response() return application.make_fail_response(f'Flow filter {selected_filter_name} not found!')
def _get_menu_by_status_item_name(self, name): plugin_server = application.server['plugin'] for plugin in plugin_server.plugins.values(): for status_item in plugin.status: if status_item.name != name: continue menu_list = status_item.get_detail() return application.make_ok_response(data=menu_list) return application.make_fail_response( f'Status item not found, id={name}')
def _get_menu_by_status_item_name(self, name): plugin_server = application.server['plugin'] for plugin_name in plugin_server.plugins: plugin = plugin_server.plugins[plugin_name] for status_item in plugin.status: if status_item.name == name: menu_list = [ menu_item.json() for menu_item in status_item.get_menu() ] return application.make_ok_response(data=menu_list) return application.make_fail_response( f'Status item not found, id={name}')
def ui_cache(key): if request.method == 'GET': data = cache.get(key) if data: return application.make_ok_response(data=data) else: return application.make_fail_response(f'Not found cache by key {key}') elif request.method == 'POST': data = request.json for field in data: if 'extraMsg' in field: del field['extraMsg'] cache.put(key, data) return application.make_ok_response()
def get(self, checker_id=None): if not checker_id: checkers = application.checkers script_info_list = [ checkers[checker_name].json() for checker_name in checkers ] script_info_list.sort(key=lambda x: x['name']) return application.make_ok_response(data=script_info_list) _checker = application.checkers.get(checker_id) if not _checker: return application.make_fail_response( f'Checker {checker_id} not found') content = _checker.read() return application.make_ok_response(data=content)
def post(self, action): if action == 'save': _ids = request.json.get('ids') record_items = [] for _id in _ids: for item in context.application.cache.items(): if _id == item['id']: record_items.append(item) break dm = context.application.data_manager for flow in record_items: dm.save_data(flow) return application.make_ok_response() elif action == 'search': filter_obj = request.json.get('selectedFilter') context.application.selected_filter = filter_obj req_list = get_flow_list_by_filter(filter_obj) return Response(json.dumps(req_list, ensure_ascii=False), mimetype='application/json', status=200) else: return application.make_fail_response( f'action: {action} is not supported')
def issue(): issue_req = request.json template_info = issue_req['template'] template = template_loader.get_template(template_info['path']) issue_data = issue_req['issue'] attachments = issue_req['attachments'] # Set bugit script context context = {'issue': issue_data, 'attachments': attachments} # Set submit actions submit_action_functions = template.submit() # Do submit for action_function in submit_action_functions: try: action_function(context) except Exception: error_message = traceback.format_exc() trace = "<br/>".join(error_message.split("\n")) lyrebird.report({ 'action': 'bugit.submit', 'bugit': { 'status': 'failure', 'data': issue_data, 'error': error_message } }) return application.make_fail_response(f'Script function "{action_function.__name__}" crash.<br/>{trace}') lyrebird.report({ 'action': 'bugit.submit', 'bugit': { 'status': 'success', 'data': issue_data } }) return application.make_ok_response(message="Create issue success!")
def on_500(error): return application.make_fail_response( f'{error.__class__.__name__} {error}')