async def update_object(self, request: web.Request): data, access, obj_id, query, search = await self._parse_common_data_from_request(request) obj = self._api_manager.find_and_update_object(self.ram_key, data, search) if not obj: raise JsonHttpNotFound(f'{self.description.capitalize()} not found: {obj_id}') return obj
async def get_agent(self, operation: Operation, data: dict): paw = data['paw'] try: agent = [a for a in operation.agents if a.paw == paw][0] except IndexError: raise JsonHttpNotFound(f'Agent {data["paw"]} was not found.') return agent
async def get_config_with_name(self, request): config_name = request.match_info['name'] try: config = self._api_manager.get_filtered_config(config_name) except ConfigNotFound: raise JsonHttpNotFound(f'Config not found: {config_name}') return web.json_response(config)
async def get_operation_object(self, operation_id: str, access: dict): try: operation = (await self._data_svc.locate('operations', {'id': operation_id}))[0] except IndexError: raise JsonHttpNotFound(f'Operation not found: {operation_id}') if operation.match(access): return operation raise JsonHttpForbidden( f'Cannot view operation due to insufficient permissions: {operation_id}' )
async def delete_object(self, request: web.Request): obj_id = request.match_info.get(self.id_property) access = await self.get_request_permissions(request) query = {self.id_property: obj_id} search = {**query, **access} if not self._api_manager.find_object(self.ram_key, search): raise JsonHttpNotFound(f'{self.description.capitalize()} not found: {obj_id}') await self._api_manager.remove_object_from_memory_by_id(identifier=obj_id, ram_key=self.ram_key, id_property=self.id_property)
async def get_object(self, request: web.Request): data, access, obj_id, query, search = await self._parse_common_data_from_request(request) obj = self._api_manager.find_object(self.ram_key, query) if not obj: raise JsonHttpNotFound(f'{self.description.capitalize()} not found: {obj_id}') elif obj.access not in access['access']: raise JsonHttpForbidden(f'Cannot view {self.description} due to insufficient permissions: {obj_id}') include = request['querystring'].get('include') exclude = request['querystring'].get('exclude') return self._api_manager.dump_object_with_filters(obj, include, exclude)
async def get_planner_by_id(self, request: web.Request): planner_id = request.match_info['planner_id'] include = request['querystring'].get('include') exclude = request['querystring'].get('exclude') search = dict(planner_id=planner_id) planner = self._api_manager.get_object_with_filters('planners', search=search, include=include, exclude=exclude) if not planner: raise JsonHttpNotFound(f'Planner not found: {planner_id}') return web.json_response(planner)
async def get_potential_links(self, operation_id: str, access: dict, paw: str = None): operation = await self.get_operation_object(operation_id, access) if operation.finish: return [] if paw: agents = [a for a in operation.agents if a.paw == paw] if not agents: raise JsonHttpNotFound(f'Agent not found: {paw}') else: agents = operation.agents potential_abilities = await self.services[ 'rest_svc'].build_potential_abilities(operation) operation.potential_links = await self.services[ 'rest_svc'].build_potential_links(operation, agents, potential_abilities) potential_links = [ potential_link.display for potential_link in operation.potential_links ] return potential_links
def get_contact_report(self, contact: str = None): contact = contact.upper() if contact in self.contact_svc.report: return self.contact_svc.report.get(contact) raise JsonHttpNotFound(f'Contact not found: {contact}')
def search_operation_for_link(self, operation: Operation, link_id: str): for link in operation.chain: if link.id == link_id: return link raise JsonHttpNotFound( f'Link {link_id} was not found in Operation {operation.id}')