Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EC2: Generate longer resource IDs #8338

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 17 additions & 23 deletions moto/ec2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"


Expand All @@ -98,19 +98,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 @@ -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:
Expand All @@ -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"], size=17
)
return random_id(prefix=EC2_RESOURCE_TO_PREFIX["egress-only-internet-gateway"])


def random_route_table_id() -> str:
Expand All @@ -226,27 +224,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 @@ -258,15 +256,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 @@ -318,10 +316,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
Loading