diff --git a/apps/assets/api/asset/common.py b/apps/assets/api/asset/common.py index 017986fbf..4cec7495a 100644 --- a/apps/assets/api/asset/common.py +++ b/apps/assets/api/asset/common.py @@ -31,7 +31,6 @@ class AssetViewSet(SuggestionMixin, FilterAssetByNodeMixin, OrgBulkModelViewSet) 'hostname': ['exact'], 'ip': ['exact'], 'system_users__id': ['exact'], - 'platform__base': ['exact'], 'is_active': ['exact'], 'protocols': ['exact', 'icontains'] } diff --git a/apps/assets/api/platform.py b/apps/assets/api/platform.py index a7cb798ae..030f90905 100644 --- a/apps/assets/api/platform.py +++ b/apps/assets/api/platform.py @@ -10,7 +10,7 @@ __all__ = ['AssetPlatformViewSet'] class AssetPlatformViewSet(ModelViewSet): queryset = Platform.objects.all() serializer_class = PlatformSerializer - filterset_fields = ['name', 'base'] + filterset_fields = ['name'] search_fields = ['name'] def check_object_permissions(self, request, obj): diff --git a/apps/assets/migrations/0095_auto_20220406_1541.py b/apps/assets/migrations/0095_auto_20220406_1541.py index 7ba5fb36a..9647aed54 100644 --- a/apps/assets/migrations/0095_auto_20220406_1541.py +++ b/apps/assets/migrations/0095_auto_20220406_1541.py @@ -20,7 +20,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='asset', name='type', - field=models.CharField(default='linux', max_length=128, verbose_name='Type'), + field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], max_length=128, verbose_name='Type'), preserve_default=False, ), ] diff --git a/apps/assets/migrations/0097_auto_20220407_1726.py b/apps/assets/migrations/0097_auto_20220407_1726.py index 5459d8cee..e16a6ed92 100644 --- a/apps/assets/migrations/0097_auto_20220407_1726.py +++ b/apps/assets/migrations/0097_auto_20220407_1726.py @@ -22,9 +22,9 @@ class Migration(migrations.Migration): preserve_default=False, ), migrations.AlterField( - model_name='asset', + model_name='platform', name='type', - field=models.CharField(choices=[('linux', 'Linux'), ('unix', 'Unix'), ('windows', 'Windows'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], max_length=128, verbose_name='Type'), + field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], default='Linux', max_length=32, verbose_name='Type'), ), migrations.AlterField( model_name='systemuser', diff --git a/apps/assets/migrations/0098_auto_20220407_1730.py b/apps/assets/migrations/0098_auto_20220407_1730.py deleted file mode 100644 index a50986cbc..000000000 --- a/apps/assets/migrations/0098_auto_20220407_1730.py +++ /dev/null @@ -1,23 +0,0 @@ -# Generated by Django 3.1.14 on 2022-04-07 09:30 - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('assets', '0097_auto_20220407_1726'), - ] - - operations = [ - migrations.AlterField( - model_name='asset', - name='type', - field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], max_length=128, verbose_name='Type'), - ), - migrations.AlterField( - model_name='platform', - name='type', - field=models.CharField(choices=[('linux', 'Linux'), ('windows', 'Windows'), ('unix', 'Unix'), ('bsd', 'BSD'), ('macos', 'MacOS'), ('mainframe', 'Mainframe'), ('other_host', 'Other host'), ('switch', 'Switch'), ('router', 'Router'), ('firewall', 'Firewall'), ('other_network', 'Other device'), ('mysql', 'MySQL'), ('mariadb', 'MariaDB'), ('postgresql', 'PostgreSQL'), ('oracle', 'Oracle'), ('sqlserver', 'SQLServer'), ('mongodb', 'MongoDB'), ('redis', 'Redis'), ('chrome', 'Chrome'), ('vsphere', 'vSphere client'), ('mysql_workbench', 'MySQL workbench'), ('custom_remote_app', 'Custom'), ('k8s', 'Kubernetes')], default='Linux', max_length=32, verbose_name='Base'), - ), - ] diff --git a/apps/assets/models/asset/__init__.py b/apps/assets/models/asset/__init__.py index 51640e7cf..8f2ad65ee 100644 --- a/apps/assets/models/asset/__init__.py +++ b/apps/assets/models/asset/__init__.py @@ -1,2 +1,3 @@ from .common import * +from .device_info import * from .host import * diff --git a/apps/assets/models/asset/device_info.py b/apps/assets/models/asset/device_info.py new file mode 100644 index 000000000..7410af04f --- /dev/null +++ b/apps/assets/models/asset/device_info.py @@ -0,0 +1,51 @@ +from django.utils.translation import gettext_lazy as _ +from django.db import models + +from common.mixins.models import CommonModelMixin + +__all__ = ['DeviceInfo'] + + +class DeviceInfo(CommonModelMixin): + # Collect + vendor = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Vendor')) + model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model')) + sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number')) + + cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model')) + cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count')) + cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores')) + cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus')) + memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory')) + disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) + disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) + + os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) + os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) + os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) + hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw')) + + @property + def cpu_info(self): + info = "" + if self.cpu_model: + info += self.cpu_model + if self.cpu_count and self.cpu_cores: + info += "{}*{}".format(self.cpu_count, self.cpu_cores) + return info + + @property + def hardware_info(self): + if self.cpu_count: + return '{} Core {} {}'.format( + self.cpu_vcpus or self.cpu_count * self.cpu_cores, + self.memory, self.disk_total + ) + else: + return '' + + def __str__(self): + return '{} of {}'.format(self.hardware_info, self.host.hostname) + + class Meta: + verbose_name = _("DeviceInfo") diff --git a/apps/assets/models/asset/host.py b/apps/assets/models/asset/host.py index a729c2da6..3dbe4ca3d 100644 --- a/apps/assets/models/asset/host.py +++ b/apps/assets/models/asset/host.py @@ -1,59 +1,17 @@ -from django.utils.translation import gettext_lazy as _ from django.db import models +from django.utils.translation import gettext_lazy as _ from assets.const import Category -from common.mixins.models import CommonModelMixin from .common import Asset +from .device_info import DeviceInfo class Host(Asset): + device_info = models.ForeignKey(DeviceInfo, on_delete=models.SET_NULL, + null=True, verbose_name=_("Device info")) + def save(self, *args, **kwargs): self.category = Category.HOST return super().save(*args, **kwargs) -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')) - model = models.CharField(max_length=54, null=True, blank=True, verbose_name=_('Model')) - sn = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Serial number')) - - cpu_model = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU model')) - cpu_count = models.IntegerField(null=True, verbose_name=_('CPU count')) - cpu_cores = models.IntegerField(null=True, verbose_name=_('CPU cores')) - cpu_vcpus = models.IntegerField(null=True, verbose_name=_('CPU vcpus')) - memory = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Memory')) - disk_total = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk total')) - disk_info = models.CharField(max_length=1024, null=True, blank=True, verbose_name=_('Disk info')) - - os = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('OS')) - os_version = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('OS version')) - os_arch = models.CharField(max_length=16, blank=True, null=True, verbose_name=_('OS arch')) - hostname_raw = models.CharField(max_length=128, blank=True, null=True, verbose_name=_('Hostname raw')) - - @property - def cpu_info(self): - info = "" - if self.cpu_model: - info += self.cpu_model - if self.cpu_count and self.cpu_cores: - info += "{}*{}".format(self.cpu_count, self.cpu_cores) - return info - - @property - def hardware_info(self): - if self.cpu_count: - return '{} Core {} {}'.format( - self.cpu_vcpus or self.cpu_count * self.cpu_cores, - self.memory, self.disk_total - ) - else: - return '' - - def __str__(self): - return '{} of {}'.format(self.hardware_info, self.host.hostname) - - class Meta: - verbose_name = _("DeviceInfo") diff --git a/apps/assets/models/asset/network.py b/apps/assets/models/asset/network.py index 42d35e06b..a17d99a2e 100644 --- a/apps/assets/models/asset/network.py +++ b/apps/assets/models/asset/network.py @@ -1,5 +1,10 @@ +from django.db import models +from django.utils.translation import gettext_lazy as _ + from .common import Asset +from .device_info import DeviceInfo class Network(Asset): - pass + device_info = models.ForeignKey(DeviceInfo, on_delete=models.SET_NULL, + null=True, verbose_name=_("Device info")) diff --git a/apps/assets/models/platform.py b/apps/assets/models/platform.py index 63ea42ff1..675d3542f 100644 --- a/apps/assets/models/platform.py +++ b/apps/assets/models/platform.py @@ -12,17 +12,9 @@ class Platform(models.Model): ('utf8', 'UTF-8'), ('gbk', 'GBK'), ) - BASE_CHOICES = ( - ('Linux', 'Linux'), - ('Unix', 'Unix'), - ('MacOS', 'MacOS'), - ('BSD', 'BSD'), - ('Windows', 'Windows'), - ('Other', 'Other'), - ) name = models.SlugField(verbose_name=_("Name"), unique=True, allow_unicode=True) category = models.CharField(max_length=16, choices=Category.choices, verbose_name=_("Category")) - type = models.CharField(choices=AllTypes.choices, max_length=32, default='Linux', verbose_name=_("Base")) + type = models.CharField(choices=AllTypes.choices, max_length=32, default='Linux', verbose_name=_("Type")) charset = models.CharField(default='utf8', choices=CHARSET_CHOICES, max_length=8, verbose_name=_("Charset")) meta = JsonDictTextField(blank=True, null=True, verbose_name=_("Meta")) internal = models.BooleanField(default=False, verbose_name=_("Internal")) @@ -36,10 +28,10 @@ class Platform(models.Model): return linux.id def is_windows(self): - return self.base.lower() in ('windows',) + return self.type.lower() in ('windows',) def is_unixlike(self): - return self.base.lower() in ("linux", "unix", "macos", "bsd") + return self.type.lower() in ("linux", "unix", "macos", "bsd") def __str__(self): return self.name diff --git a/apps/assets/serializers/asset/common.py b/apps/assets/serializers/asset/common.py index 94e5be260..4070c6fc7 100644 --- a/apps/assets/serializers/asset/common.py +++ b/apps/assets/serializers/asset/common.py @@ -182,7 +182,7 @@ class PlatformSerializer(serializers.ModelSerializer): class Meta: model = Platform fields = [ - 'id', 'name', 'base', 'charset', + 'id', 'name', 'category', 'type', 'charset', 'internal', 'meta', 'comment' ]