[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
This commit is contained in:
parent
c52964a6f4
commit
ddc3465408
8 changed files with 37 additions and 173 deletions
|
@ -144,15 +144,7 @@ from ansible.module_utils.ec2 import (boto3_conn, ec2_argument_spec, get_aws_con
|
|||
import traceback
|
||||
|
||||
try:
|
||||
import boto
|
||||
import boto.ec2
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
from botocore.exceptions import ClientError, NoCredentialsError, NoRegionError, WaiterError
|
||||
from botocore.exceptions import ClientError, NoCredentialsError, WaiterError
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
@ -166,8 +158,6 @@ def copy_image(module, ec2):
|
|||
ec2: ec2 connection object
|
||||
"""
|
||||
|
||||
tags = module.params.get('tags')
|
||||
|
||||
params = {'SourceRegion': module.params.get('source_region'),
|
||||
'SourceImageId': module.params.get('source_image_id'),
|
||||
'Name': module.params.get('name'),
|
||||
|
@ -213,20 +203,9 @@ def main():
|
|||
|
||||
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)
|
||||
|
||||
if HAS_BOTO3:
|
||||
|
||||
try:
|
||||
ec2 = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url,
|
||||
**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)
|
||||
|
||||
|
|
|
@ -26,6 +26,7 @@ description:
|
|||
- Gather facts about ec2 ENI interfaces in AWS
|
||||
version_added: "2.0"
|
||||
author: "Rob White (@wimnat)"
|
||||
requirements: [ boto3 ]
|
||||
options:
|
||||
filters:
|
||||
description:
|
||||
|
@ -185,27 +186,18 @@ network_interfaces:
|
|||
'''
|
||||
|
||||
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
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils.ec2 import (AnsibleAWSError,
|
||||
ansible_dict_to_boto3_filter_list, boto3_conn,
|
||||
boto3_tag_list_to_ansible_dict, camel_dict_to_snake_dict,
|
||||
connect_to_aws, ec2_argument_spec, get_aws_connection_info)
|
||||
from ansible.module_utils.ec2 import 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
|
||||
from ansible.module_utils.ec2 import 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:
|
||||
filters = []
|
||||
|
@ -266,22 +258,6 @@ def get_eni_info(interface):
|
|||
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():
|
||||
argument_spec = ec2_argument_spec()
|
||||
argument_spec.update(
|
||||
|
@ -292,28 +268,12 @@ def main():
|
|||
|
||||
module = AnsibleModule(argument_spec=argument_spec)
|
||||
|
||||
if not HAS_BOTO:
|
||||
module.fail_json(msg='boto required for this module')
|
||||
if not HAS_BOTO3:
|
||||
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)
|
||||
|
||||
if region:
|
||||
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)
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ description:
|
|||
- Creates or terminates ecs clusters.
|
||||
version_added: "2.0"
|
||||
author: Mark Chance(@Java1Guy)
|
||||
requirements: [ boto, boto3 ]
|
||||
requirements: [ boto3 ]
|
||||
options:
|
||||
state:
|
||||
description:
|
||||
|
@ -103,12 +103,6 @@ status:
|
|||
'''
|
||||
import time
|
||||
|
||||
try:
|
||||
import boto
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
HAS_BOTO3 = True
|
||||
|
@ -125,14 +119,10 @@ class EcsClusterManager:
|
|||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
# self.ecs = boto3.client('ecs')
|
||||
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)
|
||||
except boto.exception.NoAuthHandlerFound as e:
|
||||
self.module.fail_json(msg="Can't authorize connection - %s" % str(e))
|
||||
self.ecs = boto3_conn(module, conn_type='client', resource='ecs',
|
||||
region=region, endpoint=ec2_url, **aws_connect_kwargs)
|
||||
|
||||
def find_in_array(self, array_of_clusters, cluster_name, field_name='clusterArn'):
|
||||
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)
|
||||
|
||||
if not HAS_BOTO:
|
||||
module.fail_json(msg='boto is required.')
|
||||
|
||||
if not HAS_BOTO3:
|
||||
module.fail_json(msg='boto3 is required.')
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ author:
|
|||
- "Stephane Maarek (@simplesteph)"
|
||||
- "Zac Blazic (@zacblazic)"
|
||||
|
||||
requirements: [ json, boto, botocore, boto3 ]
|
||||
requirements: [ json, botocore, boto3 ]
|
||||
options:
|
||||
state:
|
||||
description:
|
||||
|
@ -267,14 +267,7 @@ DEPLOYMENT_CONFIGURATION_TYPE_MAP = {
|
|||
|
||||
|
||||
try:
|
||||
import boto
|
||||
import botocore
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
@ -289,13 +282,8 @@ class EcsServiceManager:
|
|||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
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)
|
||||
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'):
|
||||
for c in array_of_services:
|
||||
|
@ -403,9 +391,6 @@ def main():
|
|||
required_together=[['load_balancers', 'role']]
|
||||
)
|
||||
|
||||
if not HAS_BOTO:
|
||||
module.fail_json(msg='boto is required.')
|
||||
|
||||
if not HAS_BOTO3:
|
||||
module.fail_json(msg='boto3 is required.')
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ version_added: "2.1"
|
|||
author:
|
||||
- "Mark Chance (@java1guy)"
|
||||
- "Darek Kaczynski (@kaczynskid)"
|
||||
requirements: [ json, boto, botocore, boto3 ]
|
||||
requirements: [ json, botocore, boto3 ]
|
||||
options:
|
||||
details:
|
||||
description:
|
||||
|
@ -124,14 +124,7 @@ services:
|
|||
''' # NOQA
|
||||
|
||||
try:
|
||||
import boto
|
||||
import botocore
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
@ -146,14 +139,9 @@ class EcsServiceManager:
|
|||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
# self.ecs = boto3.client('ecs')
|
||||
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)
|
||||
except boto.exception.NoAuthHandlerFound as e:
|
||||
self.module.fail_json(msg="Can't authorize connection - %s" % str(e))
|
||||
|
||||
# def list_clusters(self):
|
||||
# return self.client.list_clusters()
|
||||
|
@ -211,9 +199,6 @@ def main():
|
|||
|
||||
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:
|
||||
module.fail_json(msg='boto3 is required.')
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@ description:
|
|||
- Creates or deletes instances of task definitions.
|
||||
version_added: "2.0"
|
||||
author: Mark Chance(@Java1Guy)
|
||||
requirements: [ json, boto, botocore, boto3 ]
|
||||
requirements: [ json, botocore, boto3 ]
|
||||
options:
|
||||
operation:
|
||||
description:
|
||||
|
@ -149,15 +149,9 @@ task:
|
|||
returned: only when details is true
|
||||
type: string
|
||||
'''
|
||||
try:
|
||||
import boto
|
||||
import botocore
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
import botocore
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
@ -172,13 +166,8 @@ class EcsExecManager:
|
|||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
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)
|
||||
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):
|
||||
response = self.ecs.list_tasks(
|
||||
|
@ -241,9 +230,6 @@ def main():
|
|||
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
|
||||
|
||||
# Validate Requirements
|
||||
if not HAS_BOTO:
|
||||
module.fail_json(msg='boto is required.')
|
||||
|
||||
if not HAS_BOTO3:
|
||||
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)
|
||||
version_added: "2.0"
|
||||
author: Mark Chance(@Java1Guy)
|
||||
requirements: [ json, boto, botocore, boto3 ]
|
||||
requirements: [ json, botocore, boto3 ]
|
||||
options:
|
||||
state:
|
||||
description:
|
||||
|
@ -114,15 +114,9 @@ taskdefinition:
|
|||
type: dict
|
||||
returned: always
|
||||
'''
|
||||
try:
|
||||
import boto
|
||||
import botocore
|
||||
HAS_BOTO = True
|
||||
except ImportError:
|
||||
HAS_BOTO = False
|
||||
|
||||
try:
|
||||
import boto3
|
||||
import botocore
|
||||
HAS_BOTO3 = True
|
||||
except ImportError:
|
||||
HAS_BOTO3 = False
|
||||
|
@ -138,13 +132,8 @@ class EcsTaskManager:
|
|||
def __init__(self, module):
|
||||
self.module = module
|
||||
|
||||
try:
|
||||
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)
|
||||
except boto.exception.NoAuthHandlerFound as e:
|
||||
module.fail_json(msg="Can't authorize connection - " % str(e))
|
||||
|
||||
def describe_task(self, task_name):
|
||||
try:
|
||||
|
@ -233,9 +222,6 @@ def main():
|
|||
|
||||
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:
|
||||
module.fail_json(msg='boto3 is required.')
|
||||
|
||||
|
|
|
@ -218,13 +218,9 @@ class EFSConnection(object):
|
|||
STATE_DELETED = 'deleted'
|
||||
|
||||
def __init__(self, module, region, **aws_connect_params):
|
||||
try:
|
||||
self.connection = boto3_conn(module, conn_type='client',
|
||||
resource='efs', region=region,
|
||||
**aws_connect_params)
|
||||
except Exception as e:
|
||||
module.fail_json(msg="Failed to connect to AWS: %s" % str(e))
|
||||
|
||||
self.region = region
|
||||
self.wait = module.params.get('wait')
|
||||
self.wait_timeout = module.params.get('wait_timeout')
|
||||
|
@ -490,7 +486,7 @@ class EFSConnection(object):
|
|||
|
||||
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)
|
||||
wait = 1
|
||||
|
|
Loading…
Reference in a new issue