8000 Simplify vpc id generation based on review feedback · localstack/localstack@1eaec09 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1eaec09

Browse files
committed
Simplify vpc id generation based on review feedback
1 parent 2fc8407 commit 1eaec09

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

localstack-core/localstack/services/ec2/patches.py

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import logging
2-
import random
3-
import string
42
from typing import Optional
53

64
from moto.ec2 import models as ec2_models
5+
from moto.utilities.id_generator import Tags
76

87
from localstack.constants import TAG_KEY_CUSTOM_ID
98
from localstack.services.ec2.exceptions import (
@@ -14,9 +13,7 @@
1413
from localstack.utils.id_generator import (
1514
ExistingIds,
1615
ResourceIdentifier,
17-
Tags,
1816
localstack_id,
19-
localstack_id_manager,
2017
)
2118
from localstack.utils.patch import patch
2219

@@ -29,8 +26,8 @@ def generate_vpc_id(
2926
existing_ids: ExistingIds = None,
3027
tags: Tags = None,
3128
) -> str:
32-
suffix = "".join(random.choice(string.ascii_letters) for _ in range(17))
33-
return f"vpc-{suffix}"
29+
# We return an empty string here to differentiate between when a custom ID was used, or when it was randomly generated by `moto`.
30+
return ""
3431

3532

3633
class VpcIdentifier(ResourceIdentifier):
@@ -122,17 +119,8 @@ def ec2_create_vpc(
122119
is_default: bool = False,
123120
**kwargs,
124121
):
125-
# Extract custom ID from tags if it exists
126-
tags: list[dict[str, str]] = tags or []
127-
custom_ids = [tag["Value"] for tag in tags if tag["Key"] == TAG_KEY_CUSTOM_ID]
128-
custom_id = custom_ids[0] if len(custom_ids) > 0 else None
129-
130-
# try to generate a custom id from the id store
131-
if not custom_id:
132-
resource_identifier = VpcIdentifier(self.account_id, self.region_name, cidr_block)
133-
id = localstack_id_manager.get_custom_id(resource_identifier)
134-
if id:
135-
custom_id = id
122+
resource_identifier = VpcIdentifier(self.account_id, self.region_name, cidr_block)
123+
custom_id = resource_identifier.generate(tags=tags)
136124

137125
# Check if custom id is unique
138126
if custom_id and custom_id in self.vpcs:

0 commit comments

Comments
 (0)
0