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*89229afdSVladimir Kotaldef get_repos(logger, project, uri, headers=None): 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), 41*89229afdSVladimir Kotal 'repositories'), 42*89229afdSVladimir Kotal headers=headers) 432d57dc69SVladimir Kotal except Exception: 442d57dc69SVladimir Kotal logger.error('could not get repositories for ' + project) 452d57dc69SVladimir Kotal return None 462d57dc69SVladimir Kotal 472d57dc69SVladimir Kotal ret = [] 482d57dc69SVladimir Kotal for line in r.json(): 492d57dc69SVladimir Kotal ret.append(line.strip()) 502d57dc69SVladimir Kotal 512d57dc69SVladimir Kotal return ret 522d57dc69SVladimir Kotal 532d57dc69SVladimir Kotal 54*89229afdSVladimir Kotaldef get_config_value(logger, name, uri, headers=None): 552d57dc69SVladimir Kotal """ 562d57dc69SVladimir Kotal Get list of repositories for given project name. 572d57dc69SVladimir Kotal 582d57dc69SVladimir Kotal Return string with the result on success, None on failure. 592d57dc69SVladimir Kotal """ 602d57dc69SVladimir Kotal try: 612d57dc69SVladimir Kotal r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration', 62*89229afdSVladimir Kotal urllib.parse.quote_plus(name)), 63*89229afdSVladimir Kotal headers=headers) 642d57dc69SVladimir Kotal except Exception: 652d57dc69SVladimir Kotal logger.error("Cannot get the '{}' config value from the web " 662d57dc69SVladimir Kotal "application on {}".format(name, uri)) 672d57dc69SVladimir Kotal return None 682d57dc69SVladimir Kotal 692d57dc69SVladimir Kotal return r.text 702d57dc69SVladimir Kotal 712d57dc69SVladimir Kotal 72*89229afdSVladimir Kotaldef get_repo_type(logger, repository, uri, headers=None): 732d57dc69SVladimir Kotal """ 742d57dc69SVladimir Kotal Get repository type for given path relative to sourceRoot. 752d57dc69SVladimir Kotal 762d57dc69SVladimir Kotal Return string with the result on success, None on failure. 772d57dc69SVladimir Kotal """ 782d57dc69SVladimir Kotal payload = {'repository': repository} 792d57dc69SVladimir Kotal 802d57dc69SVladimir Kotal try: 812d57dc69SVladimir Kotal r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'repositories', 82*89229afdSVladimir Kotal 'property', 'type'), params=payload, 83*89229afdSVladimir Kotal headers=headers) 842d57dc69SVladimir Kotal except Exception: 852d57dc69SVladimir Kotal logger.error('could not get repository type for {} from web' 862d57dc69SVladimir Kotal 'application on {}'.format(repository, uri)) 872d57dc69SVladimir Kotal return None 882d57dc69SVladimir Kotal 892d57dc69SVladimir Kotal line = r.text 902d57dc69SVladimir Kotal 912d57dc69SVladimir Kotal idx = line.rfind(":") 922d57dc69SVladimir Kotal return line[idx + 1:] 932d57dc69SVladimir Kotal 942d57dc69SVladimir Kotal 95*89229afdSVladimir Kotaldef get_configuration(logger, uri, headers=None): 962d57dc69SVladimir Kotal try: 97*89229afdSVladimir Kotal r = do_api_call('GET', get_uri(uri, 'api', 'v1', 'configuration'), 98*89229afdSVladimir Kotal headers=headers) 992d57dc69SVladimir Kotal except Exception: 1002d57dc69SVladimir Kotal logger.error('could not get configuration from web application on {}'. 1012d57dc69SVladimir Kotal format(uri)) 1022d57dc69SVladimir Kotal return None 1032d57dc69SVladimir Kotal 1042d57dc69SVladimir Kotal return r.text 1052d57dc69SVladimir Kotal 1062d57dc69SVladimir Kotal 107*89229afdSVladimir Kotaldef set_configuration(logger, configuration, uri, headers=None): 1082d57dc69SVladimir Kotal try: 1092d57dc69SVladimir Kotal do_api_call('PUT', get_uri(uri, 'api', 'v1', 'configuration'), 110*89229afdSVladimir Kotal data=configuration, headers=headers) 1112d57dc69SVladimir Kotal except Exception: 1122d57dc69SVladimir Kotal logger.error('could not set configuration for web application on {}'. 1132d57dc69SVladimir Kotal format(uri)) 1142d57dc69SVladimir Kotal return False 1152d57dc69SVladimir Kotal 1162d57dc69SVladimir Kotal return True 1172d57dc69SVladimir Kotal 1182d57dc69SVladimir Kotal 119*89229afdSVladimir Kotaldef list_projects(logger, uri, headers=None): 120b2d29daeSVladimir Kotal try: 121b2d29daeSVladimir Kotal r = do_api_call('GET', 122*89229afdSVladimir Kotal get_uri(uri, 'api', 'v1', 'projects'), 123*89229afdSVladimir Kotal headers=headers) 124b2d29daeSVladimir Kotal except Exception: 125b2d29daeSVladimir Kotal logger.error('could not list projects from web application ' 126b2d29daeSVladimir Kotal 'on {}'.format(uri)) 127b2d29daeSVladimir Kotal return None 128b2d29daeSVladimir Kotal 129b2d29daeSVladimir Kotal return r.json() 130b2d29daeSVladimir Kotal 131b2d29daeSVladimir Kotal 132*89229afdSVladimir Kotaldef list_indexed_projects(logger, uri, headers=None): 1332d57dc69SVladimir Kotal try: 1342d57dc69SVladimir Kotal r = do_api_call('GET', 135*89229afdSVladimir Kotal get_uri(uri, 'api', 'v1', 'projects', 'indexed'), 136*89229afdSVladimir Kotal headers=headers) 1372d57dc69SVladimir Kotal except Exception: 1382d57dc69SVladimir Kotal logger.error('could not list indexed projects from web application ' 1392d57dc69SVladimir Kotal 'on {}'.format(uri)) 1402d57dc69SVladimir Kotal return None 1412d57dc69SVladimir Kotal 1422d57dc69SVladimir Kotal return r.json() 1432d57dc69SVladimir Kotal 1442d57dc69SVladimir Kotal 145*89229afdSVladimir Kotaldef add_project(logger, project, uri, headers=None): 1462d57dc69SVladimir Kotal try: 1472d57dc69SVladimir Kotal do_api_call('POST', get_uri(uri, 'api', 'v1', 'projects'), 148*89229afdSVladimir Kotal data=project, headers=headers) 1492d57dc69SVladimir Kotal except Exception: 1502d57dc69SVladimir Kotal logger.error('could not add project {} for web application on {}'. 1512d57dc69SVladimir Kotal format(project, uri)) 1522d57dc69SVladimir Kotal return False 1532d57dc69SVladimir Kotal 1542d57dc69SVladimir Kotal return True 1552d57dc69SVladimir Kotal 1562d57dc69SVladimir Kotal 157*89229afdSVladimir Kotaldef delete_project(logger, project, uri, headers=None): 1582d57dc69SVladimir Kotal try: 1592d57dc69SVladimir Kotal do_api_call('DELETE', get_uri(uri, 'api', 'v1', 'projects', 160*89229afdSVladimir Kotal urllib.parse.quote_plus(project)), 161*89229afdSVladimir Kotal headers=headers) 1622d57dc69SVladimir Kotal except Exception: 1632d57dc69SVladimir Kotal logger.error('could not delete project {} in web application on {}'. 1642d57dc69SVladimir Kotal format(project, uri)) 1652d57dc69SVladimir Kotal return False 1662d57dc69SVladimir Kotal 1672d57dc69SVladimir Kotal return True 168