From 957ce59bf749b4ca182212694e1454a165f8d703 Mon Sep 17 00:00:00 2001 From: Jeff Bradberry Date: Thu, 25 Jan 2024 10:15:16 -0500 Subject: [PATCH] Use Counter to find duplicate peer relationships Gives a bit more readability. --- awx/api/serializers.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/awx/api/serializers.py b/awx/api/serializers.py index cc117f7927..e32016a6c4 100644 --- a/awx/api/serializers.py +++ b/awx/api/serializers.py @@ -6,7 +6,7 @@ import copy import json import logging import re -from collections import OrderedDict +from collections import Counter, OrderedDict from datetime import timedelta from uuid import uuid4 @@ -5697,10 +5697,9 @@ class InstanceSerializer(BaseSerializer): if set(p.instance.peers.all()) & instance_addresses: raise serializers.ValidationError(_(f"Instance {p.instance.hostname} is already peered to this instance.")) - # cannot peer to instance more than once - # compare length of set to original list to check for duplicates - peers_instances = [p.instance for p in attrs.get('peers', [])] - if len(set(peers_instances)) != len(peers_instances): + # cannot peer to an instance more than once + peers_instances = Counter(p.instance_id for p in attrs.get('peers', [])) + if any(count > 1 for count in peers_instances.values()): raise serializers.ValidationError(_("Cannot peer to the same instance more than once.")) # cannot enable peers_from_control_nodes if listener_port is not set