2023-07-14 15:40:18 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
2022-04-08 10:39:01 +00:00
|
|
|
from flask import current_app as app
|
|
|
|
from marshmallow import Schema as MarshmallowSchema
|
2023-07-14 15:40:18 +00:00
|
|
|
from marshmallow import ValidationError, pre_load, validates_schema
|
|
|
|
from marshmallow.fields import DateTime, Dict, Integer, List, Nested, String
|
|
|
|
from marshmallow_enum import EnumField
|
2022-04-08 10:39:01 +00:00
|
|
|
|
2023-07-14 15:40:18 +00:00
|
|
|
from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware
|
2022-04-08 10:39:01 +00:00
|
|
|
from ereuse_devicehub.resources.schemas import Thing
|
|
|
|
|
2023-07-03 16:49:15 +00:00
|
|
|
# from marshmallow_enum import EnumField
|
|
|
|
|
2022-04-08 10:39:01 +00:00
|
|
|
|
|
|
|
class Snapshot_lite_data(MarshmallowSchema):
|
2023-07-03 16:49:15 +00:00
|
|
|
hwmd_version = String(required=True)
|
2022-04-25 15:20:54 +00:00
|
|
|
lshw = Dict(required=True)
|
2023-07-03 16:49:15 +00:00
|
|
|
dmidecode = String(required=True)
|
2022-04-25 15:20:54 +00:00
|
|
|
lspci = String(required=True)
|
2023-07-03 16:49:15 +00:00
|
|
|
hwinfo = String(required=True)
|
|
|
|
smart = List(Dict(), required=False)
|
|
|
|
|
|
|
|
|
|
|
|
class Test(MarshmallowSchema):
|
|
|
|
type = String(required=True)
|
|
|
|
|
|
|
|
|
2023-07-14 15:40:18 +00:00
|
|
|
class Steps(MarshmallowSchema):
|
|
|
|
num = Integer(data_key='step', required=True)
|
2023-07-18 14:31:21 +00:00
|
|
|
start_time = DateTime(data_key='date_init', required=False)
|
|
|
|
end_time = DateTime(data_key='date_end', required=False)
|
2023-07-14 15:40:18 +00:00
|
|
|
severity = EnumField(Severity)
|
|
|
|
|
|
|
|
@pre_load
|
|
|
|
def preload_datas(self, data: dict):
|
|
|
|
data['severity'] = Severity.Info.name
|
2023-07-18 14:31:21 +00:00
|
|
|
data.pop('duration', None)
|
|
|
|
data.pop('commands', None)
|
2023-07-14 15:40:18 +00:00
|
|
|
|
|
|
|
if not data.pop('success', False):
|
|
|
|
data['severity'] = Severity.Error.name
|
|
|
|
|
|
|
|
if data.get('date_init'):
|
|
|
|
data['date_init'] = datetime.fromtimestamp(data['date_init']).isoformat()
|
2023-07-18 14:31:21 +00:00
|
|
|
|
|
|
|
if data.get('date_end'):
|
2023-07-14 15:40:18 +00:00
|
|
|
data['date_end'] = datetime.fromtimestamp(data['date_end']).isoformat()
|
|
|
|
|
|
|
|
|
2023-07-03 16:49:15 +00:00
|
|
|
class Sanitize(MarshmallowSchema):
|
2023-07-14 15:40:18 +00:00
|
|
|
steps = Nested(Steps, many=True, required=True, data_key='erasure_steps')
|
|
|
|
validation = Dict()
|
|
|
|
device_info = Dict()
|
|
|
|
method = Dict(required=True)
|
|
|
|
sanitize_version = String()
|
|
|
|
severity = EnumField(Severity, required=True)
|
|
|
|
|
|
|
|
@pre_load
|
|
|
|
def preload_datas(self, data: dict):
|
|
|
|
data['severity'] = Severity.Info.name
|
|
|
|
|
|
|
|
if not data.pop('result', False):
|
|
|
|
data['severity'] = Severity.Error.name
|
2022-04-08 10:39:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Snapshot_lite(Thing):
|
|
|
|
uuid = String(required=True)
|
|
|
|
version = String(required=True)
|
2022-04-08 14:46:57 +00:00
|
|
|
schema_api = String(required=True)
|
2023-07-14 15:40:18 +00:00
|
|
|
software = EnumField(
|
|
|
|
SnapshotSoftware,
|
|
|
|
required=True,
|
|
|
|
description='The software that generated this Snapshot.',
|
|
|
|
)
|
2022-04-25 09:45:25 +00:00
|
|
|
sid = String(required=True)
|
2022-04-08 10:39:01 +00:00
|
|
|
timestamp = String(required=True)
|
2022-12-15 09:18:22 +00:00
|
|
|
settings_version = String(required=False)
|
2023-07-03 16:49:15 +00:00
|
|
|
hwmd = Nested(Snapshot_lite_data, required=True)
|
|
|
|
tests = Nested(Test, many=True, collection_class=list, required=False)
|
|
|
|
sanitize = Nested(Sanitize, many=True, collection_class=list, required=False)
|
2022-04-08 10:39:01 +00:00
|
|
|
|
|
|
|
@validates_schema
|
|
|
|
def validate_workbench_version(self, data: dict):
|
2023-07-04 14:45:04 +00:00
|
|
|
if data['schema_api'] not in app.config['SCHEMA_HWMD']:
|
2022-04-08 10:39:01 +00:00
|
|
|
raise ValidationError(
|
|
|
|
'Min. supported Workbench version is '
|
|
|
|
'{} but yours is {}.'.format(
|
2023-07-04 14:45:04 +00:00
|
|
|
app.config['SCHEMA_HWMD'][0], data['version']
|
2022-04-08 10:39:01 +00:00
|
|
|
),
|
|
|
|
field_names=['version'],
|
|
|
|
)
|