Skip to content

Commit

Permalink
Linting cleanup (ansible-collections#1181)
Browse files Browse the repository at this point in the history
Linting cleanup

SUMMARY
Minor linting cleanup

missing whitespace
unused variables
catching Exception
unused imports

in kms_key also replaces get_account_info with module_utils.iam.get_aws_account_info
ISSUE TYPE

Feature Pull Request

COMPONENT NAME
plugins/inventory/aws_ec2.py
plugins/module_utils/cloud.py
plugins/modules/autoscaling_group.py
plugins/modules/cloudtrail_info.py
plugins/modules/cloudwatchlogs_log_group.py
plugins/modules/ec2_eip.py
plugins/modules/kms_key_info.py
plugins/modules/lambda.py
plugins/modules/lambda_execute.py
plugins/modules/rds_cluster_snapshot.py
plugins/modules/rds_instance.py
plugins/modules/rds_instance_snapshot.py
plugins/modules/route53_health_check.py
plugins/modules/s3_object_info.py
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis <None>
Reviewed-by: Gonéri Le Bouder <goneri@lebouder.net>
  • Loading branch information
tremble authored Oct 18, 2022
1 parent d6068d5 commit e3f091e
Show file tree
Hide file tree
Showing 21 changed files with 56 additions and 94 deletions.
16 changes: 16 additions & 0 deletions changelogs/fragments/1181-linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
minor_changes:
- aws_ec2 inventory - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- cloud module_utils - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- autoscaling_group - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- cloudtrail_info - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- cloudwatchlogs_log_group - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- ec2_eip - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- kms_key_info - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- lambda - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- lambda_execute - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- rds_cluster_snapshot - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- rds_instance - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- rds_instance_snapshot - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- route53_health_check - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- s3_object_info - minor linting fixes (https://github.com/ansible-collections/amazon.aws/pull/1181).
- lambda - use common ``get_aws_account_info`` helper rather than reimplementing (https://github.com/ansible-collections/amazon.aws/pull/1181).
2 changes: 1 addition & 1 deletion plugins/inventory/aws_ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ def prepare_host_vars(cls, original_host_vars, hostvars_prefix=None, hostvars_su

if hostvars_prefix or hostvars_suffix:
for hostvar, hostval in host_vars.copy().items():
del(host_vars[hostvar])
del host_vars[hostvar]
if hostvars_prefix:
hostvar = hostvars_prefix + hostvar
if hostvars_suffix:
Expand Down
1 change: 0 additions & 1 deletion plugins/module_utils/cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import time
import functools
import random
import ansible.module_utils.common.warnings as ansible_warnings


class BackoffIterator:
Expand Down
4 changes: 1 addition & 3 deletions plugins/modules/autoscaling_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,9 +1108,7 @@ def create_autoscaling_group(connection):
load_balancers = module.params['load_balancers']
target_group_arns = module.params['target_group_arns']
availability_zones = module.params['availability_zones']
launch_config_name = module.params.get('launch_config_name')
launch_template = module.params.get('launch_template')
mixed_instances_policy = module.params.get('mixed_instances_policy')
min_size = module.params['min_size']
max_size = module.params['max_size']
max_instance_lifetime = module.params.get('max_instance_lifetime')
Expand Down Expand Up @@ -1360,7 +1358,7 @@ def create_autoscaling_group(connection):
else:
try:
ag['LaunchConfigurationName'] = as_group['LaunchConfigurationName']
except Exception:
except KeyError:
launch_template = as_group['LaunchTemplate']
# Prefer LaunchTemplateId over Name as it's more specific. Only one can be used for update_asg.
ag['LaunchTemplate'] = {"LaunchTemplateId": launch_template['LaunchTemplateId'], "Version": launch_template['Version']}
Expand Down
1 change: 0 additions & 1 deletion plugins/modules/cloudtrail_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@

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


Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/cloudwatchlogs_log_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def input_retention_policy(client, log_group_name, retention, module):
permited_values = [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653]

if retention in permited_values:
response = client.put_retention_policy(logGroupName=log_group_name,
retentionInDays=retention)
client.put_retention_policy(logGroupName=log_group_name,
retentionInDays=retention)
else:
delete_log_group(client=client, log_group_name=log_group_name, module=module)
module.fail_json(msg="Invalid retention value. Valid values are: [1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653]")
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/ec2_eip.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,11 +282,11 @@ def disassociate_ip_and_device(ec2, module, address, device_id, check_mode, is_i
if not check_mode:
try:
if address['Domain'] == 'vpc':
res = ec2.disassociate_address(
ec2.disassociate_address(
AssociationId=address['AssociationId'], aws_retry=True
)
else:
res = ec2.disassociate_address(
ec2.disassociate_address(
PublicIp=address['PublicIp'], aws_retry=True
)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
Expand Down Expand Up @@ -390,7 +390,7 @@ def release_address(ec2, module, address, check_mode):
# If we're in check mode, nothing else to do
if not check_mode:
try:
result = ec2.release_address(AllocationId=address['AllocationId'], aws_retry=True)
ec2.release_address(AllocationId=address['AllocationId'], aws_retry=True)
except (botocore.exceptions.BotoCoreError, botocore.exceptions.ClientError) as e:
module.fail_json_aws(e, msg="Couldn't release Elastic IP address")

Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/kms_key_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def get_key_policy_with_backoff(connection, key_id, policy_name):
def get_enable_key_rotation_with_backoff(connection, key_id):
try:
current_rotation_status = connection.get_key_rotation_status(KeyId=key_id)
except is_boto3_error_code(['AccessDeniedException', 'UnsupportedOperationException']) as e:
except is_boto3_error_code(['AccessDeniedException', 'UnsupportedOperationException']):
return None

return current_rotation_status.get('KeyRotationEnabled')
Expand Down
44 changes: 2 additions & 42 deletions plugins/modules/lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,48 +401,13 @@
except ImportError:
pass # protected by AnsibleAWSModule

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

from ansible_collections.amazon.aws.plugins.module_utils.core import AnsibleAWSModule
from ansible_collections.amazon.aws.plugins.module_utils.core import is_boto3_error_code
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import AWSRetry
from ansible_collections.amazon.aws.plugins.module_utils.ec2 import compare_aws_tags


def get_account_info(module):
"""return the account information (account id and partition) we are currently working on
get_account_info tries too find out the account that we are working
on. It's not guaranteed that this will be easy so we try in
several different ways. Giving either IAM or STS privileges to
the account should be enough to permit this.
"""
account_id = None
partition = None
try:
sts_client = module.client('sts', retry_decorator=AWSRetry.jittered_backoff())
caller_id = sts_client.get_caller_identity(aws_retry=True)
account_id = caller_id.get('Account')
partition = caller_id.get('Arn').split(':')[1]
except (BotoCoreError, ClientError):
try:
iam_client = module.client('iam', retry_decorator=AWSRetry.jittered_backoff())
arn, partition, service, reg, account_id, resource = iam_client.get_user(aws_retry=True)['User']['Arn'].split(':')
except is_boto3_error_code('AccessDenied') as e:
try:
except_msg = to_native(e.message)
except AttributeError:
except_msg = to_native(e)
m = re.search(r"arn:(aws(-([a-z\-]+))?):iam::([0-9]{12,32}):\w+/", except_msg)
if m is None:
module.fail_json_aws(e, msg="getting account information")
account_id = m.group(4)
partition = m.group(1)
except (BotoCoreError, ClientError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="getting account information")

return account_id, partition
from ansible_collections.amazon.aws.plugins.module_utils.iam import get_aws_account_info


def get_current_function(connection, function_name, qualifier=None):
Expand Down Expand Up @@ -577,7 +542,6 @@ def _code_args(module, current_config):
s3_object_version = module.params.get('s3_object_version')
zip_file = module.params.get('zip_file')
architectures = module.params.get('architecture')
checksum_match = False

code_kwargs = {}

Expand Down Expand Up @@ -665,10 +629,6 @@ def main():
runtime = module.params.get('runtime')
role = module.params.get('role')
handler = module.params.get('handler')
s3_bucket = module.params.get('s3_bucket')
s3_key = module.params.get('s3_key')
s3_object_version = module.params.get('s3_object_version')
zip_file = module.params.get('zip_file')
description = module.params.get('description')
timeout = module.params.get('timeout')
memory_size = module.params.get('memory_size')
Expand Down Expand Up @@ -700,7 +660,7 @@ def main():
role_arn = role
else:
# get account ID and assemble ARN
account_id, partition = get_account_info(module)
account_id, partition = get_aws_account_info(module)
role_arn = 'arn:{0}:iam::{1}:role/{2}'.format(partition, account_id, role)

# create list of layer version arn
Expand Down
2 changes: 1 addition & 1 deletion plugins/modules/lambda_execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def main():
module.fail_json_aws(ce, msg="Client-side error when invoking Lambda, check inputs and specific error")
except botocore.exceptions.ParamValidationError as ve: # pylint: disable=duplicate-except
module.fail_json_aws(ve, msg="Parameters to `invoke` failed to validate")
except Exception as e:
except botocore.exceptions.BotoCoreError as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg="Unexpected failure while invoking Lambda function")

results = {
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/rds_cluster_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ def copy_snapshot(params):
method_params = get_parameters(params, 'copy_db_cluster_snapshot')
if method_params.get('Tags'):
method_params['Tags'] = ansible_dict_to_boto3_tag_list(method_params['Tags'])
result, changed = call_method(client, module, 'copy_db_cluster_snapshot', method_params)
_result, changed = call_method(client, module, 'copy_db_cluster_snapshot', method_params)

return changed

Expand Down Expand Up @@ -316,7 +316,7 @@ def create_snapshot(params):
method_params = get_parameters(params, 'create_db_cluster_snapshot')
if method_params.get('Tags'):
method_params['Tags'] = ansible_dict_to_boto3_tag_list(method_params['Tags'])
snapshot, changed = call_method(client, module, 'create_db_cluster_snapshot', method_params)
_snapshot, changed = call_method(client, module, 'create_db_cluster_snapshot', method_params)

return changed

Expand Down
22 changes: 9 additions & 13 deletions plugins/modules/rds_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ def get_rds_method_attribute_name(instance, state, creation_source, read_replica

def get_instance(client, module, db_instance_id):
try:
for i in range(3):
for _i in range(3):
try:
instance = client.describe_db_instances(DBInstanceIdentifier=db_instance_id)['DBInstances'][0]
instance['Tags'] = get_tags(client, module, instance['DBInstanceArn'])
Expand All @@ -936,7 +936,7 @@ def get_final_snapshot(client, module, snapshot_identifier):
if len(snapshots.get('DBSnapshots', [])) == 1:
return snapshots['DBSnapshots'][0]
return {}
except is_boto3_error_code('DBSnapshotNotFound') as e: # May not be using wait: True
except is_boto3_error_code('DBSnapshotNotFound'): # May not be using wait: True
return {}
except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e: # pylint: disable=duplicate-except
module.fail_json_aws(e, msg='Failed to retrieve information about the final snapshot')
Expand Down Expand Up @@ -1128,10 +1128,6 @@ def validate_options(client, module, instance):
read_replica = module.params['read_replica']
creation_source = module.params['creation_source']
source_instance = module.params['source_db_instance_identifier']
if module.params['source_region'] is not None:
same_region = bool(module.params['source_region'] == module.params['region'])
else:
same_region = True

if modified_id:
modified_instance = get_instance(client, module, modified_id)
Expand Down Expand Up @@ -1176,8 +1172,8 @@ def promote_replication_instance(client, module, instance, read_replica):
# See https://awscli.amazonaws.com/v2/documentation/api/latest/reference/rds/describe-db-instances.html
if bool(instance.get('StatusInfos')):
try:
result, changed = call_method(client, module, method_name='promote_read_replica',
parameters={'DBInstanceIdentifier': instance['DBInstanceIdentifier']})
_result, changed = call_method(client, module, method_name='promote_read_replica',
parameters={'DBInstanceIdentifier': instance['DBInstanceIdentifier']})
except is_boto3_error_message('DB Instance is not a read replica'):
pass
return changed
Expand Down Expand Up @@ -1232,7 +1228,7 @@ def reboot_running_db_instance(client, module, instance):
call_method(client, module, 'start_db_instance', parameters)
if module.params.get('force_failover') is not None:
parameters['ForceFailover'] = module.params['force_failover']
results, changed = call_method(client, module, 'reboot_db_instance', parameters)
_results, changed = call_method(client, module, 'reboot_db_instance', parameters)
return changed


Expand All @@ -1242,9 +1238,9 @@ def start_or_stop_instance(client, module, instance, state):
if state == 'stopped' and instance['DBInstanceStatus'] not in ['stopping', 'stopped']:
if module.params['db_snapshot_identifier']:
parameters['DBSnapshotIdentifier'] = module.params['db_snapshot_identifier']
result, changed = call_method(client, module, 'stop_db_instance', parameters)
_result, changed = call_method(client, module, 'stop_db_instance', parameters)
elif state == 'started' and instance['DBInstanceStatus'] not in ['available', 'starting', 'restarting']:
result, changed = call_method(client, module, 'start_db_instance', parameters)
_result, changed = call_method(client, module, 'start_db_instance', parameters)
return changed


Expand Down Expand Up @@ -1393,7 +1389,7 @@ def main():
# Exit on check_mode when parameters to modify
if module.check_mode:
module.exit_json(changed=True, **camel_dict_to_snake_dict(instance, ignore_list=['Tags', 'ProcessorFeatures']))
result, changed = call_method(client, module, method_name, parameters_to_modify)
_result, changed = call_method(client, module, method_name, parameters_to_modify)

instance_id = get_final_identifier(method_name, module)

Expand All @@ -1409,7 +1405,7 @@ def main():
if changed:
instance = get_instance(client, module, instance_id)
if state != 'absent' and (instance or not module.check_mode):
for attempt_to_wait in range(0, 10):
for _wait_attempt in range(0, 10):
instance = get_instance(client, module, instance_id)
if instance:
break
Expand Down
4 changes: 2 additions & 2 deletions plugins/modules/rds_instance_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ def create_snapshot(params):
method_params = get_parameters(params, 'create_db_snapshot')
if method_params.get('Tags'):
method_params['Tags'] = ansible_dict_to_boto3_tag_list(method_params['Tags'])
snapshot, changed = call_method(client, module, 'create_db_snapshot', method_params)
_snapshot, changed = call_method(client, module, 'create_db_snapshot', method_params)

return changed

Expand All @@ -328,7 +328,7 @@ def copy_snapshot(params):
method_params = get_parameters(params, 'copy_db_snapshot')
if method_params.get('Tags'):
method_params['Tags'] = ansible_dict_to_boto3_tag_list(method_params['Tags'])
result, changed = call_method(client, module, 'copy_db_snapshot', method_params)
_result, changed = call_method(client, module, 'copy_db_snapshot', method_params)

return changed

Expand Down
2 changes: 0 additions & 2 deletions plugins/modules/route53_health_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,11 +550,9 @@ def main():
ip_addr_in = module.params.get('ip_address')
port_in = module.params.get('port')
type_in = module.params.get('type')
resource_path_in = module.params.get('resource_path')
fqdn_in = module.params.get('fqdn')
string_match_in = module.params.get('string_match')
request_interval_in = module.params.get('request_interval')
failure_threshold_in = module.params.get('failure_threshold')
health_check_name = module.params.get('health_check_name')
tags = module.params.get('tags')

Expand Down
Loading

0 comments on commit e3f091e

Please sign in to comment.