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

View file

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

View file

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

View file

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

View file

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

View file

@ -240,18 +240,15 @@ EXAMPLES = """
instance_port: 80 instance_port: 80
""" """
import sys
import os
try: try:
import boto import boto
import boto.ec2.elb import boto.ec2.elb
import boto.ec2.elb.attributes import boto.ec2.elb.attributes
from boto.ec2.elb.healthcheck import HealthCheck from boto.ec2.elb.healthcheck import HealthCheck
from boto.regioninfo import RegionInfo from boto.regioninfo import RegionInfo
HAS_BOTO = True
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" HAS_BOTO = False
sys.exit(1)
class ElbManager(object): class ElbManager(object):
@ -652,6 +649,9 @@ def main():
argument_spec=argument_spec, 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) region, ec2_url, aws_connect_params = get_aws_connection_info(module)
if not region: 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") 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: try:
import boto.ec2 import boto.ec2
HAS_BOTO = True
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" HAS_BOTO = False
sys.exit(1)
def make_rule_key(prefix, rule, group_id, cidr_ip): def make_rule_key(prefix, rule, group_id, cidr_ip):
@ -202,6 +202,9 @@ def main():
supports_check_mode=True, supports_check_mode=True,
) )
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
name = module.params['name'] name = module.params['name']
description = module.params['description'] description = module.params['description']
vpc_id = module.params['vpc_id'] vpc_id = module.params['vpc_id']

View file

@ -84,9 +84,9 @@ EXAMPLES = '''
try: try:
import boto.ec2 import boto.ec2
HAS_BOTO = True
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" HAS_BOTO = False
sys.exit(1)
import random import random
import string import string
@ -107,6 +107,9 @@ def main():
supports_check_mode=True, supports_check_mode=True,
) )
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
name = module.params['name'] name = module.params['name']
state = module.params.get('state') state = module.params.get('state')
key_material = module.params.get('key_material') 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.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
@ -140,9 +137,9 @@ try:
import boto.ec2.autoscale import boto.ec2.autoscale
from boto.ec2.autoscale import LaunchConfiguration from boto.ec2.autoscale import LaunchConfiguration
from boto.exception import BotoServerError from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" HAS_BOTO = False
sys.exit(1)
def create_block_device(module, volume): def create_block_device(module, volume):
@ -260,6 +257,9 @@ def main():
module = AnsibleModule(argument_spec=argument_spec) 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) region, ec2_url, aws_connect_params = get_aws_connection_info(module)
try: try:

View file

@ -122,9 +122,9 @@ try:
import boto.ec2.cloudwatch import boto.ec2.cloudwatch
from boto.ec2.cloudwatch import CloudWatchConnection, MetricAlarm from boto.ec2.cloudwatch import CloudWatchConnection, MetricAlarm
from boto.exception import BotoServerError from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" HAS_BOTO = False
sys.exit(1)
def create_metric_alarm(connection, module): def create_metric_alarm(connection, module):
@ -266,6 +266,9 @@ def main():
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO:
module.fail_json(msg='boto required for this module')
state = module.params.get('state') state = module.params.get('state')
region, ec2_url, aws_connect_params = get_aws_connection_info(module) 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.basic import *
from ansible.module_utils.ec2 import * from ansible.module_utils.ec2 import *
@ -64,10 +62,9 @@ try:
import boto.ec2.autoscale import boto.ec2.autoscale
from boto.ec2.autoscale import ScalingPolicy from boto.ec2.autoscale import ScalingPolicy
from boto.exception import BotoServerError from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError: except ImportError:
print "failed=True msg='boto required for this module'" HAS_BOTO = False
sys.exit(1)
def create_scaling_policy(connection, module): def create_scaling_policy(connection, module):
@ -157,6 +154,9 @@ def main():
module = AnsibleModule(argument_spec=argument_spec) 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) region, ec2_url, aws_connect_params = get_aws_connection_info(module)
state = module.params.get('state') state = module.params.get('state')

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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