AnsibleAWSModule related cleanup: (#64931)

- fetch boto3 clients using module.client()
- remove ec2_argument_spec() use (AnsibleAWSModule adds it automatically)
- remove unused imports
- remove HAS_BOTO3 (AnsibleAWSModule handles it)
This commit is contained in:
Mark Chappell 2019-12-18 01:28:27 +01:00 committed by ansibot
parent b8729b2544
commit 8a423868d9
25 changed files with 134 additions and 284 deletions

View file

@ -227,12 +227,9 @@ arns:
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
from ansible.module_utils.aws.acm import ACMServiceManager from ansible.module_utils.aws.acm import ACMServiceManager
from ansible.module_utils._text import to_bytes, to_text from ansible.module_utils._text import to_text
from ssl import PEM_cert_to_DER_cert
import base64 import base64
import traceback
import re # regex library import re # regex library
@ -265,8 +262,7 @@ def PEM_body_to_DER(module, pem):
try: try:
der = base64.b64decode(to_text(pem)) der = base64.b64decode(to_text(pem))
except (ValueError, TypeError) as e: except (ValueError, TypeError) as e:
module.fail_json(msg="Unable to decode certificate chain", module.fail_json_aws(e, msg="Unable to decode certificate chain")
exception=traceback.format_exc())
return der return der
@ -326,9 +322,7 @@ def main():
else: else:
tags = None tags = None
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) client = module.client('acm')
client = boto3_conn(module, conn_type='client', resource='acm',
region=region, endpoint=ec2_url, **aws_connect_kwargs)
# fetch the list of certificates currently in ACM # fetch the list of certificates currently in ACM
certificates = acm.get_certificates(client=client, certificates = acm.get_certificates(client=client,

View file

@ -265,7 +265,6 @@ certificates:
''' '''
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
from ansible.module_utils.aws.acm import ACMServiceManager from ansible.module_utils.aws.acm import ACMServiceManager
@ -282,9 +281,7 @@ def main():
if module._name == 'aws_acm_facts': if module._name == 'aws_acm_facts':
module.deprecate("The 'aws_acm_facts' module has been renamed to 'aws_acm_info'", version='2.13') module.deprecate("The 'aws_acm_facts' module has been renamed to 'aws_acm_info'", version='2.13')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) client = module.client('acm')
client = boto3_conn(module, conn_type='client', resource='acm',
region=region, endpoint=ec2_url, **aws_connect_kwargs)
certificates = acm_info.get_certificates(client, module, certificates = acm_info.get_certificates(client, module,
domain_name=module.params['domain_name'], domain_name=module.params['domain_name'],

View file

@ -290,7 +290,7 @@ creation_time:
''' # NOQA ''' # NOQA
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import _camel_to_snake, camel_dict_to_snake_dict, ec2_argument_spec from ansible.module_utils.ec2 import _camel_to_snake, camel_dict_to_snake_dict
try: try:
import botocore import botocore
@ -480,8 +480,7 @@ def create_scaling_policy(connection, module):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update(
state=dict(type='str', required=True, choices=['present', 'absent']), state=dict(type='str', required=True, choices=['present', 'absent']),
policy_name=dict(type='str', required=True), policy_name=dict(type='str', required=True),
service_namespace=dict(type='str', required=True, choices=['appstream', 'dynamodb', 'ec2', 'ecs', 'elasticmapreduce']), service_namespace=dict(type='str', required=True, choices=['appstream', 'dynamodb', 'ec2', 'ecs', 'elasticmapreduce']),

View file

@ -64,8 +64,7 @@ except ImportError:
pass # handled by AnsibleAWSModule pass # handled by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, AWSRetry from ansible.module_utils.ec2 import AWSRetry
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, boto3_tag_list_to_ansible_dict
def resource_exists(client, module, params): def resource_exists(client, module, params):

View file

@ -77,8 +77,7 @@ except ImportError:
pass # handled by AnsibleAWSModule pass # handled by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, AWSRetry from ansible.module_utils.ec2 import camel_dict_to_snake_dict, AWSRetry
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, boto3_tag_list_to_ansible_dict
# this waits for an IAM role to become fully available, at the cost of # this waits for an IAM role to become fully available, at the cost of

View file

@ -91,8 +91,7 @@ except ImportError:
pass # handled by AnsibleAWSModule pass # handled by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, AWSRetry from ansible.module_utils.ec2 import camel_dict_to_snake_dict, AWSRetry
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, boto3_tag_list_to_ansible_dict
def resource_exists(client, module, params): def resource_exists(client, module, params):

View file

@ -240,14 +240,12 @@ EXAMPLES = '''
import traceback import traceback
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.aws.direct_connect import DirectConnectError, delete_virtual_interface from ansible.module_utils.aws.direct_connect import DirectConnectError, delete_virtual_interface
from ansible.module_utils.ec2 import (AWSRetry, HAS_BOTO3, boto3_conn, from ansible.module_utils.ec2 import AWSRetry, camel_dict_to_snake_dict
ec2_argument_spec, get_aws_connection_info,
camel_dict_to_snake_dict)
try: try:
from botocore.exceptions import ClientError, BotoCoreError from botocore.exceptions import ClientError, BotoCoreError
except ImportError: except ImportError:
# handled by HAS_BOTO3 # handled by AnsibleAWSModule
pass pass
@ -461,8 +459,7 @@ def ensure_state(connection, module):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update(dict(
state=dict(required=True, choices=['present', 'absent']), state=dict(required=True, choices=['present', 'absent']),
id_to_associate=dict(required=True, aliases=['link_aggregation_group_id', 'connection_id']), id_to_associate=dict(required=True, aliases=['link_aggregation_group_id', 'connection_id']),
public=dict(type='bool'), public=dict(type='bool'),
@ -476,7 +473,7 @@ def main():
cidr=dict(type='list'), cidr=dict(type='list'),
virtual_gateway_id=dict(), virtual_gateway_id=dict(),
virtual_interface_id=dict() virtual_interface_id=dict()
)) )
module = AnsibleAWSModule(argument_spec=argument_spec, module = AnsibleAWSModule(argument_spec=argument_spec,
required_one_of=[['virtual_interface_id', 'name']], required_one_of=[['virtual_interface_id', 'name']],
@ -486,8 +483,7 @@ def main():
['public', True, ['customer_address']], ['public', True, ['customer_address']],
['public', True, ['cidr']]]) ['public', True, ['cidr']]])
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) connection = module.client('directconnect')
connection = boto3_conn(module, conn_type='client', resource='directconnect', region=region, endpoint=ec2_url, **aws_connect_kwargs)
try: try:
changed, latest_state = ensure_state(connection, module) changed, latest_state = ensure_state(connection, module)

View file

@ -93,7 +93,6 @@ except ImportError:
pass # handled by AnsibleAWSModule pass # handled by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
def describe_app(ebs, app_name, module): def describe_app(ebs, app_name, module):
@ -147,15 +146,11 @@ def filter_empty(**kwargs):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
app_name=dict(aliases=['name'], type='str', required=False),
argument_spec.update( description=dict(),
dict( state=dict(choices=['present', 'absent'], default='present'),
app_name=dict(aliases=['name'], type='str', required=False), terminate_by_force=dict(type='bool', default=False, required=False)
description=dict(),
state=dict(choices=['present', 'absent'], default='present'),
terminate_by_force=dict(type='bool', default=False, required=False)
)
) )
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
@ -169,9 +164,8 @@ def main():
module.fail_json(msg='Module parameter "app_name" is required') module.fail_json(msg='Module parameter "app_name" is required')
result = {} result = {}
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
ebs = boto3_conn(module, conn_type='client', resource='elasticbeanstalk', ebs = module.client('elasticbeanstalk')
region=region, endpoint=ec2_url, **aws_connect_params)
app = describe_app(ebs, app_name, module) app = describe_app(ebs, app_name, module)

View file

@ -396,7 +396,6 @@ statement_label = {
} }
from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code
from ansible.module_utils.ec2 import ec2_argument_spec
from ansible.module_utils.ec2 import AWSRetry, camel_dict_to_snake_dict from ansible.module_utils.ec2 import AWSRetry, camel_dict_to_snake_dict
from ansible.module_utils.ec2 import boto3_tag_list_to_ansible_dict, ansible_dict_to_boto3_tag_list from ansible.module_utils.ec2 import boto3_tag_list_to_ansible_dict, ansible_dict_to_boto3_tag_list
from ansible.module_utils.ec2 import compare_aws_tags, compare_policies from ansible.module_utils.ec2 import compare_aws_tags, compare_policies
@ -926,25 +925,22 @@ def assert_policy_shape(policy):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( alias=dict(aliases=['key_alias']),
dict( policy_mode=dict(aliases=['mode'], choices=['grant', 'deny'], default='grant'),
alias=dict(aliases=['key_alias']), policy_role_name=dict(aliases=['role_name']),
policy_mode=dict(aliases=['mode'], choices=['grant', 'deny'], default='grant'), policy_role_arn=dict(aliases=['role_arn']),
policy_role_name=dict(aliases=['role_name']), policy_grant_types=dict(aliases=['grant_types'], type='list'),
policy_role_arn=dict(aliases=['role_arn']), policy_clean_invalid_entries=dict(aliases=['clean_invalid_entries'], type='bool', default=True),
policy_grant_types=dict(aliases=['grant_types'], type='list'), key_id=dict(aliases=['key_arn']),
policy_clean_invalid_entries=dict(aliases=['clean_invalid_entries'], type='bool', default=True), description=dict(),
key_id=dict(aliases=['key_arn']), enabled=dict(type='bool', default=True),
description=dict(), tags=dict(type='dict', default={}),
enabled=dict(type='bool', default=True), purge_tags=dict(type='bool', default=False),
tags=dict(type='dict', default={}), grants=dict(type='list', default=[]),
purge_tags=dict(type='bool', default=False), policy=dict(),
grants=dict(type='list', default=[]), purge_grants=dict(type='bool', default=False),
policy=dict(), state=dict(default='present', choices=['present', 'absent']),
purge_grants=dict(type='bool', default=False),
state=dict(default='present', choices=['present', 'absent']),
)
) )
module = AnsibleAWSModule( module = AnsibleAWSModule(

View file

@ -130,12 +130,11 @@ delete_parameter:
''' '''
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info
try: try:
from botocore.exceptions import ClientError from botocore.exceptions import ClientError
except ImportError: except ImportError:
pass # will be captured by imported HAS_BOTO3 pass # Handled by AnsibleAWSModule
def update_parameter(client, module, args): def update_parameter(client, module, args):
@ -225,8 +224,7 @@ def delete_parameter(client, module):
def setup_client(module): def setup_client(module):
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True) connection = module.client('ssm')
connection = boto3_conn(module, conn_type='client', resource='ssm', region=region, endpoint=ec2_url, **aws_connect_params)
return connection return connection

View file

@ -406,7 +406,6 @@ except ImportError:
pass # handled by AnsibleAWSModule pass # handled by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, ec2_argument_spec
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, AWSRetry, compare_policies from ansible.module_utils.ec2 import camel_dict_to_snake_dict, AWSRetry, compare_policies
from ansible.module_utils.aws.waf import run_func_with_change_token_backoff, MATCH_LOOKUP from ansible.module_utils.aws.waf import run_func_with_change_token_backoff, MATCH_LOOKUP
from ansible.module_utils.aws.waf import get_rule_with_backoff, list_rules_with_backoff, list_regional_rules_with_backoff from ansible.module_utils.aws.waf import get_rule_with_backoff, list_rules_with_backoff, list_regional_rules_with_backoff
@ -706,24 +705,20 @@ def main():
ip_address=dict(), ip_address=dict(),
regex_pattern=dict(), regex_pattern=dict(),
) )
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( name=dict(required=True),
dict( type=dict(required=True, choices=['byte', 'geo', 'ip', 'regex', 'size', 'sql', 'xss']),
name=dict(required=True), filters=dict(type='list'),
type=dict(required=True, choices=['byte', 'geo', 'ip', 'regex', 'size', 'sql', 'xss']), purge_filters=dict(type='bool', default=False),
filters=dict(type='list'), waf_regional=dict(type='bool', default=False),
purge_filters=dict(type='bool', default=False), state=dict(default='present', choices=['present', 'absent']),
waf_regional=dict(type='bool', default=False),
state=dict(default='present', choices=['present', 'absent']),
),
) )
module = AnsibleAWSModule(argument_spec=argument_spec, module = AnsibleAWSModule(argument_spec=argument_spec,
required_if=[['state', 'present', ['filters']]]) required_if=[['state', 'present', ['filters']]])
state = module.params.get('state') state = module.params.get('state')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
resource = 'waf' if not module.params['waf_regional'] else 'waf-regional' resource = 'waf' if not module.params['waf_regional'] else 'waf-regional'
client = boto3_conn(module, conn_type='client', resource=resource, region=region, endpoint=ec2_url, **aws_connect_kwargs) client = module.client(resource)
condition = Condition(client, module) condition = Condition(client, module)

View file

@ -120,25 +120,20 @@ wafs:
''' '''
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
from ansible.module_utils.aws.waf import list_web_acls, get_web_acl from ansible.module_utils.aws.waf import list_web_acls, get_web_acl
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( name=dict(required=False),
dict( waf_regional=dict(type='bool', default=False)
name=dict(required=False),
waf_regional=dict(type='bool', default=False),
)
) )
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
if module._name == 'aws_waf_facts': if module._name == 'aws_waf_facts':
module.deprecate("The 'aws_waf_facts' module has been renamed to 'aws_waf_info'", version='2.13') module.deprecate("The 'aws_waf_facts' module has been renamed to 'aws_waf_info'", version='2.13')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
resource = 'waf' if not module.params['waf_regional'] else 'waf-regional' resource = 'waf' if not module.params['waf_regional'] else 'waf-regional'
client = boto3_conn(module, conn_type='client', resource=resource, region=region, endpoint=ec2_url, **aws_connect_kwargs) client = module.client(resource)
web_acls = list_web_acls(client, module) web_acls = list_web_acls(client, module)
name = module.params['name'] name = module.params['name']
if name: if name:

View file

@ -150,7 +150,6 @@ except ImportError:
pass # handled by AnsibleAWSModule pass # handled by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, ec2_argument_spec
from ansible.module_utils.ec2 import camel_dict_to_snake_dict from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.aws.waf import run_func_with_change_token_backoff, list_rules_with_backoff, list_regional_rules_with_backoff, MATCH_LOOKUP from ansible.module_utils.aws.waf import run_func_with_change_token_backoff, list_rules_with_backoff, list_regional_rules_with_backoff, MATCH_LOOKUP
from ansible.module_utils.aws.waf import get_web_acl_with_backoff, list_web_acls_with_backoff, list_regional_web_acls_with_backoff from ansible.module_utils.aws.waf import get_web_acl_with_backoff, list_web_acls_with_backoff, list_regional_web_acls_with_backoff
@ -331,23 +330,19 @@ def ensure_rule_absent(client, module):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( name=dict(required=True),
dict( metric_name=dict(),
name=dict(required=True), state=dict(default='present', choices=['present', 'absent']),
metric_name=dict(), conditions=dict(type='list'),
state=dict(default='present', choices=['present', 'absent']), purge_conditions=dict(type='bool', default=False),
conditions=dict(type='list'), waf_regional=dict(type='bool', default=False),
purge_conditions=dict(type='bool', default=False),
waf_regional=dict(type='bool', default=False),
),
) )
module = AnsibleAWSModule(argument_spec=argument_spec) module = AnsibleAWSModule(argument_spec=argument_spec)
state = module.params.get('state') state = module.params.get('state')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
resource = 'waf' if not module.params['waf_regional'] else 'waf-regional' resource = 'waf' if not module.params['waf_regional'] else 'waf-regional'
client = boto3_conn(module, conn_type='client', resource=resource, region=region, endpoint=ec2_url, **aws_connect_kwargs) client = module.client(resource)
if state == 'present': if state == 'present':
(changed, results) = ensure_rule_present(client, module) (changed, results) = ensure_rule_present(client, module)
else: else:

View file

@ -169,7 +169,7 @@ import re
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.aws.waiters import get_waiter from ansible.module_utils.aws.waiters import get_waiter
from ansible.module_utils.ec2 import boto3_conn, get_aws_connection_info, ec2_argument_spec, camel_dict_to_snake_dict from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.aws.waf import list_rules_with_backoff, list_web_acls_with_backoff, list_regional_web_acls_with_backoff, \ from ansible.module_utils.aws.waf import list_rules_with_backoff, list_web_acls_with_backoff, list_regional_web_acls_with_backoff, \
run_func_with_change_token_backoff, list_regional_rules_with_backoff run_func_with_change_token_backoff, list_regional_rules_with_backoff
@ -332,25 +332,21 @@ def ensure_web_acl_absent(client, module):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( name=dict(required=True),
dict( default_action=dict(choices=['block', 'allow', 'count']),
name=dict(required=True), metric_name=dict(),
default_action=dict(choices=['block', 'allow', 'count']), state=dict(default='present', choices=['present', 'absent']),
metric_name=dict(), rules=dict(type='list'),
state=dict(default='present', choices=['present', 'absent']), purge_rules=dict(type='bool', default=False),
rules=dict(type='list'), waf_regional=dict(type='bool', default=False)
purge_rules=dict(type='bool', default=False),
waf_regional=dict(type='bool', default=False),
),
) )
module = AnsibleAWSModule(argument_spec=argument_spec, module = AnsibleAWSModule(argument_spec=argument_spec,
required_if=[['state', 'present', ['default_action', 'rules']]]) required_if=[['state', 'present', ['default_action', 'rules']]])
state = module.params.get('state') state = module.params.get('state')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
resource = 'waf' if not module.params['waf_regional'] else 'waf-regional' resource = 'waf' if not module.params['waf_regional'] else 'waf-regional'
client = boto3_conn(module, conn_type='client', resource=resource, region=region, endpoint=ec2_url, **aws_connect_kwargs) client = module.client(resource)
if state == 'present': if state == 'present':
(changed, results) = ensure_web_acl_present(client, module) (changed, results) = ensure_web_acl_present(client, module)
else: else:

View file

@ -1379,10 +1379,8 @@ from ansible.module_utils._text import to_text, to_native
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.aws.cloudfront_facts import CloudFrontFactsServiceManager from ansible.module_utils.aws.cloudfront_facts import CloudFrontFactsServiceManager
from ansible.module_utils.common.dict_transformations import recursive_diff from ansible.module_utils.common.dict_transformations import recursive_diff
from ansible.module_utils.ec2 import get_aws_connection_info from ansible.module_utils.ec2 import compare_aws_tags, ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict
from ansible.module_utils.ec2 import ec2_argument_spec, boto3_conn, compare_aws_tags from ansible.module_utils.ec2 import camel_dict_to_snake_dict, snake_dict_to_camel_dict
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, ansible_dict_to_boto3_tag_list
from ansible.module_utils.ec2 import snake_dict_to_camel_dict, boto3_tag_list_to_ansible_dict
import datetime import datetime
try: try:
@ -1396,7 +1394,7 @@ except ImportError:
try: try:
import botocore import botocore
except ImportError: except ImportError:
pass pass # caught by AnsibleAWSModule
def change_dict_key_name(dictionary, old_key, new_key): def change_dict_key_name(dictionary, old_key, new_key):
@ -2089,17 +2087,15 @@ class CloudFrontValidationManager(object):
attempts = 1 + int(wait_timeout / 60) attempts = 1 + int(wait_timeout / 60)
waiter.wait(Id=distribution_id, WaiterConfig={'MaxAttempts': attempts}) waiter.wait(Id=distribution_id, WaiterConfig={'MaxAttempts': attempts})
except botocore.exceptions.WaiterError as e: except botocore.exceptions.WaiterError as e:
self.module.fail_json(msg="Timeout waiting for CloudFront action. Waited for {0} seconds before timeout. " self.module.fail_json_aws(e, msg="Timeout waiting for CloudFront action."
"Error: {1}".format(to_text(wait_timeout), to_native(e))) " Waited for {0} seconds before timeout.".format(to_text(wait_timeout)))
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
self.module.fail_json_aws(e, msg="Error getting distribution {0}".format(distribution_id)) self.module.fail_json_aws(e, msg="Error getting distribution {0}".format(distribution_id))
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update(dict(
state=dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
caller_reference=dict(), caller_reference=dict(),
comment=dict(), comment=dict(),
@ -2130,7 +2126,7 @@ def main():
default_origin_path=dict(), default_origin_path=dict(),
wait=dict(default=False, type='bool'), wait=dict(default=False, type='bool'),
wait_timeout=dict(default=1800, type='int') wait_timeout=dict(default=1800, type='int')
)) )
result = {} result = {}
changed = True changed = True
@ -2145,8 +2141,7 @@ def main():
] ]
) )
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True) client = module.client('cloudfront')
client = boto3_conn(module, conn_type='client', resource='cloudfront', region=region, endpoint=ec2_url, **aws_connect_kwargs)
validation_mgr = CloudFrontValidationManager(module) validation_mgr = CloudFrontValidationManager(module)

View file

@ -138,8 +138,6 @@ location:
sample: https://cloudfront.amazonaws.com/2017-03-25/distribution/E1ZID6KZJECZY7/invalidation/I2G9MOWJZFV622 sample: https://cloudfront.amazonaws.com/2017-03-25/distribution/E1ZID6KZJECZY7/invalidation/I2G9MOWJZFV622
''' '''
from ansible.module_utils.ec2 import get_aws_connection_info
from ansible.module_utils.ec2 import ec2_argument_spec, boto3_conn
from ansible.module_utils.ec2 import snake_dict_to_camel_dict from ansible.module_utils.ec2 import snake_dict_to_camel_dict
from ansible.module_utils.ec2 import camel_dict_to_snake_dict from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
@ -159,11 +157,7 @@ class CloudFrontInvalidationServiceManager(object):
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
self.create_client('cloudfront') self.client = module.client('cloudfront')
def create_client(self, resource):
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(self.module, boto3=True)
self.client = boto3_conn(self.module, conn_type='client', resource=resource, region=region, endpoint=ec2_url, **aws_connect_kwargs)
def create_invalidation(self, distribution_id, invalidation_batch): def create_invalidation(self, distribution_id, invalidation_batch):
current_invalidation_response = self.get_invalidation(distribution_id, invalidation_batch['CallerReference']) current_invalidation_response = self.get_invalidation(distribution_id, invalidation_batch['CallerReference'])
@ -251,14 +245,12 @@ class CloudFrontInvalidationValidationManager(object):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update(dict(
caller_reference=dict(), caller_reference=dict(),
distribution_id=dict(), distribution_id=dict(),
alias=dict(), alias=dict(),
target_paths=dict(required=True, type='list') target_paths=dict(required=True, type='list')
)) )
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=False, mutually_exclusive=[['distribution_id', 'alias']]) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=False, mutually_exclusive=[['distribution_id', 'alias']])

View file

@ -122,8 +122,6 @@ location:
''' '''
from ansible.module_utils.ec2 import get_aws_connection_info, ec2_argument_spec
from ansible.module_utils.ec2 import boto3_conn
from ansible.module_utils.aws.cloudfront_facts import CloudFrontFactsServiceManager from ansible.module_utils.aws.cloudfront_facts import CloudFrontFactsServiceManager
from ansible.module_utils.ec2 import camel_dict_to_snake_dict from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
@ -147,14 +145,7 @@ class CloudFrontOriginAccessIdentityServiceManager(object):
def __init__(self, module): def __init__(self, module):
self.module = module self.module = module
self.create_client('cloudfront') self.client = module.client('cloudfront')
def create_client(self, resource):
try:
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(self.module, boto3=True)
self.client = boto3_conn(self.module, conn_type='client', resource=resource, region=region, endpoint=ec2_url, **aws_connect_kwargs)
except (ClientError, BotoCoreError) as e:
self.module.fail_json_aws(e, msg="Unable to establish connection.")
def create_origin_access_identity(self, caller_reference, comment): def create_origin_access_identity(self, caller_reference, comment):
try: try:
@ -242,14 +233,12 @@ class CloudFrontOriginAccessIdentityValidationManager(object):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update(dict(
state=dict(choices=['present', 'absent'], default='present'), state=dict(choices=['present', 'absent'], default='present'),
origin_access_identity_id=dict(), origin_access_identity_id=dict(),
caller_reference=dict(), caller_reference=dict(),
comment=dict(), comment=dict(),
)) )
result = {} result = {}
e_tag = None e_tag = None

View file

@ -159,8 +159,7 @@ except ImportError:
pass # handled by AnsibleAWSModule pass # handled by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, camel_dict_to_snake_dict from ansible.module_utils.ec2 import camel_dict_to_snake_dict
from ansible.module_utils.ec2 import ec2_argument_spec, get_aws_connection_info
class CloudWatchEventRule(object): class CloudWatchEventRule(object):
@ -426,28 +425,16 @@ class CloudWatchEventRuleManager(object):
return description['state'] return description['state']
def get_cloudwatchevents_client(module):
"""Returns a boto3 client for accessing CloudWatch Events"""
region, ec2_url, aws_conn_kwargs = get_aws_connection_info(module, boto3=True)
return boto3_conn(module, conn_type='client',
resource='events',
region=region, endpoint=ec2_url,
**aws_conn_kwargs)
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( name=dict(required=True),
dict( schedule_expression=dict(),
name=dict(required=True), event_pattern=dict(),
schedule_expression=dict(), state=dict(choices=['present', 'disabled', 'absent'],
event_pattern=dict(), default='present'),
state=dict(choices=['present', 'disabled', 'absent'], description=dict(),
default='present'), role_arn=dict(),
description=dict(), targets=dict(type='list', default=[]),
role_arn=dict(),
targets=dict(type='list', default=[]),
)
) )
module = AnsibleAWSModule(argument_spec=argument_spec) module = AnsibleAWSModule(argument_spec=argument_spec)
@ -456,10 +443,9 @@ def main():
) )
targets = module.params.get('targets') targets = module.params.get('targets')
state = module.params.get('state') state = module.params.get('state')
client = module.client('events')
cwe_rule = CloudWatchEventRule(module, cwe_rule = CloudWatchEventRule(module, client=client, **rule_data)
client=get_cloudwatchevents_client(module),
**rule_data)
cwe_rule_manager = CloudWatchEventRuleManager(cwe_rule, targets) cwe_rule_manager = CloudWatchEventRuleManager(cwe_rule, targets)
if state == 'present': if state == 'present':

View file

@ -173,8 +173,7 @@ RETURN = ''' # '''
__metaclass__ = type __metaclass__ = type
import traceback import traceback
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, HAS_BOTO3, \ from ansible.module_utils.ec2 import camel_dict_to_snake_dict, AWSRetry
camel_dict_to_snake_dict, get_aws_connection_info, AWSRetry
try: try:
import botocore import botocore
except ImportError: except ImportError:
@ -228,18 +227,6 @@ def endpoint_exists(endpoint):
return bool(len(endpoint['Endpoints'])) return bool(len(endpoint['Endpoints']))
def get_dms_client(aws_connect_params, client_region, ec2_url):
client_params = dict(
module=module,
conn_type='client',
resource='dms',
region=client_region,
endpoint=ec2_url,
**aws_connect_params
)
return boto3_conn(**client_params)
def delete_dms_endpoint(connection): def delete_dms_endpoint(connection):
try: try:
endpoint = describe_endpoints(connection, endpoint = describe_endpoints(connection,
@ -448,13 +435,10 @@ def main():
) )
exit_message = None exit_message = None
changed = False changed = False
if not HAS_BOTO3:
module.fail_json(msg='boto3 required for this module')
state = module.params.get('state') state = module.params.get('state')
aws_config_region, ec2_url, aws_connect_params = \
get_aws_connection_info(module, boto3=True) dmsclient = module.client('dms')
dmsclient = get_dms_client(aws_connect_params, aws_config_region, ec2_url)
endpoint = describe_endpoints(dmsclient, endpoint = describe_endpoints(dmsclient,
module.params.get('endpointidentifier')) module.params.get('endpointidentifier'))
if state == 'present': if state == 'present':

View file

@ -61,11 +61,10 @@ EXAMPLES = '''
''' '''
RETURN = ''' # ''' RETURN = ''' # '''
__metaclass__ = type
import traceback import traceback
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, HAS_BOTO3, \ from ansible.module_utils.ec2 import camel_dict_to_snake_dict, AWSRetry
camel_dict_to_snake_dict, get_aws_connection_info, AWSRetry
try: try:
import botocore import botocore
except ImportError: except ImportError:
@ -103,18 +102,6 @@ def replication_subnet_group_delete(module, connection):
return connection.delete_replication_subnet_group(**delete_parameters) return connection.delete_replication_subnet_group(**delete_parameters)
def get_dms_client(module, aws_connect_params, client_region, ec2_url):
client_params = dict(
module=module,
conn_type='client',
resource='dms',
region=client_region,
endpoint=ec2_url,
**aws_connect_params
)
return boto3_conn(**client_params)
def replication_subnet_exists(subnet): def replication_subnet_exists(subnet):
""" Returns boolean based on the existence of the endpoint """ Returns boolean based on the existence of the endpoint
:param endpoint: dict containing the described endpoint :param endpoint: dict containing the described endpoint
@ -208,13 +195,9 @@ def main():
) )
exit_message = None exit_message = None
changed = False changed = False
if not HAS_BOTO3:
module.fail_json(msg='boto3 required for this module')
state = module.params.get('state') state = module.params.get('state')
aws_config_region, ec2_url, aws_connect_params = \ dmsclient = module.client('dms')
get_aws_connection_info(module, boto3=True)
dmsclient = get_dms_client(module, aws_connect_params, aws_config_region, ec2_url)
subnet_group = describe_subnet_group(dmsclient, subnet_group = describe_subnet_group(dmsclient,
module.params.get('identifier')) module.params.get('identifier'))
if state == 'present': if state == 'present':

View file

@ -364,14 +364,13 @@ snapshots_deleted:
''' '''
import time import time
from ansible.module_utils.ec2 import get_aws_connection_info, ec2_argument_spec, boto3_conn, camel_dict_to_snake_dict from ansible.module_utils.ec2 import ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict, camel_dict_to_snake_dict, compare_aws_tags
from ansible.module_utils.ec2 import ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict, compare_aws_tags
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
try: try:
import botocore import botocore
except ImportError: except ImportError:
pass pass # caught by AnsibleAWSModule
def get_block_device_mapping(image): def get_block_device_mapping(image):
@ -686,8 +685,7 @@ def rename_item_if_exists(dict_object, attribute, new_attribute, child_node=None
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update(dict(
instance_id=dict(), instance_id=dict(),
image_id=dict(), image_id=dict(),
architecture=dict(default='x86_64'), architecture=dict(default='x86_64'),
@ -710,7 +708,7 @@ def main():
ramdisk_id=dict(), ramdisk_id=dict(),
sriov_net_support=dict(), sriov_net_support=dict(),
purge_tags=dict(type='bool', default=False) purge_tags=dict(type='bool', default=False)
)) )
module = AnsibleAWSModule( module = AnsibleAWSModule(
argument_spec=argument_spec, argument_spec=argument_spec,
@ -724,11 +722,7 @@ def main():
if not any([module.params['image_id'], module.params['name']]): if not any([module.params['image_id'], module.params['name']]):
module.fail_json(msg="one of the following is required: name, image_id") module.fail_json(msg="one of the following is required: name, image_id")
try: connection = module.client('ec2')
region, ec2_url, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_kwargs)
except botocore.exceptions.NoRegionError:
module.fail_json(msg=("Region must be specified as a parameter in AWS_DEFAULT_REGION environment variable or in boto configuration file."))
if module.params.get('state') == 'absent': if module.params.get('state') == 'absent':
deregister_image(module, connection) deregister_image(module, connection)

View file

@ -140,15 +140,13 @@ image_id:
''' '''
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import ec2_argument_spec
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, ansible_dict_to_boto3_tag_list from ansible.module_utils.ec2 import camel_dict_to_snake_dict, ansible_dict_to_boto3_tag_list
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
try: try:
from botocore.exceptions import ClientError, NoCredentialsError, WaiterError, BotoCoreError from botocore.exceptions import ClientError, NoCredentialsError, WaiterError, BotoCoreError
HAS_BOTO3 = True
except ImportError: except ImportError:
HAS_BOTO3 = False pass # caught by AnsibleAWSModule
def copy_image(module, ec2): def copy_image(module, ec2):
@ -206,8 +204,7 @@ def copy_image(module, ec2):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update(dict(
source_region=dict(required=True), source_region=dict(required=True),
source_image_id=dict(required=True), source_image_id=dict(required=True),
name=dict(default='default'), name=dict(default='default'),
@ -216,7 +213,7 @@ def main():
kms_key_id=dict(type='str', required=False), kms_key_id=dict(type='str', required=False),
wait=dict(type='bool', default=False), wait=dict(type='bool', default=False),
wait_timeout=dict(type='int', default=600), wait_timeout=dict(type='int', default=600),
tags=dict(type='dict')), tags=dict(type='dict'),
tag_equality=dict(type='bool', default=False)) tag_equality=dict(type='bool', default=False))
module = AnsibleAWSModule(argument_spec=argument_spec) module = AnsibleAWSModule(argument_spec=argument_spec)

View file

@ -205,11 +205,10 @@ images:
try: try:
from botocore.exceptions import ClientError, BotoCoreError from botocore.exceptions import ClientError, BotoCoreError
except ImportError: except ImportError:
pass pass # caught by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (boto3_conn, ec2_argument_spec, get_aws_connection_info, ansible_dict_to_boto3_filter_list, from ansible.module_utils.ec2 import ansible_dict_to_boto3_filter_list, camel_dict_to_snake_dict, boto3_tag_list_to_ansible_dict
camel_dict_to_snake_dict, boto3_tag_list_to_ansible_dict)
def list_ec2_images(ec2_client, module): def list_ec2_images(ec2_client, module):
@ -261,27 +260,19 @@ def list_ec2_images(ec2_client, module):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( image_ids=dict(default=[], type='list', aliases=['image_id']),
dict( filters=dict(default={}, type='dict'),
image_ids=dict(default=[], type='list', aliases=['image_id']), owners=dict(default=[], type='list', aliases=['owner']),
filters=dict(default={}, type='dict'), executable_users=dict(default=[], type='list', aliases=['executable_user']),
owners=dict(default=[], type='list', aliases=['owner']), describe_image_attributes=dict(default=False, type='bool')
executable_users=dict(default=[], type='list', aliases=['executable_user']),
describe_image_attributes=dict(default=False, type='bool')
)
) )
module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True) module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)
if module._module._name == 'ec2_ami_facts': if module._module._name == 'ec2_ami_facts':
module._module.deprecate("The 'ec2_ami_facts' module has been renamed to 'ec2_ami_info'", version='2.13') module._module.deprecate("The 'ec2_ami_facts' module has been renamed to 'ec2_ami_info'", version='2.13')
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True) ec2_client = module.client('ec2')
if region:
ec2_client = 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_images(ec2_client, module) list_ec2_images(ec2_client, module)

View file

@ -105,7 +105,6 @@ RETURN = '''
''' '''
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import boto3_conn, ec2_argument_spec, get_aws_connection_info
try: try:
import botocore import botocore
@ -222,28 +221,23 @@ def delete_lifecycle_hook(connection, module):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( autoscaling_group_name=dict(required=True, type='str'),
dict( lifecycle_hook_name=dict(required=True, type='str'),
autoscaling_group_name=dict(required=True, type='str'), transition=dict(type='str', choices=['autoscaling:EC2_INSTANCE_TERMINATING', 'autoscaling:EC2_INSTANCE_LAUNCHING']),
lifecycle_hook_name=dict(required=True, type='str'), role_arn=dict(type='str'),
transition=dict(type='str', choices=['autoscaling:EC2_INSTANCE_TERMINATING', 'autoscaling:EC2_INSTANCE_LAUNCHING']), notification_target_arn=dict(type='str'),
role_arn=dict(type='str'), notification_meta_data=dict(type='str'),
notification_target_arn=dict(type='str'), heartbeat_timeout=dict(type='int'),
notification_meta_data=dict(type='str'), default_result=dict(default='ABANDON', choices=['ABANDON', 'CONTINUE']),
heartbeat_timeout=dict(type='int'), state=dict(default='present', choices=['present', 'absent'])
default_result=dict(default='ABANDON', choices=['ABANDON', 'CONTINUE']),
state=dict(default='present', choices=['present', 'absent'])
)
) )
module = AnsibleAWSModule(argument_spec=argument_spec, module = AnsibleAWSModule(argument_spec=argument_spec,
required_if=[['state', 'present', ['transition']]]) required_if=[['state', 'present', ['transition']]])
state = module.params.get('state') state = module.params.get('state')
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True) connection = module.client('autoscaling')
connection = boto3_conn(module, conn_type='client', resource='autoscaling', region=region, endpoint=ec2_url, **aws_connect_params)
changed = False changed = False

View file

@ -88,9 +88,7 @@ except ImportError:
pass # caught by AnsibleAWSModule pass # caught by AnsibleAWSModule
from ansible.module_utils.aws.core import AnsibleAWSModule from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (ansible_dict_to_boto3_filter_list, from ansible.module_utils.ec2 import ansible_dict_to_boto3_filter_list, boto3_tag_list_to_ansible_dict, camel_dict_to_snake_dict
boto3_conn, boto3_tag_list_to_ansible_dict, camel_dict_to_snake_dict,
ec2_argument_spec, get_aws_connection_info)
def date_handler(obj): def date_handler(obj):
@ -119,12 +117,9 @@ def list_customer_gateways(connection, module):
def main(): def main():
argument_spec = ec2_argument_spec() argument_spec = dict(
argument_spec.update( customer_gateway_ids=dict(default=[], type='list'),
dict( filters=dict(default={}, type='dict')
customer_gateway_ids=dict(default=[], type='list'),
filters=dict(default={}, type='dict')
)
) )
module = AnsibleAWSModule(argument_spec=argument_spec, module = AnsibleAWSModule(argument_spec=argument_spec,
@ -133,9 +128,7 @@ def main():
if module._module._name == 'ec2_customer_gateway_facts': if module._module._name == 'ec2_customer_gateway_facts':
module._module.deprecate("The 'ec2_customer_gateway_facts' module has been renamed to 'ec2_customer_gateway_info'", version='2.13') module._module.deprecate("The 'ec2_customer_gateway_facts' module has been renamed to 'ec2_customer_gateway_info'", version='2.13')
region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True) connection = module.client('ec2')
connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_params)
list_customer_gateways(connection, module) list_customer_gateways(connection, module)