Skip to content

Commit

Permalink
Move module_utils out of subdir and update imports
Browse files Browse the repository at this point in the history
* Use relative imports
* Always get dict transformations from their canonical home
* Split imports by line to match tremble's other refactoring
* Make import order more consistent
  • Loading branch information
flowerysong committed May 14, 2020
1 parent ed25a62 commit 8ca3a67
Show file tree
Hide file tree
Showing 44 changed files with 281 additions and 210 deletions.
11 changes: 7 additions & 4 deletions plugins/lookup/aws_account_attribute.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
(or all attributes if one is not specified).
"""

import os

from ansible.errors import AnsibleError

try:
Expand All @@ -60,12 +62,13 @@
except ImportError:
raise AnsibleError("The lookup aws_account_attribute requires boto3 and botocore.")

from ansible.plugins import AnsiblePlugin
from ansible.plugins.lookup import LookupBase
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_conn, get_aws_connection_info
from ansible.module_utils._text import to_native
from ansible.module_utils.six import string_types
import os
from ansible.plugins import AnsiblePlugin
from ansible.plugins.lookup import LookupBase

from ..module_utils.ec2 import boto3_conn
from ..module_utils.ec2 import get_aws_connection_info


def _boto3_conn(region, credentials):
Expand Down
14 changes: 8 additions & 6 deletions plugins/lookup/aws_ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,21 @@
'''

from ansible.module_utils._text import to_native
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import HAS_BOTO3, boto3_tag_list_to_ansible_dict
from ansible.errors import AnsibleError
from ansible.plugins.lookup import LookupBase
from ansible.utils.display import Display

try:
from botocore.exceptions import ClientError
import botocore
import boto3
except ImportError:
pass # will be captured by imported HAS_BOTO3

from ansible.errors import AnsibleError
from ansible.module_utils._text import to_native
from ansible.plugins.lookup import LookupBase
from ansible.utils.display import Display

from ..module_utils.ec2 import HAS_BOTO3
from ..module_utils.ec2 import boto3_tag_list_to_ansible_dict

display = Display()


Expand Down
19 changes: 10 additions & 9 deletions plugins/module_utils/aws/acm.py → plugins/module_utils/acm.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,21 @@
Common Amazon Certificate Manager facts shared between modules
"""
import traceback
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info, boto3_conn
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import (camel_dict_to_snake_dict,
AWSRetry,
HAS_BOTO3,
boto3_tag_list_to_ansible_dict,
ansible_dict_to_boto3_tag_list)
from ansible.module_utils._text import to_bytes


try:
import botocore
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass # caught by imported HAS_BOTO3
pass

from ansible.module_utils._text import to_bytes
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from .ec2 import AWSRetry
from .ec2 import ansible_dict_to_boto3_tag_list
from .ec2 import boto3_conn
from .ec2 import boto3_tag_list_to_ansible_dict
from .ec2 import get_aws_connection_info


class ACMServiceManager(object):
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info, boto3_conn, snake_dict_to_camel_dict

try:
from botocore.exceptions import ClientError
except ImportError:
pass # Handled by HAS_BOTO3
pass

from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict

from .ec2 import boto3_conn
from .ec2 import get_aws_connection_info


class AWSConnection(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info, boto3_conn
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_tag_list_to_ansible_dict, camel_dict_to_snake_dict

try:
import botocore
except ImportError:
pass

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from .ec2 import boto3_conn
from .ec2 import boto3_tag_list_to_ansible_dict
from .ec2 import get_aws_connection_info


class CloudFrontFactsServiceManager(object):
"""Handles CloudFront Facts Services"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,14 @@
from io import StringIO

from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
from ansible.module_utils._text import to_native
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import HAS_BOTO3, camel_dict_to_snake_dict, ec2_argument_spec, boto3_conn
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info, get_aws_region

from .ec2 import HAS_BOTO3
from .ec2 import boto3_conn
from .ec2 import ec2_argument_spec
from .ec2 import get_aws_connection_info
from .ec2 import get_aws_region

# We will also export HAS_BOTO3 so end user modules can use it.
__all__ = ('AnsibleAWSModule', 'HAS_BOTO3', 'is_boto3_error_code')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
__metaclass__ = type

import traceback

try:
import botocore
except ImportError:
pass
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict


class DirectConnectError(Exception):
Expand Down
21 changes: 13 additions & 8 deletions plugins/module_utils/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@
import sys
import traceback

from ansible.module_utils._text import to_native
from ansible.module_utils._text import to_text
from ansible.module_utils.ansible_release import __version__
from ansible.module_utils.basic import missing_required_lib, env_fallback
from ansible.module_utils._text import to_native, to_text
from ansible_collections.amazon.aws.plugins.module_utils.cloud import CloudRetry
from ansible.module_utils.six import string_types, binary_type, text_type
from ansible.module_utils.common.dict_transformations import (
camel_dict_to_snake_dict, snake_dict_to_camel_dict,
_camel_to_snake, _snake_to_camel,
)
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.basic import missing_required_lib
from ansible.module_utils.common.dict_transformations import _camel_to_snake
from ansible.module_utils.common.dict_transformations import _snake_to_camel
from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
from ansible.module_utils.six import binary_type
from ansible.module_utils.six import string_types
from ansible.module_utils.six import text_type

from .cloud import CloudRetry

BOTO_IMP_ERR = None
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry

# Non-ansible imports
try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass

from .ec2 import AWSRetry


def get_elb(connection, module, elb_name):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

# Ansible imports
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict, get_ec2_security_group_ids_from_names, \
ansible_dict_to_boto3_tag_list, boto3_tag_list_to_ansible_dict, compare_policies as compare_dicts, \
AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.aws.elb_utils import get_elb, get_elb_listener, convert_tg_name_to_arn
import traceback
from copy import deepcopy

# Non-ansible imports
try:
from botocore.exceptions import BotoCoreError, ClientError
except ImportError:
pass
import traceback
from copy import deepcopy

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from .ec2 import AWSRetry
from .ec2 import ansible_dict_to_boto3_tag_list
from .ec2 import boto3_tag_list_to_ansible_dict
from .ec2 import get_ec2_security_group_ids_from_names
from .elb_utils import convert_tg_name_to_arn
from .elb_utils import get_elb
from .elb_utils import get_elb_listener


class ElasticLoadBalancerV2(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
try:
from botocore.exceptions import ClientError, NoCredentialsError
except ImportError:
pass # caught by HAS_BOTO3
pass

from ansible.module_utils._text import to_native

Expand Down
18 changes: 9 additions & 9 deletions plugins/module_utils/aws/rds.py → plugins/module_utils/rds.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible.module_utils._text import to_text
from ansible_collections.amazon.aws.plugins.module_utils.aws.waiters import get_waiter
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import (compare_aws_tags,
AWSRetry,
ansible_dict_to_boto3_tag_list,
boto3_tag_list_to_ansible_dict)
from collections import namedtuple
from time import sleep

try:
from botocore.exceptions import BotoCoreError, ClientError, WaiterError
except ImportError:
pass

from collections import namedtuple
from time import sleep
from ansible.module_utils._text import to_text
from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict

from .ec2 import AWSRetry
from .ec2 import ansible_dict_to_boto3_tag_list
from .ec2 import boto3_tag_list_to_ansible_dict
from .ec2 import compare_aws_tags
from .waiters import get_waiter

Boto3ClientMethod = namedtuple('Boto3ClientMethod', ['name', 'waiter', 'operation_description', 'cluster', 'instance'])
# Whitelist boto3 client methods for cluster and instance resources
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@
import hmac
import operator

from ansible.module_utils.urls import open_url
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import boto3_conn, get_aws_connection_info, HAS_BOTO3
from ansible.module_utils.six.moves.urllib.parse import urlencode

try:
from boto3 import session
except ImportError:
pass

from ansible.module_utils.six.moves.urllib.parse import urlencode
from ansible.module_utils.urls import open_url

from .ec2 import HAS_BOTO3
from .ec2 import boto3_conn
from .ec2 import get_aws_connection_info


def hexdigest(s):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict, AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.aws.waiters import get_waiter

try:
import botocore
except ImportError:
pass # caught by imported HAS_BOTO3

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from .ec2 import AWSRetry
from .waiters import get_waiter


MATCH_LOOKUP = {
'byte': {
Expand Down
File renamed without changes.
9 changes: 6 additions & 3 deletions plugins/modules/aws_az_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@
]"
'''

from ansible_collections.amazon.aws.plugins.module_utils.aws.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry, ansible_dict_to_boto3_filter_list, camel_dict_to_snake_dict

try:
from botocore.exceptions import ClientError, BotoCoreError
except ImportError:
pass # Handled by AnsibleAWSModule

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ..module_utils.core import AnsibleAWSModule
from ..module_utils.ec2 import AWSRetry
from ..module_utils.ec2 import ansible_dict_to_boto3_filter_list


def main():
argument_spec = dict(
Expand Down
9 changes: 5 additions & 4 deletions plugins/modules/aws_caller_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@
sample: 123456789012:my-federated-user-name
'''

from ansible_collections.amazon.aws.plugins.module_utils.aws.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import camel_dict_to_snake_dict

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

from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict

from ..module_utils.core import AnsibleAWSModule


def main():
Expand Down
17 changes: 11 additions & 6 deletions plugins/modules/aws_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,22 @@

import mimetypes
import os
from ansible.module_utils.six.moves.urllib.parse import urlparse
from ssl import SSLError
from ansible.module_utils.basic import to_text, to_native
from ansible_collections.amazon.aws.plugins.module_utils.aws.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.aws.s3 import calculate_etag, HAS_MD5
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import get_aws_connection_info, boto3_conn

try:
import botocore
except ImportError:
pass # will be detected by imported AnsibleAWSModule
pass # Handled by AnsibleAWSModule

from ansible.module_utils.basic import to_text
from ansible.module_utils.basic import to_native
from ansible.module_utils.six.moves.urllib.parse import urlparse

from ..module_utils.core import AnsibleAWSModule
from ..module_utils.ec2 import boto3_conn
from ..module_utils.ec2 import get_aws_connection_info
from ..module_utils.s3 import HAS_MD5
from ..module_utils.s3 import calculate_etag

IGNORE_S3_DROP_IN_EXCEPTIONS = ['XNotImplemented', 'NotImplemented']

Expand Down
Loading

0 comments on commit 8ca3a67

Please sign in to comment.