xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/opengrok.py (revision d52c09ac52ae7aede630f6d1778b696d79a601ca)
12d57dc69SVladimir Kotal#
22d57dc69SVladimir Kotal# CDDL HEADER START
32d57dc69SVladimir Kotal#
42d57dc69SVladimir Kotal# The contents of this file are subject to the terms of the
52d57dc69SVladimir Kotal# Common Development and Distribution License (the "License").
62d57dc69SVladimir Kotal# You may not use this file except in compliance with the License.
72d57dc69SVladimir Kotal#
82d57dc69SVladimir Kotal# See LICENSE.txt included in this distribution for the specific
92d57dc69SVladimir Kotal# language governing permissions and limitations under the License.
102d57dc69SVladimir Kotal#
112d57dc69SVladimir Kotal# When distributing Covered Code, include this CDDL HEADER in each
122d57dc69SVladimir Kotal# file and include the License file at LICENSE.txt.
132d57dc69SVladimir Kotal# If applicable, add the following below this CDDL HEADER, with the
142d57dc69SVladimir Kotal# fields enclosed by brackets "[]" replaced with your own identifying
152d57dc69SVladimir Kotal# information: Portions Copyright [yyyy] [name of copyright owner]
162d57dc69SVladimir Kotal#
172d57dc69SVladimir Kotal# CDDL HEADER END
182d57dc69SVladimir Kotal#
192d57dc69SVladimir Kotal
202d57dc69SVladimir Kotal#
21cc7022c6SVladimir Kotal# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
222d57dc69SVladimir Kotal#
232d57dc69SVladimir Kotal
242d57dc69SVladimir Kotalimport urllib.parse
2596aeefc4SVladimir Kotalfrom requests.exceptions import RequestException
262d57dc69SVladimir Kotalfrom .webutil import get_uri
272d57dc69SVladimir Kotalfrom .restful import do_api_call
282d57dc69SVladimir Kotal
292d57dc69SVladimir Kotal
30732be1c2SVladimir Kotaldef get_repos(logger, project, uri, headers=None, timeout=None):
312d57dc69SVladimir Kotal    """
322d57dc69SVladimir Kotal    :param logger: logger instance
332d57dc69SVladimir Kotal    :param project: project name
342d57dc69SVladimir Kotal    :param uri: web application URI
35732be1c2SVladimir Kotal    :param headers: optional dictionary of HTTP headers
36732be1c2SVladimir Kotal    :param timeout: optional timeout in seconds
372d57dc69SVladimir Kotal    :return: list of repository paths (can be empty if no match)
382d57dc69SVladimir Kotal             or None on failure
392d57dc69SVladimir Kotal    """
402d57dc69SVladimir Kotal
412d57dc69SVladimir Kotal    try:
42e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'projects',
432d57dc69SVladimir Kotal                                         urllib.parse.quote_plus(project),
4489229afdSVladimir Kotal                                         'repositories'),
45732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
4696aeefc4SVladimir Kotal    except RequestException as exception:
47a71dcf5fSVladimir Kotal        logger.error("could not get repositories for project '{}': {}".
48e775f3b3SVladimir Kotal                     format(project, exception))
492d57dc69SVladimir Kotal        return None
502d57dc69SVladimir Kotal
512d57dc69SVladimir Kotal    ret = []
52e775f3b3SVladimir Kotal    for line in res.json():
532d57dc69SVladimir Kotal        ret.append(line.strip())
542d57dc69SVladimir Kotal
552d57dc69SVladimir Kotal    return ret
562d57dc69SVladimir Kotal
572d57dc69SVladimir Kotal
58732be1c2SVladimir Kotaldef get_config_value(logger, name, uri, headers=None, timeout=None):
592d57dc69SVladimir Kotal    """
601822b591SVladimir Kotal    Get configuration value.
612d57dc69SVladimir Kotal
622d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
632d57dc69SVladimir Kotal    """
642d57dc69SVladimir Kotal    try:
65e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration',
6689229afdSVladimir Kotal                                         urllib.parse.quote_plus(name)),
67732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
6896aeefc4SVladimir Kotal    except RequestException as exception:
692d57dc69SVladimir Kotal        logger.error("Cannot get the '{}' config value from the web "
70e775f3b3SVladimir Kotal                     "application: {}".format(name, exception))
712d57dc69SVladimir Kotal        return None
722d57dc69SVladimir Kotal
73e775f3b3SVladimir Kotal    return res.text
742d57dc69SVladimir Kotal
752d57dc69SVladimir Kotal
761822b591SVladimir Kotaldef set_config_value(logger, name, value, uri, headers=None, timeout=None):
771822b591SVladimir Kotal    """
781822b591SVladimir Kotal    Set configuration value.
791822b591SVladimir Kotal    :param logger: logger instance
801822b591SVladimir Kotal    :param name: name of the configuration field
811822b591SVladimir Kotal    :param value: field value
821822b591SVladimir Kotal    :param uri: web app URI
831822b591SVladimir Kotal    :param headers: optional dictionary of HTTP headers
841822b591SVladimir Kotal    :param timeout: optional request timeout
851822b591SVladimir Kotal    :return: True on success, False on failure
861822b591SVladimir Kotal    """
871822b591SVladimir Kotal    try:
881822b591SVladimir Kotal        local_headers = {}
891822b591SVladimir Kotal        if headers:
901822b591SVladimir Kotal            local_headers.update(headers)
911822b591SVladimir Kotal        local_headers['Content-type'] = 'application/text'
921822b591SVladimir Kotal        do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration', name),
931822b591SVladimir Kotal                    data=value, headers=local_headers, timeout=timeout)
9496aeefc4SVladimir Kotal    except RequestException as exception:
95c761ec98SVladimir Kotal        logger.error("Cannot set the '{}' config field to '{}' in the web "
96e775f3b3SVladimir Kotal                     "application: {}".format(name, value, exception))
971822b591SVladimir Kotal        return False
981822b591SVladimir Kotal
991822b591SVladimir Kotal    return True
1001822b591SVladimir Kotal
1011822b591SVladimir Kotal
102732be1c2SVladimir Kotaldef get_repo_type(logger, repository, uri, headers=None, timeout=None):
1032d57dc69SVladimir Kotal    """
1042d57dc69SVladimir Kotal    Get repository type for given path relative to sourceRoot.
1052d57dc69SVladimir Kotal
1062d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
1072d57dc69SVladimir Kotal    """
1082d57dc69SVladimir Kotal    payload = {'repository': repository}
1092d57dc69SVladimir Kotal
1102d57dc69SVladimir Kotal    try:
111e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'repositories',
11289229afdSVladimir Kotal                                         'property', 'type'), params=payload,
113e775f3b3SVladimir Kotal                          headers=headers, timeout=timeout)
11496aeefc4SVladimir Kotal    except RequestException as exception:
115a71dcf5fSVladimir Kotal        logger.error("could not get repository type for '{}' from web"
116e775f3b3SVladimir Kotal                     "application: {}".format(repository, exception))
1172d57dc69SVladimir Kotal        return None
1182d57dc69SVladimir Kotal
119e775f3b3SVladimir Kotal    line = res.text
1202d57dc69SVladimir Kotal
1212d57dc69SVladimir Kotal    idx = line.rfind(":")
1222d57dc69SVladimir Kotal    return line[idx + 1:]
1232d57dc69SVladimir Kotal
1242d57dc69SVladimir Kotal
125732be1c2SVladimir Kotaldef get_configuration(logger, uri, headers=None, timeout=None):
1262d57dc69SVladimir Kotal    try:
127e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration'),
128732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
12996aeefc4SVladimir Kotal    except RequestException as exception:
130a71dcf5fSVladimir Kotal        logger.error('could not get configuration from web application: {}'.
131e775f3b3SVladimir Kotal                     format(exception))
1322d57dc69SVladimir Kotal        return None
1332d57dc69SVladimir Kotal
134e775f3b3SVladimir Kotal    return res.text
1352d57dc69SVladimir Kotal
1362d57dc69SVladimir Kotal
1371c258122SVladimir Kotaldef set_configuration(logger, configuration, uri, headers=None, timeout=None, api_timeout=None):
1382d57dc69SVladimir Kotal    try:
1391c258122SVladimir Kotal        r = do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration'),
1401c258122SVladimir Kotal                        data=configuration, headers=headers, timeout=timeout, api_timeout=api_timeout)
1411c258122SVladimir Kotal        if r is None or r.status_code != 201:
142*d52c09acSVladimir Kotal            logger.error(f'could not set configuration to web application {r}')
1431c258122SVladimir Kotal            return False
14496aeefc4SVladimir Kotal    except RequestException as exception:
145a71dcf5fSVladimir Kotal        logger.error('could not set configuration to web application: {}'.
146e775f3b3SVladimir Kotal                     format(exception))
1472d57dc69SVladimir Kotal        return False
1482d57dc69SVladimir Kotal
1492d57dc69SVladimir Kotal    return True
1502d57dc69SVladimir Kotal
1512d57dc69SVladimir Kotal
152732be1c2SVladimir Kotaldef list_projects(logger, uri, headers=None, timeout=None):
153b2d29daeSVladimir Kotal    try:
154e775f3b3SVladimir Kotal        res = do_api_call('GET',
15589229afdSVladimir Kotal                          get_uri(uri, 'api', 'v1', 'projects'),
156732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
15796aeefc4SVladimir Kotal    except RequestException as exception:
158a71dcf5fSVladimir Kotal        logger.error("could not list projects from web application: {}".
159e775f3b3SVladimir Kotal                     format(exception))
160b2d29daeSVladimir Kotal        return None
161b2d29daeSVladimir Kotal
162e775f3b3SVladimir Kotal    return res.json()
163b2d29daeSVladimir Kotal
164b2d29daeSVladimir Kotal
165732be1c2SVladimir Kotaldef list_indexed_projects(logger, uri, headers=None, timeout=None):
1662d57dc69SVladimir Kotal    try:
167e775f3b3SVladimir Kotal        res = do_api_call('GET',
16889229afdSVladimir Kotal                          get_uri(uri, 'api', 'v1', 'projects', 'indexed'),
169732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
17096aeefc4SVladimir Kotal    except RequestException as exception:
171a71dcf5fSVladimir Kotal        logger.error("could not list indexed projects from web application: {}".
172e775f3b3SVladimir Kotal                     format(exception))
1732d57dc69SVladimir Kotal        return None
1742d57dc69SVladimir Kotal
175e775f3b3SVladimir Kotal    return res.json()
1762d57dc69SVladimir Kotal
1772d57dc69SVladimir Kotal
178f438e25eSVladimir Kotaldef add_project(logger, project, uri, headers=None, timeout=None, api_timeout=None):
1792d57dc69SVladimir Kotal    try:
180f438e25eSVladimir Kotal        r = do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'),
181f438e25eSVladimir Kotal                        data=project, headers=headers, timeout=timeout, api_timeout=api_timeout)
182f438e25eSVladimir Kotal        if r is None or r.status_code != 201:
183*d52c09acSVladimir Kotal            logger.error(f"could not add project '{project}' in web application: {r}")
184f438e25eSVladimir Kotal            return False
18596aeefc4SVladimir Kotal    except RequestException as exception:
1862fad3e77SVladimir Kotal        logger.error("could not add project '{}' to web application: {}".
187e775f3b3SVladimir Kotal                     format(project, exception))
1882d57dc69SVladimir Kotal        return False
1892d57dc69SVladimir Kotal
1902d57dc69SVladimir Kotal    return True
1912d57dc69SVladimir Kotal
1922d57dc69SVladimir Kotal
193f321caadSVladimir Kotaldef _delete_project(logger, project, uri, headers=None, timeout=None, api_timeout=None):
1942d57dc69SVladimir Kotal    try:
195f321caadSVladimir Kotal        r = do_api_call('DELETE', uri,
1961c258122SVladimir Kotal                        headers=headers, timeout=timeout, api_timeout=api_timeout)
197cc7022c6SVladimir Kotal        if r is None or r.status_code != 204:
198*d52c09acSVladimir Kotal            logger.error(f"could not delete project '{project}' in web application: {r}")
1991c258122SVladimir Kotal            return False
20096aeefc4SVladimir Kotal    except RequestException as exception:
2012fad3e77SVladimir Kotal        logger.error("could not delete project '{}' in web application: {}".
202e775f3b3SVladimir Kotal                     format(project, exception))
2032d57dc69SVladimir Kotal        return False
2042d57dc69SVladimir Kotal
2052d57dc69SVladimir Kotal    return True
206f321caadSVladimir Kotal
207f321caadSVladimir Kotal
208f321caadSVladimir Kotaldef delete_project(logger, project, uri, headers=None, timeout=None, api_timeout=None):
209f321caadSVladimir Kotal    return _delete_project(logger, project, get_uri(uri, 'api', 'v1', 'projects',
210f321caadSVladimir Kotal                                                    urllib.parse.quote_plus(project)),
211f321caadSVladimir Kotal                           headers=headers,
212f321caadSVladimir Kotal                           timeout=timeout, api_timeout=api_timeout)
213f321caadSVladimir Kotal
214f321caadSVladimir Kotal
215f321caadSVladimir Kotaldef delete_project_data(logger, project, uri, headers=None, timeout=None, api_timeout=None):
216f321caadSVladimir Kotal    return _delete_project(logger, project, get_uri(uri, 'api', 'v1', 'projects',
217f321caadSVladimir Kotal                                                    urllib.parse.quote_plus(project), 'data'),
218f321caadSVladimir Kotal                           headers=headers,
219f321caadSVladimir Kotal                           timeout=timeout, api_timeout=api_timeout)
220