Skip to content

Commit

Permalink
Merge pull request #38 from Tompage1994/repo_req_file
Browse files Browse the repository at this point in the history
Add ability to use requirements file for repo
  • Loading branch information
sean-m-sullivan authored Jul 19, 2021
2 parents ab06e98 + 1b9db2d commit 7006538
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
5 changes: 5 additions & 0 deletions playbooks/collection_requirement_file.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
- name: redhat_cop.ah_configuration
- name: redhat_cop.tower_configuration
- name: ansible.windows
...
6 changes: 6 additions & 0 deletions playbooks/testing_playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
- name: redhat_cop.ah_configuration
- name: redhat_cop.tower_configuration

- name: Configure community repo from file
ah_repository:
name: community
url: https://galaxy.ansible.com/api/
requirements_file: collection_requirement_file.yml

- name: Sync community repo
ah_repository_sync:
name: community
Expand Down
5 changes: 5 additions & 0 deletions plugins/module_utils/ah_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,11 @@ def prepare_multipart(self, filename):

return (parser(headers)["content-type"], b_content) # Message converts to native strings

def getFileContent(self, path):
with open(to_bytes(path, errors="surrogate_or_strict"), "rb") as f:
b_file_data = f.read()
return to_text(b_file_data)

def upload(self, path, endpoint, auto_exit=True, item_type="unknown"):
ct, body = self.prepare_multipart(path)
response = self.make_request("POST", endpoint, **{"data": body, "headers": {"Content-Type": str(ct)}, "binary": True, "return_errors_on_404": True})
Expand Down
23 changes: 20 additions & 3 deletions plugins/modules/ah_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
description:
- Requirements to download from remote.
type: list
requirements_file:
description:
- A yaml requirements file to download from remote.
type: str
proxy_url:
description:
- Proxy URL to use for the connection
Expand Down Expand Up @@ -88,6 +92,12 @@
requirements:
- redhat_cop.ah_configuration
- redhat_cop.tower_configuration
- name: Configure community repo from a file
ah_repository:
name: community
url: https://galaxy.ansible.com/api/
requirements_file: "/tmp/requirements.yml"
"""

from ..module_utils.ah_module import AHModule
Expand All @@ -103,23 +113,30 @@ def main():
username=dict(),
password=dict(),
requirements=dict(type="list", elements="str"),
requirements_file=dict(),
proxy_url=dict(),
proxy_username=dict(),
proxy_password=dict(),
download_concurrency=dict(default="10"),
)

mutually_exclusive=[('requirements','requirements_file')]

# Create a module for ourselves
module = AHModule(argument_spec=argument_spec)
module = AHModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive)

# Extract our parameters
name = module.params.get("name")
new_fields = {}

requirements = module.params.get("requirements")
if requirements:
requirements_file = "\n - ".join(requirements)
new_fields["requirements_file"] = "---\ncollections:\n - " + requirements_file
requirements_content = "\n - ".join(requirements)
new_fields["requirements_file"] = "---\ncollections:\n - " + requirements_content

requirements_file = module.params.get("requirements_file")
if requirements_file:
new_fields["requirements_file"] = module.getFileContent(requirements_file)

for field_name in (
"url",
Expand Down

0 comments on commit 7006538

Please sign in to comment.