Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add loader for playbooks #298

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions galaxy_importer/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class ContentCategory(enum.Enum):


class ContentType(enum.Enum):
PLAYBOOK = "playbook"
ROLE = "role"
MODULE = "module"
MODULE_UTILS = "module_utils"
Expand All @@ -110,6 +111,7 @@ class ContentType(enum.Enum):
@property
def category(self):
return {
ContentType.PLAYBOOK: ContentCategory.PLAYBOOK,
ContentType.ROLE: ContentCategory.ROLE,
ContentType.MODULE: ContentCategory.MODULE,
ContentType.MODULE_UTILS: ContentCategory.PLUGIN,
Expand Down
15 changes: 14 additions & 1 deletion galaxy_importer/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ def _find_plugins(self, content_type, content_dir):
rel_path = os.path.relpath(file_path, self.path)
yield Result(content_type, rel_path)

def _find_playbooks(self, content_type, content_dir):
for root, dirs, filenames in os.walk(content_dir):
if root != content_dir:
continue
for filename in filenames:
_, extension = os.path.splitext(filename)
if extension and extension.lower() in [".yml", ".yaml"]:
file_path = os.path.join(root, filename)
rel_path = os.path.relpath(file_path, self.path)
yield Result(content_type, rel_path)

def _find_roles(self, content_type, content_dir):
"""Find all dirs inside roles dir where contents match a role."""

Expand All @@ -97,7 +108,9 @@ def recurse_role_dir(path):

def _content_type_dirs(self):
for content_type in constants.ContentType:
if content_type == constants.ContentType.ROLE:
if content_type == constants.ContentType.PLAYBOOK:
yield content_type, "playbooks", self._find_playbooks
elif content_type == constants.ContentType.ROLE:
yield content_type, "roles", self._find_roles
elif content_type == constants.ContentType.MODULE:
yield content_type, "plugins/modules", self._find_plugins
Expand Down
2 changes: 1 addition & 1 deletion galaxy_importer/loaders/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ def load(self):
).load()

self.content_objs = list(self._load_contents())

self.contents = self._build_contents_blob()

self.docs_blob = self._build_docs_blob()
self.requires_ansible = file_parser.RuntimeFileParser(self.path).get_requires_ansible()
self._check_ee_yml_dep_files()
Expand Down
27 changes: 26 additions & 1 deletion galaxy_importer/loaders/content.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,29 @@ def _make_path_name(rel_path, name):
return name


class PlaybookLoader(ContentLoader):

def load(self):
self._log_loading()

return schema.Content(
name=self.path_name,
content_type=self.content_type,
)

@staticmethod
def _make_name(rel_path):
return os.path.basename(rel_path)

@staticmethod
def _make_path_name(rel_path, name):
dirname_parts = Path(os.path.dirname(rel_path)).parts[1:]
return ".".join(list(dirname_parts) + [name])

def _validate_name(self):
return True


class RoleLoader(ContentLoader):
def load(self):
self._log_loading()
Expand Down Expand Up @@ -233,7 +256,9 @@ def _find_metadata_file_path(root, rel_path):


def get_loader_cls(content_type):
if content_type.category == constants.ContentCategory.ROLE:
if content_type.category == constants.ContentCategory.PLAYBOOK:
return PlaybookLoader
elif content_type.category == constants.ContentCategory.ROLE:
return RoleLoader
elif content_type.category in [
constants.ContentCategory.PLUGIN,
Expand Down
1 change: 1 addition & 0 deletions tests/integration/test_eda.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_eda_import(workdir, local_image_config):

# the data should have all the relevant bits
assert results["contents"] == [
{"content_type": "playbook", "description": None, "name": "hello.yml"},
{"content_type": "role", "description": "your role description", "name": "test_role"},
{
"content_type": "module",
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import shutil
import tempfile
import unittest
import yaml

import pytest

Expand Down Expand Up @@ -50,6 +51,9 @@ def setUp(self):
self.roles_dir = os.path.join(self.temp_dir, "roles")
os.mkdir(self.roles_dir)

self.playbooks_dir = os.path.join(self.temp_dir, "playbooks")
os.mkdir(self.playbooks_dir)

def tearDown(self):
shutil.rmtree(self.temp_dir)

Expand Down Expand Up @@ -171,6 +175,29 @@ def test_extensions_path_exists_metadata_not(self):
contents = list(ContentFinder().find_contents(self.temp_dir))
assert len(contents) == 0

def test_find_playbooks(self):

content = [
{
"name": "a test playbook",
"tasks": [
{
"name": "a task",
"shell": "whoami",
}
],
}
]

pb_path = os.path.join(self.playbooks_dir, "play1.yml")
with open(pb_path, "w") as f:
f.write(yaml.dump(content))

contents = list(ContentFinder().find_contents(self.temp_dir))
assert len(contents) == 1
assert contents[0].content_type.name == "PLAYBOOK"
assert contents[0].path == "playbooks/play1.yml"


@pytest.fixture
def walker_dir(tmp_path):
Expand Down
Loading