Skip to content

Commit 51eb109

Browse files
authored
Fix issue with some modules not honoring Controller API prefix (#16080)
* Fix issue where export module does not honor CONTROLLER_OPTIONAL_API_URLPATTERN_PREFIX * Add unit test and handle leading/trailing slashes * Reformat * Refactor for clarity * Remove unused import
1 parent 5ca76f3 commit 51eb109

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

awx_collection/plugins/module_utils/awxkit.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from .controller_api import ControllerModule
66
from ansible.module_utils.basic import missing_required_lib
7+
from os import getenv
78

89
try:
910
from awxkit.api.client import Connection
@@ -42,7 +43,13 @@ def get_api_v2_object(self):
4243
if not self.apiV2Ref:
4344
if not self.authenticated:
4445
self.authenticate()
45-
v2_index = get_registered_page('/api/v2/')(self.connection).get()
46+
prefix = getenv('CONTROLLER_OPTIONAL_API_URLPATTERN_PREFIX', '/api/')
47+
if not prefix.startswith('/'):
48+
prefix = f"/{prefix}"
49+
if not prefix.endswith('/'):
50+
prefix = f"{prefix}/"
51+
v2_path = f"{prefix}v2/"
52+
v2_index = get_registered_page(v2_path)(self.connection).get()
4653
self.api_ref = ApiV2(connection=self.connection, **{'json': v2_index})
4754
return self.api_ref
4855

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from __future__ import absolute_import, division, print_function
2+
3+
import os
4+
from unittest import mock
5+
6+
__metaclass__ = type
7+
8+
import pytest
9+
10+
11+
def mock_get_registered_page(prefix):
12+
return mock.Mock(return_value=mock.Mock(get=mock.Mock(return_value={'prefix': prefix})))
13+
14+
15+
@pytest.mark.parametrize(
16+
"env_prefix, controller_host, expected",
17+
[
18+
# without CONTROLLER_OPTIONAL_API_URLPATTERN_PREFIX env variable
19+
[None, "https://localhost", "/api/v2/"],
20+
# with CONTROLLER_OPTIONAL_API_URLPATTERN_PREFIX env variable
21+
["/api/controller/", "https://localhost", "/api/controller/v2/"],
22+
["/api/controller", "https://localhost", "/api/controller/v2/"],
23+
["api/controller", "https://localhost", "/api/controller/v2/"],
24+
["/custom/path/", "https://localhost", "/custom/path/v2/"],
25+
],
26+
)
27+
def test_controller_awxkit_get_api_v2_object(collection_import, env_prefix, controller_host, expected):
28+
controller_awxkit_class = collection_import('plugins.module_utils.awxkit').ControllerAWXKitModule
29+
controller_awxkit = controller_awxkit_class(argument_spec={}, direct_params=dict(controller_host=controller_host))
30+
with mock.patch('plugins.module_utils.awxkit.get_registered_page', mock_get_registered_page):
31+
if env_prefix:
32+
with mock.patch.dict(os.environ, {"CONTROLLER_OPTIONAL_API_URLPATTERN_PREFIX": env_prefix}):
33+
api_v2_object = controller_awxkit.get_api_v2_object()
34+
else:
35+
api_v2_object = controller_awxkit.get_api_v2_object()
36+
assert getattr(api_v2_object, 'prefix') == expected

0 commit comments

Comments
 (0)