mirror of
https://github.com/ansible/awx.git
synced 2026-03-18 17:37:30 -02:30
deny topology changes to iso instances via api
This commit is contained in:
@@ -189,6 +189,11 @@ class InstanceGroupMembershipMixin(object):
|
|||||||
ig_obj.save()
|
ig_obj.save()
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
def is_valid_relation(self, parent, sub, created=False):
|
||||||
|
if sub.is_isolated():
|
||||||
|
return {'error': _('Isolated instances may not be added or removed from instances groups via the API.')}
|
||||||
|
return None
|
||||||
|
|
||||||
def unattach(self, request, *args, **kwargs):
|
def unattach(self, request, *args, **kwargs):
|
||||||
response = super(InstanceGroupMembershipMixin, self).unattach(request, *args, **kwargs)
|
response = super(InstanceGroupMembershipMixin, self).unattach(request, *args, **kwargs)
|
||||||
sub_id, res = self.attach_validate(request)
|
sub_id, res = self.attach_validate(request)
|
||||||
|
|||||||
@@ -120,6 +120,8 @@ class Instance(BaseModel):
|
|||||||
def is_controller(self):
|
def is_controller(self):
|
||||||
return Instance.objects.filter(rampart_groups__controller__instances=self).exists()
|
return Instance.objects.filter(rampart_groups__controller__instances=self).exists()
|
||||||
|
|
||||||
|
def is_isolated(self):
|
||||||
|
return self.rampart_groups.filter(controller__isnull=False).exists()
|
||||||
|
|
||||||
def refresh_capacity(self):
|
def refresh_capacity(self):
|
||||||
cpu = get_cpu_capacity()
|
cpu = get_cpu_capacity()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import pytest
|
|||||||
|
|
||||||
from awx.api.versioning import reverse
|
from awx.api.versioning import reverse
|
||||||
from awx.main.models import (
|
from awx.main.models import (
|
||||||
|
Instance,
|
||||||
InstanceGroup,
|
InstanceGroup,
|
||||||
ProjectUpdate,
|
ProjectUpdate,
|
||||||
)
|
)
|
||||||
@@ -14,6 +15,12 @@ def tower_instance_group():
|
|||||||
return ig
|
return ig
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def instance():
|
||||||
|
instance = Instance.objects.create(hostname='iso')
|
||||||
|
return instance
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def instance_group(job_factory):
|
def instance_group(job_factory):
|
||||||
ig = InstanceGroup(name="east")
|
ig = InstanceGroup(name="east")
|
||||||
@@ -22,9 +29,11 @@ def instance_group(job_factory):
|
|||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
def isolated_instance_group(instance_group):
|
def isolated_instance_group(instance_group, instance):
|
||||||
ig = InstanceGroup(name="iso", controller=instance_group)
|
ig = InstanceGroup(name="iso", controller=instance_group)
|
||||||
ig.save()
|
ig.save()
|
||||||
|
ig.instances.set([instance])
|
||||||
|
ig.save()
|
||||||
return ig
|
return ig
|
||||||
|
|
||||||
|
|
||||||
@@ -113,3 +122,13 @@ def test_prevent_delete_iso_and_control_groups(delete, isolated_instance_group,
|
|||||||
controller_url = reverse("api:instance_group_detail", kwargs={'pk': isolated_instance_group.controller.pk})
|
controller_url = reverse("api:instance_group_detail", kwargs={'pk': isolated_instance_group.controller.pk})
|
||||||
delete(iso_url, None, admin, expect=403)
|
delete(iso_url, None, admin, expect=403)
|
||||||
delete(controller_url, None, admin, expect=403)
|
delete(controller_url, None, admin, expect=403)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.django_db
|
||||||
|
def test_prevent_isolated_instance_in_non_isolated_instance_group(post, admin, instance, instance_group, isolated_instance_group):
|
||||||
|
url = reverse("api:instance_group_instance_list", kwargs={'pk': instance_group.pk})
|
||||||
|
|
||||||
|
assert True is instance.is_isolated()
|
||||||
|
resp = post(url, {'associate': True, 'id': instance.id}, admin, expect=400)
|
||||||
|
assert u"Isolated instances may not be added or removed from instances groups via the API." == resp.data['error']
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user