mirror of
https://github.com/ansible/awx.git
synced 2026-03-05 18:51:06 -03:30
Fix detecting if peers changed in serializer
Add a check_peers_changed() utility method to determine if peers in attrs matches the current instance peers. Other changes: - Set ip_address default to "", and do not allow null.
This commit is contained in:
@@ -5477,6 +5477,14 @@ class InstanceSerializer(BaseSerializer):
|
|||||||
def get_field_from_model_or_attrs(fd):
|
def get_field_from_model_or_attrs(fd):
|
||||||
return attrs.get(fd, self.instance and getattr(self.instance, fd) or None)
|
return attrs.get(fd, self.instance and getattr(self.instance, fd) or None)
|
||||||
|
|
||||||
|
def check_peers_changed():
|
||||||
|
'''
|
||||||
|
return True if
|
||||||
|
- 'peers' in attrs
|
||||||
|
- instance peers matches peers in attrs
|
||||||
|
'''
|
||||||
|
return self.instance and 'peers' in attrs and set(self.instance.peers.all()) != set(attrs['peers'])
|
||||||
|
|
||||||
if not self.instance and not settings.IS_K8S:
|
if not self.instance and not settings.IS_K8S:
|
||||||
raise serializers.ValidationError(_("Can only create instances on Kubernetes or OpenShift."))
|
raise serializers.ValidationError(_("Can only create instances on Kubernetes or OpenShift."))
|
||||||
|
|
||||||
@@ -5489,7 +5497,7 @@ class InstanceSerializer(BaseSerializer):
|
|||||||
raise serializers.ValidationError(_("peers_from_control_nodes can only be enabled for execution or hop nodes."))
|
raise serializers.ValidationError(_("peers_from_control_nodes can only be enabled for execution or hop nodes."))
|
||||||
|
|
||||||
if node_type in [Instance.Types.CONTROL, Instance.Types.HYBRID]:
|
if node_type in [Instance.Types.CONTROL, Instance.Types.HYBRID]:
|
||||||
if self.instance and 'peers' in attrs and set(self.instance.peers.all()) != set(attrs['peers']):
|
if check_peers_changed():
|
||||||
raise serializers.ValidationError(
|
raise serializers.ValidationError(
|
||||||
_("Setting peers manually for control nodes is not allowed. Enable peers_from_control_nodes on the hop and execution nodes instead.")
|
_("Setting peers manually for control nodes is not allowed. Enable peers_from_control_nodes on the hop and execution nodes instead.")
|
||||||
)
|
)
|
||||||
@@ -5505,7 +5513,7 @@ class InstanceSerializer(BaseSerializer):
|
|||||||
raise serializers.ValidationError(_("Field listener_port must be set on peer ") + peer.hostname + ".")
|
raise serializers.ValidationError(_("Field listener_port must be set on peer ") + peer.hostname + ".")
|
||||||
|
|
||||||
if not settings.IS_K8S:
|
if not settings.IS_K8S:
|
||||||
if self.instance and set(self.instance.peers.all()) != set(peers):
|
if check_peers_changed():
|
||||||
raise serializers.ValidationError(_("Cannot change peers."))
|
raise serializers.ValidationError(_("Cannot change peers."))
|
||||||
|
|
||||||
return super().validate(attrs)
|
return super().validate(attrs)
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class Migration(migrations.Migration):
|
|||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='instance',
|
model_name='instance',
|
||||||
name='ip_address',
|
name='ip_address',
|
||||||
field=models.CharField(blank=True, default=None, max_length=50, null=True),
|
field=models.CharField(blank=True, default='', max_length=50),
|
||||||
),
|
),
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name='instance',
|
model_name='instance',
|
||||||
|
|||||||
@@ -105,8 +105,7 @@ class Instance(HasPolicyEditsMixin, BaseModel):
|
|||||||
hostname = models.CharField(max_length=250, unique=True)
|
hostname = models.CharField(max_length=250, unique=True)
|
||||||
ip_address = models.CharField(
|
ip_address = models.CharField(
|
||||||
blank=True,
|
blank=True,
|
||||||
null=True,
|
default="",
|
||||||
default=None,
|
|
||||||
max_length=50,
|
max_length=50,
|
||||||
)
|
)
|
||||||
# Auto-fields, implementation is different from BaseModel
|
# Auto-fields, implementation is different from BaseModel
|
||||||
|
|||||||
Reference in New Issue
Block a user