-
Notifications
You must be signed in to change notification settings - Fork 0
Service bus std #14
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
base: main
Are you sure you want to change the base?
Service bus std #14
Changes from all commits
6547d29
b477fb9
1f7d981
dbf8146
c8c05e9
4bfb346
44ad023
ad972ea
ed06bdf
64e0ef7
6d81831
bb220b5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,23 +6,28 @@ | |
| import os | ||
|
|
||
| import azure.functions as func | ||
| from azure.storage.blob.aio import BlobServiceClient | ||
|
|
||
| from shared_code import constants, parsers | ||
| from shared_code.blob_operations import get_blob_info_from_topic_and_subject, get_blob_client_from_blob_info | ||
| from shared_code import constants, parsers, sb_helpers | ||
| from shared_code.blob_operations import get_blob_info_from_topic_and_subject, get_credential, get_account_url | ||
|
|
||
|
|
||
| def main(msg: func.ServiceBusMessage, | ||
| stepResultEvent: func.Out[func.EventGridOutputEvent], | ||
| dataDeletionEvent: func.Out[func.EventGridOutputEvent]): | ||
| async def main(msg: func.ServiceBusMessage, | ||
| stepResultEvent: func.Out[func.EventGridOutputEvent], | ||
| dataDeletionEvent: func.Out[func.EventGridOutputEvent]): | ||
|
|
||
| logging.info("Python ServiceBus topic trigger processed message - A new blob was created!.") | ||
| body = msg.get_body().decode('utf-8') | ||
| logging.info('Python ServiceBus queue trigger processed message: %s', body) | ||
| logging.info('Python ServiceBus topic trigger raw body: %s', body) | ||
| payload = await sb_helpers.receive_message_payload(body) | ||
| json_body = json.loads(payload) | ||
|
|
||
| json_body = json.loads(body) | ||
| topic = json_body["topic"] | ||
| request_id = re.search(r'/blobServices/default/containers/(.*?)/blobs', json_body["subject"]).group(1) | ||
|
|
||
| completed_step = None | ||
| new_status = None | ||
|
|
||
| # message originated from in-progress blob creation | ||
| if constants.STORAGE_ACCOUNT_NAME_IMPORT_INPROGRESS in topic or constants.STORAGE_ACCOUNT_NAME_EXPORT_INPROGRESS in topic: | ||
| try: | ||
|
|
@@ -35,7 +40,7 @@ def main(msg: func.ServiceBusMessage, | |
| # If malware scanning is enabled, the fact that the blob was created can be dismissed. | ||
| # It will be consumed by the malware scanning service | ||
| logging.info('Malware scanning is enabled. no action to perform.') | ||
| send_delete_event(dataDeletionEvent, json_body, request_id) | ||
| await send_delete_event(dataDeletionEvent, json_body, request_id) | ||
| return | ||
| else: | ||
| logging.info('Malware scanning is disabled. Completing the submitted stage (moving to in_review).') | ||
|
|
@@ -57,31 +62,41 @@ def main(msg: func.ServiceBusMessage, | |
| new_status = constants.STAGE_BLOCKED_BY_SCAN | ||
|
|
||
| # reply with a step completed event | ||
| stepResultEvent.set( | ||
| func.EventGridOutputEvent( | ||
| id=str(uuid.uuid4()), | ||
| data={"completed_step": completed_step, "new_status": new_status, "request_id": request_id}, | ||
| subject=request_id, | ||
| event_type="Airlock.StepResult", | ||
| event_time=datetime.datetime.now(datetime.UTC), | ||
| data_version=constants.STEP_RESULT_EVENT_DATA_VERSION)) | ||
| if completed_step and new_status: | ||
| data = {"completed_step": completed_step, "new_status": new_status, "request_id": request_id} | ||
| offloaded_data = await sb_helpers.wrap_payload_for_offloading(data) | ||
| stepResultEvent.set( | ||
| func.EventGridOutputEvent( | ||
| id=str(uuid.uuid4()), | ||
| data=offloaded_data, | ||
| subject=request_id, | ||
| event_type="Airlock.StepResult", | ||
| event_time=datetime.datetime.now(datetime.UTC), | ||
| data_version=constants.STEP_RESULT_EVENT_DATA_VERSION)) | ||
|
|
||
| send_delete_event(dataDeletionEvent, json_body, request_id) | ||
| await send_delete_event(dataDeletionEvent, json_body, request_id) | ||
|
|
||
|
|
||
| def send_delete_event(dataDeletionEvent: func.Out[func.EventGridOutputEvent], json_body, request_id): | ||
| async def send_delete_event(dataDeletionEvent: func.Out[func.EventGridOutputEvent], json_body, request_id): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this function is now |
||
| # check blob metadata to find the blob it was copied from | ||
| blob_client = get_blob_client_from_blob_info( | ||
| *get_blob_info_from_topic_and_subject(topic=json_body["topic"], subject=json_body["subject"])) | ||
| blob_metadata = blob_client.get_blob_properties()["metadata"] | ||
| copied_from = json.loads(blob_metadata["copied_from"]) | ||
| logging.info(f"copied from history: {copied_from}") | ||
| storage_account_name, container_name, blob_name = get_blob_info_from_topic_and_subject(topic=json_body["topic"], subject=json_body["subject"]) | ||
|
|
||
| credential = await get_credential() | ||
| async with credential: | ||
| async with BlobServiceClient(account_url=get_account_url(storage_account_name), credential=credential) as blob_service_client: | ||
| blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name) | ||
| blob_properties = await blob_client.get_blob_properties() | ||
| blob_metadata = blob_properties["metadata"] | ||
| copied_from = json.loads(blob_metadata["copied_from"]) | ||
| logging.info(f"copied from history: {copied_from}") | ||
|
|
||
| # signal that the container where we copied from can now be deleted | ||
| data = {"blob_to_delete": copied_from[-1]} # last container in copied_from is the one we just copied from | ||
| offloaded_data = await sb_helpers.wrap_payload_for_offloading(data) | ||
| dataDeletionEvent.set( | ||
| func.EventGridOutputEvent( | ||
| id=str(uuid.uuid4()), | ||
| data={"blob_to_delete": copied_from[-1]}, # last container in copied_from is the one we just copied from | ||
| data=offloaded_data, | ||
| subject=request_id, | ||
| event_type="Airlock.DataDeletion", | ||
| event_time=datetime.datetime.now(datetime.UTC), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The log message has been updated from
queue trigger processed messagetotopic trigger raw body. Whileraw bodyis more descriptive, please ensure that this trigger is indeed a 'topic' trigger and not a 'queue' trigger to maintain accuracy in logging.