Skip to content

Commit

Permalink
EC2: Generate longer resource IDs (#8338)
Browse files Browse the repository at this point in the history
  • Loading branch information
viren-nadkarni authored Nov 23, 2024
1 parent 60328d5 commit 763fd44
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
40 changes: 17 additions & 23 deletions moto/ec2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,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)}"


Expand All @@ -99,19 +99,19 @@ 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:
return random_id(prefix=EC2_RESOURCE_TO_PREFIX["reservation"])


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:
Expand Down Expand Up @@ -181,7 +181,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:
Expand All @@ -201,9 +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
)
return random_id(prefix=EC2_RESOURCE_TO_PREFIX["egress-only-internet-gateway"])


def random_route_table_id() -> str:
Expand All @@ -227,27 +225,27 @@ 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
)
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"], size=17
)
return random_id(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:
Expand All @@ -259,15 +257,15 @@ 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:
return f"54.214.{random.choice(range(255))}.{random.choice(range(255))}"


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:
Expand Down Expand Up @@ -319,10 +317,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 {
"dns_name": f"{vpc_endpoint_id}-{random_resource_id(8)}.{service_name}",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_moto_api/seeder/test_seeder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 763fd44

Please sign in to comment.