From 67f80c1fc0c75932646a1f1a29cab96ae56ddf0e Mon Sep 17 00:00:00 2001 From: Sebastien Quioc Date: Wed, 25 Sep 2024 19:12:22 +0200 Subject: [PATCH] refactor(Script): handle http errors when updating a module --- sekoia_automation/scripts/sync_library.py | 32 +++++++++++++++++------ 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/sekoia_automation/scripts/sync_library.py b/sekoia_automation/scripts/sync_library.py index b7f084c..ff41c40 100755 --- a/sekoia_automation/scripts/sync_library.py +++ b/sekoia_automation/scripts/sync_library.py @@ -104,12 +104,17 @@ def sync_list( elif response.status_code == 404: data: dict = obj.copy() data.update({"module_uuid": module_uuid}) - requests.post( + r = requests.post( urljoin(self.playbook_url, f"{name}s"), json=data, headers=self.headers, ) - created.append(f"{obj_name}") + if r.status_code == 200: + created.append(f"{obj_name}") + else: + errors.append( + f"{{{obj_name}: {response.status_code} - {response.text}}}" + ) else: content: dict = response.json() @@ -128,12 +133,17 @@ def sync_list( up_to_date.append(f"{obj_name}") else: - updated.append(f"{obj_name}") - requests.patch( + r = requests.patch( urljoin(self.playbook_url, f"{name}s", obj_uuid), json=obj, headers=self.headers, ) + if r.status_code == 200: + updated.append(f"{obj_name}") + else: + errors.append( + f"{{{obj_name}: {response.status_code} - {response.text}}}" + ) print(f"\t{module_name}/{name}") self.pprint( @@ -162,12 +172,15 @@ def sync_module(self, module_info: dict[str, Any]): print(f"[red]Error {response.status_code}[/red]") elif response.status_code == 404: - requests.post( + r = requests.post( urljoin(self.playbook_url, "modules"), json=module_info, headers=self.headers, ) - print("Created") + if r.status_code == 200: + print("Created") + else: + print(f"[red]Error {response.status_code} - {response.text}[/red]") else: content: dict = response.json() @@ -179,12 +192,15 @@ def sync_module(self, module_info: dict[str, Any]): if not module_info: print("Already-Up-To-Date") else: - requests.patch( + r = requests.patch( urljoin(self.playbook_url, "modules", module_uuid), json=module_info, headers=self.headers, ) - print("Updated") + if r.status_code == 200: + print("Updated") + else: + print(f"[red]Error {response.status_code} - {response.text}[/red]") def load_actions(self, module_path: Path) -> list: """Load JSON files representing the actions linked to a module