diff --git a/plugins/modules/deb_distribution.py b/plugins/modules/deb_distribution.py index 48644ab..1ba1938 100644 --- a/plugins/modules/deb_distribution.py +++ b/plugins/modules/deb_distribution.py @@ -26,6 +26,12 @@ - Href of the publication to be served type: str required: false + repository: + description: + - Name of the repository whose latest version should be served + type: str + required: false + version_added: "0.5.0" content_guard: description: - Name of the content guard for the served content @@ -60,6 +66,16 @@ publication: /pub/api/v3/publications/deb/deb/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa/ state: present +- name: Create a deb distribution that tracks the latest repository version + pulp.squeezer.deb_distribution: + pulp_url: https://pulp.example.org + username: admin + password: password + name: latest_deb_distribution + base_path: latest/deb/dist + repository: my_deb_repository + state: present + - name: Delete a deb distribution pulp.squeezer.deb_distribution: pulp_url: https://pulp.example.org @@ -98,13 +114,14 @@ try: from pulp_glue.deb import __version__ as pulp_glue_deb_version - from pulp_glue.deb.context import PulpAptDistributionContext + from pulp_glue.deb.context import PulpAptDistributionContext, PulpAptRepositoryContext assert_version(GLUE_DEB_VERSION_SPEC, pulp_glue_deb_version, "pulp-glue-deb") PULP_GLUE_DEB_IMPORT_ERR = None except ImportError: PULP_GLUE_DEB_IMPORT_ERR = traceback.format_exc() PulpAptDistributionContext = None + PulpAptRepositoryContext = None def main(): @@ -120,14 +137,17 @@ def main(): "name": {}, "base_path": {}, "publication": {}, + "repository": {}, "content_guard": {}, }, required_if=[ ("state", "present", ["name", "base_path"]), ("state", "absent", ["name"]), ], + mutually_exclusive=[("publication", "repository")], ) as module: content_guard_name = module.params["content_guard"] + repository_name = module.params["repository"] natural_key = {"name": module.params["name"]} desired_attributes = { @@ -136,6 +156,15 @@ def main(): if module.params[key] is not None } + if repository_name is not None: + if repository_name: + repository_ctx = PulpAptRepositoryContext( + module.pulp_ctx, entity={"name": repository_name} + ) + desired_attributes["repository"] = repository_ctx.pulp_href + else: + desired_attributes["repository"] = "" + if content_guard_name is not None: if content_guard_name: content_guard_ctx = PulpContentGuardContext( diff --git a/tests/test_deb_distribution.py b/tests/test_deb_distribution.py new file mode 100644 index 0000000..b421870 --- /dev/null +++ b/tests/test_deb_distribution.py @@ -0,0 +1,64 @@ +from ansible_collections.pulp.squeezer.plugins.modules import deb_distribution + + +class FakeModule: + def __init__(self, repository): + self.params = { + "name": "test_deb_distribution", + "base_path": "test_deb_base_path", + "publication": None, + "repository": repository, + "content_guard": None, + } + self.pulp_ctx = object() + self.process_args = None + + def __enter__(self): + return self + + def __exit__(self, *_args): + return None + + def process(self, natural_key, desired_attributes): + self.process_args = (natural_key, desired_attributes) + + +def test_repository_distribution_resolves_repository_name(monkeypatch): + module = FakeModule("test_deb_repository") + module_kwargs = {} + + def module_factory(**kwargs): + module_kwargs.update(kwargs) + return module + + class FakeRepositoryContext: + def __init__(self, pulp_ctx, entity): + assert pulp_ctx is module.pulp_ctx + assert entity == {"name": "test_deb_repository"} + self.pulp_href = "/pulp/api/v3/repositories/deb/apt/test/" + + monkeypatch.setattr(deb_distribution, "PulpEntityAnsibleModule", module_factory) + monkeypatch.setattr(deb_distribution, "PulpAptRepositoryContext", FakeRepositoryContext) + + deb_distribution.main() + + assert module_kwargs["mutually_exclusive"] == [("publication", "repository")] + assert module.process_args == ( + {"name": "test_deb_distribution"}, + { + "base_path": "test_deb_base_path", + "repository": "/pulp/api/v3/repositories/deb/apt/test/", + }, + ) + + +def test_repository_distribution_can_clear_repository(monkeypatch): + module = FakeModule("") + monkeypatch.setattr(deb_distribution, "PulpEntityAnsibleModule", lambda **_kwargs: module) + + deb_distribution.main() + + assert module.process_args == ( + {"name": "test_deb_distribution"}, + {"base_path": "test_deb_base_path", "repository": ""}, + )