2018-08-16 12:36:27 +02:00
# -*- coding: utf-8 -*-
import sys
import time
2020-03-01 10:57:40 +01:00
import subprocess
2020-04-02 09:30:09 +02:00
import client
2018-08-16 12:36:27 +02:00
import swagger_client
2020-02-14 09:11:38 +01:00
import v2_swagger_client
2019-10-17 06:00:51 +02:00
try :
from urllib import getproxies
except ImportError :
from urllib . request import getproxies
2018-08-16 12:36:27 +02:00
class Server :
def __init__ ( self , endpoint , verify_ssl ) :
self . endpoint = endpoint
self . verify_ssl = verify_ssl
class Credential :
def __init__ ( self , type , username , password ) :
self . type = type
self . username = username
self . password = password
2020-02-14 09:11:38 +01:00
def _create_client ( server , credential , debug , api_type = " products " ) :
2018-08-16 12:36:27 +02:00
cfg = swagger_client . Configuration ( )
cfg . host = server . endpoint
cfg . verify_ssl = server . verify_ssl
# support basic auth only for now
cfg . username = credential . username
cfg . password = credential . password
cfg . debug = debug
2019-10-17 06:00:51 +02:00
proxies = getproxies ( )
proxy = proxies . get ( ' http ' , proxies . get ( ' all ' , None ) )
if proxy :
cfg . proxy = proxy
2020-02-14 09:11:38 +01:00
return {
2020-04-02 09:30:09 +02:00
" chart " : client . ChartRepositoryApi ( client . ApiClient ( cfg ) ) ,
2020-02-21 10:08:27 +01:00
" products " : swagger_client . ProductsApi ( swagger_client . ApiClient ( cfg ) ) ,
2020-03-19 07:56:37 +01:00
" projectv2 " : v2_swagger_client . ProjectApi ( v2_swagger_client . ApiClient ( cfg ) ) ,
2020-02-21 10:08:27 +01:00
" artifact " : v2_swagger_client . ArtifactApi ( v2_swagger_client . ApiClient ( cfg ) ) ,
" repository " : v2_swagger_client . RepositoryApi ( v2_swagger_client . ApiClient ( cfg ) ) ,
2020-03-10 07:55:55 +01:00
" scan " : v2_swagger_client . ScanApi ( v2_swagger_client . ApiClient ( cfg ) ) ,
2020-03-16 03:13:28 +01:00
" scanner " : swagger_client . ScannersApi ( swagger_client . ApiClient ( cfg ) ) ,
2020-02-14 09:11:38 +01:00
} . get ( api_type , ' Error: Wrong API type ' )
2018-08-16 12:36:27 +02:00
2018-10-31 06:13:31 +01:00
def _assert_status_code ( expect_code , return_code ) :
if str ( return_code ) != str ( expect_code ) :
2018-11-29 11:27:53 +01:00
raise Exception ( r " HTTPS status code s not as we expected. Expected {} , while actual HTTPS status code is {} . " . format ( expect_code , return_code ) )
def _assert_status_body ( expect_status_body , returned_status_body ) :
if expect_status_body . strip ( ) != returned_status_body . strip ( ) :
raise Exception ( r " HTTPS status body s not as we expected. Expected {} , while actual HTTPS status body is {} . " . format ( expect_status_body , returned_status_body ) )
2018-10-31 06:13:31 +01:00
2018-08-16 12:36:27 +02:00
def _random_name ( prefix ) :
2018-09-19 11:49:43 +02:00
return " %s - %d " % ( prefix , int ( round ( time . time ( ) * 1000 ) ) )
2018-08-16 12:36:27 +02:00
def _get_id_from_header ( header ) :
2018-11-01 11:26:04 +01:00
try :
location = header [ " Location " ]
2019-09-03 09:52:41 +02:00
return int ( location . split ( " / " ) [ - 1 ] )
2018-11-01 11:26:04 +01:00
except Exception :
return None
2018-08-16 12:36:27 +02:00
2018-10-31 06:13:31 +01:00
def _get_string_from_unicode ( udata ) :
result = ' '
for u in udata :
tmp = u . encode ( ' utf8 ' )
result = result + tmp . strip ( ' \n \r \t ' )
return result
2020-03-01 10:57:40 +01:00
def run_command ( command ) :
print " Command: " , subprocess . list2cmdline ( command )
try :
output = subprocess . check_output ( command ,
stderr = subprocess . STDOUT ,
universal_newlines = True )
except subprocess . CalledProcessError as e :
raise Exception ( ' Error: Exited with error code: %s . Output: %s ' % ( e . returncode , e . output ) )
return output
2018-08-16 12:36:27 +02:00
class Base :
2019-08-01 09:29:42 +02:00
def __init__ ( self ,
2018-08-16 12:36:27 +02:00
server = Server ( endpoint = " http://localhost:8080/api " , verify_ssl = False ) ,
credential = Credential ( type = " basic_auth " , username = " admin " , password = " Harbor12345 " ) ,
2020-02-14 09:11:38 +01:00
debug = True , api_type = " products " ) :
2018-08-16 12:36:27 +02:00
if not isinstance ( server . verify_ssl , bool ) :
server . verify_ssl = server . verify_ssl == " True "
self . server = server
self . credential = credential
self . debug = debug
2020-02-14 09:11:38 +01:00
self . api_type = api_type
self . client = _create_client ( server , credential , debug , api_type = api_type )
2018-08-16 12:36:27 +02:00
def _get_client ( self , * * kwargs ) :
if len ( kwargs ) == 0 :
return self . client
server = self . server
2020-02-14 09:11:38 +01:00
if " api_type " in kwargs :
server . api_type = kwargs . get ( " api_type " )
2018-08-16 12:36:27 +02:00
if " endpoint " in kwargs :
server . endpoint = kwargs . get ( " endpoint " )
if " verify_ssl " in kwargs :
if not isinstance ( kwargs . get ( " verify_ssl " ) , bool ) :
server . verify_ssl = kwargs . get ( " verify_ssl " ) == " True "
else :
server . verify_ssl = kwargs . get ( " verify_ssl " )
credential = self . credential
if " type " in kwargs :
credential . type = kwargs . get ( " type " )
if " username " in kwargs :
credential . username = kwargs . get ( " username " )
if " password " in kwargs :
credential . password = kwargs . get ( " password " )
2020-02-21 10:08:27 +01:00
return _create_client ( server , credential , self . debug , self . api_type )