2020-03-09 10:11:07 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
|
|
|
|
# Copyright (c) 2018, Marcus Watkins <marwatk@marcuswatkins.net>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-06-24 21:50:36 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.module_utils.basic import missing_required_lib
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2023-05-08 19:40:52 +02:00
|
|
|
from ansible.module_utils.six import integer_types, string_types
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2021-12-24 18:34:48 +01:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.version import LooseVersion
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
2021-12-20 21:59:12 +01:00
|
|
|
from urlparse import urljoin
|
2020-03-09 10:11:07 +01:00
|
|
|
except ImportError:
|
2022-09-12 20:30:02 +02:00
|
|
|
from urllib.parse import urljoin # Python 3+
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
import traceback
|
|
|
|
|
2024-05-06 19:45:53 +02:00
|
|
|
|
|
|
|
def _determine_list_all_kwargs(version):
|
|
|
|
gitlab_version = LooseVersion(version)
|
|
|
|
if gitlab_version >= LooseVersion('4.0.0'):
|
|
|
|
# 4.0.0 removed 'as_list'
|
|
|
|
return {'iterator': True, 'per_page': 100}
|
|
|
|
elif gitlab_version >= LooseVersion('3.7.0'):
|
|
|
|
# 3.7.0 added 'get_all'
|
|
|
|
return {'as_list': False, 'get_all': True, 'per_page': 100}
|
|
|
|
else:
|
|
|
|
return {'as_list': False, 'all': True, 'per_page': 100}
|
|
|
|
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
GITLAB_IMP_ERR = None
|
|
|
|
try:
|
|
|
|
import gitlab
|
2021-12-20 21:59:12 +01:00
|
|
|
import requests
|
2020-03-09 10:11:07 +01:00
|
|
|
HAS_GITLAB_PACKAGE = True
|
2024-05-06 19:45:53 +02:00
|
|
|
list_all_kwargs = _determine_list_all_kwargs(gitlab.__version__)
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception:
|
2022-09-12 20:30:02 +02:00
|
|
|
gitlab = None
|
2020-03-09 10:11:07 +01:00
|
|
|
GITLAB_IMP_ERR = traceback.format_exc()
|
|
|
|
HAS_GITLAB_PACKAGE = False
|
2024-05-06 19:45:53 +02:00
|
|
|
list_all_kwargs = {}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-12-20 21:59:12 +01:00
|
|
|
def auth_argument_spec(spec=None):
|
|
|
|
arg_spec = (dict(
|
|
|
|
api_token=dict(type='str', no_log=True),
|
|
|
|
api_oauth_token=dict(type='str', no_log=True),
|
|
|
|
api_job_token=dict(type='str', no_log=True),
|
|
|
|
))
|
|
|
|
if spec:
|
|
|
|
arg_spec.update(spec)
|
|
|
|
return arg_spec
|
|
|
|
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
def find_project(gitlab_instance, identifier):
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
|
|
|
project = gitlab_instance.projects.get(identifier)
|
|
|
|
except Exception as e:
|
|
|
|
current_user = gitlab_instance.user
|
|
|
|
try:
|
|
|
|
project = gitlab_instance.projects.get(current_user.username + '/' + identifier)
|
|
|
|
except Exception as e:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return project
|
|
|
|
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
def find_group(gitlab_instance, identifier):
|
2020-03-09 10:11:07 +01:00
|
|
|
try:
|
|
|
|
project = gitlab_instance.groups.get(identifier)
|
|
|
|
except Exception as e:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return project
|
|
|
|
|
|
|
|
|
2022-09-12 20:30:02 +02:00
|
|
|
def ensure_gitlab_package(module):
|
|
|
|
if not HAS_GITLAB_PACKAGE:
|
|
|
|
module.fail_json(
|
|
|
|
msg=missing_required_lib("python-gitlab", url='https://python-gitlab.readthedocs.io/en/stable/'),
|
|
|
|
exception=GITLAB_IMP_ERR
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
def gitlab_authentication(module):
|
2020-03-09 10:11:07 +01:00
|
|
|
gitlab_url = module.params['api_url']
|
|
|
|
validate_certs = module.params['validate_certs']
|
|
|
|
gitlab_user = module.params['api_username']
|
|
|
|
gitlab_password = module.params['api_password']
|
|
|
|
gitlab_token = module.params['api_token']
|
2021-12-20 21:59:12 +01:00
|
|
|
gitlab_oauth_token = module.params['api_oauth_token']
|
|
|
|
gitlab_job_token = module.params['api_job_token']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-09-12 20:30:02 +02:00
|
|
|
ensure_gitlab_package(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
# python-gitlab library remove support for username/password authentication since 1.13.0
|
|
|
|
# Changelog : https://github.com/python-gitlab/python-gitlab/releases/tag/v1.13.0
|
|
|
|
# This condition allow to still support older version of the python-gitlab library
|
2021-12-24 18:34:48 +01:00
|
|
|
if LooseVersion(gitlab.__version__) < LooseVersion("1.13.0"):
|
2020-03-09 10:11:07 +01:00
|
|
|
gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, email=gitlab_user, password=gitlab_password,
|
|
|
|
private_token=gitlab_token, api_version=4)
|
|
|
|
else:
|
2021-12-20 21:59:12 +01:00
|
|
|
# We can create an oauth_token using a username and password
|
|
|
|
# https://docs.gitlab.com/ee/api/oauth2.html#authorization-code-flow
|
|
|
|
if gitlab_user:
|
|
|
|
data = {'grant_type': 'password', 'username': gitlab_user, 'password': gitlab_password}
|
|
|
|
resp = requests.post(urljoin(gitlab_url, "oauth/token"), data=data, verify=validate_certs)
|
|
|
|
resp_data = resp.json()
|
|
|
|
gitlab_oauth_token = resp_data["access_token"]
|
|
|
|
|
|
|
|
gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, private_token=gitlab_token,
|
|
|
|
oauth_token=gitlab_oauth_token, job_token=gitlab_job_token, api_version=4)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
gitlab_instance.auth()
|
|
|
|
except (gitlab.exceptions.GitlabAuthenticationError, gitlab.exceptions.GitlabGetError) as e:
|
|
|
|
module.fail_json(msg="Failed to connect to GitLab server: %s" % to_native(e))
|
|
|
|
except (gitlab.exceptions.GitlabHttpError) as e:
|
|
|
|
module.fail_json(msg="Failed to connect to GitLab server: %s. \
|
|
|
|
GitLab remove Session API now that private tokens are removed from user API endpoints since version 10.2." % to_native(e))
|
|
|
|
|
|
|
|
return gitlab_instance
|
2022-12-10 21:40:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
def filter_returned_variables(gitlab_variables):
|
|
|
|
# pop properties we don't know
|
|
|
|
existing_variables = [dict(x.attributes) for x in gitlab_variables]
|
2023-09-20 22:04:19 +02:00
|
|
|
KNOWN = ['key', 'value', 'masked', 'protected', 'variable_type', 'environment_scope', 'raw']
|
2022-12-10 21:40:36 +01:00
|
|
|
for item in existing_variables:
|
|
|
|
for key in list(item.keys()):
|
|
|
|
if key not in KNOWN:
|
|
|
|
item.pop(key)
|
|
|
|
return existing_variables
|
2023-05-08 19:40:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
def vars_to_variables(vars, module):
|
|
|
|
# transform old vars to new variables structure
|
|
|
|
variables = list()
|
|
|
|
for item, value in vars.items():
|
|
|
|
if isinstance(value, (string_types, integer_types, float)):
|
|
|
|
variables.append(
|
|
|
|
{
|
|
|
|
"name": item,
|
|
|
|
"value": str(value),
|
|
|
|
"masked": False,
|
|
|
|
"protected": False,
|
2023-08-29 13:05:58 +02:00
|
|
|
"raw": False,
|
2023-05-08 19:40:52 +02:00
|
|
|
"variable_type": "env_var",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
elif isinstance(value, dict):
|
|
|
|
new_item = {
|
|
|
|
"name": item,
|
|
|
|
"value": value.get('value'),
|
|
|
|
"masked": value.get('masked'),
|
|
|
|
"protected": value.get('protected'),
|
2023-08-29 13:05:58 +02:00
|
|
|
"raw": value.get('raw'),
|
2023-05-08 19:40:52 +02:00
|
|
|
"variable_type": value.get('variable_type'),
|
|
|
|
}
|
|
|
|
|
|
|
|
if value.get('environment_scope'):
|
|
|
|
new_item['environment_scope'] = value.get('environment_scope')
|
|
|
|
|
|
|
|
variables.append(new_item)
|
|
|
|
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="value must be of type string, integer, float or dict")
|
|
|
|
|
|
|
|
return variables
|