From 1f99382dfb395c1b993b2812122761371da1bad6 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 11 May 2015 13:21:17 -0700 Subject: [PATCH] Finish up cleanups to modules: * Add python>= 2.6 to documented requirements when a module's deps need python>= 2.6 so we know when a module can use python2.6+ syntax * Remove BabyJSON usage * Change modules to use if __name__ == '__main__' so that they can potentially be unittested The BabJSON changes Fixes #1211 --- cloud/openstack/_nova_compute.py | 15 ++++++++---- cloud/openstack/_quantum_subnet.py | 14 ++++++++--- cloud/openstack/glance_image.py | 23 +++++++++++++++---- cloud/openstack/keystone_user.py | 4 +++- cloud/openstack/nova_keypair.py | 14 +++++++---- cloud/openstack/os_auth.py | 8 +++++-- cloud/openstack/os_server.py | 7 ++++-- cloud/openstack/os_server_facts.py | 10 +++++--- cloud/openstack/os_server_volume.py | 7 ++++-- cloud/openstack/os_subnet.py | 7 ++++-- cloud/openstack/os_volume.py | 7 ++++-- cloud/openstack/quantum_floating_ip.py | 18 +++++++++++---- .../quantum_floating_ip_associate.py | 17 ++++++++++---- cloud/openstack/quantum_network.py | 14 ++++++++--- cloud/openstack/quantum_router.py | 13 ++++++++--- cloud/openstack/quantum_router_gateway.py | 14 ++++++++--- cloud/openstack/quantum_router_interface.py | 14 ++++++++--- 17 files changed, 157 insertions(+), 49 deletions(-) diff --git a/cloud/openstack/_nova_compute.py b/cloud/openstack/_nova_compute.py index 369281bc42b..00380de32e5 100644 --- a/cloud/openstack/_nova_compute.py +++ b/cloud/openstack/_nova_compute.py @@ -19,15 +19,16 @@ import operator import os +import time try: from novaclient.v1_1 import client as nova_client from novaclient.v1_1 import floating_ips from novaclient import exceptions from novaclient import utils - import time + HAS_NOVACLIENT = True except ImportError: - print("failed=True msg='novaclient is required for this module'") + HAS_NOVACLIENT = False DOCUMENTATION = ''' --- @@ -175,7 +176,9 @@ options: required: false default: None version_added: "1.9" -requirements: ["novaclient"] +requirements: + - "python >= 2.6" + - "python-novaclient" ''' EXAMPLES = ''' @@ -563,6 +566,9 @@ def main(): ], ) + if not HAS_NOVACLIENT: + module.fail_json(msg='python-novaclient is required for this module') + nova = nova_client.Client(module.params['login_username'], module.params['login_password'], module.params['login_tenant_name'], @@ -589,4 +595,5 @@ def main(): # this is magic, see lib/ansible/module_common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/_quantum_subnet.py b/cloud/openstack/_quantum_subnet.py index 19e079adf72..105ca32c582 100644 --- a/cloud/openstack/_quantum_subnet.py +++ b/cloud/openstack/_quantum_subnet.py @@ -22,8 +22,9 @@ try: except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient + HAVE_DEPS = True except ImportError: - print("failed=True msg='quantumclient (or neutronclient) and keystoneclient are required'") + HAVE_DEPS = False DOCUMENTATION = ''' --- @@ -115,7 +116,10 @@ options: - From the subnet pool the last IP that should be assigned to the virtual machines required: false default: None -requirements: ["quantumclient", "neutronclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-neutronclient or python-quantumclient" + - "python-keystoneclient" ''' EXAMPLES = ''' @@ -268,6 +272,9 @@ def main(): allocation_pool_end = dict(default=None), )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_DEPS: + module.fail_json(msg='python-keystoneclient and either python-neutronclient or python-quantumclient are required') + neutron = _get_neutron_client(module, module.params) _set_tenant_id(module) if module.params['state'] == 'present': @@ -288,5 +295,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/glance_image.py b/cloud/openstack/glance_image.py index 6425fa2ca5d..97b89f03484 100644 --- a/cloud/openstack/glance_image.py +++ b/cloud/openstack/glance_image.py @@ -111,7 +111,10 @@ options: required: false default: publicURL version_added: "1.7" -requirements: ["glanceclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-glanceclient" + - "python-keystoneclient" ''' @@ -130,9 +133,14 @@ EXAMPLES = ''' import time try: import glanceclient - from keystoneclient.v2_0 import client as ksclient + HAS_GLANCECLIENT = True except ImportError: - print("failed=True msg='glanceclient and keystone client are required'") + HAS_GLANCECLIENT = False +try: + from keystoneclient.v2_0 import client as ksclient + HAS_KEYSTONECLIENT = True +except ImportError: + HAS_KEYSTONECLIENT= False def _get_ksclient(module, kwargs): @@ -237,6 +245,12 @@ def main(): argument_spec=argument_spec, mutually_exclusive = [['file','copy_from']], ) + + if not HAVE_GLANCECLIENT: + module.fail_json(msg='python-glanceclient is required for this module') + if not HAVE_KEYSTONECLIENT: + module.fail_json(msg='python-keystoneclient is required for this module') + if module.params['state'] == 'present': if not module.params['file'] and not module.params['copy_from']: module.fail_json(msg="Either file or copy_from variable should be set to create the image") @@ -257,4 +271,5 @@ def main(): # this is magic, see lib/ansible/module_common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/keystone_user.py b/cloud/openstack/keystone_user.py index 4af254bfe6d..89afe53fbd4 100644 --- a/cloud/openstack/keystone_user.py +++ b/cloud/openstack/keystone_user.py @@ -72,7 +72,9 @@ options: - Indicate desired state of the resource choices: ['present', 'absent'] default: present -requirements: [ python-keystoneclient ] +requirements: + - "python >= 2.6" + - python-keystoneclient author: Lorin Hochstein ''' diff --git a/cloud/openstack/nova_keypair.py b/cloud/openstack/nova_keypair.py index c7c9affb3e6..686484cf37f 100644 --- a/cloud/openstack/nova_keypair.py +++ b/cloud/openstack/nova_keypair.py @@ -17,12 +17,13 @@ # You should have received a copy of the GNU General Public License # along with this software. If not, see . +import time try: from novaclient.v1_1 import client as nova_client from novaclient import exceptions as exc - import time + HAS_NOVACLIENT = True except ImportError: - print("failed=True msg='novaclient is required for this module to work'") + HAS_NOVACLIENT = False DOCUMENTATION = ''' --- @@ -73,7 +74,9 @@ options: required: false default: None -requirements: ["novaclient"] +requirements: + - "python >= 2.6" + - "python-novaclient" ''' EXAMPLES = ''' # Creates a key pair with the running users public key @@ -94,6 +97,8 @@ def main(): state = dict(default='present', choices=['absent', 'present']) )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_NOVACLIENT: + module.fail_json(msg='python-novaclient is required for this module to work') nova = nova_client.Client(module.params['login_username'], module.params['login_password'], @@ -135,5 +140,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/os_auth.py b/cloud/openstack/os_auth.py index 87e5b465082..ec0e8414fd2 100644 --- a/cloud/openstack/os_auth.py +++ b/cloud/openstack/os_auth.py @@ -28,7 +28,10 @@ module: os_auth short_description: Retrieve an auth token version_added: "2.0" description: - - Retrieve an auth token from an OpenStack Cloud + - Retrieve an auth token from an OpenStack Cloud +requirements: + - "python >= 2.6" + - "shade" extends_documentation_fragment: openstack ''' @@ -61,4 +64,5 @@ def main(): # this is magic, see lib/ansible/module_common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/os_server.py b/cloud/openstack/os_server.py index 195d114ee46..a14c738be4a 100644 --- a/cloud/openstack/os_server.py +++ b/cloud/openstack/os_server.py @@ -140,7 +140,9 @@ options: - Should the resource be present or absent. choices: [present, absent] default: present -requirements: ["shade"] +requirements: + - "python >= 2.6" + - "shade" ''' EXAMPLES = ''' @@ -443,4 +445,5 @@ def main(): # this is magic, see lib/ansible/module_common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/os_server_facts.py b/cloud/openstack/os_server_facts.py index ee18f19536f..fee14c7456c 100644 --- a/cloud/openstack/os_server_facts.py +++ b/cloud/openstack/os_server_facts.py @@ -28,9 +28,12 @@ module: os_server_facts short_description: Retrieve facts about a compute instance version_added: "2.0" description: - - Retrieve facts about a server instance from OpenStack. + - Retrieve facts about a server instance from OpenStack. notes: - - Facts are placed in the C(openstack) variable. + - Facts are placed in the C(openstack) variable. +requirements: + - "python >= 2.6" + - "shade" options: server: description: @@ -71,5 +74,6 @@ def main(): # this is magic, see lib/ansible/module_common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/os_server_volume.py b/cloud/openstack/os_server_volume.py index a041a070c6e..47e1f433853 100644 --- a/cloud/openstack/os_server_volume.py +++ b/cloud/openstack/os_server_volume.py @@ -53,7 +53,9 @@ options: - Device you want to attach. Defaults to auto finding a device name. required: false default: None -requirements: ["shade"] +requirements: + - "python >= 2.6" + - "shade" ''' EXAMPLES = ''' @@ -148,4 +150,5 @@ def main(): # this is magic, see lib/ansible/module_utils/common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/os_subnet.py b/cloud/openstack/os_subnet.py index d9a5c99119c..61152d75456 100644 --- a/cloud/openstack/os_subnet.py +++ b/cloud/openstack/os_subnet.py @@ -91,7 +91,9 @@ options: - A list of host route dictionaries for the subnet. required: false default: None -requirements: ["shade"] +requirements: + - "python >= 2.6" + - "shade" ''' EXAMPLES = ''' @@ -251,4 +253,5 @@ def main(): # this is magic, see lib/ansible/module_common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ '__main__': + main() diff --git a/cloud/openstack/os_volume.py b/cloud/openstack/os_volume.py index bd3bf637d75..d5baffb96c6 100644 --- a/cloud/openstack/os_volume.py +++ b/cloud/openstack/os_volume.py @@ -66,7 +66,9 @@ options: - Should the resource be present or absent. choices: [present, absent] default: present -requirements: ["shade"] +requirements: + - "python >= 2.6" + - "shade" ''' EXAMPLES = ''' @@ -155,4 +157,5 @@ def main(): # this is magic, see lib/ansible/module_common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/quantum_floating_ip.py b/cloud/openstack/quantum_floating_ip.py index a9f440d353d..e89f23caa79 100644 --- a/cloud/openstack/quantum_floating_ip.py +++ b/cloud/openstack/quantum_floating_ip.py @@ -16,6 +16,8 @@ # You should have received a copy of the GNU General Public License # along with this software. If not, see . +import time + try: from novaclient.v1_1 import client as nova_client try: @@ -23,9 +25,9 @@ try: except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient - import time + HAVE_DEPS = True except ImportError: - print("failed=True msg='novaclient,keystoneclient and quantumclient (or neutronclient) are required'") + HAVE_DEPS = False DOCUMENTATION = ''' --- @@ -81,7 +83,11 @@ options: required: false default: None version_added: "1.5" -requirements: ["novaclient", "quantumclient", "neutronclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-novaclient" + - "python-neutronclient or python-quantumclient" + - "python-keystoneclient" ''' EXAMPLES = ''' @@ -239,6 +245,9 @@ def main(): )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_DEPS: + module.fail_json(msg='python-novaclient, python-keystoneclient, and either python-neutronclient or python-quantumclient are required') + try: nova = nova_client.Client(module.params['login_username'], module.params['login_password'], module.params['login_tenant_name'], module.params['auth_url'], region_name=module.params['region_name'], service_type='compute') @@ -272,5 +281,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/quantum_floating_ip_associate.py b/cloud/openstack/quantum_floating_ip_associate.py index 91df2690b62..b7e9f71e5fd 100644 --- a/cloud/openstack/quantum_floating_ip_associate.py +++ b/cloud/openstack/quantum_floating_ip_associate.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this software. If not, see . +import time try: from novaclient.v1_1 import client as nova_client try: @@ -23,9 +24,9 @@ try: except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient - import time + HAVE_DEPS = True except ImportError: - print "failed=True msg='novaclient, keystone, and quantumclient (or neutronclient) client are required'" + HAVE_DEPS = False DOCUMENTATION = ''' --- @@ -75,7 +76,11 @@ options: - floating ip that should be assigned to the instance required: true default: None -requirements: ["quantumclient", "neutronclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-novaclient" + - "python-neutronclient or python-quantumclient" + - "python-keystoneclient" ''' EXAMPLES = ''' @@ -186,6 +191,9 @@ def main(): )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_DEPS: + module.fail_json(msg='python-novaclient, python-keystoneclient, and either python-neutronclient or python-quantumclient are required') + try: nova = nova_client.Client(module.params['login_username'], module.params['login_password'], module.params['login_tenant_name'], module.params['auth_url'], service_type='compute') @@ -214,5 +222,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/quantum_network.py b/cloud/openstack/quantum_network.py index 6b0c66e7a12..ff8b2683f37 100644 --- a/cloud/openstack/quantum_network.py +++ b/cloud/openstack/quantum_network.py @@ -22,8 +22,9 @@ try: except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient + HAVE_DEPS = True except ImportError: - print("failed=True msg='quantumclient (or neutronclient) and keystone client are required'") + HAVE_DEPS = False DOCUMENTATION = ''' --- @@ -103,7 +104,10 @@ options: - Whether the state should be marked as up or down required: false default: true -requirements: ["quantumclient", "neutronclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-neutronclient or python-quantumclient" + - "python-keystoneclient" ''' @@ -244,6 +248,9 @@ def main(): )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_DEPS: + module.fail_json(msg='python-keystoneclient and either python-neutronclient or python-quantumclient are required') + if module.params['provider_network_type'] in ['vlan' , 'flat']: if not module.params['provider_physical_network']: module.fail_json(msg = " for vlan and flat networks, variable provider_physical_network should be set.") @@ -275,5 +282,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/quantum_router.py b/cloud/openstack/quantum_router.py index 38d479128f2..9588fc0951e 100644 --- a/cloud/openstack/quantum_router.py +++ b/cloud/openstack/quantum_router.py @@ -22,8 +22,9 @@ try: except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient + HAVE_DEPS = True except ImportError: - print("failed=True msg='quantumclient (or neutronclient) and keystone client are required'") + HAVE_DEPS = False DOCUMENTATION = ''' --- @@ -78,7 +79,10 @@ options: - desired admin state of the created router . required: false default: true -requirements: ["quantumclient", "neutronclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-neutronclient or python-quantumclient" + - "python-keystoneclient" ''' EXAMPLES = ''' @@ -183,6 +187,8 @@ def main(): admin_state_up = dict(type='bool', default=True), )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_DEPS: + module.fail_json(msg='python-keystoneclient and either python-neutronclient or python-quantumclient are required') neutron = _get_neutron_client(module, module.params) _set_tenant_id(module) @@ -206,5 +212,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/quantum_router_gateway.py b/cloud/openstack/quantum_router_gateway.py index 5de19fd4785..6e8047c8e8d 100644 --- a/cloud/openstack/quantum_router_gateway.py +++ b/cloud/openstack/quantum_router_gateway.py @@ -22,8 +22,10 @@ try: except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient + HAVE_DEPS = True except ImportError: - print("failed=True msg='quantumclient (or neutronclient) and keystone client are required'") + HAVE_DEPS = False + DOCUMENTATION = ''' --- module: quantum_router_gateway @@ -72,7 +74,10 @@ options: - Name of the external network which should be attached to the router. required: true default: None -requirements: ["quantumclient", "neutronclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-neutronclient or python-quantumclient" + - "python-keystoneclient" ''' EXAMPLES = ''' @@ -181,6 +186,8 @@ def main(): state = dict(default='present', choices=['absent', 'present']), )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_DEPS: + module.fail_json(msg='python-keystoneclient and either python-neutronclient or python-quantumclient are required') neutron = _get_neutron_client(module, module.params) router_id = _get_router_id(module, neutron) @@ -209,5 +216,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main() diff --git a/cloud/openstack/quantum_router_interface.py b/cloud/openstack/quantum_router_interface.py index c5828ad4106..7d42ec6ff1d 100644 --- a/cloud/openstack/quantum_router_interface.py +++ b/cloud/openstack/quantum_router_interface.py @@ -22,8 +22,10 @@ try: except ImportError: from quantumclient.quantum import client from keystoneclient.v2_0 import client as ksclient + HAVE_DEPS = True except ImportError: - print("failed=True msg='quantumclient (or neutronclient) and keystone client are required'") + HAVE_DEPS = False + DOCUMENTATION = ''' --- module: quantum_router_interface @@ -77,7 +79,10 @@ options: - Name of the tenant whose subnet has to be attached. required: false default: None -requirements: ["quantumclient", "keystoneclient"] +requirements: + - "python >= 2.6" + - "python-neutronclient or python-quantumclient" + - "python-keystoneclient" ''' EXAMPLES = ''' @@ -216,6 +221,8 @@ def main(): state = dict(default='present', choices=['absent', 'present']), )) module = AnsibleModule(argument_spec=argument_spec) + if not HAVE_DEPS: + module.fail_json(msg='python-keystoneclient and either python-neutronclient or python-quantumclient are required') neutron = _get_neutron_client(module, module.params) _set_tenant_id(module) @@ -245,5 +252,6 @@ def main(): # this is magic, see lib/ansible/module.params['common.py from ansible.module_utils.basic import * from ansible.module_utils.openstack import * -main() +if __name__ == '__main__': + main()