Skip to content

Commit

Permalink
Black-en plugins/plugin_utils (#1375)
Browse files Browse the repository at this point in the history
Black-en plugins/plugin_utils

SUMMARY
Minor linting related changes to plugins/plugin_utils
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
plugins/plugin_utils
ADDITIONAL INFORMATION
This code hasn't been released yet.

Reviewed-by: Jill R
Reviewed-by: Alina Buzachis
  • Loading branch information
tremble authored Feb 20, 2023
1 parent e6e58bf commit c90a6b0
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 30 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/1375-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
trivial:
- plugins/plugin_utils - minor dark / pylint cleanup (https://github.com/ansible-collections/amazon.aws/pull/1375).
9 changes: 3 additions & 6 deletions plugins/plugin_utils/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@


class AWSPluginBase:

def warn(self, message):
display.warning(message)

Expand All @@ -40,22 +39,20 @@ def client(self, service, retry_decorator=None, **extra_params):
region, endpoint_url, aws_connect_kwargs = get_aws_connection_info(self)
kw_args = dict(region=region, endpoint=endpoint_url, **aws_connect_kwargs)
kw_args.update(extra_params)
conn = boto3_conn(self, conn_type='client', resource=service, **kw_args)
conn = boto3_conn(self, conn_type="client", resource=service, **kw_args)
return conn if retry_decorator is None else RetryingBotoClientWrapper(conn, retry_decorator)

def resource(self, service, **extra_params):
region, endpoint_url, aws_connect_kwargs = get_aws_connection_info(self)
kw_args = dict(region=region, endpoint=endpoint_url, **aws_connect_kwargs)
kw_args.update(extra_params)
return boto3_conn(self, conn_type='resource', resource=service, **kw_args)
return boto3_conn(self, conn_type="resource", resource=service, **kw_args)

@property
def region(self):
return get_aws_region(self)

def require_aws_sdk(self, botocore_version=None, boto3_version=None):
return check_sdk_version_supported(
botocore_version=botocore_version,
boto3_version=boto3_version,
warn=self.warn
botocore_version=botocore_version, boto3_version=boto3_version, warn=self.warn
)
19 changes: 11 additions & 8 deletions plugins/plugin_utils/botocore.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ def boto3_conn(plugin, conn_type=None, resource=None, region=None, endpoint=None
try:
return _boto3_conn(conn_type=conn_type, resource=resource, region=region, endpoint=endpoint, **params)
except ValueError as e:
plugin.fail_aws("Couldn't connect to AWS: {0}".format(to_native(e)))
except (botocore.exceptions.ProfileNotFound, botocore.exceptions.PartialCredentialsError,
botocore.exceptions.NoCredentialsError, botocore.exceptions.ConfigParseError) as e:
plugin.fail_aws(f"Couldn't connect to AWS: {to_native(e)}")
except (
botocore.exceptions.ProfileNotFound,
botocore.exceptions.PartialCredentialsError,
botocore.exceptions.NoCredentialsError,
botocore.exceptions.ConfigParseError,
) as e:
plugin.fail_aws(to_native(e))
except botocore.exceptions.NoRegionError:
# ansible_name is added in 2.14
if hasattr(plugin, 'ansible_name'):
if hasattr(plugin, "ansible_name"):
plugin.fail_aws(
"The {0} plugin requires a region and none was found in configuration, "
"environment variables or module parameters".format(plugin.ansible_name)
f"The {plugin.ansible_name} plugin requires a region and none was found in configuration, "
"environment variables or module parameters"
)
plugin.fail_aws(
"A region is required and none was found in configuration, "
"environment variables or module parameters"
"A region is required and none was found in configuration, environment variables or module parameters"
)


Expand Down
1 change: 0 additions & 1 deletion plugins/plugin_utils/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class AWSConnectionBase(AWSPluginBase, ConnectionBase):

def _do_fail(self, message):
raise AnsibleConnectionFailure(message)

Expand Down
31 changes: 17 additions & 14 deletions plugins/plugin_utils/inventory.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
# Copyright: (c) 2022, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

try:
import boto3
import botocore
Expand All @@ -26,12 +23,15 @@ def _boto3_session(profile_name=None):


class AWSInventoryBase(BaseInventoryPlugin, Constructable, Cacheable, AWSPluginBase):

class TemplatedOptions:
# When someone looks up the TEMPLATABLE_OPTIONS using get() any templates
# will be templated using the loader passed to parse.
TEMPLATABLE_OPTIONS = (
"access_key", "secret_key", "session_token", "profile", "iam_role_name",
"access_key",
"secret_key",
"session_token",
"profile",
"iam_role_name",
)

def __init__(self, templar, options):
Expand Down Expand Up @@ -65,6 +65,7 @@ def __init__(self):
super().__init__()
self._frozen_credentials = {}

# pylint: disable=too-many-arguments
def parse(self, inventory, loader, path, cache=True, botocore_version=None, boto3_version=None):
super().parse(inventory, loader, path)
self.require_aws_sdk(botocore_version=botocore_version, boto3_version=boto3_version)
Expand Down Expand Up @@ -145,24 +146,26 @@ def _boto3_regions(self, service):
return regions

# fallback to local list hardcoded in boto3 if still no regions
session = _boto3_session(options.get('profile'))
session = _boto3_session(options.get("profile"))
regions = session.get_available_regions(service)

if not regions:
# I give up, now you MUST give me regions
self.fail_aws('Unable to get regions list from available methods, you must specify the "regions" option to continue.')
self.fail_aws(
"Unable to get regions list from available methods, you must specify the 'regions' option to continue."
)

return regions

def all_clients(self, service):
"""
Generator that yields a boto3 client and the region
Generator that yields a boto3 client and the region
:param service: The boto3 service to connect to.
:param service: The boto3 service to connect to.
Note: For services which don't support 'DescribeRegions' this may include bad
endpoints, and as such EndpointConnectionError should be cleanly handled as a non-fatal
error.
Note: For services which don't support 'DescribeRegions' this may include bad
endpoints, and as such EndpointConnectionError should be cleanly handled as a non-fatal
error.
"""
regions = self._boto3_regions(service=service)

Expand Down Expand Up @@ -202,8 +205,8 @@ def update_cached_result(self, path, cache, result):

def verify_file(self, path):
"""
:param path: the path to the inventory config file
:return the contents of the config file
:param path: the path to the inventory config file
:return the contents of the config file
"""
if not super().verify_file(path):
return False
Expand Down
1 change: 0 additions & 1 deletion plugins/plugin_utils/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class AWSLookupBase(AWSPluginBase, LookupBase):

def _do_fail(self, message):
raise AnsibleLookupError(message)

Expand Down

0 comments on commit c90a6b0

Please sign in to comment.