From 261c40bac795e7a7e3eac1ed5515d5f577fa0e27 Mon Sep 17 00:00:00 2001 From: Adrian Likins Date: Tue, 11 Jul 2017 14:07:20 -0400 Subject: [PATCH] Fix azure module use of module_utils.facts (#25318) Module was importing '*' from facts to get to TimeoutError but that has moved to facts.timeout, so import is updated. Also rm old style imports to new style imports at the start of the module. 'signal' py module was used and referenced but never imported, presumably it was using the 'signal' previously imported into module_utils.facts. Now imported directly. 'AnsibleModule' was also from a * import, so now imported directly. A ref to 'module' was in _delete_disks_when_detached(), so now it is updated to raise an AzureException() with its message, and let its caller catch it and call module.fail_json() --- lib/ansible/modules/cloud/azure/azure.py | 12 ++++++++---- test/sanity/pep8/legacy-files.txt | 1 - 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/cloud/azure/azure.py b/lib/ansible/modules/cloud/azure/azure.py index 1a567a91a7..68c070872e 100644 --- a/lib/ansible/modules/cloud/azure/azure.py +++ b/lib/ansible/modules/cloud/azure/azure.py @@ -192,9 +192,11 @@ EXAMPLES = ''' import base64 import datetime import os +import signal import time from urlparse import urlparse -from ansible.module_utils.facts import * # TimeoutError +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.facts.timeout import TimeoutError AZURE_LOCATIONS = ['South Central US', 'Central US', @@ -279,6 +281,7 @@ except ImportError: from types import MethodType import json + def _wait_for_completion(azure, promise, wait_timeout, msg): if not promise: return @@ -291,6 +294,7 @@ def _wait_for_completion(azure, promise, wait_timeout, msg): raise AzureException('Timed out waiting for async operation ' + msg + ' "' + str(promise.request_id) + '" to complete.') + def _delete_disks_when_detached(azure, wait_timeout, disk_names): def _handle_timeout(signum, frame): raise TimeoutError("Timeout reached while waiting for disks to become detached.") @@ -305,10 +309,11 @@ def _delete_disks_when_detached(azure, wait_timeout, disk_names): azure.delete_disk(disk.name, True) disk_names.remove(disk_name) except AzureException as e: - module.fail_json(msg="failed to get or delete disk %s, error was: %s" % (disk_name, str(e))) + raise AzureException("failed to get or delete disk %s, error was: %s" % (disk_name, str(e))) finally: signal.alarm(0) + def get_ssh_certificate_tokens(module, ssh_cert_path): """ Returns the sha1 fingerprint and a base64-encoded PKCS12 version of the certificate. @@ -376,7 +381,7 @@ def create_virtual_machine(module, azure): disable_ssh_password_authentication = not password vm_config = LinuxConfigurationSet(hostname, user, password, disable_ssh_password_authentication) else: - #Create Windows Config + # Create Windows Config vm_config = WindowsConfigurationSet(hostname, password, None, module.params.get('auto_updates'), None, user) vm_config.domain_join = None if module.params.get('enable_winrm'): @@ -620,6 +625,5 @@ class Wrapper(object): # import module snippets -from ansible.module_utils.basic import * if __name__ == '__main__': main() diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 88334f81e7..b5bfdf4afc 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -66,7 +66,6 @@ lib/ansible/modules/cloud/amazon/sts_assume_role.py lib/ansible/modules/cloud/amazon/sts_session_token.py lib/ansible/modules/cloud/atomic/atomic_host.py lib/ansible/modules/cloud/atomic/atomic_image.py -lib/ansible/modules/cloud/azure/azure.py lib/ansible/modules/cloud/azure/azure_rm_deployment.py lib/ansible/modules/cloud/azure/azure_rm_networkinterface.py lib/ansible/modules/cloud/azure/azure_rm_networkinterface_facts.py