Skip to content

Commit

Permalink
Add utils for ec2_vpc_vgw_* (#2331)
Browse files Browse the repository at this point in the history
* move api from ec2_vpc_vgw_* to module_utils/ec2

* sanity fix

* add changelog fragment

* modified based on feedback

* add custom waiter for vpn gateway

* modfied based on feedback

* minor fixes

* minor fix

* Format using black

Signed-off-by: Alina Buzachis <[email protected]>

---------

Signed-off-by: Alina Buzachis <[email protected]>
Co-authored-by: Alina Buzachis <[email protected]>
  • Loading branch information
mandar242 and alinabuzachis authored Oct 22, 2024
1 parent e104e40 commit 75ba725
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- module_utils/ec2 - add utils for the ec2_vpc_vgw_* modules (https://github.com/ansible-collections/amazon.aws/pull/2331).
48 changes: 47 additions & 1 deletion plugins/module_utils/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def reject_vpc_peering_connection(client, peering_id: str) -> bool:
return True


# EC2 VPC VPN
# EC2 vpn
class EC2VpnErrorHandler(AWSErrorHandler):
_CUSTOM_EXCEPTION = AnsibleEC2Error

Expand Down Expand Up @@ -648,6 +648,52 @@ def create_dhcp_options(
return client.create_dhcp_options(**params)["DhcpOptions"]


# EC2 vpn Gateways
class EC2VpnGatewaysErrorHandler(AWSErrorHandler):
_CUSTOM_EXCEPTION = AnsibleEC2Error

@classmethod
def _is_missing(cls):
return is_boto3_error_code(["InvalidVpnGatewayID.NotFound", "InvalidVpnGatewayState"])


@EC2VpnGatewaysErrorHandler.list_error_handler("describe vpn gateways", [])
@AWSRetry.jittered_backoff()
def describe_vpn_gateways(
client, **params: Dict[str, Union[List[str], int, List[Dict[str, Union[str, List[str]]]]]]
) -> List[Dict[str, Any]]:
return client.describe_vpn_gateways(**params)["VpnGateways"]


@EC2VpnGatewaysErrorHandler.common_error_handler("create vpn gateway")
@AWSRetry.jittered_backoff(catch_extra_error_codes=["VpnGatewayLimitExceeded"])
def create_vpn_gateway(
client, **params: Dict[str, Union[List[str], int, List[Dict[str, Union[str, List[str]]]]]]
) -> Dict[str, Any]:
return client.create_vpn_gateway(**params)["VpnGateway"]


@EC2VpnGatewaysErrorHandler.deletion_error_handler("delete vpn gateway")
@AWSRetry.jittered_backoff()
def delete_vpn_gateway(client, vpn_gateway_id: str) -> bool:
client.delete_vpn_gateway(VpnGatewayId=vpn_gateway_id)
return True


@EC2VpnGatewaysErrorHandler.common_error_handler("attach vpn gateway")
@AWSRetry.jittered_backoff()
def attach_vpn_gateway(client, vpc_id: str, vpn_gateway_id: str) -> bool:
client.attach_vpn_gateway(VpcId=vpc_id, VpnGatewayId=vpn_gateway_id)
return True


@EC2VpnGatewaysErrorHandler.common_error_handler("detach vpn gateway")
@AWSRetry.jittered_backoff()
def detach_vpn_gateway(client, vpc_id: str, vpn_gateway_id: str) -> bool:
client.detach_vpn_gateway(VpcId=vpc_id, VpnGatewayId=vpn_gateway_id)
return True


# EC2 Volumes
class EC2VolumeErrorHandler(AWSErrorHandler):
_CUSTOM_EXCEPTION = AnsibleEC2Error
Expand Down
24 changes: 21 additions & 3 deletions plugins/module_utils/waiters.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,10 +318,23 @@
"operation": "DescribeVpnGateways",
"acceptors": [
{
"matcher": "path",
"expected": True,
"argument": "VpnGateways[0].State == 'available'",
"expected": "detached",
"matcher": "pathAll",
"state": "success",
"argument": "VpnGateways[].VpcAttachments[].State",
},
],
},
"VpnGatewayAttached": {
"delay": 5,
"maxAttempts": 40,
"operation": "DescribeVpnGateways",
"acceptors": [
{
"expected": "attached",
"matcher": "pathAll",
"state": "success",
"argument": "VpnGateways[].VpcAttachments[].State",
},
],
},
Expand Down Expand Up @@ -920,6 +933,11 @@ def route53_model(name):
ec2_model("VpnGatewayDetached"),
core_waiter.NormalizedOperationMethod(ec2.describe_vpn_gateways),
),
("EC2", "vpn_gateway_attached"): lambda ec2: core_waiter.Waiter(
"vpn_gateway_attached",
ec2_model("VpnGatewayAttached"),
core_waiter.NormalizedOperationMethod(ec2.describe_vpn_gateways),
),
("EC2", "nat_gateway_deleted"): lambda ec2: core_waiter.Waiter(
"nat_gateway_deleted",
ec2_model("NatGatewayDeleted"),
Expand Down

0 comments on commit 75ba725

Please sign in to comment.