xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/opengrok.py (revision f438e25eb6fb169e0b6a8c78d94cfce3f8c4e645)
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
252d57dc69SVladimir Kotalfrom .webutil import get_uri
262d57dc69SVladimir Kotalfrom .restful import do_api_call
272d57dc69SVladimir Kotal
282d57dc69SVladimir Kotal
29732be1c2SVladimir Kotaldef get_repos(logger, project, uri, headers=None, timeout=None):
302d57dc69SVladimir Kotal    """
312d57dc69SVladimir Kotal    :param logger: logger instance
322d57dc69SVladimir Kotal    :param project: project name
332d57dc69SVladimir Kotal    :param uri: web application URI
34732be1c2SVladimir Kotal    :param headers: optional dictionary of HTTP headers
35732be1c2SVladimir Kotal    :param timeout: optional timeout in seconds
362d57dc69SVladimir Kotal    :return: list of repository paths (can be empty if no match)
372d57dc69SVladimir Kotal             or None on failure
382d57dc69SVladimir Kotal    """
392d57dc69SVladimir Kotal
402d57dc69SVladimir Kotal    try:
41e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'projects',
422d57dc69SVladimir Kotal                                         urllib.parse.quote_plus(project),
4389229afdSVladimir Kotal                                         'repositories'),
44732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
45e775f3b3SVladimir Kotal    except Exception as exception:
46a71dcf5fSVladimir Kotal        logger.error("could not get repositories for project '{}': {}".
47e775f3b3SVladimir Kotal                     format(project, exception))
482d57dc69SVladimir Kotal        return None
492d57dc69SVladimir Kotal
502d57dc69SVladimir Kotal    ret = []
51e775f3b3SVladimir Kotal    for line in res.json():
522d57dc69SVladimir Kotal        ret.append(line.strip())
532d57dc69SVladimir Kotal
542d57dc69SVladimir Kotal    return ret
552d57dc69SVladimir Kotal
562d57dc69SVladimir Kotal
57732be1c2SVladimir Kotaldef get_config_value(logger, name, uri, headers=None, timeout=None):
582d57dc69SVladimir Kotal    """
591822b591SVladimir Kotal    Get configuration value.
602d57dc69SVladimir Kotal
612d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
622d57dc69SVladimir Kotal    """
632d57dc69SVladimir Kotal    try:
64e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration',
6589229afdSVladimir Kotal                                         urllib.parse.quote_plus(name)),
66732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
67e775f3b3SVladimir Kotal    except Exception as exception:
682d57dc69SVladimir Kotal        logger.error("Cannot get the '{}' config value from the web "
69e775f3b3SVladimir Kotal                     "application: {}".format(name, exception))
702d57dc69SVladimir Kotal        return None
712d57dc69SVladimir Kotal
72e775f3b3SVladimir Kotal    return res.text
732d57dc69SVladimir Kotal
742d57dc69SVladimir Kotal
751822b591SVladimir Kotaldef set_config_value(logger, name, value, uri, headers=None, timeout=None):
761822b591SVladimir Kotal    """
771822b591SVladimir Kotal    Set configuration value.
781822b591SVladimir Kotal    :param logger: logger instance
791822b591SVladimir Kotal    :param name: name of the configuration field
801822b591SVladimir Kotal    :param value: field value
811822b591SVladimir Kotal    :param uri: web app URI
821822b591SVladimir Kotal    :param headers: optional dictionary of HTTP headers
831822b591SVladimir Kotal    :param timeout: optional request timeout
841822b591SVladimir Kotal    :return: True on success, False on failure
851822b591SVladimir Kotal    """
861822b591SVladimir Kotal    try:
871822b591SVladimir Kotal        local_headers = {}
881822b591SVladimir Kotal        if headers:
891822b591SVladimir Kotal            local_headers.update(headers)
901822b591SVladimir Kotal        local_headers['Content-type'] = 'application/text'
911822b591SVladimir Kotal        do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration', name),
921822b591SVladimir Kotal                    data=value, headers=local_headers, timeout=timeout)
93e775f3b3SVladimir Kotal    except Exception as exception:
94c761ec98SVladimir Kotal        logger.error("Cannot set the '{}' config field to '{}' in the web "
95e775f3b3SVladimir Kotal                     "application: {}".format(name, value, exception))
961822b591SVladimir Kotal        return False
971822b591SVladimir Kotal
981822b591SVladimir Kotal    return True
991822b591SVladimir Kotal
1001822b591SVladimir Kotal
101732be1c2SVladimir Kotaldef get_repo_type(logger, repository, uri, headers=None, timeout=None):
1022d57dc69SVladimir Kotal    """
1032d57dc69SVladimir Kotal    Get repository type for given path relative to sourceRoot.
1042d57dc69SVladimir Kotal
1052d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
1062d57dc69SVladimir Kotal    """
1072d57dc69SVladimir Kotal    payload = {'repository': repository}
1082d57dc69SVladimir Kotal
1092d57dc69SVladimir Kotal    try:
110e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'repositories',
11189229afdSVladimir Kotal                                         'property', 'type'), params=payload,
112e775f3b3SVladimir Kotal                          headers=headers, timeout=timeout)
113e775f3b3SVladimir Kotal    except Exception as exception:
114a71dcf5fSVladimir Kotal        logger.error("could not get repository type for '{}' from web"
115e775f3b3SVladimir Kotal                     "application: {}".format(repository, exception))
1162d57dc69SVladimir Kotal        return None
1172d57dc69SVladimir Kotal
118e775f3b3SVladimir Kotal    line = res.text
1192d57dc69SVladimir Kotal
1202d57dc69SVladimir Kotal    idx = line.rfind(":")
1212d57dc69SVladimir Kotal    return line[idx + 1:]
1222d57dc69SVladimir Kotal
1232d57dc69SVladimir Kotal
124732be1c2SVladimir Kotaldef get_configuration(logger, uri, headers=None, timeout=None):
1252d57dc69SVladimir Kotal    try:
126e775f3b3SVladimir Kotal        res = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration'),
127732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
128e775f3b3SVladimir Kotal    except Exception as exception:
129a71dcf5fSVladimir Kotal        logger.error('could not get configuration from web application: {}'.
130e775f3b3SVladimir Kotal                     format(exception))
1312d57dc69SVladimir Kotal        return None
1322d57dc69SVladimir Kotal
133e775f3b3SVladimir Kotal    return res.text
1342d57dc69SVladimir Kotal
1352d57dc69SVladimir Kotal
1361c258122SVladimir Kotaldef set_configuration(logger, configuration, uri, headers=None, timeout=None, api_timeout=None):
1372d57dc69SVladimir Kotal    try:
1381c258122SVladimir Kotal        r = do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration'),
1391c258122SVladimir Kotal                        data=configuration, headers=headers, timeout=timeout, api_timeout=api_timeout)
1401c258122SVladimir Kotal        if r is None or r.status_code != 201:
1411c258122SVladimir Kotal            logger.error('could not set configuration to web application')
1421c258122SVladimir Kotal            return False
143e775f3b3SVladimir Kotal    except Exception as exception:
144a71dcf5fSVladimir Kotal        logger.error('could not set configuration to web application: {}'.
145e775f3b3SVladimir Kotal                     format(exception))
1462d57dc69SVladimir Kotal        return False
1472d57dc69SVladimir Kotal
1482d57dc69SVladimir Kotal    return True
1492d57dc69SVladimir Kotal
1502d57dc69SVladimir Kotal
151732be1c2SVladimir Kotaldef list_projects(logger, uri, headers=None, timeout=None):
152b2d29daeSVladimir Kotal    try:
153e775f3b3SVladimir Kotal        res = do_api_call('GET',
15489229afdSVladimir Kotal                          get_uri(uri, 'api', 'v1', 'projects'),
155732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
156e775f3b3SVladimir Kotal    except Exception as exception:
157a71dcf5fSVladimir Kotal        logger.error("could not list projects from web application: {}".
158e775f3b3SVladimir Kotal                     format(exception))
159b2d29daeSVladimir Kotal        return None
160b2d29daeSVladimir Kotal
161e775f3b3SVladimir Kotal    return res.json()
162b2d29daeSVladimir Kotal
163b2d29daeSVladimir Kotal
164732be1c2SVladimir Kotaldef list_indexed_projects(logger, uri, headers=None, timeout=None):
1652d57dc69SVladimir Kotal    try:
166e775f3b3SVladimir Kotal        res = do_api_call('GET',
16789229afdSVladimir Kotal                          get_uri(uri, 'api', 'v1', 'projects', 'indexed'),
168732be1c2SVladimir Kotal                          headers=headers, timeout=timeout)
169e775f3b3SVladimir Kotal    except Exception as exception:
170a71dcf5fSVladimir Kotal        logger.error("could not list indexed projects from web application: {}".
171e775f3b3SVladimir Kotal                     format(exception))
1722d57dc69SVladimir Kotal        return None
1732d57dc69SVladimir Kotal
174e775f3b3SVladimir Kotal    return res.json()
1752d57dc69SVladimir Kotal
1762d57dc69SVladimir Kotal
177*f438e25eSVladimir Kotaldef add_project(logger, project, uri, headers=None, timeout=None, api_timeout=None):
1782d57dc69SVladimir Kotal    try:
179*f438e25eSVladimir Kotal        r = do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'),
180*f438e25eSVladimir Kotal                        data=project, headers=headers, timeout=timeout, api_timeout=api_timeout)
181*f438e25eSVladimir Kotal        if r is None or r.status_code != 201:
182*f438e25eSVladimir Kotal            logger.error(f"could not add project '{project}' in web application")
183*f438e25eSVladimir Kotal            return False
184e775f3b3SVladimir Kotal    except Exception as exception:
1852fad3e77SVladimir Kotal        logger.error("could not add project '{}' to web application: {}".
186e775f3b3SVladimir Kotal                     format(project, exception))
1872d57dc69SVladimir Kotal        return False
1882d57dc69SVladimir Kotal
1892d57dc69SVladimir Kotal    return True
1902d57dc69SVladimir Kotal
1912d57dc69SVladimir Kotal
1921c258122SVladimir Kotaldef delete_project(logger, project, uri, headers=None, timeout=None, api_timeout=None):
1932d57dc69SVladimir Kotal    try:
1941c258122SVladimir Kotal        r = do_api_call('DELETE', get_uri(uri, 'api', 'v1', 'projects',
19589229afdSVladimir Kotal                                          urllib.parse.quote_plus(project)),
1961c258122SVladimir Kotal                        headers=headers, timeout=timeout, api_timeout=api_timeout)
197cc7022c6SVladimir Kotal        if r is None or r.status_code != 204:
1981c258122SVladimir Kotal            logger.error(f"could not delete project '{project}' in web application")
1991c258122SVladimir Kotal            return False
200e775f3b3SVladimir Kotal    except Exception 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
206