class Config_Action_Response_Schema(Config_Response_Schema): """Action part in a single item of the config response.""" stdout = Raw(description='Standard output of the execution.') stderr = Raw(description='Standard error output of the execution.') duration = Float(description='Execution time of the action (in seconds') return_code = Integer(required=True, example=0, description='Exit code of the execution (0: no error).')
class BaseSchema(ExpandableSchema): id = String(dump_only=True) score = Float(dump_only=True) highlight = String(dump_only=True) # these are raw because dumping fails if the dates are already strings, as # in the case of data coming from the ES index. created_at = Raw(dump_only=True) updated_at = Raw(dump_only=True)
class Config_Action_Response_Schema(Config_Response_Schema): """Action part in a single item of the config response.""" executed = Str(required=True, example='ls -al', description='Command executed.') stdout = Raw(many=True, description='Standard output of the execution.') stderr = Raw(many=True, description='Standard error output of the execution.') duration = Float(description='Execution time of the action (in seconds') daemon = Str(example='firewall', description='Key used to execute the command as daemon.') return_code = Integer( required=True, example=0, description='Exit code of the execution (0: no error).')
class ChoiceSchema(Schema): label = Str() id = Raw() @staticmethod def from_dto(choice: CustomFieldOption): return ChoiceSchema().dump(choice)
class NotificationSchema(BaseSchema): SCHEMATA = { Alert: AlertSchema, Role: RoleSchema, Document: CombinedSchema, Entity: CombinedSchema, Collection: CollectionSchema } actor_id = String() event = Nested(EventSchema(), dump_only=True) params = Raw() @pre_dump(pass_many=True) def expand(self, objs, many=False): results = [] for obj in ensure_list(objs): params = {} for name, clazz, value in obj.iterparams(): schema = self.SCHEMATA.get(clazz) data = self._get_object(clazz, value) if data is not None: params[name], _ = schema().dump(data) results.append({ 'id': obj.id, 'created_at': obj.created_at, 'actor_id': obj.actor_id, 'event': obj.event, 'params': params }) return results
class FileSchema(ContentSchema): ''' Schema for the File model ''' content_id = Integer() mime_id = Integer(dump_only=True) original_name = String(dump_only=True) file_size = Float(dump_only=True) content = Raw(load_only=True)
class ContributionFieldValueSchema(mm.Schema): id = column2field(ContributionFieldValue.contribution_field_id, attribute='contribution_field_id') name = String(attribute='contribution_field.title') value = Raw(attribute='friendly_data') class Meta: model = ContributionFieldValue fields = ('id', 'name', 'value')
class eBPF_Program_Instance_Parameter_Schema(Schema): """Parameter of the eBPF program instance installed in an execution environment.""" id = Str(required=True, example='interface', description='Parameter id.') value = Raw(required=True, example='en0', description='Paremeter value.'), timestamp = Date_Time( format=FORMAT, readonly=True, description="Timestamp of the last time the parameter was set.")
class Config_Response_Schema(Base_Schema): """Response for config endpoint.""" id = Str(required=True, example='start', description='Config id.') data = Raw(description='Configuration data.') timestamp = Date_Time(format=FORMAT, required=True, description='Timestamp when the configuration is done.') type = Str(enum=RESPONSE_TYPES, example=RESPONSE_TYPES[0], description='Type of the response.', validate=validate.OneOf(RESPONSE_TYPES)) error = Boolean(default=False, example=True, description='Indicate the presence of an error.')
class Config_Parameter_Request_Schema(Base_Schema): """Parameter part in a single item of the code request.""" id = Str(required=True, example='period', description='Id of the parameter.') schema = Str(required=True, enum=PARAMETER_SCHEMAS, example='yaml', description='Scheme.', validate=validate.OneOf(PARAMETER_SCHEMAS)) source = Str(required=True, example=EXAMPLE_FILENAME, description='Source filename.') path = List(Str(required=True, example='period', description='Key path.')) value = Raw(required=True, example='10s', description='Parameter new value.')
class Exception_Response_Schema(Schema): reason = Raw(required=True, example='Connection timeout', description='Exception reason.') filename = Str(required=True, example='lib/connection.py', description='Filename where the exception is raised.') line = Integer(required=True, example=80, description='Line where the exception is raised.')
class ExperimentResultDataSchema(BaseSchema): """Schema for ExperimentResultData.""" counts = Nested(ObjSchema, validate=PatternProperties( {Regexp('^0x([0-9A-Fa-f])+$'): Integer()})) snapshots = Nested(ObjSchema) memory = List(Raw(), validate=Length(min=1)) statevector = List(Complex(), validate=Length(min=1)) unitary = List(List(Complex(), validate=Length(min=1)), validate=Length(min=1))
class Algorithm_Catalog_Parameter_Schema(Schema): """Algorithm parameter.""" id = Str(required=True, example='frequency', description='Parameter id.') type = Str(required=True, enum=PARAMETER_TYPES, example=PARAMETER_TYPES[0], description='Parameter type.', validate=validate.OneOf(PARAMETER_TYPES)) list = Bool(default=False, example=True, description='Indicate if the parameter can have multiple values.') values = List_or_One(Str, example='mysql', description='Possible values if the parameter type is choice.') encoding_scheme = Str(default='base64', example='base64', description='Encoding scheme used to store the binary data') description = Str(example='Enable the algorithm.', description='Short description of the parameter.') example = Raw(example='10s', description='Example of parameter value.')
class NotificationSchema(BaseSchema): SCHEMATA = { Alert: AlertSchema, Role: RoleSchema, Document: CombinedSchema, Entity: CombinedSchema, Collection: CollectionSchema } actor_id = String() event = Nested(EventSchema(), dump_only=True) params = Raw() def _resolve_alerts(self, cache): alerts = set() for (type_, id_) in cache.keys(): if type_ == Alert: alerts.add(id_) if not len(alerts): return for alert in Alert.all_by_ids(alerts, deleted=True): cache[(Alert, str(alert.id))] = alert @pre_dump(pass_many=True) def expand(self, objs, many=False): cache = {} for obj in ensure_list(objs): for name, clazz, value in obj.iterparams(): cache[(clazz, str(value))] = None self._resolve_alerts(cache) self._resolve_roles(cache) self._resolve_index(cache) results = [] for obj in ensure_list(objs): params = {} for name, clazz, value in obj.iterparams(): schema = self.SCHEMATA.get(clazz) value = cache.get((clazz, str(value))) if value is not None: params[name], _ = schema().dump(value) results.append({ 'id': obj.id, 'created_at': obj.created_at, 'actor_id': obj.actor_id, 'event': obj.event, 'params': params }) return results
class eBPF_Program_Catalog_Parameter_Schema(Schema): """eBPF program configuration.""" id = Str(required=True, example='interface', description='Parameter id.') type = Str(required=True, description='Parameter type.', enum=PARAMETER_TYPES, example='integer', validate=validate.OneOf(PARAMETER_TYPES)) list = Bool( default=False, example=True, description='Indicate if the parameter can have multiple values.') values = List_or_One( Str, example='yes', description='Possible values if the parameter type is choice.') description = Str(example='Network Interface to attach.', description='Short description of the parameter.') example = Raw(example='eno0', description='Example of parameter value.')
class NotificationSchema(BaseSchema): SCHEMATA = { Alert: AlertSchema, Role: RoleSchema, Document: CombinedSchema, Entity: CombinedSchema, Collection: CollectionSchema } actor_id = String() event = Nested(EventSchema(), dump_only=True) params = Raw() @pre_dump(pass_many=True) def expand(self, objs, many=False): cache = {} for obj in ensure_list(objs): for name, clazz, value in obj.iterparams(): cache[(clazz, str(value))] = None self._resolve_alerts(cache) self._resolve_roles(cache) self._resolve_entities(cache) self._resolve_collections(cache) results = [] for obj in ensure_list(objs): params = {} for name, clazz, value in obj.iterparams(): schema = self.SCHEMATA.get(clazz) value = cache.get((clazz, str(value))) if value is not None: params[name], _ = schema().dump(value) results.append({ 'id': obj.id, 'created_at': obj.created_at, 'actor_id': obj.actor_id, 'event': obj.event, 'params': params }) return results
class Agent_Catalog_Parameter_Schema(Schema): """Agent parameter.""" id = Str(required=True, example='log-period', description='Parameter id.') type = Str(required=True, enum=PARAMETER_TYPES, example=PARAMETER_TYPES[0], description='Parameter type.', validate=validate.OneOf(PARAMETER_TYPES)) config = Nested(Agent_Catalog_Parameter_Config_Schema, unknown='INCLUDE', required=True, description='Parameter configuration.') list = Bool( default=False, example=True, description='Indicate if the parameter can have multiple values.') values = List_or_One( Str, example='mysql', description='Possible values if the parameter type is choice.') description = Str(example='Enable the agent.', description='Short description of the parameter.') example = Raw(example='10s', description='Example of parameter value.')
class Geometry(Schema): type = EnumField(GeometryType, default=GeometryType.POINT) coordinates = Raw()
class Algorithm_Instance_Parameter_Schema(Schema): """Parameter of the algorithm instance.""" id = Str(required=True, example='period', description='Parameter id.') value = Raw(required=True, example='10s', description='Paremeter value.'),
class Agent_Instance_Parameter_Schema(Schema): """Parameter of the agent instance installed in an execution environment.""" id = Str(required=True, example='period', description='Parameter id.') value = Raw(required=True, example='10s', description='Paremeter value.'),
class DatedSchema(object): # these are raw because dumping fails if the dates are already strings, as # in the case of data coming from the ES index. created_at = Raw(dump_only=True) updated_at = Raw(dump_only=True)
class Config_Parameter_Value_Response_Schema(Base_Schema): """Parameter value part in a single item of the config response.""" new = Raw(required=True, example='5s', description='New value.') old = Raw(required=True, example='10s', description='Old value', allow_none=True)