Skip to content

Commit

Permalink
Merge branch 'main' into migrate_ec2_placement_group
Browse files Browse the repository at this point in the history
  • Loading branch information
GomathiselviS authored Oct 23, 2024
2 parents bab01ae + 2b0bf35 commit 290ade7
Show file tree
Hide file tree
Showing 4 changed files with 165 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_transit_gateway_* modules (https://github.com/ansible-collections/amazon.aws/pull/2325).
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).
81 changes: 80 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 Expand Up @@ -1737,3 +1783,36 @@ def determine_iam_arn_from_name(iam_client, name_or_arn: str) -> str:
if not iam_instance_profiles:
raise AnsibleEC2Error(message=f"Could not find IAM instance profile {name_or_arn}")
return iam_instance_profiles[0]["Arn"]


# EC2 Transit Gateway
class EC2TransitGatewayErrorHandler(AWSErrorHandler):
_CUSTOM_EXCEPTION = AnsibleEC2Error

@classmethod
def _is_missing(cls):
return is_boto3_error_code("InvalidTransitGatewayID.NotFound")


@EC2TransitGatewayErrorHandler.list_error_handler("describe transit gateway", [])
@AWSRetry.jittered_backoff()
def describe_ec2_transit_gateways(
client, **params: Dict[str, Union[List[str], List[Dict[str, Union[str, List[str]]]]]]
) -> List[Dict[str, Any]]:
paginator = client.get_paginator("describe_transit_gateways")
return paginator.paginate(**params).build_full_result()["TransitGateways"]


@EC2TransitGatewayErrorHandler.common_error_handler("create transit gateway")
@AWSRetry.jittered_backoff()
def create_ec2_transit_gateway(
client, **params: Dict[str, Union[List[str], List[Dict[str, Union[str, List[str]]]]]]
) -> Dict[str, Any]:
return client.create_transit_gateway(**params)["TransitGateway"]


@EC2TransitGatewayErrorHandler.deletion_error_handler("delete transit gateway")
@AWSRetry.jittered_backoff()
def delete_ec2_transit_gateway(client, transit_gateway_id: str) -> bool:
client.delete_transit_gateway(TransitGatewayId=transit_gateway_id)
return True
84 changes: 81 additions & 3 deletions plugins/module_utils/waiters.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,56 @@
{"matcher": "error", "expected": "InvalidInternetGatewayID.NotFound", "state": "retry"},
],
},
"TransitGatewayAvailable": {
"operation": "DescribeTransitGateways",
"delay": 5,
"maxAttempts": 120,
"acceptors": [
{
"state": "success",
"matcher": "pathAll",
"argument": "TransitGateways[].State",
"expected": "available",
},
{
"state": "retry",
"matcher": "pathAll",
"argument": "TransitGateways[].State",
"expected": "pending",
},
{
"state": "failure",
"matcher": "pathAny",
"argument": "TransitGateways[].State",
"expected": "failed",
},
],
},
"TransitGatewayDeleted": {
"operation": "DescribeTransitGateways",
"delay": 5,
"maxAttempts": 120,
"acceptors": [
{
"state": "success",
"matcher": "pathAll",
"argument": "TransitGateways[].State",
"expected": "deleted",
},
{
"state": "retry",
"matcher": "pathAll",
"argument": "TransitGateways[].State",
"expected": "deleting",
},
{
"state": "failure",
"matcher": "pathAny",
"argument": "TransitGateways[].State",
"expected": "failed",
},
],
},
"TGWVpcAttachmentAvailable": {
"operation": "DescribeTransitGatewayVpcAttachments",
"delay": 5,
Expand Down Expand Up @@ -318,10 +368,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 @@ -820,6 +883,16 @@ def route53_model(name):
ec2_model("InternetGatewayAttached"),
core_waiter.NormalizedOperationMethod(ec2.describe_internet_gateways),
),
("EC2", "transit_gateway_available"): lambda ec2: core_waiter.Waiter(
"transit_gateway_available",
ec2_model("TransitGatewayAvailable"),
core_waiter.NormalizedOperationMethod(ec2.describe_transit_gateways),
),
("EC2", "transit_gateway_deleted"): lambda ec2: core_waiter.Waiter(
"transit_gateway_deleted",
ec2_model("TransitGatewayDeleted"),
core_waiter.NormalizedOperationMethod(ec2.describe_transit_gateways),
),
("EC2", "transit_gateway_vpc_attachment_available"): lambda ec2: core_waiter.Waiter(
"transit_gateway_vpc_attachment_available",
ec2_model("TGWVpcAttachmentAvailable"),
Expand Down Expand Up @@ -920,6 +993,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 290ade7

Please sign in to comment.