-
Notifications
You must be signed in to change notification settings - Fork 6
/
memsource_job_targetfile.py
117 lines (95 loc) · 2.82 KB
/
memsource_job_targetfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
from __future__ import absolute_import, division, print_function
__metaclass__ = type
DOCUMENTATION = """
module: memsource_job_targetfile
short_description: Download a Memsource target file
version_added: 0.0.1
description:
- Download a Memsource target file
author: 'Yanis Guenane (@Spredzy)'
options:
project_uid:
description:
- UID of the project the job belongs to
required: true
type: str
job_uid:
description:
- UID of the job the target file is attached to
required: true
type: str
path:
description:
- Path where to install the downloaded file. If not specified
it will rely on filename and original directory provided by
the Memsource API
required: false
type: path
force:
description:
- Whether to force write the downloaded file
required: false
type: bool
extends_documentation_fragment:
- ansible.memsource.memsource
requirements: [memsource]
"""
EXAMPLES = """
- name: Download job target file and rely on filename and original directory name for dest
ansible.memsource.memsource_job_targetfile:
project_uid: xxx
jobs_uid: yyy
- name: Download job target file and write it in path
ansible.memsource.memsource_job_targetfile:
project_uid: xxx
jobs_uid: yyy
path: /tmp/foo
"""
RETURN = """
job_targetfile:
returned: on success
description: >
TBD
type: dict
"""
import os.path
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.ansible.memsource.plugins.module_utils.memsource import (
get_action,
get_default_argspec,
get_memsource_client,
)
def main():
argument_spec = get_default_argspec()
argument_spec.update(
dict(
project_uid=dict(type="str", required=True),
job_uid=dict(type="str", required=True),
path=dict(type="path"),
force=dict(type="bool"),
),
)
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False)
_memsource = get_memsource_client(module.params)
_result = {}
res = _memsource.get_job_targetfile(
module.params.get("job_uid"), module.params.get("project_uid")
)
job = _memsource.get_job_by_id(
module.params.get("job_uid"), module.params.get("project_uid")
)
if module.params.get("path") is not None:
_dest = module.params.get("path")
else:
_dest = "%s%s" % (job["originalFileDirectory"], job["filename"])
if os.path.exists(_dest) and not module.params.get("force", False):
pass
else:
content = "%s\n" % str(res.content.decode("utf-8")).split("\n\r\n")[0]
with open(_dest, "w+") as ftw:
ftw.write(content)
_result.update({"changed": True})
module.exit_json(**_result)
if __name__ == "__main__":
main()