Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2_vpc_igw: migrate to boto3 #45346

Merged
merged 5 commits into from
Sep 20, 2018
Merged

Conversation

zeenlym
Copy link
Contributor

@zeenlym zeenlym commented Sep 7, 2018

SUMMARY

AWS boto only modules present issues with corporate internet proxies. Migrate to boto3 solves problems. Code is simplier and more ansible aws helpers are used.

Fixes #44889

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME
  • lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py
ANSIBLE VERSION
2.5 and 2.6
ADDITIONAL INFORMATION

@zeenlym zeenlym changed the title ec2_vpc_igw: draft migrate to boto3 ec2_vpc_igw: migrate to boto3 Sep 7, 2018
@ansibot
Copy link
Contributor

ansibot commented Sep 7, 2018

The test ansible-test sanity --test pylint [explain] failed with 2 errors:

lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:125:62: undefined-variable Undefined variable 'to_native'
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:125:86: undefined-variable Undefined variable 'traceback'

The test ansible-test sanity --test pep8 [explain] failed with 6 errors:

lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:88:9: E261 at least two spaces before inline comment
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:106:1: E302 expected 2 blank lines, found 1
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:122:37: E128 continuation line under-indented for visual indent
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:158:9: E303 too many blank lines (3)
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:230:5: E303 too many blank lines (2)
lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:286:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot
Copy link
Contributor

ansibot commented Sep 7, 2018

@ansibot
Copy link
Contributor

ansibot commented Sep 7, 2018

Hi @zeenlym,

Thank you for the pullrequest, just so you are aware we have a dedicated Working Group for aws.
You can find other people interested in this in #ansible-aws on Freenode IRC
For more information about communities, meetings and agendas see https://github.com/ansible/community

click here for bot help

@ansibot ansibot added affects_2.8 This issue/PR affects Ansible v2.8 aws bug This issue/PR relates to a bug. ci_verified Changes made in this PR are causing tests to fail. cloud module This issue/PR relates to a module. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. support:certified This issue/PR relates to certified code. labels Sep 7, 2018
@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Sep 7, 2018
Copy link
Contributor

@s-hertel s-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be in favor of not having a class in the module just to simplify things a little but LGTM even with it. There should be integration tests added to help ensure backward compatibility.

'tags': igw.tags,
'vpc_id': igw.vpc_id
}
class AnsibleEc2Igw(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most AWS modules don't implement classes as using a few functions is often more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it easier to read within a class, and using attributes prevent from passing lots of parameters to functions.
I think if all AWS modules uses classes even if module complexity is very high or very low readability will be the same.

def get_resource_tags(vpc_conn, resource_id):
return dict((t.name, t.value) for t in
vpc_conn.get_all_tags(filters={'resource-id': resource_id}))
def connect(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to access get_aws_connection_info and boto3_conn directly and any errors with the connection should be handled by boto3_conn. After creating the module by module = AnsibleAWSModule(... the connection can be created with module.client('ec2').

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

igws = response['InternetGateways']
except botocore.exceptions.ClientError as e:
self.fail(msg=str(e))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove some extra whitespace

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

if igw is None:
if check_mode:
return {'changed': True, 'gateway_id': None}
self.start_timer()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't appear to do anything for the module.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's rigth, sorry for this


igw.vpc_id = vpc_id
if to_delete and not add_only:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think rather than checking for to_delete and add_only here (and having two hardcoded values for add_only and the purge_tags parameter for compare_aws_tags), I'd consolidate the options since they do the same thing and only have one hardcoded value:

purge_tags = bool(not add_only)
to_update, to_delete = compare_aws_tags(boto3_tag_list_to_ansible_dict(cur_tags.get('Tags')), tags, purge_tags)

And then to_delete will only be populated if tags are being removed and you don't have to reference add_only in other places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok


from ansible.module_utils.aws.core import AnsibleAWSModule
from ansible.module_utils.ec2 import (
AnsibleAWSError,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

@ansibot ansibot added committer_review In order to be merged, this PR must follow the certified review workflow. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Sep 10, 2018
@willthames
Copy link
Contributor

@s-hertel has said what I would say in regard to integration tests.

Copy link
Contributor

@willthames willthames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few minor issues but nothing that would stop this module working

tags_list.append({'Key': key})

AWSRetry.exponential_backoff(
catch_extra_error_codes=['InvalidSubnetID.NotFound']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem right to me - you may not need the catch_extra_error_codes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok some copy paste from other modules, but I couldn't understand purpose. I am removing it.

try:
response = self._connection.describe_internet_gateways(Filters=filters)
igws = response.get('InternetGateways', [])
except botocore.exceptions.ClientError as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to catch BotoCoreError here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

'changed': changed,
'gateway': igw_info
}
def fail(self, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is really needed (and doesn't seem to be used)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

final_tags.update(to_update)
else:
AWSRetry.exponential_backoff(
catch_extra_error_codes=['InvalidInternetID.NotFound']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InvalidInternetID doesn't seem to exist according to botocore's source

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed line

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed committer_review In order to be merged, this PR must follow the certified review workflow. labels Sep 10, 2018
@ansibot
Copy link
Contributor

ansibot commented Sep 10, 2018

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

lib/ansible/modules/cloud/amazon/ec2_vpc_igw.py:255:1: E302 expected 2 blank lines, found 1

click here for bot help

@ansibot ansibot added the ci_verified Changes made in this PR are causing tests to fail. label Sep 10, 2018
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Sep 10, 2018
@zeenlym
Copy link
Contributor Author

zeenlym commented Sep 10, 2018

@willthames Do I need to remove classes ?

@willthames
Copy link
Contributor

@zeenlym no, you don't need to. It has been our preference to not use classes where not required, but the classes might be useful in future anyway.

Once there is a test suite, this should be good to merge.

@zeenlym
Copy link
Contributor Author

zeenlym commented Sep 11, 2018

Why is there no helper to update of aws resource tags ? The only changes are client, resource-id and resource-type ?

@willthames
Copy link
Contributor

@zeenlym, the reason why there is no update tag helper is that each AWS resource type implements their tag management API differently. Other than that, the reason is that no-one has written one.

@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Sep 20, 2018
@zeenlym
Copy link
Contributor Author

zeenlym commented Sep 20, 2018

Thanks for all checks, for the next 2 weeks I cannot fix nether add integration tests, I will do it after.

@ansibot ansibot added committer_review In order to be merged, this PR must follow the certified review workflow. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. stale_review Updates were made after the last review and the last review is more than 7 days old. labels Sep 20, 2018
@willthames
Copy link
Contributor

I've created a test suite in #45903 (based almost entirely on the egress_igw tests). I ran it before this change, then re-ran it with this change, and it's all good.

The test suite can be merged separately.

@willthames willthames merged commit 45c7fac into ansible:devel Sep 20, 2018
@willthames
Copy link
Contributor

Thanks so much for the boto3 migration @zeenlym, what a great first contribution!

@ansible ansible locked and limited conversation to collaborators Jul 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.8 This issue/PR affects Ansible v2.8 aws bug This issue/PR relates to a bug. cloud committer_review In order to be merged, this PR must follow the certified review workflow. module This issue/PR relates to a module. new_contributor This PR is the first contribution by a new community member. stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. support:certified This issue/PR relates to certified code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EC2 IGW module timeout when using corporate proxy
5 participants