-
Notifications
You must be signed in to change notification settings - Fork 53
Add NATGateway
CRD
#43
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left a comment inline to address in a future PR. All the generated code and tests in here look A-OK, thank you @RedbackThomson :)
CreateNatGateway: | ||
output_wrapper_field_path: NatGateway | ||
CreateVpcEndpoint: | ||
output_wrapper_field_path: VpcEndpoint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't think either of these is necessary... we should be figuring out the wrapper field automagically when the resource name matches the wrapper field name... (not needed to change in this patch, just sayin')
@@ -36,6 +36,19 @@ def assert_internet_gateway(self, ig_id: str, exists=True): | |||
pass | |||
assert res_found is exists | |||
|
|||
def assert_nat_gateway(self, ngw_id: str, exists=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/cc @brycahta
Not something to change in this PR, but a future PR...
I know this EC2Validator class was recently added with this style of assertions, but I would recommend reworking the code so that assert
statements are kept out of the helper class methods and instead remain only in the test methods themselves.
In other words, have helper classes or methods that get some state and then have the test method assert
that state matches some expectation.
For example, consider reworking this class to be like this:
class EC2Helper:
def __init__(self, ec2_client):
self.ec2_client = ec2_client
...
def nat_gateway(self, ngw_id):
"""Returns a dict containing the NAT gateway info or None
if no such gateway exists
"""
try:
aws_res = self.ec2_client.describe_nat_gateways(NatGatewayIds=[ngw_id])
return aws_res["NatGateways"][0]
except self.ec2_client.exceptions.ClientError:
return None
def nat_gateway_exists(self, ngw_id):
"""Returns whether a NAT gateway exists (deleting counts as not existing)"""
# NATGateway may take awhile to be removed server-side, so
# treat 'deleting' and 'deleted' states as resource no longer existing
ngw = self.nate_gateway(ngw_id)
return ngw is not None and ngw['State'] != "deleting" and ngw['State'] != "deleted"
and then in your test methods, use assert
to validate an expectation:
# Check NAT Gateway exists in AWS
ec2_helper = EC2Helper(ec2_client)
assert ec2_helper.nat_gateway_exists(resource_id)
# Delete k8s resource
_, deleted = k8s.delete_custom_resource(ref)
assert deleted is True
time.sleep(DELETE_WAIT_AFTER_SECONDS)
# Check NAT Gateway no longer exists in AWS
assert not ec2_helper.nat_gateway_exists(resource_id)
The fundamental principle at issue here is ensuring that functions/methods have as close to a single purpose as possible. Instead of having a single function that retrieves some information, checks for existence and asserts some expected condition, just have a method that does one thing and leave the assert statements for test methods, where they are obvious and explicit...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good
@@ -0,0 +1,12 @@ | |||
# Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the purpose of this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can do relative imports from inside tests. I needed it for from .test_elastic_ip_address import RESOURCE_PLURAL as ELASTIC_IP_PLURAL
.
@@ -36,6 +36,19 @@ def assert_internet_gateway(self, ig_id: str, exists=True): | |||
pass | |||
assert res_found is exists | |||
|
|||
def assert_nat_gateway(self, ngw_id: str, exists=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/lgtm
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: A-Hilaly, brycahta, jaypipes, RedbackThomson The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Issue #, if available:
Description of changes:
Creates the
NATGateway
CRD, with references toElasticIPAddress
andSubnet
for their respective fieldsBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.