From 2f26c5285aff05ed587454315e7df16524a40733 Mon Sep 17 00:00:00 2001 From: Abhijeet Kasurde Date: Wed, 10 May 2017 22:15:42 +0530 Subject: [PATCH] Refactor nxos_file_copy module (#24314) Signed-off-by: Abhijeet Kasurde --- .../modules/network/nxos/nxos_file_copy.py | 32 ++++++++----------- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_file_copy.py b/lib/ansible/modules/network/nxos/nxos_file_copy.py index 213dafa6ef3..2ea010f7201 100644 --- a/lib/ansible/modules/network/nxos/nxos_file_copy.py +++ b/lib/ansible/modules/network/nxos/nxos_file_copy.py @@ -99,6 +99,7 @@ try: except ImportError: HAS_SCP = False + def execute_show_command(command, module, command_type='cli_show'): if module.params['transport'] == 'cli': cmds = [command] @@ -152,9 +153,6 @@ def enough_space(module): def transfer_file(module, dest): 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): 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, file_size=file_size) scp.close() + ssh.close() return True @@ -206,46 +205,43 @@ def main(): if not HAS_SCP: module.fail_json( msg='library scp is required but does not appear to be ' - 'installed. It can be installed using `pip install scp`' - ) + 'installed. It can be installed using `pip install scp`' + ) warnings = list() check_args(module, warnings) + results = dict(changed=False, warnings=warnings) + local_file = module.params['local_file'] remote_file = module.params['remote_file'] file_system = module.params['file_system'] - changed = False - transfer_status = 'No Transfer' + results['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)) dest = remote_file or os.path.basename(local_file) remote_exists = remote_file_exists(module, dest, file_system=file_system) if not remote_exists: - changed = True + results['changed'] = True file_exists = False else: file_exists = True if not module.check_mode and not file_exists: transfer_file(module, dest) - transfer_status = 'Sent' + results['transfer_status'] = 'Sent' if remote_file is None: remote_file = os.path.basename(local_file) + results['remote_file'] = remote_file - module.exit_json(changed=changed, - transfer_status=transfer_status, - local_file=local_file, - remote_file=remote_file, - warnings=warnings, - file_system=file_system) - + module.exit_json(**results) if __name__ == '__main__': main() - diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 2d2ca8bbf9b..7634d4a6992 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -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_facts.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_profile_management.py lib/ansible/modules/network/nxos/nxos_hsrp.py