diff --git a/apps/assets/api/asset/__init__.py b/apps/assets/api/asset/__init__.py index 85972fb52..d34ec0922 100644 --- a/apps/assets/api/asset/__init__.py +++ b/apps/assets/api/asset/__init__.py @@ -1,2 +1,3 @@ from .common import * +from .host import * from .permission import * diff --git a/apps/assets/api/asset/common.py b/apps/assets/api/asset/common.py index 6b7fa7d2c..017986fbf 100644 --- a/apps/assets/api/asset/common.py +++ b/apps/assets/api/asset/common.py @@ -36,7 +36,7 @@ class AssetViewSet(SuggestionMixin, FilterAssetByNodeMixin, OrgBulkModelViewSet) 'protocols': ['exact', 'icontains'] } search_fields = ("hostname", "ip") - ordering_fields = ("hostname", "ip", "port", "cpu_cores") + ordering_fields = ("hostname", "ip", "port") ordering = ('hostname', ) serializer_classes = { 'default': serializers.AssetSerializer, diff --git a/apps/assets/api/asset/host.py b/apps/assets/api/asset/host.py new file mode 100644 index 000000000..a27d731f2 --- /dev/null +++ b/apps/assets/api/asset/host.py @@ -0,0 +1,15 @@ + +from assets.models import Host +from assets.serializers import HostSerializer +from .common import AssetViewSet + +__all__ = ['HostViewSet'] + + +class HostViewSet(AssetViewSet): + model = Host + + def get_serializer_classes(self): + serializer_classes = super().get_serializer_classes() + serializer_classes['default'] = HostSerializer + return serializer_classes diff --git a/apps/assets/migrations/0092_hardware.py b/apps/assets/migrations/0092_hardware.py index b158363ef..83d456b26 100644 --- a/apps/assets/migrations/0092_hardware.py +++ b/apps/assets/migrations/0092_hardware.py @@ -13,7 +13,7 @@ class Migration(migrations.Migration): operations = [ migrations.CreateModel( - name='HostInfo', + name='DeviceInfo', fields=[ ('vendor', models.CharField(blank=True, max_length=64, null=True, verbose_name='Vendor')), ('model', models.CharField(blank=True, max_length=54, null=True, verbose_name='Model')), @@ -36,7 +36,7 @@ class Migration(migrations.Migration): ('host', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='assets.host', related_name='info', verbose_name='Host')), ], options={ - 'verbose_name': 'HostInfo', + 'verbose_name': 'DeviceInfo', }, ), ] diff --git a/apps/assets/migrations/0093_auto_20220403_1627.py b/apps/assets/migrations/0093_auto_20220403_1627.py index e01febe9b..497a56c6c 100644 --- a/apps/assets/migrations/0093_auto_20220403_1627.py +++ b/apps/assets/migrations/0093_auto_20220403_1627.py @@ -7,7 +7,7 @@ from django.db import migrations def migrate_hardware(apps, *args): host_model = apps.get_model('assets', 'Host') asset_model = apps.get_model('assets', 'Asset') - hardware_model = apps.get_model('assets', 'HostInfo') + hardware_model = apps.get_model('assets', 'DeviceInfo') created = 0 batch_size = 1000 diff --git a/apps/assets/models/asset/host.py b/apps/assets/models/asset/host.py index f7533d5fd..ddaa1766d 100644 --- a/apps/assets/models/asset/host.py +++ b/apps/assets/models/asset/host.py @@ -9,8 +9,8 @@ class Host(Asset): pass -class HostInfo(CommonModelMixin): - host = models.OneToOneField(Host, related_name='info', on_delete=models.CASCADE, +class DeviceInfo(CommonModelMixin): + host = models.OneToOneField(Host, related_name='device_info', on_delete=models.CASCADE, verbose_name=_("Host"), unique=True) # Collect vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor')) @@ -53,4 +53,4 @@ class HostInfo(CommonModelMixin): return '{} of {}'.format(self.hardware_info, self.host.hostname) class Meta: - verbose_name = _("HostInfo") + verbose_name = _("DeviceInfo") diff --git a/apps/assets/models/asset/remote_app.py b/apps/assets/models/asset/remote_app.py index e69de29bb..60e8872c8 100644 --- a/apps/assets/models/asset/remote_app.py +++ b/apps/assets/models/asset/remote_app.py @@ -0,0 +1,5 @@ +from .common import Asset + + +class RemoteApp(Asset): + pass diff --git a/apps/assets/serializers/asset/__init__.py b/apps/assets/serializers/asset/__init__.py index 55e5f844b..51640e7cf 100644 --- a/apps/assets/serializers/asset/__init__.py +++ b/apps/assets/serializers/asset/__init__.py @@ -1 +1,2 @@ from .common import * +from .host import * diff --git a/apps/assets/serializers/asset/host.py b/apps/assets/serializers/asset/host.py index 1b40426cc..08d159713 100644 --- a/apps/assets/serializers/asset/host.py +++ b/apps/assets/serializers/asset/host.py @@ -1,12 +1,14 @@ from rest_framework import serializers from .common import AssetSerializer -from assets.models import HostInfo +from assets.models import DeviceInfo, Host + +__all__ = ['DeviceSerializer', 'HostSerializer'] -class HardwareSerializer(serializers.ModelSerializer): +class DeviceSerializer(serializers.ModelSerializer): class Meta: - model = HostInfo + model = DeviceInfo fields = [ 'id', 'vendor', 'model', 'sn', 'cpu_model', 'cpu_count', 'cpu_cores', 'cpu_vcpus', 'memory', 'disk_total', 'disk_info', @@ -16,4 +18,8 @@ class HardwareSerializer(serializers.ModelSerializer): class HostSerializer(AssetSerializer): - hardware_info = HardwareSerializer(read_only=True) + device_info = DeviceSerializer(read_only=True, allow_null=True) + + class Meta(AssetSerializer.Meta): + model = Host + fields = AssetSerializer.Meta.fields + ['device_info'] diff --git a/apps/assets/urls/api_urls.py b/apps/assets/urls/api_urls.py index 434f09441..6788a7329 100644 --- a/apps/assets/urls/api_urls.py +++ b/apps/assets/urls/api_urls.py @@ -11,6 +11,7 @@ app_name = 'assets' router = BulkRouter() router.register(r'assets', api.AssetViewSet, 'asset') +router.register(r'hosts', api.HostViewSet, 'asset') router.register(r'accounts', api.AccountViewSet, 'account') router.register(r'account-secrets', api.AccountSecretsViewSet, 'account-secret') router.register(r'platforms', api.AssetPlatformViewSet, 'platform') diff --git a/apps/common/mixins/api/serializer.py b/apps/common/mixins/api/serializer.py index 52b0637df..a416fa990 100644 --- a/apps/common/mixins/api/serializer.py +++ b/apps/common/mixins/api/serializer.py @@ -15,8 +15,12 @@ class SerializerMixin: serializer_classes = None single_actions = ['put', 'retrieve', 'patch'] + def get_serializer_classes(self): + return getattr(self, 'serializer_classes', None) + def get_serializer_class_by_view_action(self): - if not hasattr(self, 'serializer_classes'): + serializer_classes = self.get_serializer_classes() + if serializer_classes is None: return None if not isinstance(self.serializer_classes, dict): return None