1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

gitlab_runner: Make owned and project mutually exclusive (#4136) (#4210)

* gitlab_runner: Set owned and project mutually exclusive

* gitlab_runner : Refactor _runners_endpoint usage

(cherry picked from commit 05c3e0d69f)

Co-authored-by: Léo GATELLIER <26511053+lgatellier@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2022-02-17 13:29:19 +01:00 committed by GitHub
parent 57fa900f40
commit b1459b13fe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 23 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- 'gitlab_runner - make ``project`` and ``owned`` mutually exclusive (https://github.com/ansible-collections/community.general/pull/4136).'

View file

@ -39,6 +39,7 @@ options:
project: project:
description: description:
- ID or full path of the project in the form of group/name. - 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 type: str
version_added: '3.7.0' version_added: '3.7.0'
description: description:
@ -63,6 +64,7 @@ options:
owned: owned:
description: description:
- Searches only runners available to the user when searching for existing, when false admin token required. - 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 default: no
type: bool type: bool
version_added: 2.0.0 version_added: 2.0.0
@ -199,7 +201,13 @@ class GitLabRunner(object):
# Whether to operate on GitLab-instance-wide or project-wide runners # Whether to operate on GitLab-instance-wide or project-wide runners
# See https://gitlab.com/gitlab-org/gitlab-ce/issues/60774 # See https://gitlab.com/gitlab-org/gitlab-ce/issues/60774
# for group runner token access # 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 self.runner_object = None
def create_or_update_runner(self, description, options): def create_or_update_runner(self, description, options):
@ -281,11 +289,8 @@ class GitLabRunner(object):
''' '''
@param description Description of the runner @param description Description of the runner
''' '''
def find_runner(self, description, owned=False): def find_runner(self, description):
if owned: runners = self._runners_endpoint(as_list=False)
runners = self._runners_endpoint.list(as_list=False)
else:
runners = self._runners_endpoint.all(as_list=False)
for runner in runners: for runner in runners:
# python-gitlab 2.2 through at least 2.5 returns a list of dicts for list() instead of a Runner # 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 @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. # When runner exists, object will be stored in self.runner_object.
runner = self.find_runner(description, owned) runner = self.find_runner(description)
if runner: if runner:
self.runner_object = runner self.runner_object = runner
@ -343,6 +348,7 @@ def main():
['api_username', 'api_job_token'], ['api_username', 'api_job_token'],
['api_token', 'api_oauth_token'], ['api_token', 'api_oauth_token'],
['api_token', 'api_job_token'], ['api_token', 'api_job_token'],
['project', 'owned'],
], ],
required_together=[ required_together=[
['api_username', 'api_password'], ['api_username', 'api_password'],
@ -357,7 +363,6 @@ def main():
) )
state = module.params['state'] state = module.params['state']
owned = module.params['owned']
runner_description = module.params['description'] runner_description = module.params['description']
runner_active = module.params['active'] runner_active = module.params['active']
tag_list = module.params['tag_list'] 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)) module.fail_json(msg='No such a project %s' % project, exception=to_native(e))
gitlab_runner = GitLabRunner(module, gitlab_instance, gitlab_project) 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 state == 'absent':
if runner_exists: if runner_exists:

View file

@ -17,8 +17,9 @@ import gitlab
class FakeAnsibleModule(object): class FakeAnsibleModule(object):
def __init__(self): def __init__(self, module_params=None):
self.check_mode = False self.check_mode = False
self.params = module_params if module_params else {}
def fail_json(self, **args): def fail_json(self, **args):
pass pass

View file

@ -19,7 +19,8 @@ def _dummy(x):
pytestmark = [] pytestmark = []
try: try:
from .gitlab import (GitlabModuleTestCase, from .gitlab import (FakeAnsibleModule,
GitlabModuleTestCase,
python_version_match_requirement, python_version_match_requirement,
resp_find_runners_all, resp_find_runners_all,
resp_find_runners_list, resp_get_runner, resp_find_runners_list, resp_get_runner,
@ -49,33 +50,34 @@ class TestGitlabRunner(GitlabModuleTestCase):
def setUp(self): def setUp(self):
super(TestGitlabRunner, self).setUp() 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_find_runners_all)
@with_httmock(resp_get_runner) @with_httmock(resp_get_runner)
def test_runner_exist_all(self): 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) 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) self.assertEqual(rvalue, False)
@with_httmock(resp_find_runners_list) @with_httmock(resp_find_runners_list)
@with_httmock(resp_get_runner) @with_httmock(resp_get_runner)
def test_runner_exist_owned(self): 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) 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) self.assertEqual(rvalue, False)
@with_httmock(resp_create_runner) @with_httmock(resp_create_runner)
def test_create_runner(self): 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(type(runner), Runner)
self.assertEqual(runner.description, "test-1-20150125") self.assertEqual(runner.description, "test-1-20150125")
@ -83,15 +85,15 @@ class TestGitlabRunner(GitlabModuleTestCase):
@with_httmock(resp_find_runners_all) @with_httmock(resp_find_runners_all)
@with_httmock(resp_get_runner) @with_httmock(resp_get_runner)
def test_update_runner(self): 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(changed, True)
self.assertEqual(type(newRunner), Runner) self.assertEqual(type(newRunner), Runner)
self.assertEqual(newRunner.description, "Runner description") 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(changed, False)
self.assertEqual(newRunner.description, "Runner description") self.assertEqual(newRunner.description, "Runner description")
@ -100,8 +102,8 @@ class TestGitlabRunner(GitlabModuleTestCase):
@with_httmock(resp_get_runner) @with_httmock(resp_get_runner)
@with_httmock(resp_delete_runner) @with_httmock(resp_delete_runner)
def test_delete_runner(self): 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) self.assertEqual(rvalue, None)