Skip to content

Commit

Permalink
feat: remove CollectionReader instances outside of Collections
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulFarault committed Oct 31, 2024
1 parent 3935dd7 commit d53347f
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
6 changes: 1 addition & 5 deletions tdp/cli/params/collections_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import click
from click.decorators import FC

from tdp.core.collection import CollectionReader
from tdp.core.collections import Collections


Expand All @@ -29,10 +28,7 @@ def _collections_from_paths(
if not value:
raise click.BadParameter("cannot be empty", ctx=ctx, param=param)

collections_list = [CollectionReader.from_path(path) for path in value]
collections = Collections(collections_list)

return collections
return Collections.from_collection_paths(value)


def collections_option(func: FC) -> FC:
Expand Down
9 changes: 7 additions & 2 deletions tdp/core/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,21 @@ def __init__(
check_collection_structure(self._path)
self._inventory_reader = inventory_reader or InventoryReader()

# ? Is this method really useful?
@staticmethod
def from_path(path: PathLike) -> CollectionReader:
def from_path(
path: PathLike,
inventory_reader: Optional[InventoryReader] = None,
) -> CollectionReader:
"""Factory method to create a collection from a path.
Args:
path: The path to the collection.
Returns: A collection.
"""
return CollectionReader(path=Path(path).expanduser().resolve())
inventory_reader = inventory_reader or InventoryReader()
return CollectionReader(Path(path).expanduser().resolve(), inventory_reader)

@property
def name(self) -> str:
Expand Down
16 changes: 14 additions & 2 deletions tdp/core/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@
import logging
from collections.abc import Iterable
from pathlib import Path
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional

from tdp.core.collection import CollectionReader
from tdp.core.entities.hostable_entity_name import ServiceComponentName
from tdp.core.entities.operation import Operations, Playbook
from tdp.core.inventory_reader import InventoryReader
from tdp.core.operation import Operation
from tdp.core.variables.schema.service_schema import ServiceSchema

if TYPE_CHECKING:
from tdp.core.collection import CollectionReader
from tdp.core.types import PathLike


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -52,6 +54,16 @@ def __init__(self, collections: Iterable[CollectionReader]):
self._schemas = self._init_schemas(self._collections)
self._services_components = self._init_hostable_entities(self.operations)

@staticmethod
def from_collection_paths(
paths: Iterable[PathLike], inventory_reader: Optional[InventoryReader] = None
):
inventory_reader = inventory_reader or InventoryReader()
collection_readers = [
CollectionReader.from_path(path, inventory_reader) for path in paths
]
return Collections(collection_readers)

@property
def dag_operations(self) -> Operations:
"""Mapping of operation name that are defined in dag files to their Operation instance."""
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/core/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import pytest

from tdp.core.cluster_status import ClusterStatus
from tdp.core.collection import CollectionReader
from tdp.core.collections import Collections
from tdp.core.dag import Dag
from tdp.core.variables import ClusterVariables
Expand Down Expand Up @@ -59,7 +58,7 @@ def mock_collections(tmp_path_factory: pytest.TempPathFactory) -> Collections:
},
}
generate_collection_at_path(path=temp_collection_path, dag=mock_dag, vars=mock_vars)
return Collections([CollectionReader.from_path(temp_collection_path)])
return Collections.from_collection_paths([temp_collection_path])


@pytest.fixture(scope="session")
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/core/models/test_deployment_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import pytest
from sqlalchemy import Engine

from tdp.core.collection import CollectionReader
from tdp.core.collections import (
Collections,
)
Expand Down Expand Up @@ -163,8 +162,9 @@ def test_multiple_host(self, tmp_path_factory: pytest.TempPathFactory):
]
}
generate_collection_at_path(collection_path, dag_service_operations, {})
collection = CollectionReader(collection_path, MockInventoryReader(hosts))
collections = Collections([collection])
collections = Collections.from_collection_paths(
[collection_path], MockInventoryReader(hosts)
)

deployment = DeploymentModel.from_operations(
collections, operations_names, hosts
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/core/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest

from tdp.core.collection import CollectionReader
from tdp.core.collections import Collections
from tests.conftest import generate_collection_at_path

Expand Down Expand Up @@ -45,9 +44,9 @@ def test_collections_from_collection_list(tmp_path_factory: pytest.TempPathFacto
collection_path_2, dag_service_operations_2, service_vars_2
)

collection1 = CollectionReader.from_path(collection_path_1)
collection2 = CollectionReader.from_path(collection_path_2)
collections = Collections([collection1, collection2])
collections = Collections.from_collection_paths(
[collection_path_1, collection_path_2]
)

assert collections.dag_operations is not None
assert "service1_install" in collections.dag_operations
Expand Down

0 comments on commit d53347f

Please sign in to comment.