Skip to content

Commit

Permalink
refactor(RHINENG-12780): enhance delete events
Browse files Browse the repository at this point in the history
  • Loading branch information
FabriciaDinizRH authored and jpramos123 committed Dec 5, 2024
1 parent 4de3048 commit 3794c10
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 7 deletions.
6 changes: 6 additions & 0 deletions api/host.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def delete_hosts_by_filter(


def _delete_host_list(host_id_list, rbac_filter):
request_host = flask.request.headers.get("Host", "")
current_identity = get_current_identity()
payload_tracker = get_payload_tracker(
account=current_identity.account_number, org_id=current_identity.org_id, request_id=threadctx.request_id
Expand All @@ -260,6 +261,7 @@ def _delete_host_list(host_id_list, rbac_filter):
inventory_config().host_delete_chunk_size,
identity=current_identity,
control_rule=get_control_rule(),
is_manual_delete=check_manual_deletion(request_host),
)

deleted_id_list = [str(r.host_row.id) for r in result_list]
Expand Down Expand Up @@ -312,6 +314,10 @@ def delete_host_by_id(host_id_list, rbac_filter=None):
return flask.Response(None, HTTPStatus.OK)


def check_manual_deletion(origin_host: str = ""):
return origin_host in ["console.stage.redhat.com", "console.redhat.com"]


@api_operation
@rbac(RbacResourceType.HOSTS, RbacPermission.READ)
@metrics.api_request_time.time()
Expand Down
5 changes: 4 additions & 1 deletion app/queue/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class HostDeleteEvent(Schema):
org_id = fields.Str()
insights_id = fields.Str()
request_id = fields.Str()
subscription_manager_id = fields.Str()
manual_delete = fields.Bool()
platform_metadata = fields.Dict()
metadata = fields.Nested(HostEventMetadataSchema())

Expand Down Expand Up @@ -113,14 +115,15 @@ def host_create_update_event(event_type, host, platform_metadata=None):
)


def host_delete_event(event_type, host, platform_metadata=None):
def host_delete_event(event_type, host, is_manual_delete=False, platform_metadata=None):
delete_event = {
"timestamp": datetime.now(timezone.utc),
"type": event_type.name,
"id": host.id,
**serialize_canonical_facts(host.canonical_facts),
"org_id": host.org_id,
"account": host.account,
"manual_delete": is_manual_delete,
"request_id": threadctx.request_id,
"platform_metadata": platform_metadata,
"metadata": {"request_id": threadctx.request_id},
Expand Down
9 changes: 7 additions & 2 deletions app/queue/host_mq.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,13 @@ def handle_message(message, notification_event_producer, message_operation=add_h
raise


def write_delete_event_message(event_producer: EventProducer, result: OperationResult):
event = build_event(EventType.delete, result.host_row, platform_metadata=result.platform_metadata)
def write_delete_event_message(event_producer: EventProducer, result: OperationResult, is_manual_delete: bool):
event = build_event(
EventType.delete,
result.host_row,
platform_metadata=result.platform_metadata,
is_manual_delete=is_manual_delete,
)
headers = message_headers(
EventType.delete,
result.host_row.canonical_facts.get("insights_id"),
Expand Down
12 changes: 9 additions & 3 deletions lib/host_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@ def _delete_host_db_records(select_query, chunk_size, identity, interrupt, contr


def _send_delete_messages_for_batch(
processed_rows: list[OperationResult], event_producer: EventProducer, notification_event_producer: EventProducer
processed_rows: list[OperationResult],
event_producer: EventProducer,
notification_event_producer: EventProducer,
is_manual_delete: bool,
):
for result in processed_rows:
if result is not None:
delete_host_count.inc()
write_delete_event_message(event_producer, result)
write_delete_event_message(event_producer, result, is_manual_delete)
send_notification(notification_event_producer, NotificationType.system_deleted, vars(result.host_row))


Expand All @@ -56,12 +59,15 @@ def delete_hosts(
interrupt=lambda: False,
identity=None,
control_rule=None,
is_manual_delete=False,
):
while select_query.count():
if kafka_available():
with session_guard(select_query.session):
batch_events = _delete_host_db_records(select_query, chunk_size, identity, interrupt, control_rule)
_send_delete_messages_for_batch(batch_events, event_producer, notification_event_producer)
_send_delete_messages_for_batch(
batch_events, event_producer, notification_event_producer, is_manual_delete
)

# yield the items in batch_events
yield from batch_events
Expand Down
2 changes: 2 additions & 0 deletions tests/helpers/mq_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def assert_delete_event_is_valid(
"org_id",
"insights_id",
"request_id",
"subscription_manager_id",
"manual_delete",
"platform_metadata",
"metadata",
}
Expand Down
11 changes: 10 additions & 1 deletion tests/test_api_hosts_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,13 @@ class DeleteHostsMock:
@classmethod
def create_mock(cls, hosts_ids_to_delete):
def _constructor(
select_query, event_producer, notification_event_producer, chunk_size, identity=None, control_rule=None
select_query,
event_producer,
notification_event_producer,
chunk_size,
identity=None,
control_rule=None,
is_manual_delete=False,
):
return cls(
hosts_ids_to_delete,
Expand All @@ -601,6 +607,7 @@ def _constructor(
chunk_size,
identity=identity,
control_rule=control_rule,
is_manual_delete=is_manual_delete,
)

return _constructor
Expand All @@ -614,6 +621,7 @@ def __init__(
chunk_size,
identity=None,
control_rule=None,
is_manual_delete=False,
):
self.host_ids_to_delete = host_ids_to_delete
self.original_query = delete_hosts(
Expand All @@ -623,6 +631,7 @@ def __init__(
chunk_size,
identity=identity,
control_rule=control_rule,
is_manual_delete=is_manual_delete,
)

def __getattr__(self, item):
Expand Down

0 comments on commit 3794c10

Please sign in to comment.