xref: /OpenGrok/tools/src/main/python/opengrok_tools/utils/opengrok.py (revision b2d29dae9a94943b44f7145a695b3511eb3ba2b1)
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#
21*b2d29daeSVladimir 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
292d57dc69SVladimir Kotaldef get_repos(logger, project, uri):
302d57dc69SVladimir Kotal    """
312d57dc69SVladimir Kotal    :param logger: logger instance
322d57dc69SVladimir Kotal    :param project: project name
332d57dc69SVladimir Kotal    :param uri: web application URI
342d57dc69SVladimir Kotal    :return: list of repository paths (can be empty if no match)
352d57dc69SVladimir Kotal             or None on failure
362d57dc69SVladimir Kotal    """
372d57dc69SVladimir Kotal
382d57dc69SVladimir Kotal    try:
392d57dc69SVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'projects',
402d57dc69SVladimir Kotal                                       urllib.parse.quote_plus(project),
412d57dc69SVladimir Kotal                                       'repositories'))
422d57dc69SVladimir Kotal    except Exception:
432d57dc69SVladimir Kotal        logger.error('could not get repositories for ' + project)
442d57dc69SVladimir Kotal        return None
452d57dc69SVladimir Kotal
462d57dc69SVladimir Kotal    ret = []
472d57dc69SVladimir Kotal    for line in r.json():
482d57dc69SVladimir Kotal        ret.append(line.strip())
492d57dc69SVladimir Kotal
502d57dc69SVladimir Kotal    return ret
512d57dc69SVladimir Kotal
522d57dc69SVladimir Kotal
532d57dc69SVladimir Kotaldef get_config_value(logger, name, uri):
542d57dc69SVladimir Kotal    """
552d57dc69SVladimir Kotal    Get list of repositories for given project name.
562d57dc69SVladimir Kotal
572d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
582d57dc69SVladimir Kotal    """
592d57dc69SVladimir Kotal    try:
602d57dc69SVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration',
612d57dc69SVladimir Kotal                                       urllib.parse.quote_plus(name)))
622d57dc69SVladimir Kotal    except Exception:
632d57dc69SVladimir Kotal        logger.error("Cannot get the '{}' config value from the web "
642d57dc69SVladimir Kotal                     "application on {}".format(name, uri))
652d57dc69SVladimir Kotal        return None
662d57dc69SVladimir Kotal
672d57dc69SVladimir Kotal    return r.text
682d57dc69SVladimir Kotal
692d57dc69SVladimir Kotal
702d57dc69SVladimir Kotaldef get_repo_type(logger, repository, uri):
712d57dc69SVladimir Kotal    """
722d57dc69SVladimir Kotal    Get repository type for given path relative to sourceRoot.
732d57dc69SVladimir Kotal
742d57dc69SVladimir Kotal    Return string with the result on success, None on failure.
752d57dc69SVladimir Kotal    """
762d57dc69SVladimir Kotal    payload = {'repository': repository}
772d57dc69SVladimir Kotal
782d57dc69SVladimir Kotal    try:
792d57dc69SVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'repositories',
802d57dc69SVladimir Kotal                                       'property', 'type'), params=payload)
812d57dc69SVladimir Kotal    except Exception:
822d57dc69SVladimir Kotal        logger.error('could not get repository type for {} from web'
832d57dc69SVladimir Kotal                     'application on {}'.format(repository, uri))
842d57dc69SVladimir Kotal        return None
852d57dc69SVladimir Kotal
862d57dc69SVladimir Kotal    line = r.text
872d57dc69SVladimir Kotal
882d57dc69SVladimir Kotal    idx = line.rfind(":")
892d57dc69SVladimir Kotal    return line[idx + 1:]
902d57dc69SVladimir Kotal
912d57dc69SVladimir Kotal
922d57dc69SVladimir Kotaldef get_configuration(logger, uri):
932d57dc69SVladimir Kotal    try:
942d57dc69SVladimir Kotal        r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration'))
952d57dc69SVladimir Kotal    except Exception:
962d57dc69SVladimir Kotal        logger.error('could not get configuration from web application on {}'.
972d57dc69SVladimir Kotal                     format(uri))
982d57dc69SVladimir Kotal        return None
992d57dc69SVladimir Kotal
1002d57dc69SVladimir Kotal    return r.text
1012d57dc69SVladimir Kotal
1022d57dc69SVladimir Kotal
1032d57dc69SVladimir Kotaldef set_configuration(logger, configuration, uri):
1042d57dc69SVladimir Kotal    try:
1052d57dc69SVladimir Kotal        do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration'),
1062d57dc69SVladimir Kotal                    data=configuration)
1072d57dc69SVladimir Kotal    except Exception:
1082d57dc69SVladimir Kotal        logger.error('could not set configuration for web application on {}'.
1092d57dc69SVladimir Kotal                     format(uri))
1102d57dc69SVladimir Kotal        return False
1112d57dc69SVladimir Kotal
1122d57dc69SVladimir Kotal    return True
1132d57dc69SVladimir Kotal
1142d57dc69SVladimir Kotal
115*b2d29daeSVladimir Kotaldef list_projects(logger, uri):
116*b2d29daeSVladimir Kotal    try:
117*b2d29daeSVladimir Kotal        r = do_api_call('GET',
118*b2d29daeSVladimir Kotal                        get_uri(uri, 'api', 'v1', 'projects'))
119*b2d29daeSVladimir Kotal    except Exception:
120*b2d29daeSVladimir Kotal        logger.error('could not list projects from web application '
121*b2d29daeSVladimir Kotal                     'on {}'.format(uri))
122*b2d29daeSVladimir Kotal        return None
123*b2d29daeSVladimir Kotal
124*b2d29daeSVladimir Kotal    return r.json()
125*b2d29daeSVladimir Kotal
126*b2d29daeSVladimir Kotal
1272d57dc69SVladimir Kotaldef list_indexed_projects(logger, uri):
1282d57dc69SVladimir Kotal    try:
1292d57dc69SVladimir Kotal        r = do_api_call('GET',
1302d57dc69SVladimir Kotal                        get_uri(uri, 'api', 'v1', 'projects', 'indexed'))
1312d57dc69SVladimir Kotal    except Exception:
1322d57dc69SVladimir Kotal        logger.error('could not list indexed projects from web application '
1332d57dc69SVladimir Kotal                     'on {}'.format(uri))
1342d57dc69SVladimir Kotal        return None
1352d57dc69SVladimir Kotal
1362d57dc69SVladimir Kotal    return r.json()
1372d57dc69SVladimir Kotal
1382d57dc69SVladimir Kotal
1392d57dc69SVladimir Kotaldef add_project(logger, project, uri):
1402d57dc69SVladimir Kotal    try:
1412d57dc69SVladimir Kotal        do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'),
1422d57dc69SVladimir Kotal                    data=project)
1432d57dc69SVladimir Kotal    except Exception:
1442d57dc69SVladimir Kotal        logger.error('could not add project {} for web application on {}'.
1452d57dc69SVladimir Kotal                     format(project, uri))
1462d57dc69SVladimir Kotal        return False
1472d57dc69SVladimir Kotal
1482d57dc69SVladimir Kotal    return True
1492d57dc69SVladimir Kotal
1502d57dc69SVladimir Kotal
1512d57dc69SVladimir Kotaldef delete_project(logger, project, uri):
1522d57dc69SVladimir Kotal    try:
1532d57dc69SVladimir Kotal        do_api_call('DELETE', get_uri(uri, 'api', 'v1', 'projects',
1542d57dc69SVladimir Kotal                                      urllib.parse.quote_plus(project)))
1552d57dc69SVladimir Kotal    except Exception:
1562d57dc69SVladimir Kotal        logger.error('could not delete project {} in web application on {}'.
1572d57dc69SVladimir Kotal                     format(project, uri))
1582d57dc69SVladimir Kotal        return False
1592d57dc69SVladimir Kotal
1602d57dc69SVladimir Kotal    return True
161