diff --git a/CHANGELOG.md b/CHANGELOG.md index 4506f69..7ebc30b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/pyproject.toml b/pyproject.toml index 1d95998..7a189c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" diff --git a/sekoia_automation/scripts/sync_library.py b/sekoia_automation/scripts/sync_library.py index 206ce2e..bcb4308 100755 --- a/sekoia_automation/scripts/sync_library.py +++ b/sekoia_automation/scripts/sync_library.py @@ -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: diff --git a/tests/scripts/test_sync_library.py b/tests/scripts/test_sync_library.py index a1f2b38..18e95b8 100644 --- a/tests/scripts/test_sync_library.py +++ b/tests/scripts/test_sync_library.py @@ -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", + } + )