diff --git a/awx/main/models/inventory.py b/awx/main/models/inventory.py index a4ddaabafd..fe1b09e23d 100644 --- a/awx/main/models/inventory.py +++ b/awx/main/models/inventory.py @@ -17,6 +17,7 @@ from django.db import models, connection from django.utils.translation import gettext_lazy as _ from django.db import transaction from django.core.exceptions import ValidationError +from django.urls import resolve from django.utils.timezone import now from django.db.models import Q @@ -206,8 +207,14 @@ class Inventory(CommonModelNameNotUnique, ResourceMixin, RelatedJobsMixin): ) def get_absolute_url(self, request=None): - if self.kind == 'constructed': - return reverse('api:constructed_inventory_detail', kwargs={'pk': self.pk}, request=request) + if request is not None: + # circular import + from awx.api.urls.inventory import constructed_inventory_urls + + route = resolve(request.path_info) + if any(route.url_name == url.name for url in constructed_inventory_urls): + return reverse('api:constructed_inventory_detail', kwargs={'pk': self.pk}, request=request) + return reverse('api:inventory_detail', kwargs={'pk': self.pk}, request=request) variables_dict = VarsDictProperty('variables') diff --git a/awx/main/tests/functional/api/test_inventory.py b/awx/main/tests/functional/api/test_inventory.py index ab84ff236d..71a48383c4 100644 --- a/awx/main/tests/functional/api/test_inventory.py +++ b/awx/main/tests/functional/api/test_inventory.py @@ -680,3 +680,22 @@ class TestConstructedInventory: inv_src = constructed_inventory.inventory_sources.first() assert inv_src.update_cache_timeout == 55 assert inv_src.limit == 'foobar' + + def test_get_absolute_url_for_constructed_inventory(self, constructed_inventory, admin_user, get): + """ + If we are using the normal inventory API endpoint to look at a + constructed inventory, then we should get a normal inventory API route + back. If we are accessing it via the special constructed inventory + endpoint, then we should get that back. + """ + + url_const = reverse('api:constructed_inventory_detail', kwargs={'pk': constructed_inventory.pk}) + url_inv = reverse('api:inventory_detail', kwargs={'pk': constructed_inventory.pk}) + + const_r = get(url=url_const, user=admin_user, expect=200) + inv_r = get(url=url_inv, user=admin_user, expect=200) + assert const_r.data['url'] == url_const + assert inv_r.data['url'] == url_inv + assert inv_r.data['url'] != const_r.data['url'] + assert inv_r.data['related']['constructed_url'] == url_const + assert const_r.data['related']['constructed_url'] == url_const