Add the state fields and the peer relationships to the serializers

This commit is contained in:
Jeff Bradberry
2022-07-18 13:38:05 -04:00
parent 170795ab76
commit d2a6be7ca9

View File

@@ -4753,7 +4753,7 @@ class ScheduleSerializer(LaunchConfigurationBaseSerializer, SchedulePreviewSeria
class InstanceLinkSerializer(BaseSerializer): class InstanceLinkSerializer(BaseSerializer):
class Meta: class Meta:
model = InstanceLink model = InstanceLink
fields = ('source', 'target') fields = ('source', 'target', 'link_state')
source = serializers.SlugRelatedField(slug_field="hostname", read_only=True) source = serializers.SlugRelatedField(slug_field="hostname", read_only=True)
target = serializers.SlugRelatedField(slug_field="hostname", read_only=True) target = serializers.SlugRelatedField(slug_field="hostname", read_only=True)
@@ -4762,31 +4762,25 @@ class InstanceLinkSerializer(BaseSerializer):
class InstanceNodeSerializer(BaseSerializer): class InstanceNodeSerializer(BaseSerializer):
class Meta: class Meta:
model = Instance model = Instance
fields = ('id', 'hostname', 'node_type', 'node_state') fields = ('id', 'hostname', 'node_type', 'node_state', 'enabled')
node_state = serializers.SerializerMethodField()
def get_node_state(self, obj):
if not obj.enabled:
return "disabled"
return "error" if obj.errors else "healthy"
class InstanceSerializer(BaseSerializer): class InstanceSerializer(BaseSerializer):
consumed_capacity = serializers.SerializerMethodField() consumed_capacity = serializers.SerializerMethodField()
percent_capacity_remaining = serializers.SerializerMethodField() percent_capacity_remaining = serializers.SerializerMethodField()
jobs_running = serializers.IntegerField(help_text=_('Count of jobs in the running or waiting state that ' 'are targeted for this instance'), read_only=True) jobs_running = serializers.IntegerField(help_text=_('Count of jobs in the running or waiting state that are targeted for this instance'), read_only=True)
jobs_total = serializers.IntegerField(help_text=_('Count of all jobs that target this instance'), read_only=True) jobs_total = serializers.IntegerField(help_text=_('Count of all jobs that target this instance'), read_only=True)
class Meta: class Meta:
model = Instance model = Instance
read_only_fields = ('uuid', 'hostname', 'version', 'node_type') read_only_fields = ('uuid', 'hostname', 'version', 'node_type', 'node_state')
fields = ( fields = (
"id", "id",
"type", "type",
"url", "url",
"related", "related",
"summary_fields",
"uuid", "uuid",
"hostname", "hostname",
"created", "created",
@@ -4808,6 +4802,7 @@ class InstanceSerializer(BaseSerializer):
"enabled", "enabled",
"managed_by_policy", "managed_by_policy",
"node_type", "node_type",
"node_state",
) )
def get_related(self, obj): def get_related(self, obj):
@@ -4819,6 +4814,14 @@ class InstanceSerializer(BaseSerializer):
res['health_check'] = self.reverse('api:instance_health_check', kwargs={'pk': obj.pk}) res['health_check'] = self.reverse('api:instance_health_check', kwargs={'pk': obj.pk})
return res return res
def get_summary_fields(self, obj):
summary = super().get_summary_fields(obj)
if self.is_detail_view:
summary['links'] = InstanceLinkSerializer(InstanceLink.objects.select_related('target', 'source').filter(source=obj), many=True).data
return summary
def get_consumed_capacity(self, obj): def get_consumed_capacity(self, obj):
return obj.consumed_capacity return obj.consumed_capacity