Skip to content

Commit

Permalink
Add utils for ec2_transit_gateway_* (#2325)
Browse files Browse the repository at this point in the history
SUMMARY

Added api calls used by ec2_transit_gateway and ec2_transit_gateway_info to ec2 module_utils

Required for ansible-collections/community.aws#2158
ISSUE TYPE


Feature Pull Request

COMPONENT NAME

module_utils/ec2
ADDITIONAL INFORMATION

Reviewed-by: Alina Buzachis
Reviewed-by: Bikouo Aubin
Reviewed-by: GomathiselviS <[email protected]>
  • Loading branch information
mandar242 authored Oct 23, 2024
1 parent 75ba725 commit 2b0bf35
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 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).
33 changes: 33 additions & 0 deletions plugins/module_utils/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1783,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
60 changes: 60 additions & 0 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 @@ -833,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

0 comments on commit 2b0bf35

Please sign in to comment.