1
1
import logging
2
- import random
3
- import string
4
2
from typing import Optional
5
3
6
4
from moto .ec2 import models as ec2_models
5
+ from moto .utilities .id_generator import Tags
7
6
8
7
from localstack .constants import TAG_KEY_CUSTOM_ID
9
8
from localstack .services .ec2 .exceptions import (
14
13
from localstack .utils .id_generator import (
15
14
ExistingIds ,
16
15
ResourceIdentifier ,
17
- Tags ,
18
16
localstack_id ,
19
- localstack_id_manager ,
20
17
)
21
18
from localstack .utils .patch import patch
22
19
@@ -29,8 +26,8 @@ def generate_vpc_id(
29
26
existing_ids : ExistingIds = None ,
30
27
tags : Tags = None ,
31
28
) -> 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 " "
34
31
35
32
36
33
class VpcIdentifier (ResourceIdentifier ):
@@ -122,17 +119,8 @@ def ec2_create_vpc(
122
119
is_default : bool = False ,
123
120
** kwargs ,
124
121
):
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 )
136
124
137
125
# Check if custom id is unique
138
126
if custom_id and custom_id in self .vpcs :
0 commit comments