Skip to content

Commit

Permalink
Merge pull request #84 from SEKOIA-IO/fix/scripts
Browse files Browse the repository at this point in the history
fix: Various fixes in scripts
  • Loading branch information
Darkheir authored Oct 20, 2023
2 parents 0a34ff0 + b044b7d commit df43840
Show file tree
Hide file tree
Showing 9 changed files with 1,174 additions and 279 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- Added support for Connectors
- In script to generate new modules lock requirements

### Fixed

- In script to generate new modules fix module import for modules with spaces

## [1.5.2] - 2023-10-04

Expand Down
1,394 changes: 1,118 additions & 276 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ python-slugify = "^5.0.2"
PyYAML = "^6.0"
Jinja2 = "^3.0.3"
black = "*" # To format files in cli tools
poetry = "*" # To lock requirements when creating new module
prometheus-client = "^0.16.0"
aiohttp = { version = "^3.8.4", optional = true }
aiolimiter = { version = "^1.1.0", optional = true }
Expand Down
8 changes: 6 additions & 2 deletions sekoia_automation/scripts/files_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ def inspect_module(
modules: set[type[Module]],
actions: set[type[Action]],
triggers: set[type[Trigger]],
connectors: set[type[Connector]],
):
module = import_module(name)

for _, obj in getmembers(module, isclass):
if not isabstract(obj):
if issubclass(obj, Action):
actions.add(obj)
elif issubclass(obj, Connector):
connectors.add(obj)
elif issubclass(obj, Trigger):
triggers.add(obj)
elif issubclass(obj, Module) and obj != Module:
Expand All @@ -58,10 +61,11 @@ def execute(self):
modules = set()
actions = set()
triggers = set()
connectors = set()

for _, name, ispkg in walk_packages([self.base_path.as_posix()]):
if not ispkg and not (name == "tests" or name.startswith("tests.")):
self.inspect_module(name, modules, actions, triggers)
self.inspect_module(name, modules, actions, triggers, connectors)

if len(modules) != 1:
print("[bold red][!] Found 0 or more than 1 module, aborting[/bold red]")
Expand All @@ -71,7 +75,7 @@ def execute(self):
self.generate_main(module, actions, triggers)
self.generate_action_manifests(actions)
self.generate_trigger_manifests(triggers)
self.generate_connector_manifests(triggers)
self.generate_connector_manifests(connectors)
self.update_module_manifest(module)

sys.path = _old_path
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
poetry lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from {{cookiecutter.module_name.lower()}}_modules import {{cookiecutter.module_name.capitalize()}}Module
from {{cookiecutter.module_name.lower().replace(" ", "_")}}_modules import {{cookiecutter.module_name.title().replace(" ", "")}}Module

if __name__ == "__main__":
module = {{cookiecutter.module_name.title().replace(" ", "")}}Module()
Expand Down
10 changes: 10 additions & 0 deletions tests/data/sample_module/sample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydantic import BaseModel, Field

from sekoia_automation.action import Action
from sekoia_automation.connector import Connector
from sekoia_automation.module import Module
from sekoia_automation.trigger import Trigger

Expand Down Expand Up @@ -47,3 +48,12 @@ class SampleAction(Action):

def run(self, arguments: SampleArguments):
raise NotImplementedError


class SampleConnector(Connector):
module: SampleModule
name = "Sample Connector"
description = "My Sample Connector Description"

def run(self):
raise NotImplementedError
25 changes: 25 additions & 0 deletions tests/expectations/sample_module/connector_sample_connector.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "Sample Connector",
"description": "My Sample Connector Description",
"uuid": "4721e6af-a6b0-5785-9860-b33d0f218435",
"docker_parameters": "SampleConnector",
"arguments": {
"title": "DefaultConnectorConfiguration",
"type": "object",
"properties": {
"intake_server": {
"title": "Intake Server",
"default": "https://intake.sekoia.io",
"type": "string"
},
"intake_key": {
"title": "Intake Key",
"type": "string"
}
},
"required": [
"intake_key"
]
},
"results": {}
}
6 changes: 6 additions & 0 deletions tests/scripts/test_files_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def test_files_generator(sample_module):
actual, expected = get_actual_and_expected(sample_module, "manifest.json")
assert actual == expected

# A manifest should have been generated for the connector
actual, expected = get_actual_and_expected(
sample_module, "connector_sample_connector.json"
)
assert actual == expected


def test_files_generator_wrong_module_path():
with pytest.raises(click.exceptions.Exit):
Expand Down

0 comments on commit df43840

Please sign in to comment.