2018-02-07 15:25:15 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2022-09-23 10:59:19 +00:00
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
from rest_framework import serializers
|
2019-05-21 08:24:01 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2022-08-24 08:36:42 +00:00
|
|
|
from django.db.transaction import atomic
|
2022-08-22 10:32:33 +00:00
|
|
|
from django.db.models import F
|
2019-05-21 08:24:01 +00:00
|
|
|
|
2022-10-19 02:21:05 +00:00
|
|
|
from common.drf.serializers import WritableNestedModelSerializer
|
2022-09-01 13:00:04 +00:00
|
|
|
from common.drf.fields import LabeledChoiceField, ObjectRelatedField
|
2022-10-19 02:21:05 +00:00
|
|
|
from orgs.mixins.serializers import OrgResourceSerializerMixin
|
2022-07-17 06:28:55 +00:00
|
|
|
from ..account import AccountSerializer
|
2022-09-19 12:11:55 +00:00
|
|
|
from ...models import Asset, Node, Platform, Label, Domain, Account, Protocol
|
2022-08-18 09:58:59 +00:00
|
|
|
from ...const import Category, AllTypes
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2018-04-24 03:07:09 +00:00
|
|
|
__all__ = [
|
2021-08-27 08:38:23 +00:00
|
|
|
'AssetSerializer', 'AssetSimpleSerializer', 'MiniAssetSerializer',
|
2022-11-16 07:07:35 +00:00
|
|
|
'AssetTaskSerializer', 'AssetsTaskSerializer', 'AssetProtocolsSerializer',
|
2018-04-24 03:07:09 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2022-08-05 07:46:36 +00:00
|
|
|
class AssetProtocolsSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Protocol
|
2022-08-09 08:53:43 +00:00
|
|
|
fields = ['id', 'name', 'port']
|
2019-07-05 10:07:10 +00:00
|
|
|
|
2019-06-19 11:25:21 +00:00
|
|
|
|
2022-08-05 07:46:36 +00:00
|
|
|
class AssetLabelSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Label
|
2022-09-28 04:10:39 +00:00
|
|
|
fields = ['id', 'name', 'value']
|
2022-08-08 03:39:55 +00:00
|
|
|
extra_kwargs = {
|
2022-08-09 08:53:43 +00:00
|
|
|
'name': {'required': False},
|
2022-08-08 03:39:55 +00:00
|
|
|
'value': {'required': False}
|
|
|
|
}
|
2022-05-05 08:18:05 +00:00
|
|
|
|
2019-06-19 11:25:21 +00:00
|
|
|
|
2022-08-08 03:39:55 +00:00
|
|
|
class AssetPlatformSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Platform
|
2022-08-09 08:53:43 +00:00
|
|
|
fields = ['id', 'name']
|
2022-08-08 03:39:55 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'name': {'required': False}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-07 12:24:48 +00:00
|
|
|
class AssetAccountSerializer(AccountSerializer):
|
|
|
|
add_org_fields = False
|
|
|
|
|
|
|
|
class Meta(AccountSerializer.Meta):
|
|
|
|
fields_mini = [
|
2022-09-23 10:59:19 +00:00
|
|
|
'id', 'name', 'username', 'privileged',
|
|
|
|
'version', 'secret_type',
|
2022-09-13 13:07:20 +00:00
|
|
|
]
|
|
|
|
fields_write_only = [
|
2022-12-05 03:21:01 +00:00
|
|
|
'secret', 'push_now'
|
2022-09-07 12:24:48 +00:00
|
|
|
]
|
|
|
|
fields = fields_mini + fields_write_only
|
|
|
|
|
|
|
|
|
2022-10-19 02:21:05 +00:00
|
|
|
class AssetSerializer(OrgResourceSerializerMixin, WritableNestedModelSerializer):
|
2022-09-01 06:46:31 +00:00
|
|
|
category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category'))
|
2022-09-26 10:03:48 +00:00
|
|
|
type = LabeledChoiceField(choices=AllTypes.choices(), read_only=True, label=_('Type'))
|
2022-09-28 04:10:39 +00:00
|
|
|
domain = ObjectRelatedField(required=False, queryset=Domain.objects, label=_('Domain'), allow_null=True)
|
2022-09-01 13:00:04 +00:00
|
|
|
platform = ObjectRelatedField(required=False, queryset=Platform.objects, label=_('Platform'))
|
|
|
|
nodes = ObjectRelatedField(many=True, required=False, queryset=Node.objects, label=_('Nodes'))
|
2022-09-01 06:46:31 +00:00
|
|
|
labels = AssetLabelSerializer(many=True, required=False, label=_('Labels'))
|
|
|
|
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
|
2022-11-04 11:18:15 +00:00
|
|
|
accounts = AssetAccountSerializer(many=True, required=False, label=_('Accounts'))
|
2022-12-06 03:03:14 +00:00
|
|
|
enabled_info = serializers.SerializerMethodField()
|
2022-09-09 03:00:09 +00:00
|
|
|
|
2018-02-07 15:25:15 +00:00
|
|
|
class Meta:
|
|
|
|
model = Asset
|
2022-12-06 03:03:14 +00:00
|
|
|
fields_mini = ['id', 'name', 'address', 'enabled_info']
|
2022-09-09 03:00:09 +00:00
|
|
|
fields_small = fields_mini + ['is_active', 'comment']
|
|
|
|
fields_fk = ['domain', 'platform', 'platform']
|
2020-04-23 03:14:02 +00:00
|
|
|
fields_m2m = [
|
2022-11-04 11:18:15 +00:00
|
|
|
'nodes', 'labels', 'protocols', 'accounts', 'nodes_display',
|
2019-05-21 08:24:01 +00:00
|
|
|
]
|
2020-04-23 03:14:02 +00:00
|
|
|
read_only_fields = [
|
2022-11-07 08:10:26 +00:00
|
|
|
'category', 'type', 'specific', 'info',
|
2022-12-05 03:21:01 +00:00
|
|
|
'connectivity', 'date_verified', 'created_by',
|
2022-12-05 03:48:41 +00:00
|
|
|
'date_created'
|
2021-07-08 06:23:18 +00:00
|
|
|
]
|
2022-04-02 10:35:46 +00:00
|
|
|
fields = fields_small + fields_fk + fields_m2m + read_only_fields
|
2021-11-11 03:56:30 +00:00
|
|
|
extra_kwargs = {
|
2022-08-11 07:45:03 +00:00
|
|
|
'name': {'label': _("Name")},
|
2022-09-21 03:17:14 +00:00
|
|
|
'address': {'label': _('Address')},
|
2021-11-11 03:56:30 +00:00
|
|
|
}
|
2018-02-07 15:25:15 +00:00
|
|
|
|
2022-10-19 10:56:46 +00:00
|
|
|
def get_field_names(self, declared_fields, info):
|
|
|
|
names = super().get_field_names(declared_fields, info)
|
|
|
|
if self.__class__.__name__ != 'AssetSerializer':
|
2022-11-01 03:52:51 +00:00
|
|
|
names.remove('specific')
|
2022-10-19 10:56:46 +00:00
|
|
|
return names
|
|
|
|
|
2022-12-05 03:21:01 +00:00
|
|
|
@staticmethod
|
2022-12-06 03:03:14 +00:00
|
|
|
def get_enabled_info(obj):
|
|
|
|
platform = obj.platform
|
|
|
|
automation = platform.automation
|
2022-12-05 03:21:01 +00:00
|
|
|
return {
|
2022-12-06 03:03:14 +00:00
|
|
|
'su_enabled': platform.su_enabled,
|
2022-12-05 03:21:01 +00:00
|
|
|
'ping_enabled': automation.ping_enabled,
|
2022-12-06 03:03:14 +00:00
|
|
|
'domain_enabled': platform.domain_enabled,
|
2022-12-05 03:21:01 +00:00
|
|
|
'ansible_enabled': automation.ansible_enabled,
|
2022-12-06 03:03:14 +00:00
|
|
|
'protocols_enabled': platform.protocols_enabled,
|
2022-12-05 03:21:01 +00:00
|
|
|
'gather_facts_enabled': automation.gather_facts_enabled,
|
|
|
|
'push_account_enabled': automation.push_account_enabled,
|
|
|
|
'change_secret_enabled': automation.change_secret_enabled,
|
|
|
|
'verify_account_enabled': automation.verify_account_enabled,
|
|
|
|
'gather_accounts_enabled': automation.gather_accounts_enabled,
|
|
|
|
}
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2022-08-24 08:36:42 +00:00
|
|
|
queryset = queryset.prefetch_related('domain', 'platform', 'protocols') \
|
|
|
|
.annotate(category=F("platform__category")) \
|
2022-08-22 10:32:33 +00:00
|
|
|
.annotate(type=F("platform__type"))
|
2020-09-27 08:02:44 +00:00
|
|
|
queryset = queryset.prefetch_related('nodes', 'labels')
|
2018-12-17 10:20:44 +00:00
|
|
|
return queryset
|
|
|
|
|
2020-10-30 02:16:49 +00:00
|
|
|
def perform_nodes_display_create(self, instance, nodes_display):
|
|
|
|
if not nodes_display:
|
|
|
|
return
|
|
|
|
nodes_to_set = []
|
|
|
|
for full_value in nodes_display:
|
2022-10-25 11:31:13 +00:00
|
|
|
if not full_value.startswith('/'):
|
|
|
|
full_value = '/' + instance.org.name + '/' + full_value
|
2020-10-30 02:16:49 +00:00
|
|
|
node = Node.objects.filter(full_value=full_value).first()
|
|
|
|
if node:
|
|
|
|
nodes_to_set.append(node)
|
|
|
|
else:
|
|
|
|
node = Node.create_node_by_full_value(full_value)
|
|
|
|
nodes_to_set.append(node)
|
|
|
|
instance.nodes.set(nodes_to_set)
|
|
|
|
|
2022-09-09 03:00:09 +00:00
|
|
|
def validate_nodes(self, nodes):
|
|
|
|
if nodes:
|
|
|
|
return nodes
|
|
|
|
request = self.context.get('request')
|
|
|
|
if not request:
|
|
|
|
return []
|
|
|
|
node_id = request.query_params.get('node_id')
|
|
|
|
if not node_id:
|
|
|
|
return []
|
|
|
|
|
2022-10-18 12:37:17 +00:00
|
|
|
def validate_protocols(self, protocols_data):
|
|
|
|
if not protocols_data:
|
|
|
|
protocols_data = []
|
|
|
|
platform_id = self.initial_data.get('platform')
|
|
|
|
if isinstance(platform_id, dict):
|
|
|
|
platform_id = platform_id.get('id') or platform_id.get('pk')
|
|
|
|
platform = Platform.objects.filter(id=platform_id).first()
|
|
|
|
if not platform:
|
|
|
|
raise serializers.ValidationError({'platform': _("Platform not exist")})
|
|
|
|
|
|
|
|
protocols_data_map = {p['name']: p for p in protocols_data}
|
|
|
|
platform_protocols = platform.protocols.all()
|
|
|
|
protocols_default = [p for p in platform_protocols if p.default]
|
|
|
|
protocols_required = [p for p in platform_protocols if p.required or p.primary]
|
|
|
|
|
|
|
|
if not protocols_data_map:
|
|
|
|
protocols_data_map = {
|
|
|
|
p.name: {'name': p.name, 'port': p.port}
|
|
|
|
for p in protocols_required + protocols_default
|
|
|
|
}
|
|
|
|
|
|
|
|
protocols_not_found = [p.name for p in protocols_required if p.name not in protocols_data_map]
|
|
|
|
if protocols_not_found:
|
|
|
|
raise serializers.ValidationError({
|
|
|
|
'protocols': _("Protocol is required: {}").format(', '.join(protocols_not_found))
|
|
|
|
})
|
|
|
|
return protocols_data_map.values()
|
|
|
|
|
2022-08-24 08:36:42 +00:00
|
|
|
@atomic
|
2019-07-05 10:07:10 +00:00
|
|
|
def create(self, validated_data):
|
2020-10-31 04:52:24 +00:00
|
|
|
nodes_display = validated_data.pop('nodes_display', '')
|
2019-06-13 10:58:43 +00:00
|
|
|
instance = super().create(validated_data)
|
2020-10-30 02:16:49 +00:00
|
|
|
self.perform_nodes_display_create(instance, nodes_display)
|
2019-06-13 10:58:43 +00:00
|
|
|
return instance
|
|
|
|
|
2022-08-24 08:36:42 +00:00
|
|
|
@atomic
|
2019-06-13 10:58:43 +00:00
|
|
|
def update(self, instance, validated_data):
|
2020-10-31 04:52:24 +00:00
|
|
|
nodes_display = validated_data.pop('nodes_display', '')
|
2020-10-30 02:16:49 +00:00
|
|
|
instance = super().update(instance, validated_data)
|
|
|
|
self.perform_nodes_display_create(instance, nodes_display)
|
|
|
|
return instance
|
2018-02-07 15:25:15 +00:00
|
|
|
|
|
|
|
|
2021-08-27 08:38:23 +00:00
|
|
|
class MiniAssetSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = AssetSerializer.Meta.fields_mini
|
|
|
|
|
|
|
|
|
2018-12-18 09:28:45 +00:00
|
|
|
class AssetSimpleSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
2022-04-29 10:28:12 +00:00
|
|
|
fields = [
|
2022-09-21 03:17:14 +00:00
|
|
|
'id', 'name', 'address', 'port',
|
2022-04-29 10:28:12 +00:00
|
|
|
'connectivity', 'date_verified'
|
|
|
|
]
|
2020-03-12 08:24:38 +00:00
|
|
|
|
|
|
|
|
2021-07-20 06:48:38 +00:00
|
|
|
class AssetsTaskSerializer(serializers.Serializer):
|
2020-03-12 08:24:38 +00:00
|
|
|
ACTION_CHOICES = (
|
|
|
|
('refresh', 'refresh'),
|
|
|
|
('test', 'test'),
|
|
|
|
)
|
|
|
|
task = serializers.CharField(read_only=True)
|
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
2021-01-06 03:08:35 +00:00
|
|
|
assets = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=Asset.objects, required=False, allow_empty=True, many=True
|
|
|
|
)
|
2021-07-20 06:48:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetTaskSerializer(AssetsTaskSerializer):
|
|
|
|
ACTION_CHOICES = tuple(list(AssetsTaskSerializer.ACTION_CHOICES) + [
|
|
|
|
('push_system_user', 'push_system_user'),
|
|
|
|
('test_system_user', 'test_system_user')
|
|
|
|
])
|
2021-08-17 06:02:13 +00:00
|
|
|
action = serializers.ChoiceField(choices=ACTION_CHOICES, write_only=True)
|
2021-07-20 06:48:38 +00:00
|
|
|
asset = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=Asset.objects, required=False, allow_empty=True, many=False
|
|
|
|
)
|
2022-11-02 09:27:47 +00:00
|
|
|
accounts = serializers.PrimaryKeyRelatedField(
|
|
|
|
queryset=Account.objects, required=False, allow_empty=True, many=True
|
|
|
|
)
|