issue #994: use HAS_BOTO to determine if import was successful: - removed import of sys module. - HAS_BOTO constant to check if import was successful. - trigger a failure when import fails. - removed unnecessary imports.

This commit is contained in:
steenzout 2015-04-01 17:16:54 -06:00 committed by Matt Clay
parent 0a48d54c1c
commit 67f769d9a6
21 changed files with 121 additions and 74 deletions

View file

@ -487,7 +487,6 @@ EXAMPLES = '''
'''
import sys
import time
from ast import literal_eval
@ -496,9 +495,10 @@ try:
from boto.ec2.blockdevicemapping import BlockDeviceType, BlockDeviceMapping
from boto.exception import EC2ResponseError
from boto.vpc import VPCConnection
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def find_running_instances_by_count_tag(module, ec2, count_tag, zone=None):
@ -782,7 +782,6 @@ def create_instances(module, ec2, vpc, override_count=None):
# group_id and group_name are exclusive of each other
if group_id and group_name:
module.fail_json(msg = str("Use only one type of parameter (group_name) or (group_id)"))
sys.exit(1)
vpc_id = None
if vpc_subnet_id:
@ -1203,6 +1202,9 @@ def main():
],
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
ec2 = ec2_connect(module)
ec2_url, aws_access_key, aws_secret_key, region = get_ec2_creds(module)

View file

@ -129,15 +129,17 @@ EXAMPLES = '''
state: absent
'''
import sys
import time
try:
import boto
import boto.ec2
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def create_image(module, ec2):
"""
@ -226,6 +228,7 @@ def deregister_image(module, ec2):
module.exit_json(msg="AMI deregister/delete operation complete", changed=True)
sys.exit(0)
def main():
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
@ -242,6 +245,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
try:
ec2 = ec2_connect(module)
except Exception, e:

View file

@ -189,7 +189,6 @@ to "replace_instances":
region: us-east-1
'''
import sys
import time
from ansible.module_utils.basic import *
@ -199,9 +198,9 @@ try:
import boto.ec2.autoscale
from boto.ec2.autoscale import AutoScaleConnection, AutoScalingGroup, Tag
from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
ASG_ATTRIBUTES = ('availability_zones', 'default_cooldown', 'desired_capacity',
'health_check_period', 'health_check_type', 'launch_config_name',
@ -617,6 +616,10 @@ def main():
argument_spec=argument_spec,
mutually_exclusive = [['replace_all_instances', 'replace_instances']]
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
state = module.params.get('state')
replace_instances = module.params.get('replace_instances')
replace_all_instances = module.params.get('replace_all_instances')

View file

@ -90,10 +90,9 @@ EXAMPLES = '''
try:
import boto.ec2
HAS_BOTO = True
except ImportError:
boto_found = False
else:
boto_found = True
HAS_BOTO = False
wait_timeout = 0
@ -251,8 +250,8 @@ def main():
supports_check_mode=True
)
if not boto_found:
module.fail_json(msg="boto is required")
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
ec2 = ec2_connect(module)

View file

@ -99,17 +99,16 @@ post_tasks:
"""
import time
import sys
import os
try:
import boto
import boto.ec2
import boto.ec2.elb
from boto.regioninfo import RegionInfo
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
class ElbManager:
"""Handles EC2 instance ELB registration and de-registration"""
@ -299,6 +298,9 @@ def main():
argument_spec=argument_spec,
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
if not region:

View file

@ -240,18 +240,15 @@ EXAMPLES = """
instance_port: 80
"""
import sys
import os
try:
import boto
import boto.ec2.elb
import boto.ec2.elb.attributes
from boto.ec2.elb.healthcheck import HealthCheck
from boto.regioninfo import RegionInfo
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
class ElbManager(object):
@ -652,6 +649,9 @@ def main():
argument_spec=argument_spec,
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
if not region:
module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file")

View file

@ -113,9 +113,9 @@ EXAMPLES = '''
try:
import boto.ec2
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def make_rule_key(prefix, rule, group_id, cidr_ip):
@ -202,6 +202,9 @@ def main():
supports_check_mode=True,
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
name = module.params['name']
description = module.params['description']
vpc_id = module.params['vpc_id']

View file

@ -84,9 +84,9 @@ EXAMPLES = '''
try:
import boto.ec2
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
import random
import string
@ -107,6 +107,9 @@ def main():
supports_check_mode=True,
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
name = module.params['name']
state = module.params.get('state')
key_material = module.params.get('key_material')

View file

@ -129,9 +129,6 @@ EXAMPLES = '''
'''
import sys
import time
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
@ -140,9 +137,9 @@ try:
import boto.ec2.autoscale
from boto.ec2.autoscale import LaunchConfiguration
from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def create_block_device(module, volume):
@ -260,6 +257,9 @@ def main():
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
try:

View file

@ -122,9 +122,9 @@ try:
import boto.ec2.cloudwatch
from boto.ec2.cloudwatch import CloudWatchConnection, MetricAlarm
from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def create_metric_alarm(connection, module):
@ -266,6 +266,9 @@ def main():
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
state = module.params.get('state')
region, ec2_url, aws_connect_params = get_aws_connection_info(module)

View file

@ -55,8 +55,6 @@ EXAMPLES = '''
'''
import sys
from ansible.module_utils.basic import *
from ansible.module_utils.ec2 import *
@ -64,10 +62,9 @@ try:
import boto.ec2.autoscale
from boto.ec2.autoscale import ScalingPolicy
from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def create_scaling_policy(connection, module):
@ -157,6 +154,9 @@ def main():
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
state = module.params.get('state')

View file

@ -106,14 +106,14 @@ EXAMPLES = '''
state: absent
'''
import sys
import time
try:
import boto.ec2
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def main():
argument_spec = ec2_argument_spec()
@ -132,6 +132,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
volume_id = module.params.get('volume_id')
snapshot_id = module.params.get('snapshot_id')
description = module.params.get('description')

View file

@ -72,13 +72,13 @@ tasks:
'''
import sys
import time
try:
import boto.ec2
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def main():
argument_spec = ec2_argument_spec()
@ -90,6 +90,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
resource = module.params.get('resource')
tags = module.params.get('tags')
state = module.params.get('state')

View file

@ -186,16 +186,16 @@ EXAMPLES = '''
device_name: /dev/xvdf
'''
import sys
import time
from distutils.version import LooseVersion
try:
import boto.ec2
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def get_volume(module, ec2):
name = module.params.get('name')
@ -364,6 +364,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
id = module.params.get('id')
name = module.params.get('name')
instance = module.params.get('instance')

View file

@ -156,16 +156,17 @@ the delete will fail until those dependencies are removed.
'''
import sys
import time
try:
import boto.ec2
import boto.vpc
from boto.exception import EC2ResponseError
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def get_vpc_info(vpc):
"""
@ -576,6 +577,9 @@ def main():
argument_spec=argument_spec,
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
state = module.params.get('state')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module)

View file

@ -131,16 +131,15 @@ EXAMPLES = """
"""
import sys
import os
import time
try:
import boto
from boto.elasticache.layer1 import ElastiCacheConnection
from boto.regioninfo import RegionInfo
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
class ElastiCacheManager(object):
@ -497,6 +496,9 @@ def main():
argument_spec=argument_spec,
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module)
name = module.params['name']

View file

@ -294,9 +294,9 @@ import time
try:
import boto.rds
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
try:
import boto.rds2
@ -984,6 +984,10 @@ def main():
module = AnsibleModule(
argument_spec=argument_spec,
)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
invocations = {
'create': create_db_instance,
'replicate': replicate_db_instance,

View file

@ -87,9 +87,6 @@ EXAMPLES = '''
name: norwegian_blue
'''
import sys
import time
VALID_ENGINES = [
'mysql5.1',
'mysql5.5',
@ -112,9 +109,10 @@ VALID_ENGINES = [
try:
import boto.rds
from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
# returns a tuple: (whether or not a parameter was changed, the remaining parameters that weren't found in this parameter group)
@ -220,6 +218,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
state = module.params.get('state')
group_name = module.params.get('name').lower()
group_engine = module.params.get('engine')

View file

@ -73,15 +73,13 @@ EXAMPLES = '''
name: norwegian-blue
'''
import sys
import time
try:
import boto.rds
from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def main():
argument_spec = ec2_argument_spec()
@ -94,6 +92,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
state = module.params.get('state')
group_name = module.params.get('name').lower()
group_description = module.params.get('description')

View file

@ -159,7 +159,6 @@ EXAMPLES = '''
'''
import sys
import time
try:
@ -167,9 +166,10 @@ try:
from boto import route53
from boto.route53 import Route53Connection
from boto.route53.record import ResourceRecordSets
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def commit(changes, retry_interval):
"""Commit changes, but retry PriorRequestNotComplete errors."""
@ -203,6 +203,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
command_in = module.params.get('command')
zone_in = module.params.get('zone').lower()
ttl_in = module.params.get('ttl')

View file

@ -107,20 +107,19 @@ EXAMPLES = '''
- s3: bucket=mybucket mode=delete
'''
import sys
import os
import urlparse
import hashlib
from boto.s3.connection import OrdinaryCallingFormat
try:
import boto
from boto.s3.connection import Location
from boto.s3.connection import OrdinaryCallingFormat
from boto.s3.connection import S3Connection
HAS_BOTO = True
except ImportError:
print "failed=True msg='boto required for this module'"
sys.exit(1)
HAS_BOTO = False
def key_check(module, s3, bucket, obj):
try:
@ -278,6 +277,9 @@ def main():
)
module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
bucket = module.params.get('bucket')
obj = module.params.get('object')
src = module.params.get('src')