Debian Codegraph @

lib/ansible/plugins/action/uri.py

raw
1 # -*- coding: utf-8 -*-
2 # (c) 2015, Brian Coca <[email protected]>
3 # (c) 2018, Matt Martz <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5
6 from __future__ import annotations
7
8 import collections.abc as _c
9 import os
10 from copy import deepcopy
11
12 from ansible.errors import AnsibleActionFail
13 from ansible.module_utils.parsing.convert_bool import boolean
14 from ansible.plugins.action import ActionBase
15
16
17 class ActionModule(ActionBase):
18
19 TRANSFERS_FILES = True
20
21 def run(self, tmp=None, task_vars=None):
22 self._supports_async = True
23 self._supports_check_mode = False
24
25 if task_vars is None:
26 task_vars = dict()
27
28 super(ActionModule, self).run(tmp, task_vars)
29 del tmp # tmp no longer has any effect
30
31 body_format = self._task.args.get('body_format', 'raw')
32 body = self._task.args.get('body')
33 src = self._task.args.get('src', None)
34 remote_src = boolean(self._task.args.get('remote_src', 'no'), strict=False)
35
36 try:
37 if remote_src:
38 # everything is remote, so we just execute the module
39 # without changing any of the module arguments
40 # call with ansible.legacy prefix to prevent collections collisions while allowing local override
41 return self._execute_module(module_name='ansible.legacy.uri', task_vars=task_vars, wrap_async=self._task.async_val)
42
43 kwargs = {}
44
45 if src:
46 src = self._find_needle('files', src)
47
48 tmp_src = self._connection._shell.join_path(self._connection._shell.tmpdir, os.path.basename(src))
49 kwargs['src'] = tmp_src
50 self._transfer_file(src, tmp_src)
51 self._fixup_perms2((self._connection._shell.tmpdir, tmp_src))
52 elif body_format == 'form-multipart':
53 if not isinstance(body, _c.Mapping):
54 raise AnsibleActionFail(
55 'body must be mapping, cannot be type %s' % body.__class__.__name__
56 )
57 new_body = deepcopy(body)
58 for field, value in new_body.items():
59 if not isinstance(value, _c.MutableMapping):
60 continue
61 content = value.get('content')
62 filename = value.get('filename')
63 if not filename or content:
64 continue
65
66 filename = self._find_needle('files', filename)
67
68 tmp_src = self._connection._shell.join_path(
69 self._connection._shell.tmpdir,
70 os.path.basename(filename)
71 )
72 value['filename'] = tmp_src
73 self._transfer_file(filename, tmp_src)
74 self._fixup_perms2((self._connection._shell.tmpdir, tmp_src))
75 kwargs['body'] = new_body
76
77 new_module_args = self._task.args | kwargs
78
79 # call with ansible.legacy prefix to prevent collections collisions while allowing local override
80 return self._execute_module('ansible.legacy.uri', module_args=new_module_args, task_vars=task_vars, wrap_async=self._task.async_val)
81 finally:
82 if not self._task.async_val:
83 self._remove_tmp_path(self._connection._shell.tmpdir)