[cloud] Remove repeated error handling and region checking, both now in boto3_conn (#32774)

* Remove boto usage from boto3 modules

* Remove region checking

boto3_conn now takes care of region checking and handles NoRegionError
exceptions with a standard message

boto3_conn also takes care of other connection exceptions too.

* Document boto3 as a requirement for ec2_eni_facts
pull/28466/head
Will Thames 7 years ago committed by Ryan Brown
parent c52964a6f4
commit ddc3465408

@ -144,15 +144,7 @@ from ansible.module_utils.ec2 import (boto3_conn, ec2_argument_spec, get_aws_con
import traceback import traceback
try: try:
import boto from botocore.exceptions import ClientError, NoCredentialsError, WaiterError
import boto.ec2
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
try:
import boto3
from botocore.exceptions import ClientError, NoCredentialsError, NoRegionError, WaiterError
HAS_BOTO3 = True HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
@ -166,8 +158,6 @@ def copy_image(module, ec2):
ec2: ec2 connection object ec2: ec2 connection object
""" """
tags = module.params.get('tags')
params = {'SourceRegion': module.params.get('source_region'), params = {'SourceRegion': module.params.get('source_region'),
'SourceImageId': module.params.get('source_image_id'), 'SourceImageId': module.params.get('source_image_id'),
'Name': module.params.get('name'), 'Name': module.params.get('name'),
@ -213,20 +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')
# TODO: Check botocore version
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
if HAS_BOTO3:
try:
ec2 = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, ec2 = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url,
**aws_connect_params) **aws_connect_params)
except NoRegionError:
module.fail_json(msg='AWS Region is required')
else:
module.fail_json(msg='boto3 required for this module')
copy_image(module, ec2) copy_image(module, ec2)

@ -26,6 +26,7 @@ description:
- Gather facts about ec2 ENI interfaces in AWS - Gather facts about ec2 ENI interfaces in AWS
version_added: "2.0" version_added: "2.0"
author: "Rob White (@wimnat)" author: "Rob White (@wimnat)"
requirements: [ boto3 ]
options: options:
filters: filters:
description: description:
@ -185,27 +186,18 @@ network_interfaces:
''' '''
try: try:
import boto.ec2
from boto.exception import BotoServerError
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
try:
import boto3
from botocore.exceptions import ClientError, NoCredentialsError from botocore.exceptions import ClientError, NoCredentialsError
HAS_BOTO3 = True HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.ec2 import (AnsibleAWSError, from ansible.module_utils.ec2 import ansible_dict_to_boto3_filter_list, boto3_conn
ansible_dict_to_boto3_filter_list, boto3_conn, from ansible.module_utils.ec2 import boto3_tag_list_to_ansible_dict, camel_dict_to_snake_dict
boto3_tag_list_to_ansible_dict, camel_dict_to_snake_dict, from ansible.module_utils.ec2 import ec2_argument_spec, get_aws_connection_info
connect_to_aws, ec2_argument_spec, get_aws_connection_info)
def list_ec2_eni_boto3(connection, module): def list_eni(connection, module):
if module.params.get("filters") is None: if module.params.get("filters") is None:
filters = [] filters = []
@ -266,22 +258,6 @@ def get_eni_info(interface):
return interface_info return interface_info
def list_eni(connection, module):
filters = module.params.get("filters")
interface_dict_array = []
try:
all_eni = connection.get_all_network_interfaces(filters=filters)
except BotoServerError as e:
module.fail_json(msg=e.message)
for interface in all_eni:
interface_dict_array.append(get_eni_info(interface))
module.exit_json(interfaces=interface_dict_array)
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = ec2_argument_spec()
argument_spec.update( argument_spec.update(
@ -292,28 +268,12 @@ def main():
module = AnsibleModule(argument_spec=argument_spec) module = AnsibleModule(argument_spec=argument_spec)
if not HAS_BOTO: if not HAS_BOTO3:
module.fail_json(msg='boto required for this module') module.fail_json(msg='boto3 required for this module')
if HAS_BOTO3:
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
if region:
connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_params) connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_params)
else:
module.fail_json(msg="region must be specified")
list_ec2_eni_boto3(connection, module)
else:
region, ec2_url, aws_connect_params = get_aws_connection_info(module)
if region:
try:
connection = connect_to_aws(boto.ec2, region, **aws_connect_params)
except (boto.exception.NoAuthHandlerFound, AnsibleAWSError) as e:
module.fail_json(msg=str(e))
else:
module.fail_json(msg="region must be specified")
list_eni(connection, module) list_eni(connection, module)

@ -22,7 +22,7 @@ description:
- Creates or terminates ecs clusters. - Creates or terminates ecs clusters.
version_added: "2.0" version_added: "2.0"
author: Mark Chance(@Java1Guy) author: Mark Chance(@Java1Guy)
requirements: [ boto, boto3 ] requirements: [ boto3 ]
options: options:
state: state:
description: description:
@ -103,12 +103,6 @@ status:
''' '''
import time import time
try:
import boto
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
try: try:
import boto3 import boto3
HAS_BOTO3 = True HAS_BOTO3 = True
@ -125,14 +119,10 @@ class EcsClusterManager:
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
try:
# self.ecs = boto3.client('ecs') # self.ecs = boto3.client('ecs')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
if not region: self.ecs = boto3_conn(module, conn_type='client', resource='ecs',
module.fail_json(msg="Region must be specified as a parameter, in EC2_REGION or AWS_REGION environment variables or in boto configuration file") region=region, endpoint=ec2_url, **aws_connect_kwargs)
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except boto.exception.NoAuthHandlerFound as e:
self.module.fail_json(msg="Can't authorize connection - %s" % str(e))
def find_in_array(self, array_of_clusters, cluster_name, field_name='clusterArn'): def find_in_array(self, array_of_clusters, cluster_name, field_name='clusterArn'):
for c in array_of_clusters: for c in array_of_clusters:
@ -176,9 +166,6 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_together=required_together) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True, required_together=required_together)
if not HAS_BOTO:
module.fail_json(msg='boto is required.')
if not HAS_BOTO3: if not HAS_BOTO3:
module.fail_json(msg='boto3 is required.') module.fail_json(msg='boto3 is required.')

@ -36,7 +36,7 @@ author:
- "Stephane Maarek (@simplesteph)" - "Stephane Maarek (@simplesteph)"
- "Zac Blazic (@zacblazic)" - "Zac Blazic (@zacblazic)"
requirements: [ json, boto, botocore, boto3 ] requirements: [ json, botocore, boto3 ]
options: options:
state: state:
description: description:
@ -267,14 +267,7 @@ DEPLOYMENT_CONFIGURATION_TYPE_MAP = {
try: try:
import boto
import botocore import botocore
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
try:
import boto3
HAS_BOTO3 = True HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
@ -289,13 +282,8 @@ class EcsServiceManager:
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
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")
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs) self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except boto.exception.NoAuthHandlerFound as e:
self.module.fail_json(msg="Can't authorize connection - %s" % str(e))
def find_in_array(self, array_of_services, service_name, field_name='serviceArn'): def find_in_array(self, array_of_services, service_name, field_name='serviceArn'):
for c in array_of_services: for c in array_of_services:
@ -403,9 +391,6 @@ def main():
required_together=[['load_balancers', 'role']] required_together=[['load_balancers', 'role']]
) )
if not HAS_BOTO:
module.fail_json(msg='boto is required.')
if not HAS_BOTO3: if not HAS_BOTO3:
module.fail_json(msg='boto3 is required.') module.fail_json(msg='boto3 is required.')

@ -22,7 +22,7 @@ version_added: "2.1"
author: author:
- "Mark Chance (@java1guy)" - "Mark Chance (@java1guy)"
- "Darek Kaczynski (@kaczynskid)" - "Darek Kaczynski (@kaczynskid)"
requirements: [ json, boto, botocore, boto3 ] requirements: [ json, botocore, boto3 ]
options: options:
details: details:
description: description:
@ -124,14 +124,7 @@ services:
''' # NOQA ''' # NOQA
try: try:
import boto
import botocore import botocore
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
try:
import boto3
HAS_BOTO3 = True HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
@ -146,14 +139,9 @@ class EcsServiceManager:
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
try:
# self.ecs = boto3.client('ecs') # self.ecs = boto3.client('ecs')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
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")
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs) self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except boto.exception.NoAuthHandlerFound as e:
self.module.fail_json(msg="Can't authorize connection - %s" % str(e))
# def list_clusters(self): # def list_clusters(self):
# return self.client.list_clusters() # return self.client.list_clusters()
@ -211,9 +199,6 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
if not HAS_BOTO:
module.fail_json(msg='boto is required.')
if not HAS_BOTO3: if not HAS_BOTO3:
module.fail_json(msg='boto3 is required.') module.fail_json(msg='boto3 is required.')

@ -19,7 +19,7 @@ description:
- Creates or deletes instances of task definitions. - Creates or deletes instances of task definitions.
version_added: "2.0" version_added: "2.0"
author: Mark Chance(@Java1Guy) author: Mark Chance(@Java1Guy)
requirements: [ json, boto, botocore, boto3 ] requirements: [ json, botocore, boto3 ]
options: options:
operation: operation:
description: description:
@ -149,15 +149,9 @@ task:
returned: only when details is true returned: only when details is true
type: string type: string
''' '''
try:
import boto
import botocore
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
try: try:
import boto3 import botocore
HAS_BOTO3 = True HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
@ -172,13 +166,8 @@ class EcsExecManager:
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
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")
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs) self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except boto.exception.NoAuthHandlerFound as e:
module.fail_json(msg="Can't authorize connection - %s " % str(e))
def list_tasks(self, cluster_name, service_name, status): def list_tasks(self, cluster_name, service_name, status):
response = self.ecs.list_tasks( response = self.ecs.list_tasks(
@ -241,9 +230,6 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
# Validate Requirements # Validate Requirements
if not HAS_BOTO:
module.fail_json(msg='boto is required.')
if not HAS_BOTO3: if not HAS_BOTO3:
module.fail_json(msg='boto3 is required.') module.fail_json(msg='boto3 is required.')

@ -27,7 +27,7 @@ description:
- Registers or deregisters task definitions in the Amazon Web Services (AWS) EC2 Container Service (ECS) - Registers or deregisters task definitions in the Amazon Web Services (AWS) EC2 Container Service (ECS)
version_added: "2.0" version_added: "2.0"
author: Mark Chance(@Java1Guy) author: Mark Chance(@Java1Guy)
requirements: [ json, boto, botocore, boto3 ] requirements: [ json, botocore, boto3 ]
options: options:
state: state:
description: description:
@ -114,15 +114,9 @@ taskdefinition:
type: dict type: dict
returned: always returned: always
''' '''
try:
import boto
import botocore
HAS_BOTO = True
except ImportError:
HAS_BOTO = False
try: try:
import boto3 import botocore
HAS_BOTO3 = True HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False HAS_BOTO3 = False
@ -138,13 +132,8 @@ class EcsTaskManager:
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
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")
self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs) self.ecs = boto3_conn(module, conn_type='client', resource='ecs', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except boto.exception.NoAuthHandlerFound as e:
module.fail_json(msg="Can't authorize connection - " % str(e))
def describe_task(self, task_name): def describe_task(self, task_name):
try: try:
@ -233,9 +222,6 @@ def main():
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
if not HAS_BOTO:
module.fail_json(msg='boto is required.')
if not HAS_BOTO3: if not HAS_BOTO3:
module.fail_json(msg='boto3 is required.') module.fail_json(msg='boto3 is required.')

@ -218,13 +218,9 @@ class EFSConnection(object):
STATE_DELETED = 'deleted' STATE_DELETED = 'deleted'
def __init__(self, module, region, **aws_connect_params): def __init__(self, module, region, **aws_connect_params):
try:
self.connection = boto3_conn(module, conn_type='client', self.connection = boto3_conn(module, conn_type='client',
resource='efs', region=region, resource='efs', region=region,
**aws_connect_params) **aws_connect_params)
except Exception as e:
module.fail_json(msg="Failed to connect to AWS: %s" % str(e))
self.region = region self.region = region
self.wait = module.params.get('wait') self.wait = module.params.get('wait')
self.wait_timeout = module.params.get('wait_timeout') self.wait_timeout = module.params.get('wait_timeout')
@ -490,7 +486,7 @@ class EFSConnection(object):
def iterate_all(attr, map_method, **kwargs): def iterate_all(attr, map_method, **kwargs):
""" """
Method creates iterator from boto result set Method creates iterator from result set
""" """
args = dict((key, value) for (key, value) in kwargs.items() if value is not None) args = dict((key, value) for (key, value) in kwargs.items() if value is not None)
wait = 1 wait = 1

Loading…
Cancel
Save