def delete_event_source(trigger_storage: TriggerStorage, workspace: str, event_source_name: str): if trigger_storage.key_exists(workspace=workspace, document_id='event_sources', key=event_source_name): trigger_storage.delete_key(workspace=workspace, document_id='event_sources', key=event_source_name) return { "message": "Event source {} deleted".format(event_source_name) }, 200 else: return { "error": "Event source {} not found".format(event_source_name) }, 404
def get_workspace(trigger_storage: TriggerStorage, workspace: str): triggers = trigger_storage.get(workspace=workspace, document_id='triggers') triggerIDs = [trigger["id"] for trigger in triggers] event_sources = trigger_storage.get(workspace=workspace, document_id='event_sources') event_source_names = [ event_source['name'] for event_source in event_sources ] global_context = trigger_storage.get(workspace=workspace, document_id='global_context') return { 'triggers': triggerIDs, 'event_sources': event_source_names, 'global_context': global_context }, 200
def create_workspace(trigger_storage: TriggerStorage, workspace: str, global_context: dict, event_source: dict): if trigger_storage.workspace_exists(workspace=workspace): return { 'error': 'Workspace {} already exists'.format(workspace), 'err_code': 2 }, 400 # Workspace name can only contain alphanumeric, hyphens or underscore characters if not re.fullmatch(r"^[a-zA-Z0-9.-]*$", workspace): return {'error': 'Illegal workspace name', 'err_code': 3}, 400 if {'name', 'class', 'parameters'} != set(event_source): return {'error': 'Invalid event source', 'err_code': 4}, 400 trigger_storage.create_workspace(workspace, {event_source['name']: event_source}, global_context) return {'message': 'Created workspace {}'.format(workspace)}, 200
def delete_trigger(trigger_storage: TriggerStorage, workspace: str, trigger_id: str): deleted = trigger_storage.delete_key(workspace=workspace, document_id='triggers', key=trigger_id) if deleted != 0: return {'message': 'Deleted {}'.format(trigger_id)}, 200 else: return {'error': 'Trigger {} not found'.format(trigger_id)}, 404
def add_triggers(trigger_storage: TriggerStorage, workspace: str, triggers: list): accepted_triggers = [] rejected_triggers = [] for trigger in triggers: # Check trigger schema if not isinstance(trigger, dict): continue if { 'id', 'condition', 'action', 'context', 'context_parser', 'activation_events', 'transient' } != set(trigger): trigger['reason'] = 'Malformed trigger schema' rejected_triggers.append(trigger) continue # Named trigger, check if it already exists if trigger['id'] and trigger_storage.key_exists(workspace=workspace, document_id='triggers', key=trigger['id']): trigger['reason'] = "Trigger {} already exists".format( trigger['id']) rejected_triggers.append(trigger) continue trigger_uuid = uuid4() trigger[ 'id'] = trigger_uuid.hex if not trigger['id'] else trigger['id'] trigger['uuid'] = str(trigger_uuid) trigger['workspace'] = workspace trigger['timestamp'] = datetime.utcnow().isoformat() trigger_storage.set_key(workspace=workspace, document_id='triggers', key=trigger['id'], value=trigger) accepted_triggers.append(trigger) return { 'accepted_triggers': accepted_triggers, 'rejected_triggers': rejected_triggers }, 200
def get_trigger(trigger_storage: TriggerStorage, workspace: str, trigger_id: str): trigger = trigger_storage.get_key(workspace=workspace, document_id='triggers', key=trigger_id) if trigger is not None: return {trigger_id: trigger}, 200 else: return {'error': 'Trigger {} not found'.format(trigger_id)}, 404
def get_event_source(trigger_storage: TriggerStorage, workspace: str, event_source_name: str): event_source = trigger_storage.get_key(workspace=workspace, document_id='event_sources', key=event_source_name) if event_source is not None: return {event_source_name: event_source}, 200 else: return { "error": "Event source {} not found".format(event_source_name) }, 404
def add_event_source(trigger_storage: TriggerStorage, workspace: str, event_source: dict, overwrite: bool): # Check eventsource schema if {'name', 'class', 'parameters'} != set(event_source): return {"error": "Invalid eventsource object"} exists = trigger_storage.key_exists(workspace=workspace, document_id='event_sources', key=event_source['name']) if not exists or (exists and overwrite): trigger_storage.set_key(workspace=workspace, document_id='event_sources', key=event_source['name'], value=event_source.copy()) res = { "message": "Created/updated {}".format(event_source['name']) }, 201 else: res = { "error": "Event source {} already exists".format(event_source['name']) }, 409 return res
def delete_workspace(trigger_storage: TriggerStorage, workspace: str): trigger_storage.delete_workspace(workspace=workspace) return {'message': 'Workspace {} deleted'.format(workspace)}, 200
def list_triggers(trigger_storage: TriggerStorage, workspace: str): trigger_ids = trigger_storage.keys(workspace=workspace, document_id='triggers') return trigger_ids, 200
def list_event_sources(trigger_storage: TriggerStorage, workspace: str): event_sources = trigger_storage.keys(workspace=workspace, document_id='event_sources') return event_sources, 200