mirror of
https://github.com/ansible/awx.git
synced 2026-01-11 01:57:35 -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:
parent
c47acc5988
commit
2a0be898e6
@ -5477,6 +5477,14 @@ class InstanceSerializer(BaseSerializer):
|
||||
def get_field_from_model_or_attrs(fd):
|
||||
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:
|
||||
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."))
|
||||
|
||||
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(
|
||||
_("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 + ".")
|
||||
|
||||
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."))
|
||||
|
||||
return super().validate(attrs)
|
||||
|
||||
@ -30,7 +30,7 @@ class Migration(migrations.Migration):
|
||||
migrations.AlterField(
|
||||
model_name='instance',
|
||||
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(
|
||||
model_name='instance',
|
||||
|
||||
@ -105,8 +105,7 @@ class Instance(HasPolicyEditsMixin, BaseModel):
|
||||
hostname = models.CharField(max_length=250, unique=True)
|
||||
ip_address = models.CharField(
|
||||
blank=True,
|
||||
null=True,
|
||||
default=None,
|
||||
default="",
|
||||
max_length=50,
|
||||
)
|
||||
# Auto-fields, implementation is different from BaseModel
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user