2022-04-30 15:19:43 +00:00
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
|
|
from .common import AssetSerializer
|
|
|
|
from assets.models import DeviceInfo, Host, Database
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'DeviceSerializer', 'HostSerializer', 'DatabaseSerializer'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class DeviceSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = DeviceInfo
|
|
|
|
fields = [
|
|
|
|
'id', 'vendor', 'model', 'sn', 'cpu_model', 'cpu_count',
|
|
|
|
'cpu_cores', 'cpu_vcpus', 'memory', 'disk_total', 'disk_info',
|
2022-08-11 07:45:03 +00:00
|
|
|
'os', 'os_version', 'os_arch', 'hostname_raw', 'number',
|
2022-04-30 15:19:43 +00:00
|
|
|
'cpu_info', 'hardware_info', 'date_updated'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class HostSerializer(AssetSerializer):
|
|
|
|
device_info = DeviceSerializer(read_only=True, allow_null=True)
|
|
|
|
|
|
|
|
class Meta(AssetSerializer.Meta):
|
|
|
|
model = Host
|
|
|
|
fields = AssetSerializer.Meta.fields + ['device_info']
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseSerializer(AssetSerializer):
|
|
|
|
class Meta(AssetSerializer.Meta):
|
|
|
|
model = Database
|
|
|
|
fields_mini = [
|
2022-08-11 07:45:03 +00:00
|
|
|
'id', 'name', 'ip', 'port', 'db_name',
|
2022-04-30 15:19:43 +00:00
|
|
|
]
|
|
|
|
fields_small = fields_mini + [
|
|
|
|
'is_active', 'comment',
|
|
|
|
]
|
|
|
|
fields_fk = [
|
|
|
|
'domain', 'domain_display', 'platform',
|
|
|
|
]
|
|
|
|
fields_m2m = [
|
|
|
|
'nodes', 'nodes_display', 'labels', 'labels_display',
|
|
|
|
]
|
|
|
|
read_only_fields = [
|
|
|
|
'category', 'category_display', 'type', 'type_display',
|
|
|
|
'created_by', 'date_created',
|
|
|
|
]
|
|
|
|
fields = fields_small + fields_fk + fields_m2m + read_only_fields
|
|
|
|
extra_kwargs = {
|
|
|
|
**AssetSerializer.Meta.extra_kwargs,
|
|
|
|
'db_name': {'required': True}
|
|
|
|
}
|