def lambda_handler(event, context): """ This is the main entry point for performing an action. The action evaluator puts an action message on the actions queue and here's where they're popped off and dealt with. """ records_table = dynamodb.Table(DYNAMODB_TABLE) lambda_init_once() for sqs_record in event["Records"]: # TODO research max # sqs records / lambda_handler invocation action_message = ActionMessage.from_aws_json(sqs_record["body"]) logger.info("Performing action: action_message = %s", action_message) if action_performer := ActionPerformer.get( action_message.action_label.value): action_performer.perform_action(action_message) ActionEvent( content_id=action_message.content_key, performed_at=datetime.datetime.now(), action_label=action_message.action_label.value, # v0 Hacks: the label rules model is super mutable we store basically the whole state # for each action performed. (~gross but until we have a unique id and version # it's what we've got). # Right now this just make json blob for action_performer and a list of # json blobs for action_rules that we can store and recover if needed. action_performer=action_performer.to_aws_json(), action_rules=[ rule.to_aws_json() for rule in action_message.action_rules ], ).write_to_table(records_table)
def fetch_all_actions() -> FetchAllActionsResponse: """ Return all action configs. """ action_configs = ActionPerformer.get_all() return FetchAllActionsResponse( actions_response=[config.__dict__ for config in action_configs])
def create_action(request: CreateUpdateActionRequest) -> CreateActionResponse: """ Create an action. """ config = ActionPerformer._get_subtypes_by_name()[request.config_subtype]( **{"name": request.name, **request.fields} ) hmaconfig.create_config(config) return CreateActionResponse(response="The action config is created.")
def create_action( self, action_performer: ActionPerformer, ): return self.api.create_action( name=action_performer.name, config_subtype=action_performer.get_config_subtype(), fields={ key: value for key, value in vars(action_performer).items() if key not in {"name", "config_subtype"} }, )
def update_action(request: CreateUpdateActionRequest, old_name: str, old_config_sub_stype: str) -> UpdateActionResponse: """ Update the action, name, url, and headers for action with name=<old_name> and subtype=<old_config_sub_stype>. """ if old_name != request.name or old_config_sub_stype != request.config_subtype: # The name field can't be updated because it is the primary key # The config sub type can't be updated because it is the config class level param delete_action(old_name) create_action(request) else: config = ActionPerformer._get_subtypes_by_name()[ request.config_subtype].getx(request.name) for key, value in request.fields.items(): setattr(config, key, value) hmaconfig.update_config(config) return UpdateActionResponse(response="The action config is updated.")