From 05c3e0d69f3653a352b5fbf1671ff4e2c0e9c812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20GATELLIER?= <26511053+lgatellier@users.noreply.github.com> Date: Wed, 16 Feb 2022 22:54:46 +0100 Subject: [PATCH] gitlab_runner: Make owned and project mutually exclusive (#4136) * gitlab_runner: Set owned and project mutually exclusive * gitlab_runner : Refactor _runners_endpoint usage --- ...-make-project-owned-mutually-exclusive.yml | 2 ++ .../source_control/gitlab/gitlab_runner.py | 25 +++++++++++------- .../modules/source_control/gitlab/gitlab.py | 3 ++- .../gitlab/test_gitlab_runner.py | 26 ++++++++++--------- 4 files changed, 33 insertions(+), 23 deletions(-) create mode 100644 changelogs/fragments/4136-gitlab_runner-make-project-owned-mutually-exclusive.yml diff --git a/changelogs/fragments/4136-gitlab_runner-make-project-owned-mutually-exclusive.yml b/changelogs/fragments/4136-gitlab_runner-make-project-owned-mutually-exclusive.yml new file mode 100644 index 0000000000..aba60a8692 --- /dev/null +++ b/changelogs/fragments/4136-gitlab_runner-make-project-owned-mutually-exclusive.yml @@ -0,0 +1,2 @@ +bugfixes: + - 'gitlab_runner - make ``project`` and ``owned`` mutually exclusive (https://github.com/ansible-collections/community.general/pull/4136).' diff --git a/plugins/modules/source_control/gitlab/gitlab_runner.py b/plugins/modules/source_control/gitlab/gitlab_runner.py index 08c0f54bf7..c31030ab01 100644 --- a/plugins/modules/source_control/gitlab/gitlab_runner.py +++ b/plugins/modules/source_control/gitlab/gitlab_runner.py @@ -39,6 +39,7 @@ options: project: description: - ID or full path of the project in the form of group/name. + - Mutually exclusive with I(owned) since community.general 4.5.0. type: str version_added: '3.7.0' description: @@ -63,6 +64,7 @@ options: owned: description: - Searches only runners available to the user when searching for existing, when false admin token required. + - Mutually exclusive with I(project) since community.general 4.5.0. default: no type: bool version_added: 2.0.0 @@ -199,7 +201,13 @@ class GitLabRunner(object): # Whether to operate on GitLab-instance-wide or project-wide runners # See https://gitlab.com/gitlab-org/gitlab-ce/issues/60774 # for group runner token access - self._runners_endpoint = project.runners if project else gitlab_instance.runners + if project: + self._runners_endpoint = project.runners.list + elif module.params['owned']: + self._runners_endpoint = gitlab_instance.runners.list + else: + self._runners_endpoint = gitlab_instance.runners.all + self.runner_object = None def create_or_update_runner(self, description, options): @@ -281,11 +289,8 @@ class GitLabRunner(object): ''' @param description Description of the runner ''' - def find_runner(self, description, owned=False): - if owned: - runners = self._runners_endpoint.list(as_list=False) - else: - runners = self._runners_endpoint.all(as_list=False) + def find_runner(self, description): + runners = self._runners_endpoint(as_list=False) for runner in runners: # python-gitlab 2.2 through at least 2.5 returns a list of dicts for list() instead of a Runner @@ -300,9 +305,9 @@ class GitLabRunner(object): ''' @param description Description of the runner ''' - def exists_runner(self, description, owned=False): + def exists_runner(self, description): # When runner exists, object will be stored in self.runner_object. - runner = self.find_runner(description, owned) + runner = self.find_runner(description) if runner: self.runner_object = runner @@ -343,6 +348,7 @@ def main(): ['api_username', 'api_job_token'], ['api_token', 'api_oauth_token'], ['api_token', 'api_job_token'], + ['project', 'owned'], ], required_together=[ ['api_username', 'api_password'], @@ -357,7 +363,6 @@ def main(): ) state = module.params['state'] - owned = module.params['owned'] runner_description = module.params['description'] runner_active = module.params['active'] tag_list = module.params['tag_list'] @@ -380,7 +385,7 @@ def main(): module.fail_json(msg='No such a project %s' % project, exception=to_native(e)) gitlab_runner = GitLabRunner(module, gitlab_instance, gitlab_project) - runner_exists = gitlab_runner.exists_runner(runner_description, owned) + runner_exists = gitlab_runner.exists_runner(runner_description) if state == 'absent': if runner_exists: diff --git a/tests/unit/plugins/modules/source_control/gitlab/gitlab.py b/tests/unit/plugins/modules/source_control/gitlab/gitlab.py index de51144069..5c39c9afaf 100644 --- a/tests/unit/plugins/modules/source_control/gitlab/gitlab.py +++ b/tests/unit/plugins/modules/source_control/gitlab/gitlab.py @@ -17,8 +17,9 @@ import gitlab class FakeAnsibleModule(object): - def __init__(self): + def __init__(self, module_params=None): self.check_mode = False + self.params = module_params if module_params else {} def fail_json(self, **args): pass diff --git a/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_runner.py b/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_runner.py index 0c13bc79c3..d86e27493d 100644 --- a/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_runner.py +++ b/tests/unit/plugins/modules/source_control/gitlab/test_gitlab_runner.py @@ -19,7 +19,8 @@ def _dummy(x): pytestmark = [] try: - from .gitlab import (GitlabModuleTestCase, + from .gitlab import (FakeAnsibleModule, + GitlabModuleTestCase, python_version_match_requirement, resp_find_runners_all, resp_find_runners_list, resp_get_runner, @@ -49,33 +50,34 @@ class TestGitlabRunner(GitlabModuleTestCase): def setUp(self): super(TestGitlabRunner, self).setUp() - self.moduleUtil = GitLabRunner(module=self.mock_module, gitlab_instance=self.gitlab_instance) + self.module_util_all = GitLabRunner(module=FakeAnsibleModule({"owned": False}), gitlab_instance=self.gitlab_instance) + self.module_util_owned = GitLabRunner(module=FakeAnsibleModule({"owned": True}), gitlab_instance=self.gitlab_instance) @with_httmock(resp_find_runners_all) @with_httmock(resp_get_runner) def test_runner_exist_all(self): - rvalue = self.moduleUtil.exists_runner("test-1-20150125") + rvalue = self.module_util_all.exists_runner("test-1-20150125") self.assertEqual(rvalue, True) - rvalue = self.moduleUtil.exists_runner("test-3-00000000") + rvalue = self.module_util_all.exists_runner("test-3-00000000") self.assertEqual(rvalue, False) @with_httmock(resp_find_runners_list) @with_httmock(resp_get_runner) def test_runner_exist_owned(self): - rvalue = self.moduleUtil.exists_runner("test-1-20201214", True) + rvalue = self.module_util_owned.exists_runner("test-1-20201214") self.assertEqual(rvalue, True) - rvalue = self.moduleUtil.exists_runner("test-3-00000000", True) + rvalue = self.module_util_owned.exists_runner("test-3-00000000") self.assertEqual(rvalue, False) @with_httmock(resp_create_runner) def test_create_runner(self): - runner = self.moduleUtil.create_runner({"token": "token", "description": "test-1-20150125"}) + runner = self.module_util_all.create_runner({"token": "token", "description": "test-1-20150125"}) self.assertEqual(type(runner), Runner) self.assertEqual(runner.description, "test-1-20150125") @@ -83,15 +85,15 @@ class TestGitlabRunner(GitlabModuleTestCase): @with_httmock(resp_find_runners_all) @with_httmock(resp_get_runner) def test_update_runner(self): - runner = self.moduleUtil.find_runner("test-1-20150125") + runner = self.module_util_all.find_runner("test-1-20150125") - changed, newRunner = self.moduleUtil.update_runner(runner, {"description": "Runner description"}) + changed, newRunner = self.module_util_all.update_runner(runner, {"description": "Runner description"}) self.assertEqual(changed, True) self.assertEqual(type(newRunner), Runner) self.assertEqual(newRunner.description, "Runner description") - changed, newRunner = self.moduleUtil.update_runner(runner, {"description": "Runner description"}) + changed, newRunner = self.module_util_all.update_runner(runner, {"description": "Runner description"}) self.assertEqual(changed, False) self.assertEqual(newRunner.description, "Runner description") @@ -100,8 +102,8 @@ class TestGitlabRunner(GitlabModuleTestCase): @with_httmock(resp_get_runner) @with_httmock(resp_delete_runner) def test_delete_runner(self): - self.moduleUtil.exists_runner("test-1-20150125") + self.module_util_all.exists_runner("test-1-20150125") - rvalue = self.moduleUtil.delete_runner() + rvalue = self.module_util_all.delete_runner() self.assertEqual(rvalue, None)