@cryptotaxi247 / CoPilot / commits / 57ef2150

chore: Refactor content pack insertion and processing (#237)

* chore: Refactor content pack insertion and processing Refactor the `insert_content_pack` function to handle specific exceptions and log appropriate messages. Also, update the `process_content_pack` function to check for the existence of a content pack using the customer name as a keyword. Additionally, modify the `insert_and_install_content_pack` function to only install the content pack if it was successfully inserted. * precommit fixes

taylor_socfortress committed Jun 10, 2024 at 11:34 UTC 57ef21506da0bde22ff7ec806e071b39a7387c55
2 files changed +14 -3
backend/app/connectors/graylog/services/content_packs.py
+8
@@ -64,6 +64,14 @@ async def insert_content_pack(content_pack: ContentPack) -> bool:
64 return True
65 else:
66 raise HTTPException(status_code=500, detail="Content pack insertion unsuccessful")
67 + except HTTPException as e:
68 + if "Content pack" in e.detail and "already found" in e.detail and "PROCESSING_PIPELINE" in content_pack["name"]:
69 + logger.info("Content pack with PROCESSING_PIPELINE already exists, skipping")
70 + return False
71 + else:
72 + error_msg = f"Failed to insert content pack: {e}"
73 + logger.error(error_msg)
74 + raise HTTPException(status_code=500, detail=error_msg)
75 except KeyError as e:
76 error_msg = f"Failed to insert content pack key: {e}"
77 logger.error(error_msg)
backend/app/stack_provisioning/graylog/services/provision.py
+6 -3
@@ -183,7 +183,7 @@ async def filter_content_packs(content_packs, protocol_type):
183
184
185 async def process_content_pack(content_pack, content_pack_request):
186 - content_pack_exists = await does_content_pack_exist(content_pack)
186 + content_pack_exists = await does_content_pack_exist(content_pack_request.keywords.customer_name)
187 if content_pack_exists is True:
188 return
189 content_pack = load_content_pack_json(f"{content_pack}.json")
@@ -202,10 +202,13 @@ async def process_content_pack(content_pack, content_pack_request):
202
203 async def insert_and_install_content_pack(content_pack):
204 logger.info(f"Inserting {content_pack} Content Pack...")
205 - await insert_content_pack(content_pack)
205 + content_pack_inserted = await insert_content_pack(content_pack)
206 id, rev = await get_id_and_rev(content_pack)
207 logger.info(f"Id: {id}, Rev: {rev}")
208 - await install_content_pack(content_pack_id=id, revision=rev)
208 + if content_pack_inserted is True:
209 + await install_content_pack(content_pack_id=id, revision=rev)
210 + else:
211 + logger.info(f"Content pack {content_pack['name']} already inserted, skipping install...")
212
213
214 async def provision_content_pack_network_connector(content_pack_request: ProvisionNetworkContentPackRequest) -> ProvisionGraylogResponse: