Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update connexion to v3.0.6 #1776

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ verify_ssl = true
name = "pypi"

[packages]
flask = "==2.2.5"
# flask = "==2.2.5"
flask = "==3.0.3"
jsonify = "==0.5" # Not semver
flask-restful = "~=0.3.7"
requests = "~=2.31.0"
flask-sqlalchemy = "~=2.5.1"
flask-sqlalchemy = "~=3.1.1"
psycopg2 = "~=2.9.9"
flask-migrate = "~=4.0.5"
itsdangerous = "==2.1.2"
gunicorn = "~=22.0.0"
connexion = {version = "~=2.14.2",extras = ["swagger-ui", "flask"]}
connexion = {version = "~=3.0.6",extras = ["swagger-ui", "flask", "uvicorn"]}
markupsafe = "==2.1.5"
openapi-schema-validator = "==0.6.2"
openapi-spec-validator = "==0.7.1"
Expand Down
1,107 changes: 772 additions & 335 deletions Pipfile.lock

Large diffs are not rendered by default.

31 changes: 1 addition & 30 deletions api/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,4 @@ def init_cache(app_config, flask_app):
else:
logger.info(f"Cache using config={CACHE_CONFIG}")
if isinstance(flask_app, Flask):
logger.info("Cache initialized with app.")
CACHE = CACHE.init_app(flask_app, config=CACHE_CONFIG)
else:
logger.info(f"Cache not initialized with app. Passed the following for the app={flask_app}.")


def _delete_keys_simple(prefix):
cache_dict = CACHE.cache._cache
for cache_key in list(cache_dict.keys()):
if cache_key.startswith(f"flask_cache_{prefix}"):
cache_dict.pop(cache_key)


def _delete_keys_redis(prefix):
global CACHE_CONFIG
try:
redis_client = Redis(host=CACHE_CONFIG.get("CACHE_REDIS_HOST"), port=CACHE_CONFIG.get("CACHE_REDIS_PORT"))
# Use SCAN to find keys to delete that start with the prefix; default prefix is flask_cache_
for key in redis_client.scan_iter(f"flask_cache_{prefix}*"):
redis_client.delete(key)
except Exception as exec:
logger.exception("Cache deletion failed", exc_info=exec)


def delete_keys(prefix):
if CACHE and CACHE.config["CACHE_TYPE"] == "SimpleCache":
_delete_keys_simple(prefix)

if CACHE and CACHE.config["CACHE_TYPE"] == "RedisCache":
_delete_keys_redis(prefix)
CACHE = CACHE.init_app(flask_app, config=cache_config)
4 changes: 3 additions & 1 deletion api/filtering/db_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from uuid import UUID

from dateutil import parser
from flask_sqlalchemy.query import Query
from sqlalchemy import and_
from sqlalchemy import DateTime
from sqlalchemy import func
Expand Down Expand Up @@ -282,7 +283,8 @@ def query_filters(
filter: dict = None,
rbac_filter: dict = None,
order_by: str = None,
) -> Tuple[List, bool]:
# ) -> Tuple[List, bool]:
) -> Tuple[List, Query]:
num_ids = 0
host_type_filter = set(HOST_TYPES)
for id_param in [fqdn, display_name, hostname_or_id, insights_id]:
Expand Down
37 changes: 23 additions & 14 deletions api/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
import flask
from confluent_kafka.error import KafkaError
from flask import current_app
from httpx import InvalidURL
from marshmallow import ValidationError

from api import api_operation
from api import build_collection_response
from api import flask_json_response
from api import metrics
from api.cache import CACHE
from api.cache import delete_keys
from api.cache_key import make_key
from api.host_query import build_paginated_host_list_response
from api.host_query import staleness_timestamps
from api.host_query_db import get_all_hosts
Expand All @@ -33,6 +31,8 @@
from app.auth import get_current_identity
from app.auth.identity import to_auth_header
from app.common import inventory_config
from app.exceptions import InventoryException
from app.exceptions import ValidationException
from app.instrumentation import get_control_rule
from app.instrumentation import log_get_host_list_failed
from app.instrumentation import log_get_host_list_succeeded
Expand Down Expand Up @@ -72,7 +72,6 @@


@api_operation
@CACHE.cached(key_prefix=make_key)
@rbac(RbacResourceType.HOSTS, RbacPermission.READ)
@metrics.api_request_time.time()
def get_host_list(
Expand Down Expand Up @@ -103,6 +102,7 @@ def get_host_list(

try:
if get_flag_value(FLAG_INVENTORY_DISABLE_XJOIN, context={"schema": current_identity.org_id}) or is_bootc:
# if True:
logger.info(f"{FLAG_INVENTORY_DISABLE_XJOIN} is applied to {current_identity.org_id}")
host_list, total, additional_fields, system_profile_fields = get_host_list_postgres(
display_name,
Expand Down Expand Up @@ -147,7 +147,7 @@ def get_host_list(
fields,
rbac_filter,
)
except ValueError as e:
except (InvalidURL, ValidationException, ValueError) as e:
log_get_host_list_failed(logger)
flask.abort(400, str(e))

Expand Down Expand Up @@ -200,6 +200,7 @@ def delete_hosts_by_filter(
current_identity = get_current_identity()
is_bootc = filter.get("system_profile", {}).get("bootc_status")
if get_flag_value(FLAG_INVENTORY_DISABLE_XJOIN, context={"schema": current_identity.org_id}) or is_bootc:
# if True:
logger.info(f"{FLAG_INVENTORY_DISABLE_XJOIN} is applied to {current_identity.org_id}")
ids_list = get_host_ids_list_postgres(
display_name,
Expand Down Expand Up @@ -285,7 +286,6 @@ def _delete_host_list(host_id_list, rbac_filter):
) as payload_tracker_processing_ctx:
payload_tracker_processing_ctx.inventory_id = host_id

delete_keys(current_identity.org_id)
return deletion_count


Expand Down Expand Up @@ -337,6 +337,7 @@ def get_host_by_id(host_id_list, page=1, per_page=100, order_by=None, order_how=
current_identity = get_current_identity()
try:
if get_flag_value(FLAG_INVENTORY_DISABLE_XJOIN, context={"schema": current_identity.org_id}):
# if True:
logger.info(f"{FLAG_INVENTORY_DISABLE_XJOIN} is applied to {current_identity.org_id}")
host_list, total, additional_fields, system_profile_fields = get_host_list_by_id_list_postgres(
host_id_list, page, per_page, order_by, order_how, fields, rbac_filter
Expand All @@ -358,7 +359,6 @@ def get_host_by_id(host_id_list, page=1, per_page=100, order_by=None, order_how=


@api_operation
@CACHE.cached(key_prefix=make_key)
@rbac(RbacResourceType.HOSTS, RbacPermission.READ)
@metrics.api_request_time.time()
def get_host_system_profile_by_id(
Expand All @@ -367,6 +367,7 @@ def get_host_system_profile_by_id(
current_identity = get_current_identity()
try:
if get_flag_value(FLAG_INVENTORY_DISABLE_XJOIN, context={"schema": current_identity.org_id}):
# if True:
logger.info(f"{FLAG_INVENTORY_DISABLE_XJOIN} is applied to {current_identity.org_id}")
total, host_list = get_sparse_system_profile_postgres(
host_id_list, page, per_page, order_by, order_how, fields, rbac_filter
Expand All @@ -375,7 +376,7 @@ def get_host_system_profile_by_id(
total, host_list = get_sparse_system_profile(
host_id_list, page, per_page, order_by, order_how, fields, rbac_filter
)
except ValueError as e:
except (ValidationException, ValueError, Exception) as e:
log_get_host_list_failed(logger)
flask.abort(400, str(e))

Expand Down Expand Up @@ -413,8 +414,20 @@ def patch_host_by_id(host_id_list, body, rbac_filter=None):
log_patch_host_failed(logger, host_id_list)
return flask.abort(HTTPStatus.NOT_FOUND, "Requested host not found.")

current_identity = get_current_identity()
staleness = get_staleness_obj(current_identity)
identity = get_current_identity()
staleness = get_staleness_obj(identity)

try:
for host in hosts_to_update:
host.patch(validated_patch_host_data)

if db.session.is_modified(host):
db.session.commit()
serialized_host = serialize_host(host, staleness_timestamps(), staleness=staleness)
_emit_patch_event(serialized_host, host)
except InventoryException as ie:
log_patch_host_failed(logger, host_id_list)
flask.abort(400, str(ie))

for host in hosts_to_update:
host.patch(validated_patch_host_data)
Expand All @@ -424,7 +437,6 @@ def patch_host_by_id(host_id_list, body, rbac_filter=None):
serialized_host = serialize_host(host, staleness_timestamps(), staleness=staleness)
_emit_patch_event(serialized_host, host)

delete_keys(current_identity.org_id)
log_patch_host_success(logger, host_id_list)
return 200

Expand Down Expand Up @@ -500,7 +512,6 @@ def update_facts_by_namespace(operation, host_id_list, namespace, fact_dict, rba


@api_operation
@CACHE.cached(key_prefix=make_key)
@rbac(RbacResourceType.HOSTS, RbacPermission.READ)
@metrics.api_request_time.time()
def get_host_tag_count(host_id_list, page=1, per_page=100, order_by=None, order_how=None, rbac_filter=None):
Expand All @@ -521,7 +532,6 @@ def get_host_tag_count(host_id_list, page=1, per_page=100, order_by=None, order_


@api_operation
@CACHE.cached(key_prefix=make_key)
@rbac(RbacResourceType.HOSTS, RbacPermission.READ)
@metrics.api_request_time.time()
def get_host_tags(host_id_list, page=1, per_page=100, order_by=None, order_how=None, search=None, rbac_filter=None):
Expand Down Expand Up @@ -560,7 +570,6 @@ def host_checkin(body, rbac_filter=None):
db.session.commit()
serialized_host = serialize_host(existing_host, staleness_timestamps(), staleness=staleness)
_emit_patch_event(serialized_host, existing_host)
delete_keys(current_identity.org_id)
return flask_json_response(serialized_host, 201)
else:
flask.abort(404, "No hosts match the provided canonical facts.")
7 changes: 6 additions & 1 deletion api/host_group.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from http import HTTPStatus

import flask
from flask import abort
from flask import current_app
from flask import Response
Expand All @@ -10,6 +11,7 @@
from api.group_query import build_group_response
from app import RbacPermission
from app import RbacResourceType
from app.exceptions import InventoryException
from app.instrumentation import log_host_group_add_succeeded
from app.instrumentation import log_patch_group_failed
from app.logging import get_logger
Expand Down Expand Up @@ -47,7 +49,10 @@ def add_host_list_to_group(group_id, body, rbac_filter=None):

# Next, add the host-group associations
if host_id_list is not None:
add_hosts_to_group(group_id, body, current_app.event_producer)
try:
add_hosts_to_group(group_id, body, current_app.event_producer)
except InventoryException as ie:
flask.abort(HTTPStatus.BAD_REQUEST, str(ie))

updated_group = get_group_by_id_from_db(group_id)
log_host_group_add_succeeded(logger, host_id_list, group_id)
Expand Down
6 changes: 3 additions & 3 deletions api/host_query_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _get_host_list_using_filters(
.filter(*all_filters)
.order_by(*params_to_order_by(param_order_by, param_order_how))
)
query_results = host_query.paginate(page, per_page, True)
query_results = host_query.paginate(page=page, per_page=per_page, error_out=True)
db.session.close()

return query_results.items, query_results.total, additional_fields, system_profile_fields
Expand Down Expand Up @@ -428,7 +428,7 @@ def get_sap_system_info(

subquery = sap_query.subquery()
agg_query = db.session.query(subquery, func.count()).group_by("value")
query_results = agg_query.paginate(page, per_page, True)
query_results = agg_query.paginate(page=page, per_page=per_page, error_out=True)
db.session.close()
result = [{"value": qr[0], "count": qr[1]} for qr in query_results.items]
return result, query_results.total
Expand Down Expand Up @@ -515,7 +515,7 @@ def get_sparse_system_profile(
.order_by(*params_to_order_by(param_order_by, param_order_how))
)

query_results = sp_query.paginate(page, per_page, True)
query_results = sp_query.paginate(page=page, per_page=per_page, error_out=True)
db.session.close()

return query_results.total, [{"id": str(item[0]), "system_profile": item[1]} for item in query_results.items]
Expand Down
9 changes: 8 additions & 1 deletion api/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
from urllib.parse import quote
from urllib.parse import unquote

from connexion.decorators.uri_parsing import OpenAPIURIParser
from connexion.exceptions import TypeValidationError
from connexion.uri_parsing import OpenAPIURIParser
from connexion.utils import coerce_type

from app.exceptions import ValidationException

Expand Down Expand Up @@ -53,6 +55,11 @@ def resolve_params(self, params, _in):
else:
resolved_param[k] = values[-1]

try:
resolved_param[k] = coerce_type(param_defn, resolved_param[k], "parameter", k)
except TypeValidationError:
pass

return resolved_param

@staticmethod
Expand Down
Loading