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 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: 412d57dc69SVladimir Kotal r = 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) 45*a71dcf5fSVladimir Kotal except Exception as e: 46*a71dcf5fSVladimir Kotal logger.error("could not get repositories for project '{}': {}". 47*a71dcf5fSVladimir Kotal format(project, e)) 482d57dc69SVladimir Kotal return None 492d57dc69SVladimir Kotal 502d57dc69SVladimir Kotal ret = [] 512d57dc69SVladimir Kotal for line in r.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: 642d57dc69SVladimir Kotal r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration', 6589229afdSVladimir Kotal urllib.parse.quote_plus(name)), 66732be1c2SVladimir Kotal headers=headers, timeout=timeout) 67*a71dcf5fSVladimir Kotal except Exception as e: 682d57dc69SVladimir Kotal logger.error("Cannot get the '{}' config value from the web " 69*a71dcf5fSVladimir Kotal "application: {}".format(name, e)) 702d57dc69SVladimir Kotal return None 712d57dc69SVladimir Kotal 722d57dc69SVladimir Kotal return r.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) 93*a71dcf5fSVladimir Kotal except Exception as e: 941822b591SVladimir Kotal logger.error("Cannot set the '{}' config field to '{}' from the web " 95*a71dcf5fSVladimir Kotal "application: {}".format(name, value, e)) 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: 1102d57dc69SVladimir Kotal r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'repositories', 11189229afdSVladimir Kotal 'property', 'type'), params=payload, 112732be1c2SVladimir Kotal headers=headers, timeout=None) 113*a71dcf5fSVladimir Kotal except Exception as e: 114*a71dcf5fSVladimir Kotal logger.error("could not get repository type for '{}' from web" 115*a71dcf5fSVladimir Kotal "application: {}".format(repository, e)) 1162d57dc69SVladimir Kotal return None 1172d57dc69SVladimir Kotal 1182d57dc69SVladimir Kotal line = r.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: 12689229afdSVladimir Kotal r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration'), 127732be1c2SVladimir Kotal headers=headers, timeout=timeout) 128*a71dcf5fSVladimir Kotal except Exception as e: 129*a71dcf5fSVladimir Kotal logger.error('could not get configuration from web application: {}'. 130*a71dcf5fSVladimir Kotal format(e)) 1312d57dc69SVladimir Kotal return None 1322d57dc69SVladimir Kotal 1332d57dc69SVladimir Kotal return r.text 1342d57dc69SVladimir Kotal 1352d57dc69SVladimir Kotal 136732be1c2SVladimir Kotaldef set_configuration(logger, configuration, uri, headers=None, timeout=None): 1372d57dc69SVladimir Kotal try: 1382d57dc69SVladimir Kotal do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration'), 139732be1c2SVladimir Kotal data=configuration, headers=headers, timeout=timeout) 140*a71dcf5fSVladimir Kotal except Exception as e: 141*a71dcf5fSVladimir Kotal logger.error('could not set configuration to web application: {}'. 142*a71dcf5fSVladimir Kotal format(e)) 1432d57dc69SVladimir Kotal return False 1442d57dc69SVladimir Kotal 1452d57dc69SVladimir Kotal return True 1462d57dc69SVladimir Kotal 1472d57dc69SVladimir Kotal 148732be1c2SVladimir Kotaldef list_projects(logger, uri, headers=None, timeout=None): 149b2d29daeSVladimir Kotal try: 150b2d29daeSVladimir Kotal r = do_api_call('GET', 15189229afdSVladimir Kotal get_uri(uri, 'api', 'v1', 'projects'), 152732be1c2SVladimir Kotal headers=headers, timeout=timeout) 153*a71dcf5fSVladimir Kotal except Exception as e: 154*a71dcf5fSVladimir Kotal logger.error("could not list projects from web application: {}". 155*a71dcf5fSVladimir Kotal format(e)) 156b2d29daeSVladimir Kotal return None 157b2d29daeSVladimir Kotal 158b2d29daeSVladimir Kotal return r.json() 159b2d29daeSVladimir Kotal 160b2d29daeSVladimir Kotal 161732be1c2SVladimir Kotaldef list_indexed_projects(logger, uri, headers=None, timeout=None): 1622d57dc69SVladimir Kotal try: 1632d57dc69SVladimir Kotal r = do_api_call('GET', 16489229afdSVladimir Kotal get_uri(uri, 'api', 'v1', 'projects', 'indexed'), 165732be1c2SVladimir Kotal headers=headers, timeout=timeout) 166*a71dcf5fSVladimir Kotal except Exception as e: 167*a71dcf5fSVladimir Kotal logger.error("could not list indexed projects from web application: {}". 168*a71dcf5fSVladimir Kotal format(e)) 1692d57dc69SVladimir Kotal return None 1702d57dc69SVladimir Kotal 1712d57dc69SVladimir Kotal return r.json() 1722d57dc69SVladimir Kotal 1732d57dc69SVladimir Kotal 174732be1c2SVladimir Kotaldef add_project(logger, project, uri, headers=None, timeout=None): 1752d57dc69SVladimir Kotal try: 1762d57dc69SVladimir Kotal do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'), 177732be1c2SVladimir Kotal data=project, headers=headers, timeout=timeout) 178*a71dcf5fSVladimir Kotal except Exception as e: 179*a71dcf5fSVladimir Kotal logger.error("could not add project '{}' for web application on {}: {}". 180*a71dcf5fSVladimir Kotal format(project, uri, e)) 1812d57dc69SVladimir Kotal return False 1822d57dc69SVladimir Kotal 1832d57dc69SVladimir Kotal return True 1842d57dc69SVladimir Kotal 1852d57dc69SVladimir Kotal 186732be1c2SVladimir Kotaldef delete_project(logger, project, uri, headers=None, timeout=None): 1872d57dc69SVladimir Kotal try: 1882d57dc69SVladimir Kotal do_api_call('DELETE', get_uri(uri, 'api', 'v1', 'projects', 18989229afdSVladimir Kotal urllib.parse.quote_plus(project)), 190732be1c2SVladimir Kotal headers=headers, timeout=timeout) 191*a71dcf5fSVladimir Kotal except Exception as e: 192*a71dcf5fSVladimir Kotal logger.error("could not delete project '{}' in web application on {}: {}". 193*a71dcf5fSVladimir Kotal format(project, uri, e)) 1942d57dc69SVladimir Kotal return False 1952d57dc69SVladimir Kotal 1962d57dc69SVladimir Kotal return True 197