Skip to content

Commit

Permalink
AnsibleAWSModule related cleanup (batch 2) (ansible#65987)
Browse files Browse the repository at this point in the history
* Remove redundant use of ec2_argument_spec where we're using AnsibleAWSModule

* Use module.client() instead of the get_aws_connection_info/boto3_conn combo.

* AnsibleAWSModule handles 'HAS_BOTO3'

* Remove unused imports

* Update error message that lambda_policy integration test is looking for when the region's missing

* Revert redshift and s3_bucket
  • Loading branch information
tremble authored and jillr committed Jan 24, 2020
1 parent 70017e2 commit 0ceac57
Show file tree
Hide file tree
Showing 40 changed files with 212 additions and 390 deletions.
15 changes: 3 additions & 12 deletions lib/ansible/modules/cloud/amazon/_lambda_facts.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
'''

from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import camel_dict_to_snake_dict, get_aws_connection_info, boto3_conn
from ansible.module_utils.ec2 import camel_dict_to_snake_dict
import json
import datetime
import sys
Expand All @@ -100,7 +100,7 @@
try:
from botocore.exceptions import ClientError
except ImportError:
pass # protected by AnsibleAWSModule
pass # caught by AnsibleAWSModule


def fix_return(node):
Expand Down Expand Up @@ -361,16 +361,7 @@ def main():
if len(function_name) > 64:
module.fail_json(msg='Function name "{0}" exceeds 64 character limit'.format(function_name))

try:
region, endpoint, aws_connect_kwargs = get_aws_connection_info(module, boto3=True)
aws_connect_kwargs.update(dict(region=region,
endpoint=endpoint,
conn_type='client',
resource='lambda'
))
client = boto3_conn(module, **aws_connect_kwargs)
except ClientError as e:
module.fail_json_aws(e, "trying to set up boto connection")
client = module.client('lambda')

this_module = sys.modules[__name__]

Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/amazon/aws_caller_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # caught by imported HAS_BOTO3
pass # caught by AnsibleAWSModule


def main():
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/amazon/aws_codepipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@
try:
import botocore
except ImportError:
pass # will be detected by imported HAS_BOTO3
pass # caught by AnsibleAWSModule


def create_pipeline(client, name, role_arn, artifact_store, stages, version, module):
Expand Down
6 changes: 1 addition & 5 deletions lib/ansible/modules/cloud/amazon/aws_inspector_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import AWSRetry
from ansible.module_utils.ec2 import (
HAS_BOTO3,
ansible_dict_to_boto3_tag_list,
boto3_tag_list_to_ansible_dict,
camel_dict_to_snake_dict,
Expand All @@ -114,7 +113,7 @@
try:
import botocore
except ImportError:
pass # caught by imported HAS_BOTO3
pass # caught by AnsibleAWSModule


@AWSRetry.backoff(tries=5, delay=5, backoff=2.0)
Expand All @@ -133,9 +132,6 @@ def main():
required_if=required_if,
)

if not HAS_BOTO3:
module.fail_json(msg='boto3 and botocore are required for this module')

name = module.params.get('name')
state = module.params.get('state').lower()
tags = module.params.get('tags')
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/amazon/aws_ses_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # caught by imported HAS_BOTO3
pass # caught by AnsibleAWSModule


def get_verification_attributes(connection, module, identity, retries=0, retryDelay=10):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # caught by imported HAS_BOTO3
pass # caught by AnsibleAWSModule


def get_identity_policy(connection, module, identity, policy_name):
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/amazon/aws_sgw_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@
try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # caught by imported HAS_BOTO3
pass # caught by AnsibleAWSModule


class SGWInformationManager(object):
Expand Down
9 changes: 4 additions & 5 deletions lib/ansible/modules/cloud/amazon/ec2_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,10 @@
try:
import botocore.exceptions
except ImportError:
pass # Taken care of by ec2.HAS_BOTO3
pass # caught by AnsibleAWSModule

from ansible.module_utils.aws.core import AnsibleAWSModule, is_boto3_error_code
from ansible.module_utils.ec2 import AWSRetry, ansible_dict_to_boto3_filter_list, ec2_argument_spec
from ansible.module_utils.ec2 import AWSRetry, ansible_dict_to_boto3_filter_list


def associate_ip_and_device(ec2, module, address, private_ip_address, device_id, allow_reassociation, check_mode, is_instance=True):
Expand Down Expand Up @@ -529,8 +529,7 @@ def generate_tag_dict(module, tag_name, tag_value):


def main():
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
argument_spec = dict(
device_id=dict(required=False, aliases=['instance_id']),
public_ip=dict(required=False, aliases=['ip']),
state=dict(required=False, default='present',
Expand All @@ -545,7 +544,7 @@ def main():
tag_name=dict(),
tag_value=dict(),
public_ipv4_pool=dict()
))
)

module = AnsibleAWSModule(
argument_spec=argument_spec,
Expand Down
12 changes: 4 additions & 8 deletions lib/ansible/modules/cloud/amazon/ec2_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,16 +808,13 @@
import boto3
import botocore.exceptions
except ImportError:
pass
pass # caught by AnsibleAWSModule

from ansible.module_utils.six import text_type, string_types
from ansible.module_utils.six.moves.urllib import parse as urlparse
from ansible.module_utils._text import to_bytes, to_native
import ansible.module_utils.ec2 as ec2_utils
from ansible.module_utils.ec2 import (boto3_conn,
ec2_argument_spec,
get_aws_connection_info,
AWSRetry,
from ansible.module_utils.ec2 import (AWSRetry,
ansible_dict_to_boto3_filter_list,
compare_aws_tags,
boto3_tag_list_to_ansible_dict,
Expand Down Expand Up @@ -1675,8 +1672,7 @@ def run_instances(ec2, **instance_spec):

def main():
global module
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
argument_spec = dict(
state=dict(default='present', choices=['present', 'started', 'running', 'stopped', 'restarted', 'rebooted', 'terminated', 'absent']),
wait=dict(default=True, type='bool'),
wait_timeout=dict(default=600, type='int'),
Expand Down Expand Up @@ -1711,7 +1707,7 @@ def main():
instance_ids=dict(default=[], type='list'),
network=dict(default=None, type='dict'),
volumes=dict(default=None, type='list'),
))
)
# running/present are synonyms
# as are terminated/absent
module = AnsibleAWSModule(
Expand Down
24 changes: 9 additions & 15 deletions lib/ansible/modules/cloud/amazon/ec2_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@
import uuid

from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import ec2_argument_spec, get_aws_connection_info, boto3_conn
from ansible.module_utils._text import to_bytes

try:
from botocore.exceptions import ClientError
except ImportError:
pass
pass # caught by AnsibleAWSModule


def extract_key_data(key):
Expand Down Expand Up @@ -242,23 +241,18 @@ def delete_key_pair(module, ec2_client, name, finish_task=True):

def main():

argument_spec = ec2_argument_spec()
argument_spec.update(
dict(
name=dict(required=True),
key_material=dict(),
force=dict(type='bool', default=True),
state=dict(default='present', choices=['present', 'absent']),
wait=dict(type='bool', removed_in_version='2.14'),
wait_timeout=dict(type='int', removed_in_version='2.14')
)
argument_spec = dict(
name=dict(required=True),
key_material=dict(),
force=dict(type='bool', default=True),
state=dict(default='present', choices=['present', 'absent']),
wait=dict(type='bool', removed_in_version='2.14'),
wait_timeout=dict(type='int', removed_in_version='2.14')
)

module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)

region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)

ec2_client = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_params)
ec2_client = module.client('ec2')

name = module.params['name']
state = module.params.get('state')
Expand Down
23 changes: 6 additions & 17 deletions lib/ansible/modules/cloud/amazon/ec2_placement_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@
'''

from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (AWSRetry,
boto3_conn,
ec2_argument_spec,
get_aws_connection_info)
from ansible.module_utils.ec2 import AWSRetry
try:
from botocore.exceptions import (BotoCoreError, ClientError)
except ImportError:
Expand Down Expand Up @@ -167,26 +164,18 @@ def delete_placement_group(connection, module):


def main():
argument_spec = ec2_argument_spec()
argument_spec.update(
dict(
name=dict(required=True, type='str'),
state=dict(default='present', choices=['present', 'absent']),
strategy=dict(default='cluster', choices=['cluster', 'spread'])
)
argument_spec = dict(
name=dict(required=True, type='str'),
state=dict(default='present', choices=['present', 'absent']),
strategy=dict(default='cluster', choices=['cluster', 'spread'])
)

module = AnsibleAWSModule(
argument_spec=argument_spec,
supports_check_mode=True
)

region, ec2_url, aws_connect_params = get_aws_connection_info(
module, boto3=True)

connection = boto3_conn(module,
resource='ec2', conn_type='client',
region=region, endpoint=ec2_url, **aws_connect_params)
connection = module.client('ec2')

state = module.params.get("state")

Expand Down
20 changes: 4 additions & 16 deletions lib/ansible/modules/cloud/amazon/ec2_placement_group_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,10 @@
'''

from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (connect_to_aws,
boto3_conn,
ec2_argument_spec,
get_aws_connection_info)
try:
from botocore.exceptions import (BotoCoreError, ClientError)
except ImportError:
pass # caught by imported HAS_BOTO3
pass # caught by AnsibleAWSModule


def get_placement_groups_details(connection, module):
Expand Down Expand Up @@ -112,11 +108,8 @@ def get_placement_groups_details(connection, module):


def main():
argument_spec = ec2_argument_spec()
argument_spec.update(
dict(
names=dict(type='list', default=[])
)
argument_spec = dict(
names=dict(type='list', default=[])
)

module = AnsibleAWSModule(
Expand All @@ -126,12 +119,7 @@ def main():
if module._module._name == 'ec2_placement_group_facts':
module._module.deprecate("The 'ec2_placement_group_facts' module has been renamed to 'ec2_placement_group_info'", version='2.13')

region, ec2_url, aws_connect_params = get_aws_connection_info(
module, boto3=True)

connection = boto3_conn(module,
resource='ec2', conn_type='client',
region=region, endpoint=ec2_url, **aws_connect_params)
connection = module.client('ec2')

placement_groups = get_placement_groups_details(connection, module)
module.exit_json(changed=False, placement_groups=placement_groups)
Expand Down
17 changes: 5 additions & 12 deletions lib/ansible/modules/cloud/amazon/ec2_vpc_egress_igw.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,12 @@


from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (
boto3_conn,
ec2_argument_spec,
get_aws_connection_info,
camel_dict_to_snake_dict
)
from ansible.module_utils.ec2 import camel_dict_to_snake_dict

try:
import botocore
except ImportError:
pass # will be picked up by HAS_BOTO3 in AnsibleAWSModule
pass # caught by AnsibleAWSModule


def delete_eigw(module, conn, eigw_id):
Expand Down Expand Up @@ -167,16 +162,14 @@ def describe_eigws(module, conn, vpc_id):


def main():
argument_spec = ec2_argument_spec()
argument_spec.update(dict(
argument_spec = dict(
vpc_id=dict(required=True),
state=dict(default='present', choices=['present', 'absent'])
))
)

module = AnsibleAWSModule(argument_spec=argument_spec, supports_check_mode=True)

region, ec2_url, aws_connect_params = get_aws_connection_info(module, boto3=True)
connection = boto3_conn(module, conn_type='client', resource='ec2', region=region, endpoint=ec2_url, **aws_connect_params)
connection = module.client('ec2')

vpc_id = module.params.get('vpc_id')
state = module.params.get('state')
Expand Down
16 changes: 5 additions & 11 deletions lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,11 @@
try:
import botocore
except ImportError:
pass # Handled by AnsibleAWSModule
pass # caught by AnsibleAWSModule

from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (
AWSRetry,
boto3_conn,
ec2_argument_spec,
get_aws_connection_info,
camel_dict_to_snake_dict,
boto3_tag_list_to_ansible_dict,
ansible_dict_to_boto3_filter_list,
Expand Down Expand Up @@ -257,13 +254,10 @@ def ensure_igw_present(self, vpc_id, tags):


def main():
argument_spec = ec2_argument_spec()
argument_spec.update(
dict(
vpc_id=dict(required=True),
state=dict(default='present', choices=['present', 'absent']),
tags=dict(default=dict(), required=False, type='dict', aliases=['resource_tags'])
)
argument_spec = dict(
vpc_id=dict(required=True),
state=dict(default='present', choices=['present', 'absent']),
tags=dict(default=dict(), required=False, type='dict', aliases=['resource_tags'])
)

module = AnsibleAWSModule(
Expand Down
Loading

0 comments on commit 0ceac57

Please sign in to comment.