From a959542961fe6efc4578915b9038499d218723d0 Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Tue, 24 Sep 2024 14:50:09 +0200 Subject: [PATCH 1/2] Add utils for ec_vpc_peer* modules Signed-off-by: Alina Buzachis --- changelogs/fragments/20240924-ec2-utils.yml | 2 + plugins/module_utils/ec2.py | 47 +++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 changelogs/fragments/20240924-ec2-utils.yml diff --git a/changelogs/fragments/20240924-ec2-utils.yml b/changelogs/fragments/20240924-ec2-utils.yml new file mode 100644 index 00000000000..c349cf64733 --- /dev/null +++ b/changelogs/fragments/20240924-ec2-utils.yml @@ -0,0 +1,2 @@ +minor_changes: + - module_utils/ec2 - add utils for the ec2_vpc_peer* modules (https://github.com/ansible-collections/amazon.aws/pull/2303). diff --git a/plugins/module_utils/ec2.py b/plugins/module_utils/ec2.py index 8dfeb59cc14..3be87656794 100644 --- a/plugins/module_utils/ec2.py +++ b/plugins/module_utils/ec2.py @@ -318,6 +318,53 @@ def disassociate_vpc_cidr_block(client, association_id: str) -> Dict[str, Any]: return client.disassociate_vpc_cidr_block(AssociationId=association_id) +# EC2 VPC Peering Connection +class EC2VpcPeeringErrorHandler(AWSErrorHandler): + _CUSTOM_EXCEPTION = AnsibleEC2Error + + @classmethod + def _is_missing(cls): + return is_boto3_error_code("InvalidVpcPeeringConnectionID.NotFound", "InvalidVpcPeeringConnectionId.Malformed") + + +@EC2VpcPeeringErrorHandler.list_error_handler("describe vpc peering", []) +@AWSRetry.jittered_backoff() +def describe_vpc_peering_connections(client, **params: Dict[str, Any]) -> List[Dict[str, Any]]: + result = client.describe_vpc_peering_connections( + **params, + ) + return result + + +@EC2VpcSubnetErrorHandler.common_error_handler("create vpc peering") +@AWSRetry.jittered_backoff() +def create_vpc_peering_connection( + client, **params: Dict[str, Union[str, bool, int, EC2TagSpecifications]] +) -> Dict[str, Any]: + return client.create_vpc_peering_connection(**params)["VpcPeeringConnection"] + + +@EC2VpcSubnetErrorHandler.deletion_error_handler("delete vpc peering") +@AWSRetry.jittered_backoff() +def delete_vpc_peering_connection(client, peering_id: str) -> bool: + client.delete_vpc_peering_connection(VpcPeeringConnectionId=peering_id) + return True + + +@EC2VpcSubnetErrorHandler.deletion_error_handler("accept vpc peering") +@AWSRetry.jittered_backoff() +def accept_vpc_peering_connection(client, peering_id: str) -> bool: + client.accept_vpc_peering_connection(VpcPeeringConnectionId=peering_id) + return True + + +@EC2VpcSubnetErrorHandler.deletion_error_handler("reject vpc peering") +@AWSRetry.jittered_backoff() +def reject_vpc_peering_connection(client, peering_id: str) -> bool: + client.reject_vpc_peering_connection(VpcPeeringConnectionId=peering_id) + return True + + # EC2 Internet Gateway class EC2InternetGatewayErrorHandler(AWSErrorHandler): _CUSTOM_EXCEPTION = AnsibleEC2Error From d0f8e6b966eacdf77ee5253289965a76dbbf164c Mon Sep 17 00:00:00 2001 From: Alina Buzachis Date: Mon, 30 Sep 2024 17:37:57 +0200 Subject: [PATCH 2/2] Apply suggestions Signed-off-by: Alina Buzachis --- plugins/module_utils/ec2.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/plugins/module_utils/ec2.py b/plugins/module_utils/ec2.py index 3be87656794..9690e0d5cc8 100644 --- a/plugins/module_utils/ec2.py +++ b/plugins/module_utils/ec2.py @@ -324,16 +324,14 @@ class EC2VpcPeeringErrorHandler(AWSErrorHandler): @classmethod def _is_missing(cls): - return is_boto3_error_code("InvalidVpcPeeringConnectionID.NotFound", "InvalidVpcPeeringConnectionId.Malformed") + return is_boto3_error_code("InvalidVpcPeeringConnectionID.NotFound") @EC2VpcPeeringErrorHandler.list_error_handler("describe vpc peering", []) @AWSRetry.jittered_backoff() def describe_vpc_peering_connections(client, **params: Dict[str, Any]) -> List[Dict[str, Any]]: - result = client.describe_vpc_peering_connections( - **params, - ) - return result + paginator = client.get_paginator("describe_vpc_peering_connections") + return paginator.paginate(**params).build_full_result()["VpcPeeringConnections"] @EC2VpcSubnetErrorHandler.common_error_handler("create vpc peering")