xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/opengrok.py (revision 732be1c2480241b1b9a3e4ddc958855fb839662b)
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#
21b2d29daeSVladimir Kotal# Copyright (c) 2018, 2021, 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
29*732be1c2SVladimir 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
34*732be1c2SVladimir Kotal    :param headers: optional dictionary of HTTP headers
35*732be1c2SVladimir 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:
412d57dc69SVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'projects',
422d57dc69SVladimir Kotal                                       urllib.parse.quote_plus(project),
4389229afdSVladimir Kotal                                       'repositories'),
44*732be1c2SVladimir Kotal                        headers=headers, timeout=timeout)
452d57dc69SVladimir Kotal    except Exception:
462d57dc69SVladimir Kotal        logger.error('could not get repositories for ' + project)
472d57dc69SVladimir Kotal        return None
482d57dc69SVladimir Kotal
492d57dc69SVladimir Kotal    ret = []
502d57dc69SVladimir Kotal    for line in r.json():
512d57dc69SVladimir Kotal        ret.append(line.strip())
522d57dc69SVladimir Kotal
532d57dc69SVladimir Kotal    return ret
542d57dc69SVladimir Kotal
552d57dc69SVladimir Kotal
56*732be1c2SVladimir Kotaldef get_config_value(logger, name, uri, headers=None, timeout=None):
572d57dc69SVladimir Kotal    """
582d57dc69SVladimir Kotal    Get list of repositories for given project name.
592d57dc69SVladimir Kotal
602d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
612d57dc69SVladimir Kotal    """
622d57dc69SVladimir Kotal    try:
632d57dc69SVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration',
6489229afdSVladimir Kotal                                       urllib.parse.quote_plus(name)),
65*732be1c2SVladimir Kotal                        headers=headers, timeout=timeout)
662d57dc69SVladimir Kotal    except Exception:
672d57dc69SVladimir Kotal        logger.error("Cannot get the '{}' config value from the web "
682d57dc69SVladimir Kotal                     "application on {}".format(name, uri))
692d57dc69SVladimir Kotal        return None
702d57dc69SVladimir Kotal
712d57dc69SVladimir Kotal    return r.text
722d57dc69SVladimir Kotal
732d57dc69SVladimir Kotal
74*732be1c2SVladimir Kotaldef get_repo_type(logger, repository, uri, headers=None, timeout=None):
752d57dc69SVladimir Kotal    """
762d57dc69SVladimir Kotal    Get repository type for given path relative to sourceRoot.
772d57dc69SVladimir Kotal
782d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
792d57dc69SVladimir Kotal    """
802d57dc69SVladimir Kotal    payload = {'repository': repository}
812d57dc69SVladimir Kotal
822d57dc69SVladimir Kotal    try:
832d57dc69SVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'repositories',
8489229afdSVladimir Kotal                                       'property', 'type'), params=payload,
85*732be1c2SVladimir Kotal                        headers=headers, timeout=None)
862d57dc69SVladimir Kotal    except Exception:
872d57dc69SVladimir Kotal        logger.error('could not get repository type for {} from web'
882d57dc69SVladimir Kotal                     'application on {}'.format(repository, uri))
892d57dc69SVladimir Kotal        return None
902d57dc69SVladimir Kotal
912d57dc69SVladimir Kotal    line = r.text
922d57dc69SVladimir Kotal
932d57dc69SVladimir Kotal    idx = line.rfind(":")
942d57dc69SVladimir Kotal    return line[idx + 1:]
952d57dc69SVladimir Kotal
962d57dc69SVladimir Kotal
97*732be1c2SVladimir Kotaldef get_configuration(logger, uri, headers=None, timeout=None):
982d57dc69SVladimir Kotal    try:
9989229afdSVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration'),
100*732be1c2SVladimir Kotal                        headers=headers, timeout=timeout)
1012d57dc69SVladimir Kotal    except Exception:
1022d57dc69SVladimir Kotal        logger.error('could not get configuration from web application on {}'.
1032d57dc69SVladimir Kotal                     format(uri))
1042d57dc69SVladimir Kotal        return None
1052d57dc69SVladimir Kotal
1062d57dc69SVladimir Kotal    return r.text
1072d57dc69SVladimir Kotal
1082d57dc69SVladimir Kotal
109*732be1c2SVladimir Kotaldef set_configuration(logger, configuration, uri, headers=None, timeout=None):
1102d57dc69SVladimir Kotal    try:
1112d57dc69SVladimir Kotal        do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration'),
112*732be1c2SVladimir Kotal                    data=configuration, headers=headers, timeout=timeout)
1132d57dc69SVladimir Kotal    except Exception:
1142d57dc69SVladimir Kotal        logger.error('could not set configuration for web application on {}'.
1152d57dc69SVladimir Kotal                     format(uri))
1162d57dc69SVladimir Kotal        return False
1172d57dc69SVladimir Kotal
1182d57dc69SVladimir Kotal    return True
1192d57dc69SVladimir Kotal
1202d57dc69SVladimir Kotal
121*732be1c2SVladimir Kotaldef list_projects(logger, uri, headers=None, timeout=None):
122b2d29daeSVladimir Kotal    try:
123b2d29daeSVladimir Kotal        r = do_api_call('GET',
12489229afdSVladimir Kotal                        get_uri(uri, 'api', 'v1', 'projects'),
125*732be1c2SVladimir Kotal                        headers=headers, timeout=timeout)
126b2d29daeSVladimir Kotal    except Exception:
127b2d29daeSVladimir Kotal        logger.error('could not list projects from web application '
128b2d29daeSVladimir Kotal                     'on {}'.format(uri))
129b2d29daeSVladimir Kotal        return None
130b2d29daeSVladimir Kotal
131b2d29daeSVladimir Kotal    return r.json()
132b2d29daeSVladimir Kotal
133b2d29daeSVladimir Kotal
134*732be1c2SVladimir Kotaldef list_indexed_projects(logger, uri, headers=None, timeout=None):
1352d57dc69SVladimir Kotal    try:
1362d57dc69SVladimir Kotal        r = do_api_call('GET',
13789229afdSVladimir Kotal                        get_uri(uri, 'api', 'v1', 'projects', 'indexed'),
138*732be1c2SVladimir Kotal                        headers=headers, timeout=timeout)
1392d57dc69SVladimir Kotal    except Exception:
1402d57dc69SVladimir Kotal        logger.error('could not list indexed projects from web application '
1412d57dc69SVladimir Kotal                     'on {}'.format(uri))
1422d57dc69SVladimir Kotal        return None
1432d57dc69SVladimir Kotal
1442d57dc69SVladimir Kotal    return r.json()
1452d57dc69SVladimir Kotal
1462d57dc69SVladimir Kotal
147*732be1c2SVladimir Kotaldef add_project(logger, project, uri, headers=None, timeout=None):
1482d57dc69SVladimir Kotal    try:
1492d57dc69SVladimir Kotal        do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'),
150*732be1c2SVladimir Kotal                    data=project, headers=headers, timeout=timeout)
1512d57dc69SVladimir Kotal    except Exception:
1522d57dc69SVladimir Kotal        logger.error('could not add project {} for web application on {}'.
1532d57dc69SVladimir Kotal                     format(project, uri))
1542d57dc69SVladimir Kotal        return False
1552d57dc69SVladimir Kotal
1562d57dc69SVladimir Kotal    return True
1572d57dc69SVladimir Kotal
1582d57dc69SVladimir Kotal
159*732be1c2SVladimir Kotaldef delete_project(logger, project, uri, headers=None, timeout=None):
1602d57dc69SVladimir Kotal    try:
1612d57dc69SVladimir Kotal        do_api_call('DELETE', get_uri(uri, 'api', 'v1', 'projects',
16289229afdSVladimir Kotal                                      urllib.parse.quote_plus(project)),
163*732be1c2SVladimir Kotal                    headers=headers, timeout=timeout)
1642d57dc69SVladimir Kotal    except Exception:
1652d57dc69SVladimir Kotal        logger.error('could not delete project {} in web application on {}'.
1662d57dc69SVladimir Kotal                     format(project, uri))
1672d57dc69SVladimir Kotal        return False
1682d57dc69SVladimir Kotal
1692d57dc69SVladimir Kotal    return True
170