Skip to content

Commit

Permalink
fix: Fixes synchronization script
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkheir committed Nov 4, 2024
1 parent d09010b commit 76ded3e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## 1.17.1 - 2024-11-04

### Fixed

- Change the way to handle docker image information when publishing a module
- Fix the module synchronization script

## 1.17.0 - 2024-11-04

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "poetry.core.masonry.api"
[tool.poetry]
name = "sekoia-automation-sdk"

version = "1.17.0"
version = "1.17.1"
description = "SDK to create Sekoia.io playbook modules"
license = "MIT"
readme = "README.md"
Expand Down
21 changes: 12 additions & 9 deletions sekoia_automation/scripts/sync_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,32 +162,35 @@ def sync_module(self, module_info: dict[str, Any]):
print(f"[red]Error {response.status_code}[/red]")

elif response.status_code == 404:
requests.post(
res = requests.post(
urljoin(self.playbook_url, "modules"),
json=module_info,
headers=self.headers,
)
if not res.ok:
print(f"[red]Error creating the module: {res.status_code}[/red]")
print(res.json())
raise typer.Exit(code=1)
print("Created")

else:
content: dict = response.json()
mod_list: list = list(module_info.keys())

for k in content.keys():
if k not in mod_list:
module_info[k] = None

if content[k] == module_info[k]:
for k, value in content.items():
if k in module_info and value == module_info[k]:
del module_info[k]

if not module_info:
print("Already-Up-To-Date")
else:
requests.patch(
res = requests.patch(
urljoin(self.playbook_url, "modules", module_uuid),
json=module_info,
headers=self.headers,
)
if not res.ok:
print(f"[red]Error updating the module: {res.status_code}[/red]")
print(res.json())
raise typer.Exit(code=1)
print("Updated")

def load_actions(self, module_path: Path) -> list:
Expand Down
45 changes: 45 additions & 0 deletions tests/scripts/test_sync_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,48 @@ def test_get_module_docker_name():
manifest.pop("slug")
with pytest.raises(ValueError):
lib._get_module_docker_name(manifest)


def test_sync_module_create_error(requests_mock):
lib = SyncLibrary(SYMPOHNY_URL, API_KEY, Path("tests/data"))

requests_mock.get(
f"{SYMPOHNY_URL}/modules/eaa1d29c-4c34-42c6-8275-2da1c8cca129",
status_code=404,
)
requests_mock.post(
f"{SYMPOHNY_URL}/modules",
status_code=500,
json={"error": "Internal Server Error"},
)
with pytest.raises(typer.Exit):
lib.sync_module(
{"name": "My Module", "uuid": "eaa1d29c-4c34-42c6-8275-2da1c8cca129"}
)


def test_sync_module_update_error(requests_mock):
lib = SyncLibrary(SYMPOHNY_URL, API_KEY, Path("tests/data"))

requests_mock.get(
f"{SYMPOHNY_URL}/modules/eaa1d29c-4c34-42c6-8275-2da1c8cca129",
status_code=200,
json={
"name": "My Module",
"uuid": "eaa1d29c-4c34-42c6-8275-2da1c8cca129",
"version": "0.1",
},
)
requests_mock.patch(
f"{SYMPOHNY_URL}/modules/eaa1d29c-4c34-42c6-8275-2da1c8cca129",
status_code=500,
json={"error": "Internal Server Error"},
)
with pytest.raises(typer.Exit):
lib.sync_module(
{
"name": "My Module",
"uuid": "eaa1d29c-4c34-42c6-8275-2da1c8cca129",
"version": "0.2",
}
)

0 comments on commit 76ded3e

Please sign in to comment.