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 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