Skip to content

Commit

Permalink
Rewrite rpm modules to use glue
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Oct 6, 2024
1 parent d74a57c commit 395c8eb
Show file tree
Hide file tree
Showing 63 changed files with 166,973 additions and 55,100 deletions.
10 changes: 1 addition & 9 deletions plugins/module_utils/pulp_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@
PULP_CLI_IMPORT_ERR = None
except ImportError:
PULP_CLI_IMPORT_ERR = traceback.format_exc()
else:

class PulpSqueezerContext(PulpContext):
def prompt(self, *args, **kwargs):
pass

def echo(self, *args, **kwargs):
pass


class SqueezerException(Exception):
Expand Down Expand Up @@ -93,7 +85,7 @@ def __init__(self, **kwargs):
password=self.params["password"],
)

self.pulp_ctx = PulpSqueezerContext(
self.pulp_ctx = PulpContext(
api_root="/pulp/",
api_kwargs=dict(
base_url=self.params["pulp_url"],
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/ansible_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ def main():
},
required_if=[("state", "present", ["name"]), ("state", "absent", ["name"])],
) as module:
# We need to trick this thing into polymorphism here...
if module.params["content_type"] == "collection":
module.context = PulpAnsibleCollectionRemoteContext(module.pulp_ctx)
else:
Expand Down
12 changes: 9 additions & 3 deletions plugins/modules/api_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@

from ansible_collections.pulp.squeezer.plugins.module_utils.pulp_glue import PulpAnsibleModule

try:
from pulp_glue.common.context import NotImplementedFake
except ImportError:
NotImplementedFake = None


def main():
with PulpAnsibleModule(
Expand All @@ -66,10 +71,11 @@ def main():
body = module.params["body"]
if module.pulp_ctx.api.operations[operation_id][0].upper() not in ["GET", "HEAD"]:
module.set_changed()
try:
response = module.pulp_ctx.call(operation_id, parameters=parameters, body=body)
except NotImplementedFake:
if module.check_mode:
module.set_result("response", None)
return
response = module.pulp_ctx.call(operation_id, parameters=parameters, body=body)
response = None
module.set_result("response", response)


Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/file_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ def main():
) as module:
content_guard_name = module.params["content_guard"]

natural_key = {
"name": module.params["name"],
}
natural_key = {"name": module.params["name"]}
desired_attributes = {
key: module.params[key]
for key in ["base_path", "publication"]
Expand Down
5 changes: 2 additions & 3 deletions plugins/modules/file_publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ def main():
repository_ctx = PulpFileRepositoryContext(
module.pulp_ctx, entity={"name": repository_name}
)
repository = repository_ctx.entity
# TODO check if version exists
if version:
repository_version_href = repository["versions_href"] + f"{version}/"
repository_version_href = repository_ctx.entity["versions_href"] + f"{version}/"
else:
repository_version_href = repository["latest_version_href"]
repository_version_href = repository_ctx.entity["latest_version_href"]
natural_key = {"repository_version": repository_version_href}
else:
natural_key = {"repository_version": None}
Expand Down
53 changes: 32 additions & 21 deletions plugins/modules/rpm_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@
required: false
version_added: "0.0.16"
extends_documentation_fragment:
- pulp.squeezer.pulp
- pulp.squeezer.pulp.entity_state
- pulp.squeezer.pulp.glue
- pulp.squeezer.pulp
author:
- Jacob Floyd (@cognifloyd)
"""
Expand Down Expand Up @@ -110,16 +111,26 @@
"""


from ansible_collections.pulp.squeezer.plugins.module_utils.pulp import (
PulpContentGuard,
PulpEntityAnsibleModule,
PulpRpmDistribution,
PulpRpmRepository,
)
import traceback

from ansible_collections.pulp.squeezer.plugins.module_utils.pulp_glue import PulpEntityAnsibleModule

try:
from pulp_glue.core.context import PulpContentGuardContext
from pulp_glue.rpm.context import PulpRpmDistributionContext, PulpRpmRepositoryContext

PULP_CLI_IMPORT_ERR = None
except ImportError:
PULP_CLI_IMPORT_ERR = traceback.format_exc()
PulpRpmDistributionContext = None


def main():
with PulpEntityAnsibleModule(
context_class=PulpRpmDistributionContext,
entity_singular="distribution",
entity_plural="distribuions",
import_errors=[("pulp-glue", PULP_CLI_IMPORT_ERR)],
argument_spec={
"name": {},
"base_path": {},
Expand All @@ -145,30 +156,30 @@ def main():
"base_path",
"generate_repo_config",
"publication",
"repository",
"pulp_labels",
]
if module.params[key] is not None
}

# support switching between using publication and repository
if module.params["publication"] is not None:
desired_attributes["repository"] = None
elif repository_name is not None:
desired_attributes["publication"] = None
repository = PulpRpmRepository(module, {"name": repository_name})
repository.find(failsafe=False)
desired_attributes["repository"] = repository.href
if repository_name is not None:
if repository_name:
repository_ctx = PulpRpmRepositoryContext(
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 = PulpContentGuard(module, {"name": content_guard_name})
content_guard.find(failsafe=False)
desired_attributes["content_guard"] = content_guard.href
content_guard_ctx = PulpContentGuardContext(
module.pulp_ctx, entity={"name": content_guard_name}
)
desired_attributes["content_guard"] = content_guard_ctx.pulp_href
else:
desired_attributes["content_guard"] = None
desired_attributes["content_guard"] = ""

PulpRpmDistribution(module, natural_key, desired_attributes).process()
module.process(natural_key, desired_attributes)


if __name__ == "__main__":
Expand Down
38 changes: 25 additions & 13 deletions plugins/modules/rpm_publication.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
type: int
required: false
extends_documentation_fragment:
- pulp.squeezer.pulp
- pulp.squeezer.pulp.entity_state
- pulp.squeezer.pulp.glue
- pulp.squeezer.pulp
author:
- Jacob Floyd (@cognifloyd)
"""
Expand Down Expand Up @@ -68,15 +69,25 @@
"""


from ansible_collections.pulp.squeezer.plugins.module_utils.pulp import (
PulpEntityAnsibleModule,
PulpRpmPublication,
PulpRpmRepository,
)
import traceback

from ansible_collections.pulp.squeezer.plugins.module_utils.pulp_glue import PulpEntityAnsibleModule

try:
from pulp_glue.rpm.context import PulpRpmPublicationContext, PulpRpmRepositoryContext

PULP_CLI_IMPORT_ERR = None
except ImportError:
PULP_CLI_IMPORT_ERR = traceback.format_exc()
PulpRpmPublicationContext = None


def main():
with PulpEntityAnsibleModule(
context_class=PulpRpmPublicationContext,
entity_singular="publication",
entity_plural="publications",
import_errors=[("pulp-glue", PULP_CLI_IMPORT_ERR)],
argument_spec={
"repository": {},
"version": {"type": "int"},
Expand All @@ -91,20 +102,21 @@ def main():
desired_attributes = {}

if repository_name:
repository = PulpRpmRepository(module, {"name": repository_name})
repository.find(failsafe=False)
repository_ctx = PulpRpmRepositoryContext(
module.pulp_ctx, entity={"name": repository_name}
)
# TODO check if version exists
if version:
repository_version_href = repository.entity["versions_href"] + "{version}/".format(
version=version
)
repository_version_href = repository_ctx.entity[
"versions_href"
] + "{version}/".format(version=version)
else:
repository_version_href = repository.entity["latest_version_href"]
repository_version_href = repository_ctx.entity["latest_version_href"]
natural_key = {"repository_version": repository_version_href}
else:
natural_key = {"repository_version": None}

PulpRpmPublication(module, natural_key, desired_attributes).process()
module.process(natural_key, desired_attributes)


if __name__ == "__main__":
Expand Down
51 changes: 18 additions & 33 deletions plugins/modules/rpm_remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
- on_demand
- streamed
extends_documentation_fragment:
- pulp.squeezer.pulp
- pulp.squeezer.pulp.entity_state
- pulp.squeezer.pulp.remote
- pulp.squeezer.pulp.entity_state
- pulp.squeezer.pulp.glue
- pulp.squeezer.pulp
author:
- Jacob Floyd (@cognifloyd)
"""
Expand Down Expand Up @@ -69,47 +70,31 @@
"""


from ansible_collections.pulp.squeezer.plugins.module_utils.pulp import (
PulpRemoteAnsibleModule,
PulpRpmRemote,
)
import traceback

from ansible_collections.pulp.squeezer.plugins.module_utils.pulp_glue import PulpRemoteAnsibleModule

try:
from pulp_glue.rpm.context import PulpRpmRemoteContext

PULP_CLI_IMPORT_ERR = None
except ImportError:
PULP_CLI_IMPORT_ERR = traceback.format_exc()
PulpRpmRemoteContext = None


def main():
with PulpRemoteAnsibleModule(
context_class=PulpRpmRemoteContext,
import_errors=[("pulp-glue", PULP_CLI_IMPORT_ERR)],
argument_spec={
"policy": {"choices": ["immediate", "on_demand", "streamed"]},
},
required_if=[("state", "present", ["name"]), ("state", "absent", ["name"])],
) as module:
natural_key = {"name": module.params["name"]}
desired_attributes = {
key: module.params[key]
for key in ["url", "download_concurrency", "policy", "tls_validation"]
if module.params[key] is not None
}

# Nullifiable values
if module.params["remote_username"] is not None:
desired_attributes["username"] = module.params["remote_username"] or None
if module.params["remote_password"] is not None:
desired_attributes["password"] = module.params["remote_password"] or None
desired_attributes.update(
{
key: module.params[key] or None
for key in [
"proxy_url",
"proxy_username",
"proxy_password",
"ca_cert",
"client_cert",
"client_key",
]
if module.params[key] is not None
}
)

PulpRpmRemote(module, natural_key, desired_attributes).process()

module.process(natural_key, {})


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 395c8eb

Please sign in to comment.