Ejemplo n.º 1
0
def _create_client(server, credential, debug, api_type="products"):
    cfg = None
    if api_type in ('projectv2', 'artifact', 'repository', 'scan'):
        cfg = v2_swagger_client.Configuration()
    else:
        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

    proxies = getproxies()
    proxy = proxies.get('http', proxies.get('all', None))
    if proxy:
        cfg.proxy = proxy
    return {
        "chart":
        client.ChartRepositoryApi(client.ApiClient(cfg)),
        "products":
        swagger_client.ProductsApi(swagger_client.ApiClient(cfg)),
        "projectv2":
        v2_swagger_client.ProjectApi(v2_swagger_client.ApiClient(cfg)),
        "artifact":
        v2_swagger_client.ArtifactApi(v2_swagger_client.ApiClient(cfg)),
        "repository":
        v2_swagger_client.RepositoryApi(v2_swagger_client.ApiClient(cfg)),
        "scan":
        v2_swagger_client.ScanApi(v2_swagger_client.ApiClient(cfg)),
        "scanner":
        swagger_client.ScannersApi(swagger_client.ApiClient(cfg)),
    }.get(api_type, 'Error: Wrong API type')
Ejemplo n.º 2
0
def _create_client(server, credential, debug, api_type="products"):
    cfg = None
    if api_type in ('projectv2', 'artifact', 'repository', 'scanner', 'scan',
                    'scanall', 'preheat', 'quota', 'replication', 'robot',
                    'gc', 'retention'):
        cfg = v2_swagger_client.Configuration()
    else:
        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

    proxies = getproxies()
    proxy = proxies.get('http', proxies.get('all', None))
    if proxy:
        cfg.proxy = proxy

    if cfg.username is None and cfg.password is None:
        # returns {} for auth_settings for anonymous access
        import types
        cfg.auth_settings = types.MethodType(lambda self: {}, cfg)

    return {
        "chart":
        client.ChartRepositoryApi(client.ApiClient(cfg)),
        "products":
        swagger_client.ProductsApi(swagger_client.ApiClient(cfg)),
        "projectv2":
        v2_swagger_client.ProjectApi(v2_swagger_client.ApiClient(cfg)),
        "artifact":
        v2_swagger_client.ArtifactApi(v2_swagger_client.ApiClient(cfg)),
        "preheat":
        v2_swagger_client.PreheatApi(v2_swagger_client.ApiClient(cfg)),
        "quota":
        v2_swagger_client.QuotaApi(v2_swagger_client.ApiClient(cfg)),
        "repository":
        v2_swagger_client.RepositoryApi(v2_swagger_client.ApiClient(cfg)),
        "scan":
        v2_swagger_client.ScanApi(v2_swagger_client.ApiClient(cfg)),
        "scanall":
        v2_swagger_client.ScanAllApi(v2_swagger_client.ApiClient(cfg)),
        "scanner":
        v2_swagger_client.ScannerApi(v2_swagger_client.ApiClient(cfg)),
        "replication":
        v2_swagger_client.ReplicationApi(v2_swagger_client.ApiClient(cfg)),
        "robot":
        v2_swagger_client.RobotApi(v2_swagger_client.ApiClient(cfg)),
        "gc":
        v2_swagger_client.GcApi(v2_swagger_client.ApiClient(cfg)),
        "retention":
        v2_swagger_client.RetentionApi(v2_swagger_client.ApiClient(cfg)),
    }.get(api_type, 'Error: Wrong API type')
Ejemplo n.º 3
0
    def _wait(self):
        # Create an API client using the service's init URL (falling back to
        # the base URL when there is no init URL set)
        cconfig = client.Configuration()
        try:
            cconfig.host = self.config.init_url
        except AttributeError:
            cconfig.host = self.config.base_url
        cconfig.logger["urllib3_logger"].setLevel(logging.ERROR)

        init_client = client.ApiClient(cconfig)

        # If there is an init timeout, then wait that long for the service to
        # initialize, otherwise expect it to initialize immediately
        deadline = datetime.now()
        try:
            deadline += self.config.init_timeout
        except AttributeError:
            pass

        while True:
            # Expect a 2xx response from the service
            try:
                resp = init_client.call_api("", "GET")
                if resp[1] / 100 == 2:
                    return
            except Exception as e:
                err = e
            else:
                err = None

            # Give up once the deadline is reached
            if datetime.now() > deadline:
                if err:
                    raise Exception(
                        'timed out waiting for %s service to initialize: last error was %s'
                        % (self.config.name, err))
                else:
                    raise Exception(
                        'timed out waiting for %s service to initialize: last status from %s was %s'
                        % (self.config.name, cconfig.host, resp[1]))
Ejemplo n.º 4
0
def call_api(coll, method, obj_id=None, obj_body=None, get_params=None):
    data = None
    status = None
    headers = None
    success = True
    api_instance = getattr(client, '{}Api'.format(up_first(coll)))(
        client.ApiClient(configuration))
    if method == 'get':
        if obj_id is not None:
            params = get_params or {}
            data, status, headers = getattr(
                api_instance,
                'get_{}_with_http_info'.format(cc_to_snake(coll)))(obj_id,
                                                                   **params)
        else:
            data, status, headers = getattr(
                api_instance,
                'list_{}s_with_http_info'.format(cc_to_snake(coll)))()
    elif method == 'post':
        body = json.loads(obj_body)
        obj = getattr(client, '{}Body'.format(up_first(coll)))(**body)
        data, status, headers = getattr(
            api_instance,
            'create_{}_with_http_info'.format(cc_to_snake(coll)))(obj)
    elif method == 'put':
        body = json.loads(obj_body)
        obj = getattr(client, '{}Body'.format(up_first(coll)))(**body)
        data, status, headers = getattr(
            api_instance,
            'update_{}_with_http_info'.format(cc_to_snake(coll)))(obj_id, obj)

    if not str(status).startswith('20'):
        success = False
        print(f'Error {status}')

    return success, data, status, headers
Ejemplo n.º 5
0
 def client(self):
     # Create an API client using the service's base URL
     cconfig = client.Configuration()
     cconfig.host = self.config.base_url
     cconfig.debug = strtobool(os.environ.get('MAMBA_DEBUG', 'False'))
     return client.ApiClient(cconfig)