Several cleanups to many modules:

* Fix docs to specify when python2.6+ is required (due to a library
  dep).  This helps us know when it is okay to use python2.6+ syntax in
  the file.
* remove BabyJson returns.  See #1211  This commit fixes all but the
  openstack modules.
* Use if __name__ == '__main__' to only run the main part of the module
  if the module is run as a program.  This allows for the potential to
  unittest the code later.
This commit is contained in:
Toshio Kuratomi 2015-05-11 12:15:53 -07:00 committed by Matt Clay
commit 5336217649
17 changed files with 186 additions and 114 deletions

View file

@ -87,7 +87,9 @@ options:
required: true
default: null
requirements: [ "boto 2.9+" ]
requirements:
- "python >= 2.6"
- "boto >= 2.9"
author: benno@ansible.com Note. Most of the code has been taken from the S3 module.
@ -116,16 +118,15 @@ EXAMPLES = '''
- gc_storage: bucket=mybucket mode=delete
'''
import sys
import os
import urlparse
import hashlib
try:
import boto
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto 2.9+ required for this module'"
sys.exit(1)
HAS_BOTO = False
def grant_check(module, gs, obj):
try:
@ -377,6 +378,9 @@ def main():
),
)
if not HAS_BOTO:
module.fail_json(msg='boto 2.9+ required for this module')
bucket = module.params.get('bucket')
obj = module.params.get('object')
src = module.params.get('src')
@ -445,5 +449,5 @@ def main():
# import module snippets
from ansible.module_utils.basic import *
main()
if __name__ == '__main__':
main()

View file

@ -137,7 +137,9 @@ options:
default: "true"
aliases: []
requirements: [ "libcloud" ]
requirements:
- "python >= 2.6"
- "apache-libcloud >= 0.13.3"
notes:
- Either I(name) or I(instance_names) is required.
author: Eric Johnson <erjohnso@google.com>
@ -202,25 +204,21 @@ EXAMPLES = '''
'''
import sys
try:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
ResourceExistsError, ResourceInUseError, ResourceNotFoundError
_ = Provider.GCE
HAS_LIBCLOUD = True
except ImportError:
print("failed=True " + \
"msg='libcloud with GCE support (0.13.3+) required for this module'")
sys.exit(1)
HAS_LIBCLOUD = False
try:
from ast import literal_eval
HAS_PYTHON26 = True
except ImportError:
print("failed=True " + \
"msg='GCE module requires python's 'ast' module, python v2.6+'")
sys.exit(1)
HAS_PYTHON26 = False
def get_instance_info(inst):
@ -323,11 +321,9 @@ def create_instances(module, gce, instance_names):
if not isinstance(md, dict):
raise ValueError('metadata must be a dict')
except ValueError, e:
print("failed=True msg='bad metadata: %s'" % str(e))
sys.exit(1)
module.fail_json(msg='bad metadata: %s' % str(e))
except SyntaxError, e:
print("failed=True msg='bad metadata syntax'")
sys.exit(1)
module.fail_json(msg='bad metadata syntax')
items = []
for k,v in md.items():
@ -450,6 +446,11 @@ def main():
)
)
if not HAS_PYTHON26:
module.fail_json(msg="GCE module requires python's 'ast' module, python v2.6+")
if not HAS_LIBCLOUD:
module.fail_json(msg='libcloud with GCE support (0.13.3+) required for this module')
gce = gce_connect(module)
image = module.params.get('image')
@ -503,11 +504,10 @@ def main():
json_output['changed'] = changed
print json.dumps(json_output)
sys.exit(0)
module.exit_json(**json_output)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.gce import *
main()
if __name__ == '__main__':
main()

View file

@ -131,7 +131,9 @@ options:
default: null
aliases: []
requirements: [ "libcloud" ]
requirements:
- "python >= 2.6"
- "apache-libcloud >= 0.13.3"
author: Eric Johnson <erjohnso@google.com>
'''
@ -147,9 +149,6 @@ EXAMPLES = '''
httphealthcheck_path: "/up"
'''
import sys
try:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
@ -158,10 +157,9 @@ try:
from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
ResourceExistsError, ResourceNotFoundError
_ = Provider.GCE
HAS_LIBCLOUD = True
except ImportError:
print("failed=True " + \
"msg='libcloud with GCE support required for this module.'")
sys.exit(1)
HAS_LIBCLOUD = False
def main():
@ -188,6 +186,9 @@ def main():
)
)
if not HAS_LIBCLOUD:
module.fail_json(msg='libcloud with GCE support (0.13.3+) required for this module.')
gce = gce_connect(module)
httphealthcheck_name = module.params.get('httphealthcheck_name')
@ -325,11 +326,11 @@ def main():
json_output['changed'] = changed
print json.dumps(json_output)
sys.exit(0)
module.exit_json(**json_output)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.gce import *
main()
if __name__ == '__main__':
main()

View file

@ -102,7 +102,9 @@ options:
default: null
aliases: []
requirements: [ "libcloud" ]
requirements:
- "python >= 2.6"
- "apache-libcloud >= 0.13.3"
author: Eric Johnson <erjohnso@google.com>
'''
@ -123,18 +125,15 @@ EXAMPLES = '''
'''
import sys
try:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
ResourceExistsError, ResourceNotFoundError
_ = Provider.GCE
HAS_LIBCLOUD = True
except ImportError:
print("failed=True " + \
"msg='libcloud with GCE support required for this module.'")
sys.exit(1)
HAS_LIBCLOUD = False
def format_allowed_section(allowed):
"""Format each section of the allowed list"""
@ -182,6 +181,9 @@ def main():
)
)
if not HAS_LIBCLOUD:
module.exit_json(msg='libcloud with GCE support (0.13.3+) required for this module')
gce = gce_connect(module)
allowed = module.params.get('allowed')
@ -281,11 +283,11 @@ def main():
changed = True
json_output['changed'] = changed
print json.dumps(json_output)
sys.exit(0)
module.exit_json(**json_output)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.gce import *
main()
if __name__ == '__main__':
main()

View file

@ -117,7 +117,9 @@ options:
choices: ["pd-standard", "pd-ssd"]
aliases: []
requirements: [ "libcloud" ]
requirements:
- "python >= 2.6"
- "apache-libcloud >= 0.13.3"
author: Eric Johnson <erjohnso@google.com>
'''
@ -130,18 +132,15 @@ EXAMPLES = '''
name: pd
'''
import sys
try:
from libcloud.compute.types import Provider
from libcloud.compute.providers import get_driver
from libcloud.common.google import GoogleBaseError, QuotaExceededError, \
ResourceExistsError, ResourceNotFoundError, ResourceInUseError
_ = Provider.GCE
HAS_LIBCLOUD = True
except ImportError:
print("failed=True " + \
"msg='libcloud with GCE support is required for this module.'")
sys.exit(1)
HAS_LIBCLOUD = False
def main():
@ -162,6 +161,8 @@ def main():
project_id = dict(),
)
)
if not HAS_LIBCLOUD:
module.fail_json(msg='libcloud with GCE support (0.13.3+) is required for this module')
gce = gce_connect(module)
@ -285,11 +286,11 @@ def main():
changed = True
json_output['changed'] = changed
print json.dumps(json_output)
sys.exit(0)
module.exit_json(**json_output)
# import module snippets
from ansible.module_utils.basic import *
from ansible.module_utils.gce import *
main()
if __name__ == '__main__':
main()