diff --git a/apps/assets/migrations/0114_node_domain.py b/apps/assets/migrations/0114_node_domain.py index 896a611d5..0149a5d11 100644 --- a/apps/assets/migrations/0114_node_domain.py +++ b/apps/assets/migrations/0114_node_domain.py @@ -4,18 +4,11 @@ from django.db import migrations, models import django.db.models.deletion +# TODO 最后去掉这个迁移 class Migration(migrations.Migration): dependencies = [ ('assets', '0113_alter_accounttemplate_options'), ] operations = [ - migrations.AddField( - model_name='node', - name='domain', - field=models.ForeignKey( - blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, - related_name='nodes', to='assets.domain', verbose_name='Domain' - ), - ), ] diff --git a/apps/assets/models/asset/common.py b/apps/assets/models/asset/common.py index 7f01c222d..81693252d 100644 --- a/apps/assets/models/asset/common.py +++ b/apps/assets/models/asset/common.py @@ -105,14 +105,6 @@ class Asset(NodesRelationMixin, AbsConnectivity, JMSOrgBaseModel): def __str__(self): return '{0.name}({0.address})'.format(self) - def get_domains(self): - from ..domain import Domain - node_ids = self.get_all_nodes(flat=True) - domains = Domain.objects.filter( - Q(nodes__id__in=node_ids) | Q(id=self.domain_id) - ).distinct() - return domains - @property def specific(self): if not hasattr(self, self.category): diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index bf394e982..54bdecf61 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -558,10 +558,6 @@ class Node(OrgModelMixin, SomeNodesMixin, FamilyMixin, NodeAssetsMixin): max_length=64, verbose_name=_("Parent key"), db_index=True, default='' ) assets_amount = models.IntegerField(default=0) - domain = models.ForeignKey( - "assets.Domain", null=True, blank=True, related_name='nodes', - verbose_name=_("Domain"), on_delete=models.SET_NULL - ) objects = OrgManager.from_queryset(NodeQuerySet)() is_node = True diff --git a/apps/assets/serializers/domain.py b/apps/assets/serializers/domain.py index 617aa34bc..f60cb9516 100644 --- a/apps/assets/serializers/domain.py +++ b/apps/assets/serializers/domain.py @@ -14,12 +14,8 @@ from .utils import validate_password_for_ansible, validate_ssh_key class DomainSerializer(BulkOrgResourceModelSerializer): - node_count = serializers.SerializerMethodField(label=_('Nodes amount')) asset_count = serializers.SerializerMethodField(label=_('Assets amount')) gateway_count = serializers.SerializerMethodField(label=_('Gateways count')) - nodes = ObjectRelatedField( - many=True, required=False, queryset=Node.objects, label=_('Node') - ) assets = ObjectRelatedField( many=True, required=False, queryset=Asset.objects, label=_('Asset') ) @@ -29,17 +25,13 @@ class DomainSerializer(BulkOrgResourceModelSerializer): fields_mini = ['id', 'name'] fields_small = fields_mini + ['comment'] fields_m2m = ['nodes', 'assets'] - read_only_fields = ['node_count', 'asset_count', 'gateway_count', 'date_created'] + read_only_fields = ['asset_count', 'gateway_count', 'date_created'] fields = fields_small + fields_m2m + read_only_fields extra_kwargs = { 'assets': {'required': False, 'label': _('Assets')}, } - @staticmethod - def get_node_count(obj): - return obj.nodes.count() - @staticmethod def get_asset_count(obj): return obj.assets.count() diff --git a/apps/ops/ansible/inventory.py b/apps/ops/ansible/inventory.py index aa7f8186d..1e006ae77 100644 --- a/apps/ops/ansible/inventory.py +++ b/apps/ops/ansible/inventory.py @@ -1,7 +1,6 @@ # ~*~ coding: utf-8 ~*~ import json import os -import random from collections import defaultdict from django.utils.translation import gettext as _ @@ -118,10 +117,8 @@ class JMSInventory: host.update(ansible_config) gateway = None - domains = asset.get_domains() - if domains: - gateways = [i.select_gateway() for i in domains if i.select_gateway()] - gateway = random.choice(gateways) if gateways else None + if asset.domain: + gateway = asset.domain.select_gateway() if ansible_connection == 'local': if gateway: