Skip to content

Commit

Permalink
Contract multi-line expressions
Browse files Browse the repository at this point in the history
Used Black to contract expression spanning more than one line into a
single line, if it fits into the 119 line-length limit. If the
expression is a collection of comma-separated items, the trailing comma
has been removed. No other changes to the code has been made.

Expressions, whose line count could be reduced, but would need changing
the indentation style, have not been touched.
  • Loading branch information
Glutexo committed Jul 19, 2019
1 parent 599bc48 commit 6c71ede
Show file tree
Hide file tree
Showing 18 changed files with 162 additions and 425 deletions.
110 changes: 30 additions & 80 deletions api/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
# given priority in the host deduplication process.
# NOTE: The order of this tuple is important. The order defines
# the priority.
ELEVATED_CANONICAL_FACT_FIELDS = ("insights_id",
"subscription_manager_id",
)
ELEVATED_CANONICAL_FACT_FIELDS = ("insights_id", "subscription_manager_id")


logger = get_logger(__name__)
Expand All @@ -46,8 +44,7 @@ def add_host_list(host_list):
response_host_list.append({**e.to_json(), "host": host})
except ValidationError as e:
number_of_errors += 1
logger.exception("Input validation error while adding host",
extra={"host": host})
logger.exception("Input validation error while adding host", extra={"host": host})
response_host_list.append({"status": 400,
"title": "Bad Request",
"detail": str(e.messages),
Expand All @@ -62,9 +59,7 @@ def add_host_list(host_list):
"detail": "Could not complete operation",
"host": host})

response = {'total': len(response_host_list),
'errors': number_of_errors,
'data': response_host_list}
response = {'total': len(response_host_list), 'errors': number_of_errors, 'data': response_host_list}
return _build_json_response(response, status=207)


Expand All @@ -80,18 +75,14 @@ def _add_host(host):

input_host = Host.from_json(validated_input_host_dict.data)

if (
not current_identity.is_trusted_system and
current_identity.account_number != input_host.account
):
if not current_identity.is_trusted_system and current_identity.account_number != input_host.account:
raise InventoryException(
title="Invalid request",
detail="The account number associated with the user does not "
"match the account number associated with the host"
)

existing_host = find_existing_host(input_host.account,
input_host.canonical_facts)
existing_host = find_existing_host(input_host.account, input_host.canonical_facts)

if existing_host:
return update_existing_host(existing_host, input_host)
Expand All @@ -104,8 +95,7 @@ def find_existing_host(account_number, canonical_facts):
existing_host = _find_host_by_elevated_ids(account_number, canonical_facts)

if not existing_host:
existing_host = find_host_by_canonical_facts(account_number,
canonical_facts)
existing_host = find_host_by_canonical_facts(account_number, canonical_facts)

return existing_host

Expand All @@ -115,8 +105,7 @@ def _find_host_by_elevated_ids(account_number, canonical_facts):
for elevated_cf_name in ELEVATED_CANONICAL_FACT_FIELDS:
cf_value = canonical_facts.get(elevated_cf_name)
if cf_value:
existing_host = find_host_by_canonical_facts(account_number,
{elevated_cf_name: cf_value})
existing_host = find_host_by_canonical_facts(account_number, {elevated_cf_name: cf_value})
if existing_host:
return existing_host

Expand Down Expand Up @@ -180,23 +169,15 @@ def get_host_list(
order_how=None
):
if fqdn:
query = find_hosts_by_canonical_facts(
current_identity.account_number, {"fqdn": fqdn}
)
query = find_hosts_by_canonical_facts(current_identity.account_number, {"fqdn": fqdn})
elif display_name:
query = find_hosts_by_display_name(
current_identity.account_number, display_name
)
query = find_hosts_by_display_name(current_identity.account_number, display_name)
elif hostname_or_id:
query = find_hosts_by_hostname_or_id(
current_identity.account_number, hostname_or_id)
query = find_hosts_by_hostname_or_id(current_identity.account_number, hostname_or_id)
elif insights_id:
query = find_hosts_by_canonical_facts(
current_identity.account_number, {"insights_id": insights_id})
query = find_hosts_by_canonical_facts(current_identity.account_number, {"insights_id": insights_id})
else:
query = Host.query.filter(
Host.account == current_identity.account_number
)
query = Host.query.filter(Host.account == current_identity.account_number)

try:
order_by = _params_to_order_by(order_by, order_how)
Expand All @@ -208,9 +189,7 @@ def get_host_list(
query_results = query.paginate(page, per_page, True)
logger.debug(f"Found hosts: {query_results.items}")

return _build_paginated_host_list_response(
query_results.total, page, per_page, query_results.items
)
return _build_paginated_host_list_response(query_results.total, page, per_page, query_results.items)


def _order_how(column, order_how):
Expand All @@ -235,9 +214,7 @@ def _params_to_order_by(order_by=None, order_how=None):
else:
ordering = (Host.display_name.asc(),)
elif order_by:
raise ValueError(
"Unsupported ordering column, use \"updated\" or \"display_name\"."
)
raise ValueError("Unsupported ordering column, use \"updated\" or \"display_name\".")
elif order_how:
raise ValueError(
"Providing ordering direction without a column is not supported. "
Expand All @@ -259,17 +236,12 @@ def _build_paginated_host_list_response(total, page, per_page, host_list):


def _build_json_response(json_data, status=200):
return flask.Response(ujson.dumps(json_data),
status=status,
mimetype="application/json")
return flask.Response(ujson.dumps(json_data), status=status, mimetype="application/json")


def find_hosts_by_display_name(account, display_name):
logger.debug("find_hosts_by_display_name(%s)" % display_name)
return Host.query.filter(
(Host.account == account)
& Host.display_name.comparator.contains(display_name)
)
return Host.query.filter((Host.account == account) & Host.display_name.comparator.contains(display_name))


def find_hosts_by_canonical_facts(account_number, canonical_facts):
Expand All @@ -292,12 +264,9 @@ def find_hosts_by_hostname_or_id(account_number, hostname):
logger.debug("Adding id (uuid) to the filter list")
except Exception:
# Do not filter using the id
logger.debug("The hostname (%s) could not be converted into a UUID",
hostname,
exc_info=True)
logger.debug("The hostname (%s) could not be converted into a UUID", hostname, exc_info=True)

return Host.query.filter(sqlalchemy.and_(*[Host.account == account_number,
sqlalchemy.or_(*filter_list)]))
return Host.query.filter(sqlalchemy.and_(*[Host.account == account_number, sqlalchemy.or_(*filter_list)]))


@api_operation
Expand Down Expand Up @@ -332,8 +301,7 @@ def delete_by_id(host_id_list):
@api_operation
@metrics.api_request_time.time()
def get_host_by_id(host_id_list, page=1, per_page=100, order_by=None, order_how=None):
query = _get_host_list_by_id_list(current_identity.account_number,
host_id_list)
query = _get_host_list_by_id_list(current_identity.account_number, host_id_list)

try:
order_by = _params_to_order_by(order_by, order_how)
Expand All @@ -345,25 +313,17 @@ def get_host_by_id(host_id_list, page=1, per_page=100, order_by=None, order_how=

logger.debug(f"Found hosts: {query_results.items}")

return _build_paginated_host_list_response(
query_results.total, page, per_page, query_results.items
)
return _build_paginated_host_list_response(query_results.total, page, per_page, query_results.items)


def _get_host_list_by_id_list(account_number, host_id_list):
return Host.query.filter(
(Host.account == account_number)
& Host.id.in_(host_id_list)
)
return Host.query.filter((Host.account == account_number) & Host.id.in_(host_id_list))


@api_operation
@metrics.api_request_time.time()
def get_host_system_profile_by_id(
host_id_list, page=1, per_page=100, order_by=None, order_how=None
):
query = _get_host_list_by_id_list(current_identity.account_number,
host_id_list)
def get_host_system_profile_by_id(host_id_list, page=1, per_page=100, order_by=None, order_how=None):
query = _get_host_list_by_id_list(current_identity.account_number, host_id_list)

try:
order_by = _params_to_order_by(order_by, order_how)
Expand All @@ -373,8 +333,7 @@ def get_host_system_profile_by_id(
query = query.order_by(*order_by)
query_results = query.paginate(page, per_page, True)

response_list = [host.to_system_profile_json()
for host in query_results.items]
response_list = [host.to_system_profile_json() for host in query_results.items]

json_output = {"total": query_results.total,
"count": len(response_list),
Expand All @@ -392,17 +351,10 @@ def patch_by_id(host_id_list, host_data):
try:
validated_patch_host_data = PatchHostSchema(strict=True).load(host_data).data
except ValidationError as e:
logger.exception("Input validation error while patching host: %s - %s"
% (host_id_list, host_data))
return ({"status": 400,
"title": "Bad Request",
"detail": str(e.messages),
"type": "unknown",
},
400)

query = _get_host_list_by_id_list(current_identity.account_number,
host_id_list)
logger.exception("Input validation error while patching host: %s - %s" % (host_id_list, host_data))
return ({"status": 400, "title": "Bad Request", "detail": str(e.messages), "type": "unknown"}, 400)

query = _get_host_list_by_id_list(current_identity.account_number, host_id_list)

hosts_to_update = query.all()

Expand All @@ -421,8 +373,7 @@ def patch_by_id(host_id_list, host_data):
@api_operation
@metrics.api_request_time.time()
def replace_facts(host_id_list, namespace, fact_dict):
return update_facts_by_namespace(FactOperations.replace, host_id_list,
namespace, fact_dict)
return update_facts_by_namespace(FactOperations.replace, host_id_list, namespace, fact_dict)


@api_operation
Expand All @@ -433,8 +384,7 @@ def merge_facts(host_id_list, namespace, fact_dict):
logger.debug(error_msg)
return error_msg, 400

return update_facts_by_namespace(FactOperations.merge, host_id_list,
namespace, fact_dict)
return update_facts_by_namespace(FactOperations.merge, host_id_list, namespace, fact_dict)


def update_facts_by_namespace(operation, host_id_list, namespace, fact_dict):
Expand Down
18 changes: 6 additions & 12 deletions api/metrics.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from prometheus_client import Counter, Summary

api_request_time = Summary("inventory_request_processing_seconds",
"Time spent processing request")
api_request_time = Summary("inventory_request_processing_seconds", "Time spent processing request")
host_dedup_processing_time = Summary("inventory_dedup_processing_seconds",
"Time spent looking for existing host (dedup logic)")
find_host_using_elevated_ids = Summary("inventory_find_host_using_elevated_ids_processing_seconds",
Expand All @@ -10,18 +9,13 @@
"Time spent committing a new host to the database")
update_host_commit_processing_time = Summary("inventory_update_host_commit_seconds",
"Time spent committing a update host to the database")
api_request_count = Counter("inventory_request_count",
"The total amount of API requests")
create_host_count = Counter("inventory_create_host_count",
"The total amount of hosts created")
update_host_count = Counter("inventory_update_host_count",
"The total amount of hosts updated")
delete_host_count = Counter("inventory_delete_host_count",
"The total amount of hosts deleted")
api_request_count = Counter("inventory_request_count", "The total amount of API requests")
create_host_count = Counter("inventory_create_host_count", "The total amount of hosts created")
update_host_count = Counter("inventory_update_host_count", "The total amount of hosts updated")
delete_host_count = Counter("inventory_delete_host_count", "The total amount of hosts deleted")
delete_host_processing_time = Summary("inventory_delete_host_commit_seconds",
"Time spent deleting hosts from the database")
login_failure_count = Counter("inventory_login_failure_count",
"The total amount of failed login attempts")
login_failure_count = Counter("inventory_login_failure_count", "The total amount of failed login attempts")
system_profile_deserialization_time = Summary("inventory_system_profile_deserialization_time",
"Time spent deserializing system profile documents")
system_profile_commit_processing_time = Summary("inventory_system_profile_commit_processing_time",
Expand Down
5 changes: 1 addition & 4 deletions api/mgmt.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from flask import Blueprint, jsonify
from prometheus_client import (CollectorRegistry,
multiprocess,
generate_latest,
CONTENT_TYPE_LATEST,)
from prometheus_client import CollectorRegistry, multiprocess, generate_latest, CONTENT_TYPE_LATEST

from app.common import get_build_version

Expand Down
11 changes: 3 additions & 8 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def create_app(config_name):
app_config = Config()
app_config.log_configuration(config_name)

connexion_app = connexion.App(
"inventory", specification_dir="./swagger/", options=connexion_options
)
connexion_app = connexion.App("inventory", specification_dir="./swagger/", options=connexion_options)

# Read the swagger.yml file to configure the endpoints
with open("swagger/api.spec.yaml", "rb") as fp:
Expand Down Expand Up @@ -66,14 +64,11 @@ def create_app(config_name):

db.init_app(flask_app)

flask_app.register_blueprint(monitoring_blueprint,
url_prefix=app_config.mgmt_url_path_prefix)
flask_app.register_blueprint(monitoring_blueprint, url_prefix=app_config.mgmt_url_path_prefix)

@flask_app.before_request
def set_request_id():
threadctx.request_id = request.headers.get(
REQUEST_ID_HEADER,
UNKNOWN_REQUEST_ID_VALUE)
threadctx.request_id = request.headers.get(REQUEST_ID_HEADER, UNKNOWN_REQUEST_ID_VALUE)

init_tasks(app_config, flask_app)

Expand Down
10 changes: 3 additions & 7 deletions app/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
from app.logging import get_logger
from werkzeug.local import LocalProxy

__all__ = ["current_identity",
"bearer_token_handler",
"authentication_header_handler"]
__all__ = ["current_identity", "bearer_token_handler", "authentication_header_handler"]

logger = get_logger(__name__)

Expand All @@ -18,8 +16,7 @@ def authentication_header_handler(apikey, required_scopes=None):
validate(identity)
except Exception:
login_failure_count.inc()
logger.debug("Failed to validate identity header value",
exc_info=True)
logger.debug("Failed to validate identity header value", exc_info=True)
return None

return {"uid": identity}
Expand All @@ -31,8 +28,7 @@ def bearer_token_handler(token):
validate(identity)
except Exception:
login_failure_count.inc()
logger.debug("Failed to validate bearer token value",
exc_info=True)
logger.debug("Failed to validate bearer token value", exc_info=True)
return None

return {"uid": identity}
Expand Down
4 changes: 1 addition & 3 deletions app/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,4 @@ class HostEvent(Schema):


def delete(id):
return HostEvent(strict=True).dumps(
{"id": id, "timestamp": datetime.utcnow(), "type": "delete"}
).data
return HostEvent(strict=True).dumps({"id": id, "timestamp": datetime.utcnow(), "type": "delete"}).data
3 changes: 1 addition & 2 deletions app/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def __init__(self, status=400, title=None, detail=None, type="about:blank"):
self.type = type

def to_json(self):
return {'detail': self.detail, 'status': self.status,
'title': self.title, 'type': self.type}
return {'detail': self.detail, 'status': self.status, 'title': self.title, 'type': self.type}


class InputFormatException(InventoryException):
Expand Down
Loading

0 comments on commit 6c71ede

Please sign in to comment.