Skip to content

Commit

Permalink
Refactor container and python modules
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Jan 11, 2024
1 parent 6cd560e commit d9c3748
Show file tree
Hide file tree
Showing 112 changed files with 390,078 additions and 207,138 deletions.
15 changes: 12 additions & 3 deletions plugins/module_utils/pulp_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
try:
from packaging.requirements import SpecifierSet
from pulp_glue.common import __version__ as pulp_glue_version
from pulp_glue.common.context import PulpContext, PulpException
from pulp_glue.common.context import PulpContext, PulpException, PulpNoWait

GLUE_VERSION_SPEC = ">=0.20.0,<0.22.0"
if not SpecifierSet(GLUE_VERSION_SPEC).contains(pulp_glue_version):
Expand Down Expand Up @@ -112,7 +112,7 @@ def __exit__(self, exc_class, exc_value, tb):
if exc_class is None:
self.exit_json(changed=self._changed, **self._results)
else:
if issubclass(exc_class, (PulpException, SqueezerException)):
if issubclass(exc_class, (PulpException, PulpNoWait, SqueezerException)):
self.fail_json(msg=str(exc_value), changed=self._changed)
return True
elif issubclass(exc_class, Exception):
Expand Down Expand Up @@ -191,7 +191,7 @@ def process_present(self, entity, natural_key, desired_attributes):
entity = self.represent(entity)
self.set_changed()
else:
updated_attributes = {k: v for k, v in desired_attributes.items() if entity[k] != v}
updated_attributes = {k: v for k, v in desired_attributes.items() if entity.get(k) != v}
if updated_attributes:
if not self.check_mode:
self.context.update(body=updated_attributes)
Expand Down Expand Up @@ -235,6 +235,15 @@ def __init__(self, **kwargs):

super().__init__(argument_spec=argument_spec, **kwargs)

def represent(self, entity):
result = super().represent(entity)
result.pop("username", None)
result.pop("password", None)
result.pop("client_key", None)
result.pop("proxy_username", None)
result.pop("proxy_password", None)
return result

def process(self, natural_key, desired_attributes):
desired_attributes.update(
{
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/access_policy.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# copyright (c) 2021, Matthias Dellweg
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand Down
12 changes: 5 additions & 7 deletions plugins/modules/ansible_distribution.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# copyright (c) 2019, Matthias Dellweg
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand All @@ -23,7 +23,7 @@
required: false
base_path:
description:
- Base path to distribute a publication
- Base path to distribute a repository
type: str
required: false
repository:
Expand All @@ -40,7 +40,7 @@
content_guard:
description:
- Name of the content guard for the served content
- "Warning: This feature is not yet supported."
- Or the empty string to remove the content guard
type: str
required: false
extends_documentation_fragment:
Expand Down Expand Up @@ -132,9 +132,7 @@ def main():
version = module.params["version"]
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"] if module.params[key] is not None
}
Expand All @@ -157,7 +155,7 @@ def main():
)
desired_attributes["content_guard"] = content_guard_ctx.pulp_href
else:
desired_attributes["content_guard"] = None
desired_attributes["content_guard"] = ""

module.process(natural_key, desired_attributes)

Expand Down
1 change: 1 addition & 0 deletions plugins/modules/ansible_role.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# copyright (c) 2019, Matthias Dellweg
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand Down
1 change: 1 addition & 0 deletions plugins/modules/ansible_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# copyright (c) 2019, Matthias Dellweg
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/api_call.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# copyright (c) 2019, Matthias Dellweg
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand Down
55 changes: 35 additions & 20 deletions plugins/modules/container_distribution.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# copyright (c) 2021, Mark Goddard
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand Down Expand Up @@ -49,8 +49,9 @@
type: bool
required: false
extends_documentation_fragment:
- pulp.squeezer.pulp
- pulp.squeezer.pulp.entity_state
- pulp.squeezer.pulp.glue
- pulp.squeezer.pulp
author:
- Mark Goddard (@markgoddard)
"""
Expand Down Expand Up @@ -97,16 +98,29 @@
"""


from ansible_collections.pulp.squeezer.plugins.module_utils.pulp import (
PulpContainerDistribution,
PulpContainerRepository,
PulpContentGuard,
PulpEntityAnsibleModule,
)
import traceback

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

try:
from pulp_glue.container.context import (
PulpContainerDistributionContext,
PulpContainerRepositoryContext,
)
from pulp_glue.core.context import PulpContentGuardContext

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


def main():
with PulpEntityAnsibleModule(
context_class=PulpContainerDistributionContext,
entity_singular="distribution",
entity_plural="distribuions",
import_errors=[("pulp-glue", PULP_CLI_IMPORT_ERR)],
argument_spec=dict(
name=dict(),
base_path=dict(),
Expand All @@ -133,25 +147,26 @@ def main():
}

if repository_name:
repository = PulpContainerRepository(module, {"name": repository_name})
repository.find(failsafe=False)
# TODO check if version exists
repository_ctx = PulpContainerRepositoryContext(
module.pulp_ctx, entity={"name": repository_name}
)
if version:
desired_attributes["repository_version"] = repository.entity[
"versions_href"
] + "{version}/".format(version=version)
desired_attributes["repository_version"] = (
repository_ctx.entity["versions_href"] + f"{version}/"
)
else:
desired_attributes["repository"] = repository.href
desired_attributes["repository"] = repository_ctx.pulp_href

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"] = ""

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


if __name__ == "__main__":
Expand Down
50 changes: 18 additions & 32 deletions plugins/modules/container_remote.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# copyright (c) 2021, Mark Goddard
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand Down Expand Up @@ -39,9 +39,10 @@
type: list
elements: str
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:
- Mark Goddard (@markgoddard)
"""
Expand Down Expand Up @@ -88,14 +89,23 @@
"""


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

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

try:
from pulp_glue.container.context import PulpContainerRemoteContext

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


def main():
with PulpRemoteAnsibleModule(
context_class=PulpContainerRemoteContext,
import_errors=[("pulp-glue", PULP_CLI_IMPORT_ERR)],
argument_spec=dict(
exclude_tags=dict(type="list", elements="str"),
include_tags=dict(type="list", elements="str"),
Expand All @@ -111,38 +121,14 @@ def main():
desired_attributes = {
key: module.params[key]
for key in [
"url",
"exclude_tags",
"include_tags",
"download_concurrency",
"policy",
"tls_validation",
"upstream_name",
]
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
}
)

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


if __name__ == "__main__":
Expand Down
34 changes: 24 additions & 10 deletions plugins/modules/container_repository.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

# copyright (c) 2021, Mark Goddard
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)


from __future__ import absolute_import, division, print_function

__metaclass__ = type
Expand All @@ -25,8 +25,9 @@
- Description of the repository
type: str
extends_documentation_fragment:
- pulp.squeezer.pulp
- pulp.squeezer.pulp.entity_state
- pulp.squeezer.pulp.glue
- pulp.squeezer.pulp
author:
- Mark Goddard (@markgoddard)
"""
Expand Down Expand Up @@ -72,24 +73,37 @@
"""


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

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

try:
from pulp_glue.container.context import PulpContainerRepositoryContext

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


def main():
with PulpEntityAnsibleModule(
argument_spec=dict(name=dict(), description=dict()),
context_class=PulpContainerRepositoryContext,
entity_singular="repository",
entity_plural="repositories",
import_errors=[("pulp-glue", PULP_CLI_IMPORT_ERR)],
argument_spec=dict(
name=dict(),
description=dict(),
),
required_if=[("state", "present", ["name"]), ("state", "absent", ["name"])],
) as module:
natural_key = {"name": module.params["name"]}
desired_attributes = {}
if module.params["description"] is not None:
# In case of an empty string we nullify the description
desired_attributes["description"] = module.params["description"] or None
desired_attributes["description"] = module.params["description"]

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


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit d9c3748

Please sign in to comment.