From 36fe042124090a8722af4d8cfe988e1fd1a51f04 Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Wed, 20 Nov 2024 15:55:50 +0530 Subject: [PATCH 1/3] EC2: Generate longer resource IDs --- moto/ec2/utils.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index 23fe0677be9..fde7e44131d 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -89,7 +89,7 @@ def random_resource_id(size: int = 8) -> str: return "".join(random.choice(HEX_CHARS) for _ in range(size)) -def random_id(prefix: str = "", size: int = 8) -> str: +def random_id(prefix: str = "", size: int = 17) -> str: return f"{prefix}-{random_resource_id(size)}" @@ -98,7 +98,7 @@ def random_ami_id() -> str: def random_instance_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["instance"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["instance"]) def random_reservation_id() -> str: @@ -106,11 +106,11 @@ def random_reservation_id() -> str: def random_security_group_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["security-group"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["security-group"]) def random_security_group_rule_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["security-group-rule"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["security-group-rule"]) def random_fleet_id() -> str: @@ -180,7 +180,7 @@ def random_vpc_id() -> str: def random_vpc_ep_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["vpc-endpoint"], size=8) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["vpc-endpoint"]) def random_vpc_cidr_association_id() -> str: @@ -201,7 +201,7 @@ def random_internet_gateway_id() -> str: def random_egress_only_internet_gateway_id() -> str: return random_id( - prefix=EC2_RESOURCE_TO_PREFIX["egress-only-internet-gateway"], size=17 + prefix=EC2_RESOURCE_TO_PREFIX["egress-only-internet-gateway"] ) @@ -226,27 +226,31 @@ def random_eni_attach_id() -> str: def random_nat_gateway_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["nat-gateway"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["nat-gateway"]) def random_transit_gateway_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway"]) def random_transit_gateway_route_table_id() -> str: return random_id( - prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-route-table"], size=17 + prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-route-table"] ) def random_transit_gateway_attachment_id() -> str: return random_id( - prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-attachment"], size=17 + prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-attachment"] ) +def random_managed_prefix_list_id() -> str: + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["managed-prefix-list"]) + + def random_launch_template_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["launch-template"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["launch-template"]) def random_launch_template_name() -> str: @@ -258,7 +262,7 @@ def random_iam_instance_profile_association_id() -> str: def random_carrier_gateway_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["carrier-gateway"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["carrier-gateway"]) def random_public_ip() -> str: @@ -266,7 +270,7 @@ def random_public_ip() -> str: def random_dedicated_host_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["dedicated_host"], size=17) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["dedicated_host"]) def random_private_ip(cidr: Optional[str] = None, ipv6: bool = False) -> str: @@ -318,9 +322,6 @@ def generate_route_id( return f"{route_table_id}~{cidr_block}" -def random_managed_prefix_list_id() -> str: - return random_id(prefix=EC2_RESOURCE_TO_PREFIX["managed-prefix-list"], size=8) - def create_dns_entries(service_name: str, vpc_endpoint_id: str) -> Dict[str, str]: return { From 72c82eb2e06986b51de33f7aec28c62de817e502 Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Wed, 20 Nov 2024 16:02:56 +0530 Subject: [PATCH 2/3] Lint --- moto/ec2/utils.py | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/moto/ec2/utils.py b/moto/ec2/utils.py index fde7e44131d..23fdfa6e45b 100644 --- a/moto/ec2/utils.py +++ b/moto/ec2/utils.py @@ -200,9 +200,7 @@ def random_internet_gateway_id() -> str: def random_egress_only_internet_gateway_id() -> str: - return random_id( - prefix=EC2_RESOURCE_TO_PREFIX["egress-only-internet-gateway"] - ) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["egress-only-internet-gateway"]) def random_route_table_id() -> str: @@ -234,15 +232,11 @@ def random_transit_gateway_id() -> str: def random_transit_gateway_route_table_id() -> str: - return random_id( - prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-route-table"] - ) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-route-table"]) def random_transit_gateway_attachment_id() -> str: - return random_id( - prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-attachment"] - ) + return random_id(prefix=EC2_RESOURCE_TO_PREFIX["transit-gateway-attachment"]) def random_managed_prefix_list_id() -> str: @@ -322,7 +316,6 @@ def generate_route_id( return f"{route_table_id}~{cidr_block}" - def create_dns_entries(service_name: str, vpc_endpoint_id: str) -> Dict[str, str]: return { "dns_name": f"{vpc_endpoint_id}-{random_resource_id(8)}.{service_name}", From 3c399a97daae1890bcf325655806cfec102b0800 Mon Sep 17 00:00:00 2001 From: Viren Nadkarni Date: Wed, 20 Nov 2024 16:28:57 +0530 Subject: [PATCH 3/3] Update random gen test impacted by seed --- tests/test_moto_api/seeder/test_seeder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_moto_api/seeder/test_seeder.py b/tests/test_moto_api/seeder/test_seeder.py index 970c305244f..e85ae78b3c3 100644 --- a/tests/test_moto_api/seeder/test_seeder.py +++ b/tests/test_moto_api/seeder/test_seeder.py @@ -41,7 +41,7 @@ def test_0(self) -> None: instances = self.ec2_client.run_instances(MaxCount=1, MinCount=1)["Instances"] instance_ids = [instance["InstanceId"] for instance in instances] - assert instance_ids == ["i-73bd4755d05ad7853"] + assert instance_ids == ["i-6e8b5359309cc6273"] def test_1(self) -> None: # Create some data in a different account (111111111111)