1# -*- coding: utf-8 -*-
2
3# Copyright: (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
4# Copyright: (c) 2018, Marcus Watkins <marwatk@marcuswatkins.net>
5# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7from __future__ import (absolute_import, division, print_function)
8__metaclass__ = type
9
10import json
11from distutils.version import StrictVersion
12
13from ansible.module_utils.basic import missing_required_lib
14from ansible.module_utils.urls import fetch_url
15from ansible.module_utils.common.text.converters import to_native
16
17try:
18    from urllib import quote_plus  # Python 2.X
19except ImportError:
20    from urllib.parse import quote_plus  # Python 3+
21
22import traceback
23
24GITLAB_IMP_ERR = None
25try:
26    import gitlab
27    HAS_GITLAB_PACKAGE = True
28except Exception:
29    GITLAB_IMP_ERR = traceback.format_exc()
30    HAS_GITLAB_PACKAGE = False
31
32
33def request(module, api_url, project, path, access_token, private_token, rawdata='', method='GET'):
34    url = "%s/v4/projects/%s%s" % (api_url, quote_plus(project), path)
35    headers = {}
36    if access_token:
37        headers['Authorization'] = "Bearer %s" % access_token
38    else:
39        headers['Private-Token'] = private_token
40
41    headers['Accept'] = "application/json"
42    headers['Content-Type'] = "application/json"
43
44    response, info = fetch_url(module=module, url=url, headers=headers, data=rawdata, method=method)
45    status = info['status']
46    content = ""
47    if response:
48        content = response.read()
49    if status == 204:
50        return True, content
51    elif status == 200 or status == 201:
52        return True, json.loads(content)
53    else:
54        return False, str(status) + ": " + content
55
56
57def findProject(gitlab_instance, identifier):
58    try:
59        project = gitlab_instance.projects.get(identifier)
60    except Exception as e:
61        current_user = gitlab_instance.user
62        try:
63            project = gitlab_instance.projects.get(current_user.username + '/' + identifier)
64        except Exception as e:
65            return None
66
67    return project
68
69
70def findGroup(gitlab_instance, identifier):
71    try:
72        project = gitlab_instance.groups.get(identifier)
73    except Exception as e:
74        return None
75
76    return project
77
78
79def gitlabAuthentication(module):
80    gitlab_url = module.params['api_url']
81    validate_certs = module.params['validate_certs']
82    gitlab_user = module.params['api_username']
83    gitlab_password = module.params['api_password']
84    gitlab_token = module.params['api_token']
85
86    if not HAS_GITLAB_PACKAGE:
87        module.fail_json(msg=missing_required_lib("python-gitlab"), exception=GITLAB_IMP_ERR)
88
89    try:
90        # python-gitlab library remove support for username/password authentication since 1.13.0
91        # Changelog : https://github.com/python-gitlab/python-gitlab/releases/tag/v1.13.0
92        # This condition allow to still support older version of the python-gitlab library
93        if StrictVersion(gitlab.__version__) < StrictVersion("1.13.0"):
94            gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, email=gitlab_user, password=gitlab_password,
95                                            private_token=gitlab_token, api_version=4)
96        else:
97            gitlab_instance = gitlab.Gitlab(url=gitlab_url, ssl_verify=validate_certs, private_token=gitlab_token, api_version=4)
98
99        gitlab_instance.auth()
100    except (gitlab.exceptions.GitlabAuthenticationError, gitlab.exceptions.GitlabGetError) as e:
101        module.fail_json(msg="Failed to connect to GitLab server: %s" % to_native(e))
102    except (gitlab.exceptions.GitlabHttpError) as e:
103        module.fail_json(msg="Failed to connect to GitLab server: %s. \
104            GitLab remove Session API now that private tokens are removed from user API endpoints since version 10.2." % to_native(e))
105
106    return gitlab_instance
107