Add headers parameter to the maven_artifact module (#53116)

* Fix header parameter type
This commit is contained in:
Antoine Lucas 2019-03-13 15:19:30 +01:00 committed by Abhijeet Kasurde
parent a8e328f474
commit f4253459d6

View file

@ -61,6 +61,11 @@ options:
description: description:
- The password to authenticate with to the Maven Repository. Use AWS secret access key of the repository is hosted on S3 - The password to authenticate with to the Maven Repository. Use AWS secret access key of the repository is hosted on S3
aliases: [ "aws_secret_access_key" ] aliases: [ "aws_secret_access_key" ]
headers:
description:
- Add custom HTTP headers to a request in hash/dict format.
type: dict
version_added: "2.8"
dest: dest:
description: description:
- The path where the artifact should be written to - The path where the artifact should be written to
@ -278,12 +283,13 @@ class Artifact(object):
class MavenDownloader: class MavenDownloader:
def __init__(self, module, base="http://repo1.maven.org/maven2", local=False): def __init__(self, module, base="http://repo1.maven.org/maven2", local=False, headers=None):
self.module = module self.module = module
if base.endswith("/"): if base.endswith("/"):
base = base.rstrip("/") base = base.rstrip("/")
self.base = base self.base = base
self.local = local self.local = local
self.headers = headers
self.user_agent = "Maven Artifact Downloader/1.0" self.user_agent = "Maven Artifact Downloader/1.0"
self.latest_version_found = None self.latest_version_found = None
self.metadata_file_name = "maven-metadata-local.xml" if local else "maven-metadata.xml" self.metadata_file_name = "maven-metadata-local.xml" if local else "maven-metadata.xml"
@ -369,7 +375,7 @@ class MavenDownloader:
self.module.params['url_password'] = self.module.params.get('password', '') self.module.params['url_password'] = self.module.params.get('password', '')
self.module.params['http_agent'] = self.module.params.get('user_agent', None) self.module.params['http_agent'] = self.module.params.get('user_agent', None)
response, info = fetch_url(self.module, url_to_use, timeout=req_timeout) response, info = fetch_url(self.module, url_to_use, timeout=req_timeout, headers=self.headers)
if info['status'] == 200: if info['status'] == 200:
return response return response
if force: if force:
@ -457,6 +463,7 @@ def main():
repository_url=dict(default=None), repository_url=dict(default=None),
username=dict(default=None, aliases=['aws_secret_key']), username=dict(default=None, aliases=['aws_secret_key']),
password=dict(default=None, no_log=True, aliases=['aws_secret_access_key']), password=dict(default=None, no_log=True, aliases=['aws_secret_access_key']),
headers=dict(type='dict'),
state=dict(default="present", choices=["present", "absent"]), # TODO - Implement a "latest" state state=dict(default="present", choices=["present", "absent"]), # TODO - Implement a "latest" state
timeout=dict(default=10, type='int'), timeout=dict(default=10, type='int'),
dest=dict(type="path", required=True), dest=dict(type="path", required=True),
@ -489,6 +496,7 @@ def main():
version = module.params["version"] version = module.params["version"]
classifier = module.params["classifier"] classifier = module.params["classifier"]
extension = module.params["extension"] extension = module.params["extension"]
headers = module.params['headers']
state = module.params["state"] state = module.params["state"]
dest = module.params["dest"] dest = module.params["dest"]
b_dest = to_bytes(dest, errors='surrogate_or_strict') b_dest = to_bytes(dest, errors='surrogate_or_strict')
@ -497,7 +505,7 @@ def main():
verify_download = verify_checksum in ['download', 'always'] verify_download = verify_checksum in ['download', 'always']
verify_change = verify_checksum in ['change', 'always'] verify_change = verify_checksum in ['change', 'always']
downloader = MavenDownloader(module, repository_url, local) downloader = MavenDownloader(module, repository_url, local, headers)
try: try:
artifact = Artifact(group_id, artifact_id, version, classifier, extension) artifact = Artifact(group_id, artifact_id, version, classifier, extension)