def is_def_supported_by_cse_server():
    """Return true if CSE server is qualified to invoke Defined Entity API.

    :rtype: bool
    """
    import container_service_extension.utils as utils
    api_version = utils.get_server_api_version()
    return float(api_version) >= DEF_API_MIN_VERSION
示例#2
0
def is_v35_supported_by_cse_server():
    """Return true if CSE server is qualified to invoke Defined Entity API.

    DEF API is introduced in vCD API version 35.0

    :rtype: bool
    """
    import container_service_extension.utils as utils
    api_version = utils.get_server_api_version()
    return float(api_version) >= float(vCDApiVersion.VERSION_35.value)
示例#3
0
 def info(self, get_sysadmin_info=False):
     result = utils.get_cse_info()
     result[CSE_SERVER_API_VERSION] = utils.get_server_api_version()
     if get_sysadmin_info:
         result['consumer_threads'] = len(self.threads)
         result['all_threads'] = threading.activeCount()
         result['requests_in_progress'] = self.active_requests_count()
         result['config_file'] = self.config_file
         result['status'] = self.get_status()
     else:
         del result['python']
     return result
示例#4
0
 def info(self, get_sysadmin_info=False):
     result = utils.get_cse_info()
     result[shared_constants.CSE_SERVER_API_VERSION] = utils.get_server_api_version()  # noqa: E501
     if get_sysadmin_info:
         result['all_consumer_threads'] = 0 if self.consumer is None else \
             self.consumer.get_num_total_threads()
         result['all_threads'] = threading.activeCount()
         result['requests_in_progress'] = self.active_requests_count()
         result['config_file'] = self.config_file
         result['status'] = self.get_status()
     else:
         del result['python']
     return result
示例#5
0
def process_request(message):
    from container_service_extension.service import Service
    LOGGER.debug(f"Incoming request message: {json.dumps(message)}")

    api_version_header = _parse_accept_header(
        accept_header=message['headers'].get('Accept'))
    api_version = _get_api_version_from_accept_header(
        api_version_header=api_version_header)

    url_data = _get_url_data(method=message['method'],
                             url=message['requestUri'],
                             api_version=api_version)  # noqa: E501
    operation = url_data[_OPERATION_KEY]

    # Check api version and if server is disabled or not
    # /system operations are excluded from these checks
    if operation not in (CseOperation.SYSTEM_INFO, CseOperation.SYSTEM_UPDATE):
        if not Service().is_running():
            raise cse_exception.BadRequestError(
                error_message='CSE service is disabled. '
                'Contact the System Administrator.')
        else:
            server_api_version = utils.get_server_api_version()
            if api_version != server_api_version:
                raise cse_exception.NotAcceptableRequestError(
                    error_message="Invalid api version specified. Expected "
                    f"api version '{server_api_version}'.")

    # create request data dict from incoming message data
    request_data = {}
    is_cse_3_0_request = _is_cse_3_0_endpoint(message['requestUri'])
    if len(message['body']) > 0:
        raw_body = base64.b64decode(message['body']).decode(
            sys.getfilesystemencoding())  # noqa: E501
        request_body = json.loads(raw_body)
        if is_cse_3_0_request:
            request_data[shared_constants.RequestKey.V35_SPEC] = request_body
        else:
            request_data.update(request_body)
        LOGGER.debug(f"request body: {request_data}")
    # update request data dict with query params data
    if message['queryString']:
        query_params = dict(parse_qsl(message['queryString']))
        if is_cse_3_0_request:
            request_data[shared_constants.RequestKey.V35_QUERY] = query_params
        else:
            request_data.update(query_params)
        LOGGER.debug(f"query parameters: {query_params}")
    # update request spec with operation specific data in the url
    request_data.update(url_data)
    # remove None values from request payload
    data = {k: v for k, v in request_data.items() if v is not None}
    # extract out the authorization token
    tenant_auth_token = message['headers'].get('x-vcloud-authorization')
    is_jwt_token = False
    auth_header = message['headers'].get('Authorization')
    if auth_header:
        tokens = auth_header.split(" ")
        if len(tokens) == 2 and tokens[0].lower() == 'bearer':
            tenant_auth_token = tokens[1]
            is_jwt_token = True

    # create operation context
    operation_ctx = ctx.OperationContext(tenant_auth_token,
                                         is_jwt=is_jwt_token,
                                         request_id=message['id'])

    try:
        body_content = OPERATION_TO_HANDLER[operation](data, operation_ctx)
    finally:
        if not operation_ctx.is_async:
            operation_ctx.end()

    if not isinstance(body_content, (list, dict)):
        body_content = \
            {shared_constants.RESPONSE_MESSAGE_KEY: str(body_content)}
    response = {
        'status_code': operation.ideal_response_code,
        'body': body_content,
    }
    LOGGER.debug(f"Outgoing response: {str(response)}")
    return response