Skip to content

Commit

Permalink
refactor(Script): handle http errors when updating a module
Browse files Browse the repository at this point in the history
  • Loading branch information
squioc committed Sep 25, 2024
1 parent be74172 commit 67f80c1
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions sekoia_automation/scripts/sync_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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(
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 67f80c1

Please sign in to comment.