From 887604317e75dd8ffdd6673077763011ac6844d1 Mon Sep 17 00:00:00 2001 From: jessicamack Date: Fri, 1 Mar 2024 11:18:35 -0500 Subject: Integrate resources API in Controller (#14896) * add resources api to controller * update setting models are not the source of truth in AWX * Force creation of ServiceID object in tests * fix typo * settings fix for CI --------- Co-authored-by: Alan Rominger --- awx/main/tests/functional/conftest.py | 13 +++++++++++++ awx/main/utils/common.py | 2 +- awx/resource_api.py | 22 ++++++++++++++++++++++ awx/settings/defaults.py | 3 +++ awx/urls.py | 3 +++ requirements/requirements_git.txt | 2 +- 6 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 awx/resource_api.py diff --git a/awx/main/tests/functional/conftest.py b/awx/main/tests/functional/conftest.py index d65c80e96c..2c40b6ae09 100644 --- a/awx/main/tests/functional/conftest.py +++ b/awx/main/tests/functional/conftest.py @@ -3,15 +3,19 @@ import pytest from unittest import mock import urllib.parse from unittest.mock import PropertyMock +import importlib # Django from django.urls import resolve from django.http import Http404 +from django.apps import apps from django.core.handlers.exception import response_for_exception from django.contrib.auth.models import User from django.core.serializers.json import DjangoJSONEncoder from django.db.backends.sqlite3.base import SQLiteCursorWrapper +from django.db.models.signals import post_migrate + # AWX from awx.main.models.projects import Project from awx.main.models.ha import Instance @@ -41,10 +45,19 @@ from awx.main.models.workflow import WorkflowJobTemplate from awx.main.models.ad_hoc_commands import AdHocCommand from awx.main.models.oauth import OAuth2Application as Application from awx.main.models.execution_environments import ExecutionEnvironment +from awx.main.utils import is_testing __SWAGGER_REQUESTS__ = {} +# HACK: the dab_resource_registry app required ServiceID in migrations which checks do not run +dab_rr_initial = importlib.import_module('ansible_base.resource_registry.migrations.0001_initial') + + +if is_testing(): + post_migrate.connect(lambda **kwargs: dab_rr_initial.create_service_id(apps, None)) + + @pytest.fixture(scope="session") def swagger_autogen(requests=__SWAGGER_REQUESTS__): return requests diff --git a/awx/main/utils/common.py b/awx/main/utils/common.py index 7ab9842584..79c78a8e91 100644 --- a/awx/main/utils/common.py +++ b/awx/main/utils/common.py @@ -136,7 +136,7 @@ def underscore_to_camelcase(s): @functools.cache def is_testing(argv=None): '''Return True if running django or py.test unit tests.''' - if 'PYTEST_CURRENT_TEST' in os.environ.keys(): + if os.environ.get('DJANGO_SETTINGS_MODULE') == 'awx.main.tests.settings_for_test': return True argv = sys.argv if argv is None else argv if len(argv) >= 1 and ('py.test' in argv[0] or 'py/test.py' in argv[0]): diff --git a/awx/resource_api.py b/awx/resource_api.py new file mode 100644 index 0000000000..2009dfab8b --- /dev/null +++ b/awx/resource_api.py @@ -0,0 +1,22 @@ +from ansible_base.resource_registry.registry import ParentResource, ResourceConfig, ServiceAPIConfig, SharedResource +from ansible_base.resource_registry.shared_types import OrganizationType, TeamType, UserType + +from awx.main import models + + +class APIConfig(ServiceAPIConfig): + service_type = "awx" + + +RESOURCE_LIST = ( + ResourceConfig( + models.Organization, + shared_resource=SharedResource(serializer=OrganizationType, is_provider=False), + ), + ResourceConfig(models.User, shared_resource=SharedResource(serializer=UserType, is_provider=False), name_field="username"), + ResourceConfig( + models.Team, + shared_resource=SharedResource(serializer=TeamType, is_provider=False), + parent_resources=[ParentResource(model=models.Organization, field_name="organization")], + ), +) diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 756a2d716c..7ae79b043c 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -354,8 +354,10 @@ INSTALLED_APPS = [ 'solo', 'ansible_base.rest_filters', 'ansible_base.jwt_consumer', + 'ansible_base.resource_registry', ] + INTERNAL_IPS = ('127.0.0.1',) MAX_PAGE_SIZE = 200 @@ -1110,6 +1112,7 @@ METRICS_SUBSYSTEM_CONFIG = { # django-ansible-base ANSIBLE_BASE_TEAM_MODEL = 'main.Team' ANSIBLE_BASE_ORGANIZATION_MODEL = 'main.Organization' +ANSIBLE_BASE_RESOURCE_CONFIG_MODULE = 'awx.resource_api' from ansible_base.lib import dynamic_config # noqa: E402 diff --git a/awx/urls.py b/awx/urls.py index 605f549d23..c2d7caed43 100644 --- a/awx/urls.py +++ b/awx/urls.py @@ -4,6 +4,8 @@ from django.conf import settings from django.urls import re_path, include +from ansible_base.resource_registry.urls import urlpatterns as resource_api_urls + from awx.main.views import handle_400, handle_403, handle_404, handle_500, handle_csp_violation, handle_login_redirect @@ -11,6 +13,7 @@ urlpatterns = [ re_path(r'', include('awx.ui.urls', namespace='ui')), re_path(r'^ui_next/.*', include('awx.ui_next.urls', namespace='ui_next')), re_path(r'^api/', include('awx.api.urls', namespace='api')), + re_path(r'^api/v2/', include(resource_api_urls)), re_path(r'^sso/', include('awx.sso.urls', namespace='sso')), re_path(r'^sso/', include('social_django.urls', namespace='social')), re_path(r'^(?:api/)?400.html$', handle_400), diff --git a/requirements/requirements_git.txt b/requirements/requirements_git.txt index 19886ca520..350c2d48b0 100644 --- a/requirements/requirements_git.txt +++ b/requirements/requirements_git.txt @@ -5,4 +5,4 @@ git+https://github.com/ansible/ansible-runner.git@devel#egg=ansible-runner # specifically need https://github.com/robgolding/django-radius/pull/27 git+https://github.com/ansible/django-radius.git@develop#egg=django-radius git+https://github.com/ansible/python3-saml.git@devel#egg=python3-saml -django-ansible-base @ git+https://github.com/ansible/django-ansible-base@devel#egg=django-ansible-base[rest_filters,jwt_consumer] +django-ansible-base @ git+https://github.com/ansible/django-ansible-base@devel#egg=django-ansible-base[rest_filters,jwt_consumer,resource_registry] -- cgit v1.2.3