async def get_pod_status(self, request): pod_name = request.match_info['pod_name'] pod = self.pods.get(pod_name) if not pod: raise web.HTTPNotFound(reason=f'unknown pod {pod_name}') return web.json_response(await pod.status())
async def credential_exchange_issue(request: web.BaseRequest): """ Request handler for sending credential. Args: request: aiohttp request object Returns: The credential exchange record """ r_time = get_timer() context: AdminRequestContext = request["context"] outbound_handler = request["outbound_message_router"] body = await request.json() comment = body.get("comment") cred_ex_id = request.match_info["cred_ex_id"] cred_ex_record = None conn_record = None try: async with context.session() as session: try: cred_ex_record = await V20CredExRecord.retrieve_by_id( session, cred_ex_id, ) except StorageNotFoundError as err: raise web.HTTPNotFound(reason=err.roll_up) from err connection_id = cred_ex_record.connection_id conn_record = await ConnRecord.retrieve_by_id( session, connection_id) if not conn_record.is_ready: raise web.HTTPForbidden( reason=f"Connection {connection_id} not ready") cred_manager = V20CredManager(context.profile) (cred_ex_record, cred_issue_message) = await cred_manager.issue_credential( cred_ex_record, comment=comment, ) result = await _get_result_with_details(context.profile, cred_ex_record) except ( BaseModelError, IndyIssuerError, LedgerError, StorageError, V20CredFormatError, V20CredManagerError, ) as err: async with context.session() as session: await cred_ex_record.save_error_state(session, reason=err.message) await internal_error( err, web.HTTPBadRequest, cred_ex_record, outbound_handler, ) await outbound_handler(cred_issue_message, connection_id=connection_id) trace_event( context.settings, cred_issue_message, outcome="credential_exchange_issue.END", perf_counter=r_time, ) return web.json_response(result)
async def credential_exchange_send_bound_offer(request: web.BaseRequest): """ Request handler for sending bound credential offer. A holder initiates this sequence with a credential proposal; this message responds with an offer bound to the proposal. Args: request: aiohttp request object Returns: The credential exchange record """ r_time = get_timer() context: AdminRequestContext = request["context"] outbound_handler = request["outbound_message_router"] body = await request.json() if request.body_exists else {} filt_spec = body.get("filter") preview_spec = body.get("counter_preview") cred_ex_id = request.match_info["cred_ex_id"] cred_ex_record = None conn_record = None try: async with context.session() as session: try: cred_ex_record = await V20CredExRecord.retrieve_by_id( session, cred_ex_id, ) except StorageNotFoundError as err: raise web.HTTPNotFound(reason=err.roll_up) from err connection_id = cred_ex_record.connection_id if cred_ex_record.state != ( V20CredExRecord.STATE_PROPOSAL_RECEIVED ): # check state here: manager call creates free offers too raise V20CredManagerError( f"Credential exchange record {cred_ex_record.cred_ex_id} " f"in {cred_ex_record.state} state " f"(must be {V20CredExRecord.STATE_PROPOSAL_RECEIVED})") conn_record = await ConnRecord.retrieve_by_id( session, connection_id) if not conn_record.is_ready: raise web.HTTPForbidden( reason=f"Connection {connection_id} not ready") cred_manager = V20CredManager(context.profile) (cred_ex_record, cred_offer_message) = await cred_manager.create_offer( cred_ex_record, counter_proposal=V20CredProposal( comment=None, credential_preview=(V20CredPreview.deserialize(preview_spec)), **_formats_filters(filt_spec), ) if preview_spec else None, comment=None, ) result = cred_ex_record.serialize() except ( BaseModelError, IndyIssuerError, LedgerError, StorageError, V20CredManagerError, V20CredFormatError, ) as err: async with context.session() as session: await cred_ex_record.save_error_state(session, reason=err.message) await internal_error( err, web.HTTPBadRequest, cred_ex_record, outbound_handler, ) await outbound_handler(cred_offer_message, connection_id=connection_id) trace_event( context.settings, cred_offer_message, outcome="credential_exchange_send_bound_offer.END", perf_counter=r_time, ) return web.json_response(result)
def optional_json(o: Any, hint: str) -> StreamResponse: if o: return web.json_response(to_json(o)) else: return web.HTTPNotFound(text=hint)
async def role_handler(self, request): if self.role is None: raise web.HTTPNotFound() return web.Response(body=self.role.encode("utf-8"))
def route(request): session = yield from get_session(request) parameters = request.rel_url.query if 'uid' not in session: return web.HTTPUnauthorized() else: uid = session['uid'] try: mid = int(parameters['mid']) except: return web.HTTPBadRequest() with (yield from request.app['pool']) as connect: cursor= yield from connect.cursor() yield from cursor.execute(''' select overview.id, overview.romaji, overview.name, overview.affiliation, overview.introduction, overview.follows, overview.subscribes, follow.uid, subscription.uid from ( select member.id, member.romaji, member.name, member.affiliation, member.introduction, member.follows, member.subscribes from member where member.id = %s ) overview left join follow on follow.uid = %s and follow.mid = overview.id left join subscription on subscription.uid = %s and subscription.mid = overview.id ''',(mid,uid,uid)) data = yield from cursor.fetchone() yield from cursor.close() connect.close() if not data: return web.HTTPNotFound() json_back = { "mid": str(data[0]).zfill(4), "avatar": "/avatar/{}.jpg".format(data[1]), "name": data[2], "romaji": data[1], "affiliation": data[3], "introduction": data[4], "follows": data[5], "subscribes": data[6], "followed": True if data[7] else False, "subscribed": True if data[8] else False } return web.Response(text=tool.jsonify(json_back),content_type="application/json",charset="utf-8")
async def get_redirect(request): uri = request.match_info["uri"] redirect = await request.app["db"].get_redirect(uri) if redirect is None: return web.HTTPNotFound() return web.HTTPFound(redirect)
async def get(self, request: web.Request) -> web.StreamResponse: if self.should_render_graphiql(request): return self.render_graphiql() return web.HTTPNotFound()
async def credential_exchange_store(request: web.BaseRequest): """ Request handler for storing credential. Args: request: aiohttp request object Returns: The credential exchange record """ r_time = get_timer() context = request.app["request_context"] outbound_handler = request.app["outbound_message_router"] try: body = await request.json() or {} credential_id = body.get("credential_id") except JSONDecodeError: credential_id = None credential_exchange_id = request.match_info["cred_ex_id"] try: cred_ex_record = await V10CredentialExchange.retrieve_by_id( context, credential_exchange_id) except StorageNotFoundError as err: raise web.HTTPNotFound(reason=err.roll_up) from err connection_record = None connection_id = cred_ex_record.connection_id try: connection_record = await ConnectionRecord.retrieve_by_id( context, connection_id) if not connection_record.is_ready: raise web.HTTPForbidden( reason=f"Connection {connection_id} not ready") credential_manager = CredentialManager(context) ( cred_ex_record, credential_stored_message, ) = await credential_manager.store_credential(cred_ex_record, credential_id) result = cred_ex_record.serialize() except (StorageError, CredentialManagerError, BaseModelError) as err: await internal_error( err, web.HTTPBadRequest, cred_ex_record or connection_record, outbound_handler, ) await outbound_handler(credential_stored_message, connection_id=connection_id) trace_event( context.settings, credential_stored_message, outcome="credential_exchange_store.END", perf_counter=r_time, ) return web.json_response(result)
async def get_object_or_404(request, model, **kwargs): """ Get object or raise HttpNotFound """ try: return await request.app.objects.get(model, **kwargs) except model.DoesNotExist: raise web.HTTPNotFound()
async def favicon(request): return web.HTTPNotFound()
async def put_notification(request): """ Update an existing notification :Example: curl -X PUT http://localhost:8081/foglamp/notification/<notification_name> -d '{"description":"Test Notification modified"}' curl -X PUT http://localhost:8081/foglamp/notification/<notification_name> -d '{"rule": "threshold", "channel": "email"}' curl -X PUT http://localhost:8081/foglamp/notification/<notification_name> -d '{"notification_type": "one shot", "enabled": false}' curl -X PUT http://localhost:8081/foglamp/notification/<notification_name> -d '{"enabled": false}' curl -X PUT http://localhost:8081/foglamp/notification/<notification_name> -d '{"description":"Test Notification", "rule": "threshold", "channel": "email", "notification_type": "one shot", "enabled": false, "rule_config": {}, "delivery_config": {}}' """ try: notification_service = ServiceRegistry.get(s_type=ServiceRecord.Type.Notification.name) _address, _port = notification_service[0]._address, notification_service[0]._port except service_registry_exceptions.DoesNotExist: raise web.HTTPNotFound(reason="No Notification service available.") try: notif = request.match_info.get('notification_name', None) if notif is None: raise ValueError("Notification name is required for updation.") # TODO: Stop notification before update data = await request.json() if not isinstance(data, dict): raise ValueError('Data payload must be a valid JSON') description = data.get('description', None) rule = data.get('rule', None) channel = data.get('channel', None) notification_type = data.get('notification_type', None) enabled = data.get('enabled', None) rule_config = data.get('rule_config', {}) delivery_config = data.get('delivery_config', {}) if utils.check_reserved(notif) is False: raise ValueError('Invalid notification name parameter.') if rule is not None and utils.check_reserved(rule) is False: raise ValueError('Invalid rule property in payload.') if channel is not None and utils.check_reserved(channel) is False: raise ValueError('Invalid channel property in payload.') if notification_type is not None and notification_type not in NOTIFICATION_TYPE: raise ValueError('Invalid notification_type property in payload.') if enabled is not None: if enabled not in ['true', 'false', True, False]: raise ValueError('Only "true", "false", true, false are allowed for value of enabled.') is_enabled = "true" if ((type(enabled) is str and enabled.lower() in ['true']) or ( (type(enabled) is bool and enabled is True))) else "false" storage = connect.get_storage_async() config_mgr = ConfigurationManager(storage) current_config = await config_mgr._read_category_val(notif) rule_changed = True if rule is not None and rule != current_config['rule']['value'] else False channel_changed = True if channel is not None and channel != current_config['channel']['value'] else False try: # Get default config for rule and channel plugins url = str(request.url) url_parts = url.split("/foglamp/notification") url = '{}/foglamp/notification/plugin'.format(url_parts[0]) try: # When authentication is mandatory we need to pass token in request header auth_token = request.token except AttributeError: auth_token = None list_plugins = json.loads(await _hit_get_url(url, auth_token)) search_rule = rule if rule_changed else current_config['rule']['value'] r = list(filter(lambda rules: rules['name'] == search_rule, list_plugins['rules'])) if len(r) == 0: raise KeyError rule_plugin_config = r[0]['config'] search_channel = channel if channel_changed else current_config['channel']['value'] c = list(filter(lambda channels: channels['name'] == search_channel, list_plugins['delivery'])) if len(c) == 0: raise KeyError delivery_plugin_config = c[0]['config'] except KeyError: raise ValueError("Invalid rule plugin:{} and/or delivery plugin:{} supplied.".format(rule, channel)) # Verify if rule_config contains valid keys if rule_config != {}: for k, v in rule_config.items(): if k not in rule_plugin_config: raise ValueError("Invalid key:{} in rule plugin:{}".format(k, rule_plugin_config)) # Verify if delivery_config contains valid keys if delivery_config != {}: for k, v in delivery_config.items(): if k not in delivery_plugin_config: raise ValueError( "Invalid key:{} in delivery plugin:{}".format(k, delivery_plugin_config)) if rule_changed: # A new rule has been supplied category_desc = rule_plugin_config['plugin']['description'] category_name = "rule{}".format(notif) await config_mgr.create_category(category_name=category_name, category_description=category_desc, category_value=rule_plugin_config, keep_original_items=False) if channel_changed: # A new delivery has been supplied category_desc = delivery_plugin_config['plugin']['description'] category_name = "delivery{}".format(notif) await config_mgr.create_category(category_name=category_name, category_description=category_desc, category_value=delivery_plugin_config, keep_original_items=False) notification_config = {} if description is not None: notification_config.update({"description": description}) if rule is not None: notification_config.update({"rule": rule}) if channel is not None: notification_config.update({"channel": channel}) if notification_type is not None: notification_config.update({"notification_type": notification_type}) if enabled is not None: notification_config.update({"enable": is_enabled}) await _update_configurations(config_mgr, notif, notification_config, rule_config, delivery_config) except ValueError as ex: raise web.HTTPBadRequest(reason=str(ex)) except Exception as e: raise web.HTTPInternalServerError(reason=str(e)) else: # TODO: Start notification after update return web.json_response({'result': "Notification {} updated successfully".format(notif)})
async def post_notification(request): """ Create a new notification to run a specific plugin :Example: curl -X POST http://localhost:8081/foglamp/notification -d '{"name": "Test Notification", "description":"Test Notification", "rule": "threshold", "channel": "email", "notification_type": "one shot", "enabled": false}' curl -X POST http://localhost:8081/foglamp/notification -d '{"name": "Test Notification", "description":"Test Notification", "rule": "threshold", "channel": "email", "notification_type": "one shot", "enabled": false, "rule_config": {}, "delivery_config": {}}' """ try: notification_service = ServiceRegistry.get(s_type=ServiceRecord.Type.Notification.name) _address, _port = notification_service[0]._address, notification_service[0]._port except service_registry_exceptions.DoesNotExist: raise web.HTTPNotFound(reason="No Notification service available.") try: data = await request.json() if not isinstance(data, dict): raise ValueError('Data payload must be a valid JSON') name = data.get('name', None) description = data.get('description', None) rule = data.get('rule', None) channel = data.get('channel', None) notification_type = data.get('notification_type', None) enabled = data.get('enabled', None) rule_config = data.get('rule_config', {}) delivery_config = data.get('delivery_config', {}) if name is None or name.strip() == "": raise ValueError('Missing name property in payload.') if description is None: raise ValueError('Missing description property in payload.') if rule is None: raise ValueError('Missing rule property in payload.') if channel is None: raise ValueError('Missing channel property in payload.') if notification_type is None: raise ValueError('Missing notification_type property in payload.') if utils.check_reserved(name) is False: raise ValueError('Invalid name property in payload.') if utils.check_reserved(rule) is False: raise ValueError('Invalid rule property in payload.') if utils.check_reserved(channel) is False: raise ValueError('Invalid channel property in payload.') if notification_type not in NOTIFICATION_TYPE: raise ValueError('Invalid notification_type property in payload.') if enabled is not None: if enabled not in ['true', 'false', True, False]: raise ValueError('Only "true", "false", true, false are allowed for value of enabled.') is_enabled = "true" if ((type(enabled) is str and enabled.lower() in ['true']) or ( (type(enabled) is bool and enabled is True))) else "false" storage = connect.get_storage_async() config_mgr = ConfigurationManager(storage) curr_config = await config_mgr.get_category_all_items(name) if curr_config is not None: raise ValueError("A Category with name {} already exists.".format(name)) try: # Get default config for rule and channel plugins url = '{}/plugin'.format(request.url) try: # When authentication is mandatory we need to pass token in request header auth_token = request.token except AttributeError: auth_token = None list_plugins = json.loads(await _hit_get_url(url, auth_token)) r = list(filter(lambda rules: rules['name'] == rule, list_plugins['rules'])) c = list(filter(lambda channels: channels['name'] == channel, list_plugins['delivery'])) if len(r) == 0 or len(c) == 0: raise KeyError rule_plugin_config = r[0]['config'] delivery_plugin_config = c[0]['config'] except KeyError: raise ValueError("Invalid rule plugin {} and/or delivery plugin {} supplied.".format(rule, channel)) # Verify if rule_config contains valid keys if rule_config != {}: for k, v in rule_config.items(): if k not in rule_plugin_config: raise ValueError("Invalid key {} in rule_config {} supplied for plugin {}.".format(k, rule_config, rule)) # Verify if delivery_config contains valid keys if delivery_config != {}: for k, v in delivery_config.items(): if k not in delivery_plugin_config: raise ValueError( "Invalid key {} in delivery_config {} supplied for plugin {}.".format(k, delivery_config, channel)) # First create templates for notification and rule, channel plugins post_url = 'http://{}:{}/notification/{}'.format(_address, _port, urllib.parse.quote(name)) await _hit_post_url(post_url) # Create Notification template post_url = 'http://{}:{}/notification/{}/rule/{}'.format(_address, _port, urllib.parse.quote(name), urllib.parse.quote(rule)) await _hit_post_url(post_url) # Create Notification rule template post_url = 'http://{}:{}/notification/{}/delivery/{}'.format(_address, _port, urllib.parse.quote(name), urllib.parse.quote(channel)) await _hit_post_url(post_url) # Create Notification delivery template # Create configurations notification_config = { "description": description, "rule": rule, "channel": channel, "notification_type": notification_type, "enable":is_enabled, } await _update_configurations(config_mgr, name, notification_config, rule_config, delivery_config) audit = AuditLogger(storage) await audit.information('NTFAD', {"name": name}) except ValueError as ex: raise web.HTTPBadRequest(reason=str(ex)) except Exception as e: raise web.HTTPInternalServerError(reason=str(e)) else: return web.json_response({'result': "Notification {} created successfully".format(name)})
async def get_other_system(request): system_id = request.match_info.get("system") system = await System.get_by_hid(request["conn"], system_id) if not system: raise web.HTTPNotFound(body="null") return web.json_response(system.to_json())
async def delete(self): ad_id = int(self.request.match_info['ad_id']) ad = await Ad.description(ad_id) return web.json_response(web.HTTPNotFound())
async def credential_exchange_send_bound_offer(request: web.BaseRequest): """ Request handler for sending bound credential offer. A holder initiates this sequence with a credential proposal; this message responds with an offer bound to the proposal. Args: request: aiohttp request object Returns: The credential exchange record """ r_time = get_timer() context = request.app["request_context"] outbound_handler = request.app["outbound_message_router"] credential_exchange_id = request.match_info["cred_ex_id"] try: cred_ex_record = await V10CredentialExchange.retrieve_by_id( context, credential_exchange_id) except StorageNotFoundError as err: raise web.HTTPNotFound(reason=err.roll_up) from err connection_record = None connection_id = cred_ex_record.connection_id try: if cred_ex_record.state != ( V10CredentialExchange.STATE_PROPOSAL_RECEIVED ): # check state here: manager call creates free offers too raise CredentialManagerError( f"Credential exchange {cred_ex_record.credential_exchange_id} " f"in {cred_ex_record.state} state " f"(must be {V10CredentialExchange.STATE_PROPOSAL_RECEIVED})") connection_record = await ConnectionRecord.retrieve_by_id( context, connection_id) if not connection_record.is_ready: raise web.HTTPForbidden( reason=f"Connection {connection_id} not ready") credential_manager = CredentialManager(context) ( cred_ex_record, credential_offer_message, ) = await credential_manager.create_offer(cred_ex_record, comment=None) result = cred_ex_record.serialize() except (StorageError, BaseModelError, CredentialManagerError, LedgerError) as err: await internal_error( err, web.HTTPBadRequest, cred_ex_record or connection_record, outbound_handler, ) await outbound_handler(credential_offer_message, connection_id=connection_id) trace_event( context.settings, credential_offer_message, outcome="credential_exchange_send_bound_offer.END", perf_counter=r_time, ) return web.json_response(result)
async def delete_certificate(request): """ Delete a certificate :Example: curl -X DELETE http://localhost:8081/fledge/certificate/fledge.pem curl -X DELETE http://localhost:8081/fledge/certificate/fledge.cert curl -X DELETE http://localhost:8081/fledge/certificate/filename.cer curl -X DELETE http://localhost:8081/fledge/certificate/filename.crt curl -X DELETE http://localhost:8081/fledge/certificate/fledge.json?type=cert curl -X DELETE http://localhost:8081/fledge/certificate/fledge.pem?type=cert curl -X DELETE http://localhost:8081/fledge/certificate/fledge.key curl -X DELETE http://localhost:8081/fledge/certificate/fledge.pem?type=key """ file_name = request.match_info.get('name', None) valid_extensions = ('.cert', '.cer', '.crt', '.json', '.key', '.pem') if not file_name.endswith(valid_extensions): raise web.HTTPBadRequest( reason="Accepted file extensions are {}".format(valid_extensions)) # check if cert_name is currently set for 'certificateName' in config for 'rest_api' cf_mgr = ConfigurationManager(connect.get_storage_async()) result = await cf_mgr.get_category_item(category_name='rest_api', item_name='certificateName') if file_name.split('.')[0] == result['value']: raise web.HTTPConflict( reason= 'Certificate with name {} is already in use, you can not delete'. format(file_name)) _type = None if 'type' in request.query and request.query['type'] != '': _type = request.query['type'] if _type not in ['cert', 'key']: raise web.HTTPBadRequest( reason= "Only cert and key are allowed for the value of type param") certs_dir = _get_certs_dir('/etc/certs/') is_found = False cert_path = list() if _type and _type == 'cert': short_cert_name_valid_extensions = ('.cert', '.cer', '.crt') if not file_name.endswith(short_cert_name_valid_extensions): if os.path.isfile(certs_dir + 'pem/' + file_name): is_found = True cert_path = [certs_dir + 'pem/' + file_name] if os.path.isfile(certs_dir + 'json/' + file_name): is_found = True cert_path = [certs_dir + 'json/' + file_name] else: if os.path.isfile(certs_dir + file_name): is_found = True cert_path = [certs_dir + file_name] if _type and _type == 'key': if os.path.isfile(certs_dir + file_name): is_found = True cert_path = [certs_dir + file_name] if _type is None: for root, dirs, files in os.walk(certs_dir): if root.endswith('json'): for f in files: if file_name == f: is_found = True cert_path.append(certs_dir + 'json/' + file_name) files.remove(f) if root.endswith('pem'): for f in files: if file_name == f: is_found = True cert_path.append(certs_dir + 'pem/' + file_name) files.remove(f) for f in files: if file_name == f: is_found = True cert_path.append(certs_dir + file_name) if not is_found: raise web.HTTPNotFound( reason='Certificate with name {} does not exist'.format(file_name)) # Remove file for fp in cert_path: os.remove(fp) return web.json_response( {'result': "{} has been deleted successfully".format(file_name)})
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) roles = self.request.app['config']['authz_admin']['roles'] if self['role'] not in roles: raise web.HTTPNotFound() self._role = roles[self['role']]
async def get_task(self, req): id = req.match_info['id'] if id not in self.tasks: return web.HTTPNotFound() return web.json_response(self.tasks[id])
Args: request: aiohttp request object Returns: The connection record response """ context = request.app["request_context"] connection_id = request.match_info["conn_id"] try: record = await ConnectionRecord.retrieve_by_id(context, connection_id) <<<<<<< HEAD except StorageNotFoundError as err: raise web.HTTPNotFound(reason=err.roll_up) from err return web.json_response(record.serialize()) ======= result = record.serialize() except StorageNotFoundError as err: raise web.HTTPNotFound(reason=err.roll_up) from err except BaseModelError as err: raise web.HTTPBadRequest(reason=err.roll_up) from err return web.json_response(result) <<<<<<< HEAD >>>>>>> work in progress ======= >>>>>>> raise 400 series on exceptions within protocols
async def get_statistics_history(request): """ Args: request: Returns: a list of general set of statistics :Example: curl -X GET http://localhost:8081/foglamp/statistics/history?limit=1 curl -X GET http://localhost:8081/foglamp/statistics/history?key=READINGS """ storage_client = connect.get_storage_async() # To find the interval in secs from stats collector schedule scheduler_payload = PayloadBuilder().SELECT("schedule_interval").WHERE( ['process_name', '=', 'stats collector']).payload() result = await storage_client.query_tbl_with_payload('schedules', scheduler_payload) if len(result['rows']) > 0: scheduler = Scheduler() interval_days, interval_dt = scheduler.extract_day_time_from_interval(result['rows'][0]['schedule_interval']) interval = datetime.timedelta(days=interval_days, hours=interval_dt.hour, minutes=interval_dt.minute, seconds=interval_dt.second) interval_in_secs = interval.total_seconds() else: raise web.HTTPNotFound(reason="No stats collector schedule found") stats_history_chain_payload = PayloadBuilder().SELECT(("history_ts", "key", "value"))\ .ALIAS("return", ("history_ts", 'history_ts')).FORMAT("return", ("history_ts", "YYYY-MM-DD HH24:MI:SS.MS"))\ .ORDER_BY(['history_ts', 'desc']).chain_payload() if 'key' in request.query: stats_history_chain_payload = PayloadBuilder(stats_history_chain_payload).WHERE(['key', '=', request.query['key']]).chain_payload() if 'limit' in request.query and request.query['limit'] != '': try: limit = int(request.query['limit']) if limit < 0: raise ValueError if 'key' in request.query: limit_count = limit else: # FIXME: Hack straight away multiply the LIMIT by the group count # i.e. if there are 8 records per distinct (stats_key), and limit supplied is 2 # then internally, actual LIMIT = 2*8 # TODO: FOGL-663 Need support for "subquery" from storage service # Remove python side handling date_trunc and use # SELECT date_trunc('second', history_ts::timestamptz)::varchar as history_ts count_payload = PayloadBuilder().AGGREGATE(["count", "*"]).payload() result = await storage_client.query_tbl_with_payload("statistics", count_payload) key_count = result['rows'][0]['count_*'] limit_count = limit * key_count stats_history_chain_payload = PayloadBuilder(stats_history_chain_payload).LIMIT(limit_count).chain_payload() except ValueError: raise web.HTTPBadRequest(reason="Limit must be a positive integer") stats_history_payload = PayloadBuilder(stats_history_chain_payload).payload() result_from_storage = await storage_client.query_tbl_with_payload('statistics_history', stats_history_payload) group_dict = [] for row in result_from_storage['rows']: new_dict = {'history_ts': row['history_ts'], row['key']: row['value']} group_dict.append(new_dict) results = [] temp_dict = {} previous_ts = None for row in group_dict: # first time or when history_ts changes if previous_ts is None or previous_ts != row['history_ts']: if previous_ts is not None: results.append(temp_dict) previous_ts = row['history_ts'] temp_dict = {'history_ts': previous_ts} # Append statistics key to temp dict for key, value in row.items(): temp_dict.update({key: value}) # Append the last set of records which do not get appended above results.append(temp_dict) return web.json_response({"interval": interval_in_secs, 'statistics': results})
async def handle_termination_request(self, request): if self.terminating: return web.json_response({"action": "terminate"}) else: return web.HTTPNotFound()
async def add_task(request): """ Create a new task to run a specific plugin :Example: curl -X POST http://localhost:8081/foglamp/scheduled/task -d '{ "name": "North Readings to PI", "plugin": "pi_server", "type": "north", "schedule_type": 3, "schedule_day": 0, "schedule_time": 0, "schedule_repeat": 30, "schedule_enabled": true }' curl -sX POST http://localhost:8081/foglamp/scheduled/task -d '{"name": "PI-2", "plugin": "pi_server", "type": "north", "schedule_type": 3, "schedule_day": 0, "schedule_time": 0, "schedule_repeat": 30, "schedule_enabled": true, "config": { "producerToken": {"value": "uid=180905062754237&sig=kx5l+"}, "URL": {"value": "https://10.2.5.22:5460/ingress/messages"}}}' """ try: data = await request.json() if not isinstance(data, dict): raise ValueError('Data payload must be a valid JSON') name = data.get('name', None) plugin = data.get('plugin', None) task_type = data.get('type', None) schedule_type = data.get('schedule_type', None) schedule_day = data.get('schedule_day', None) schedule_time = data.get('schedule_time', None) schedule_repeat = data.get('schedule_repeat', None) enabled = data.get('schedule_enabled', None) config = data.get('config', None) if name is None: raise web.HTTPBadRequest( reason='Missing name property in payload.') if plugin is None: raise web.HTTPBadRequest( reason='Missing plugin property in payload.') if task_type is None: raise web.HTTPBadRequest( reason='Missing type property in payload.') if utils.check_reserved(name) is False: raise web.HTTPBadRequest( reason='Invalid name property in payload.') if utils.check_reserved(plugin) is False: raise web.HTTPBadRequest( reason='Invalid plugin property in payload.') if task_type not in ['north']: raise web.HTTPBadRequest(reason='Only north type is supported.') if schedule_type is None: raise web.HTTPBadRequest(reason='schedule_type is mandatory') if not isinstance(schedule_type, int) and not schedule_type.isdigit(): raise web.HTTPBadRequest( reason='Error in schedule_type: {}'.format(schedule_type)) if int(schedule_type) not in list(Schedule.Type): raise web.HTTPBadRequest( reason='schedule_type error: {}'.format(schedule_type)) if int(schedule_type) == Schedule.Type.STARTUP: raise web.HTTPBadRequest( reason='schedule_type cannot be STARTUP: {}'.format( schedule_type)) schedule_type = int(schedule_type) if schedule_day is not None: if isinstance(schedule_day, float) or ( isinstance(schedule_day, str) and (schedule_day.strip() != "" and not schedule_day.isdigit())): raise web.HTTPBadRequest( reason='Error in schedule_day: {}'.format(schedule_day)) else: schedule_day = int( schedule_day) if schedule_day is not None else None if schedule_time is not None and (not isinstance(schedule_time, int) and not schedule_time.isdigit()): raise web.HTTPBadRequest( reason='Error in schedule_time: {}'.format(schedule_time)) else: schedule_time = int( schedule_time) if schedule_time is not None else None if schedule_repeat is not None and (not isinstance( schedule_repeat, int) and not schedule_repeat.isdigit()): raise web.HTTPBadRequest( reason='Error in schedule_repeat: {}'.format(schedule_repeat)) else: schedule_repeat = int( schedule_repeat) if schedule_repeat is not None else None if schedule_type == Schedule.Type.TIMED: if not schedule_time: raise web.HTTPBadRequest( reason= 'schedule_time cannot be empty/None for TIMED schedule.') if schedule_day is not None and (schedule_day < 1 or schedule_day > 7): raise web.HTTPBadRequest( reason= 'schedule_day {} must either be None or must be an integer, 1(Monday) ' 'to 7(Sunday).'.format(schedule_day)) if schedule_time < 0 or schedule_time > 86399: raise web.HTTPBadRequest( reason= 'schedule_time {} must be an integer and in range 0-86399.' .format(schedule_time)) if schedule_type == Schedule.Type.INTERVAL: if schedule_repeat is None: raise web.HTTPBadRequest( reason= 'schedule_repeat {} is required for INTERVAL schedule_type.' .format(schedule_repeat)) elif not isinstance(schedule_repeat, int): raise web.HTTPBadRequest( reason='schedule_repeat {} must be an integer.'.format( schedule_repeat)) if enabled is not None: if enabled not in ['true', 'false', True, False]: raise web.HTTPBadRequest( reason= 'Only "true", "false", true, false are allowed for value of enabled.' ) is_enabled = True if ( (type(enabled) is str and enabled.lower() in ['true']) or ((type(enabled) is bool and enabled is True))) else False # Check if a valid plugin has been provided try: # "plugin_module_path" is fixed by design. It is MANDATORY to keep the plugin in the exactly similar named # folder, within the plugin_module_path. # if multiple plugin with same name are found, then python plugin import will be tried first plugin_module_path = "foglamp.plugins.{}".format(task_type) import_file_name = "{path}.{dir}.{file}".format( path=plugin_module_path, dir=plugin, file=plugin) _plugin = __import__(import_file_name, fromlist=['']) script = '["tasks/north"]' # Fetch configuration from the configuration defined in the plugin plugin_info = _plugin.plugin_info() if plugin_info['type'] != task_type: msg = "Plugin of {} type is not supported".format( plugin_info['type']) _logger.exception(msg) return web.HTTPBadRequest(reason=msg) plugin_config = plugin_info['config'] process_name = 'north' except ImportError as ex: # Checking for C-type plugins script = '["tasks/north_c"]' plugin_info = apiutils.get_plugin_info(plugin, dir=task_type) if plugin_info['type'] != task_type: msg = "Plugin of {} type is not supported".format( plugin_info['type']) _logger.exception(msg) return web.HTTPBadRequest(reason=msg) plugin_config = plugin_info['config'] process_name = 'north_c' if not plugin_config: _logger.exception("Plugin %s import problem from path %s. %s", plugin, plugin_module_path, str(ex)) raise web.HTTPNotFound( reason='Plugin "{}" import problem from path "{}"'.format( plugin, plugin_module_path)) except Exception as ex: _logger.exception("Failed to fetch plugin configuration. %s", str(ex)) raise web.HTTPInternalServerError( reason='Failed to fetch plugin configuration.') storage = connect.get_storage_async() config_mgr = ConfigurationManager(storage) # Check whether category name already exists category_info = await config_mgr.get_category_all_items( category_name=name) if category_info is not None: raise web.HTTPBadRequest( reason="The '{}' category already exists".format(name)) # Check that the schedule name is not already registered count = await check_schedules(storage, name) if count != 0: raise web.HTTPBadRequest( reason='A north instance with this name already exists') # Check that the process name is not already registered count = await check_scheduled_processes(storage, process_name) if count == 0: # Create the scheduled process entry for the new task payload = PayloadBuilder().INSERT(name=process_name, script=script).payload() try: res = await storage.insert_into_tbl("scheduled_processes", payload) except StorageServerError as ex: _logger.exception("Failed to create scheduled process. %s", ex.error) raise web.HTTPInternalServerError( reason='Failed to create north instance.') except Exception as ex: _logger.exception("Failed to create scheduled process. %s", ex) raise web.HTTPInternalServerError( reason='Failed to create north instance.') # If successful then create a configuration entry from plugin configuration try: # Create a configuration category from the configuration defined in the plugin category_desc = plugin_config['plugin']['description'] await config_mgr.create_category( category_name=name, category_description=category_desc, category_value=plugin_config, keep_original_items=True) # Create the parent category for all North tasks await config_mgr.create_category("North", {}, 'North tasks', True) await config_mgr.create_child_category("North", [name]) # If config is in POST data, then update the value for each config item if config is not None: if not isinstance(config, dict): raise ValueError('Config must be a JSON object') for k, v in config.items(): await config_mgr.set_category_item_value_entry( name, k, v['value']) except Exception as ex: await config_mgr.delete_category_and_children_recursively(name) _logger.exception("Failed to create plugin configuration. %s", str(ex)) raise web.HTTPInternalServerError( reason='Failed to create plugin configuration.') # If all successful then lastly add a schedule to run the new task at startup try: schedule = TimedSchedule() if schedule_type == Schedule.Type.TIMED else \ IntervalSchedule() if schedule_type == Schedule.Type.INTERVAL else \ ManualSchedule() schedule.name = name schedule.process_name = process_name schedule.day = schedule_day m, s = divmod(schedule_time if schedule_time is not None else 0, 60) h, m = divmod(m, 60) schedule.time = datetime.time().replace(hour=h, minute=m, second=s) schedule.repeat = datetime.timedelta( seconds=schedule_repeat if schedule_repeat is not None else 0) schedule.exclusive = True schedule.enabled = False # if "enabled" is supplied, it gets activated in save_schedule() via is_enabled flag # Save schedule await server.Server.scheduler.save_schedule(schedule, is_enabled) schedule = await server.Server.scheduler.get_schedule_by_name(name) except StorageServerError as ex: await config_mgr.delete_category_and_children_recursively(name) _logger.exception("Failed to create schedule. %s", ex.error) raise web.HTTPInternalServerError( reason='Failed to create north instance.') except Exception as ex: await config_mgr.delete_category_and_children_recursively(name) _logger.exception("Failed to create schedule. %s", str(ex)) raise web.HTTPInternalServerError( reason='Failed to create north instance.') except ValueError as e: raise web.HTTPBadRequest(reason=str(e)) else: return web.json_response({ 'name': name, 'id': str(schedule.schedule_id) })
async def create_audit_entry(request): """ Creates a new Audit entry Args: request: POST /foglamp/audit { "source" : "LMTR", # 5 char max "severity" : "WARNING", "details" : { "message" : "Engine oil pressure low" } } :Example: curl -X POST -d '{"source":"LMTR","severity":"WARNING","details":{"message":"Engine oil pressure low"}} http://localhost:8081/foglamp/audit Returns: json object representation of created audit entry { "timestamp" : "2017-06-21T09:39:51.8949395", "source" : "LMTR", "severity" : "WARNING", "details" : { "message" : "Engine oil pressure low" } } """ return_error = False err_msg = "Missing required parameter" payload = await request.json() severity = payload.get("severity") source = payload.get("source") details = payload.get("details") if severity is None or severity == "": err_msg += " severity" return_error = True if source is None or source == "": err_msg += " source" return_error = True if details is None: err_msg += " details" return_error = True if return_error: raise web.HTTPBadRequest(reason=err_msg) if not isinstance(details, dict): raise web.HTTPBadRequest(reason="Details should be a valid json object") try: audit = AuditLogger() await getattr(audit, str(severity).lower())(source, details) # Set timestamp for return message timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3] message = {'timestamp': str(timestamp), 'source': source, 'severity': severity, 'details': details } return web.json_response(message) except AttributeError as e: # Return error for wrong severity method err_msg = "severity type {} is not supported".format(severity) _logger.error("Error in create_audit_entry(): %s | %s", err_msg, str(e)) raise web.HTTPNotFound(reason=err_msg) except Exception as ex: raise web.HTTPInternalServerError(reason=str(ex))
data = await conn.execute( r""" DELETE FROM app_templates T WHERE (T.project_id = %s) and (T.user_id = %s) and (T.id = %s) RETURNING T.id """, (project_id, user["id"], template_id)) data = await data.fetchall() if len(data) > 1: logging.error(f"{data = }. query delete few template once") return web.HTTPServerError() elif len(data) == 1: return web.HTTPNoContent() else: return web.HTTPNotFound() def main(): app = Application() app.add_routes([ web.route('POST', '/v1/login', login_handler), web.route('POST', '/v1/projects', projects_handler), web.route('POST', '/v1/eye/metric_list', eye_metric_list_handler), web.route('POST', '/v1/eye/query', eye_query_handler), web.route('POST', '/v1/cts/overview', cts_handler), web.route('POST', '/v1/cts/detail', cts_detail_handler), web.route('POST', '/v1/cce/clusters/overview', clusters_overview_handler), web.route('POST', '/v1/aom/metric_list', aom_metric_list_handler), web.route('POST', '/v1/aom/query', aom_query_handler),
def wrapped(request, userdata, *args, **kwargs): if ('developer' in userdata) and userdata['developer'] == 1: return fun(request, userdata, *args, **kwargs) raise web.HTTPNotFound()
async def credential_exchange_store(request: web.BaseRequest): """ Request handler for storing credential. Args: request: aiohttp request object Returns: The credential exchange record """ r_time = get_timer() context: AdminRequestContext = request["context"] outbound_handler = request["outbound_message_router"] try: body = await request.json() or {} cred_id = body.get("credential_id") except JSONDecodeError: cred_id = None cred_ex_id = request.match_info["cred_ex_id"] cred_ex_record = None conn_record = None try: async with context.session() as session: try: cred_ex_record = await V20CredExRecord.retrieve_by_id( session, cred_ex_id, ) except StorageNotFoundError as err: raise web.HTTPNotFound(reason=err.roll_up) from err connection_id = cred_ex_record.connection_id conn_record = await ConnRecord.retrieve_by_id( session, connection_id) if not conn_record.is_ready: raise web.HTTPForbidden( reason=f"Connection {connection_id} not ready") cred_manager = V20CredManager(context.profile) cred_ex_record = await cred_manager.store_credential( cred_ex_record, cred_id) ( cred_ex_record, cred_ack_message, ) = await cred_manager.send_cred_ack(cred_ex_record) # We first need to retrieve the the cred_ex_record with detail record # as the record may be auto removed result = await _get_result_with_details(context.profile, cred_ex_record) except ( BaseModelError, IndyHolderError, StorageError, V20CredFormatError, V20CredManagerError, ) as err: # protocol finished OK: do not set cred ex record state null await internal_error( err, web.HTTPBadRequest, cred_ex_record, outbound_handler, ) trace_event( context.settings, cred_ack_message, outcome="credential_exchange_store.END", perf_counter=r_time, ) return web.json_response(result)
def test_with_text() -> None: resp = web.HTTPNotFound(text="Page not found") assert 404 == resp.status assert "Page not found" == resp.text assert "text/plain" == resp.headers["Content-Type"]
def _all(session, info_hash, **kwargs): torrent = session.get_torrent(info_hash) if not torrent: raise web.HTTPNotFound() files = torrent.files return [_to_dict(_file) for _file in files]
async def get(self): file_name = self.request.match_info['file_name'] for mod in server.mods: if mod.file and mod.file.name == file_name: return web.FileResponse(mod.file) return web.HTTPNotFound()