mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 17:37:37 -02:30
Merge pull request #7119 from thedoubl3j/hostnogroup
added change so that groups and host names cannot share the same name Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
@@ -1731,6 +1731,7 @@ class HostSerializer(BaseSerializerWithVariables):
|
|||||||
|
|
||||||
def validate(self, attrs):
|
def validate(self, attrs):
|
||||||
name = force_text(attrs.get('name', self.instance and self.instance.name or ''))
|
name = force_text(attrs.get('name', self.instance and self.instance.name or ''))
|
||||||
|
inventory = attrs.get('inventory', self.instance and self.instance.inventory or '')
|
||||||
host, port = self._get_host_port_from_name(name)
|
host, port = self._get_host_port_from_name(name)
|
||||||
|
|
||||||
if port:
|
if port:
|
||||||
@@ -1739,7 +1740,9 @@ class HostSerializer(BaseSerializerWithVariables):
|
|||||||
vars_dict = parse_yaml_or_json(variables)
|
vars_dict = parse_yaml_or_json(variables)
|
||||||
vars_dict['ansible_ssh_port'] = port
|
vars_dict['ansible_ssh_port'] = port
|
||||||
attrs['variables'] = json.dumps(vars_dict)
|
attrs['variables'] = json.dumps(vars_dict)
|
||||||
|
if Group.objects.filter(name=name, inventory=inventory).exists():
|
||||||
|
raise serializers.ValidationError(_('A Group with that name already exists.'))
|
||||||
|
|
||||||
return super(HostSerializer, self).validate(attrs)
|
return super(HostSerializer, self).validate(attrs)
|
||||||
|
|
||||||
def to_representation(self, obj):
|
def to_representation(self, obj):
|
||||||
@@ -1805,6 +1808,13 @@ class GroupSerializer(BaseSerializerWithVariables):
|
|||||||
res['inventory'] = self.reverse('api:inventory_detail', kwargs={'pk': obj.inventory.pk})
|
res['inventory'] = self.reverse('api:inventory_detail', kwargs={'pk': obj.inventory.pk})
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
def validate(self, attrs):
|
||||||
|
name = force_text(attrs.get('name', self.instance and self.instance.name or ''))
|
||||||
|
inventory = attrs.get('inventory', self.instance and self.instance.inventory or '')
|
||||||
|
if Host.objects.filter(name=name, inventory=inventory).exists():
|
||||||
|
raise serializers.ValidationError(_('A Host with that name already exists.'))
|
||||||
|
return super(GroupSerializer, self).validate(attrs)
|
||||||
|
|
||||||
def validate_name(self, value):
|
def validate_name(self, value):
|
||||||
if value in ('all', '_meta'):
|
if value in ('all', '_meta'):
|
||||||
raise serializers.ValidationError(_('Invalid group name.'))
|
raise serializers.ValidationError(_('Invalid group name.'))
|
||||||
|
|||||||
@@ -60,6 +60,42 @@ def test_inventory_source_unique_together_with_inv(inventory_factory):
|
|||||||
is2.validate_unique()
|
is2.validate_unique()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_inventory_host_name_unique(scm_inventory, post, admin_user):
|
||||||
|
inv_src = scm_inventory.inventory_sources.first()
|
||||||
|
inv_src.groups.create(name='barfoo', inventory=scm_inventory)
|
||||||
|
resp = post(
|
||||||
|
reverse('api:inventory_hosts_list', kwargs={'pk': scm_inventory.id}),
|
||||||
|
{
|
||||||
|
'name': 'barfoo',
|
||||||
|
'inventory_id': scm_inventory.id,
|
||||||
|
},
|
||||||
|
admin_user,
|
||||||
|
expect=400
|
||||||
|
)
|
||||||
|
|
||||||
|
assert resp.status_code == 400
|
||||||
|
assert "A Group with that name already exists." in json.dumps(resp.data)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_inventory_group_name_unique(scm_inventory, post, admin_user):
|
||||||
|
inv_src = scm_inventory.inventory_sources.first()
|
||||||
|
inv_src.hosts.create(name='barfoo', inventory=scm_inventory)
|
||||||
|
resp = post(
|
||||||
|
reverse('api:inventory_groups_list', kwargs={'pk': scm_inventory.id}),
|
||||||
|
{
|
||||||
|
'name': 'barfoo',
|
||||||
|
'inventory_id': scm_inventory.id,
|
||||||
|
},
|
||||||
|
admin_user,
|
||||||
|
expect=400
|
||||||
|
)
|
||||||
|
|
||||||
|
assert resp.status_code == 400
|
||||||
|
assert "A Host with that name already exists." in json.dumps(resp.data)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("role_field,expected_status_code", [
|
@pytest.mark.parametrize("role_field,expected_status_code", [
|
||||||
(None, 403),
|
(None, 403),
|
||||||
('admin_role', 200),
|
('admin_role', 200),
|
||||||
|
|||||||
Reference in New Issue
Block a user