Refactor nxos_file_copy module (#24314)

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2017-05-10 22:15:42 +05:30 committed by Trishna Guha
parent e9c2546ffe
commit 2f26c5285a
2 changed files with 14 additions and 19 deletions

View file

@ -99,6 +99,7 @@ try:
except ImportError: except ImportError:
HAS_SCP = False HAS_SCP = False
def execute_show_command(command, module, command_type='cli_show'): def execute_show_command(command, module, command_type='cli_show'):
if module.params['transport'] == 'cli': if module.params['transport'] == 'cli':
cmds = [command] cmds = [command]
@ -152,9 +153,6 @@ def enough_space(module):
def transfer_file(module, dest): def transfer_file(module, dest):
file_size = os.path.getsize(module.params['local_file']) file_size = os.path.getsize(module.params['local_file'])
if not local_file_exists(module):
module.fail_json(msg='Could not transfer file. Local file doesn\'t exist.')
if not enough_space(module): if not enough_space(module):
module.fail_json(msg='Could not transfer file. Not enough space on device.') module.fail_json(msg='Could not transfer file. Not enough space on device.')
@ -185,6 +183,7 @@ def transfer_file(module, dest):
'permissions are set.', temp_size=temp_size, 'permissions are set.', temp_size=temp_size,
file_size=file_size) file_size=file_size)
scp.close() scp.close()
ssh.close()
return True return True
@ -212,40 +211,37 @@ def main():
warnings = list() warnings = list()
check_args(module, warnings) check_args(module, warnings)
results = dict(changed=False, warnings=warnings)
local_file = module.params['local_file'] local_file = module.params['local_file']
remote_file = module.params['remote_file'] remote_file = module.params['remote_file']
file_system = module.params['file_system'] file_system = module.params['file_system']
changed = False results['transfer_status'] = 'No Transfer'
transfer_status = 'No Transfer' results['local_file'] = local_file
results['file_system'] = file_system
if not os.path.isfile(local_file): if not local_file_exists(module):
module.fail_json(msg="Local file {} not found".format(local_file)) module.fail_json(msg="Local file {} not found".format(local_file))
dest = remote_file or os.path.basename(local_file) dest = remote_file or os.path.basename(local_file)
remote_exists = remote_file_exists(module, dest, file_system=file_system) remote_exists = remote_file_exists(module, dest, file_system=file_system)
if not remote_exists: if not remote_exists:
changed = True results['changed'] = True
file_exists = False file_exists = False
else: else:
file_exists = True file_exists = True
if not module.check_mode and not file_exists: if not module.check_mode and not file_exists:
transfer_file(module, dest) transfer_file(module, dest)
transfer_status = 'Sent' results['transfer_status'] = 'Sent'
if remote_file is None: if remote_file is None:
remote_file = os.path.basename(local_file) remote_file = os.path.basename(local_file)
results['remote_file'] = remote_file
module.exit_json(changed=changed, module.exit_json(**results)
transfer_status=transfer_status,
local_file=local_file,
remote_file=remote_file,
warnings=warnings,
file_system=file_system)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

View file

@ -498,7 +498,6 @@ lib/ansible/modules/network/nxos/nxos_evpn_global.py
lib/ansible/modules/network/nxos/nxos_evpn_vni.py lib/ansible/modules/network/nxos/nxos_evpn_vni.py
lib/ansible/modules/network/nxos/nxos_facts.py lib/ansible/modules/network/nxos/nxos_facts.py
lib/ansible/modules/network/nxos/nxos_feature.py lib/ansible/modules/network/nxos/nxos_feature.py
lib/ansible/modules/network/nxos/nxos_file_copy.py
lib/ansible/modules/network/nxos/nxos_gir.py lib/ansible/modules/network/nxos/nxos_gir.py
lib/ansible/modules/network/nxos/nxos_gir_profile_management.py lib/ansible/modules/network/nxos/nxos_gir_profile_management.py
lib/ansible/modules/network/nxos/nxos_hsrp.py lib/ansible/modules/network/nxos/nxos_hsrp.py