-
Notifications
You must be signed in to change notification settings - Fork 1
Nomalized Name Collisons #142
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
Open
alexchristy
wants to merge
10
commits into
main
Choose a base branch
from
nomalized-name-collisons
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8d4b503
fix(api): update normalization function for names to create kebab cas…
ae56bb0
feat(api): add utility to create unique logical ids from the normaliz…
180dcdb
fix(api): remove duplicated class attributes
ac3755b
fix(api): update parsing logic and aws stack to use logical ids over …
496a6b0
fix(api): add tests for new naming and cdktf utilities
28e5345
fix(tests): modify multi vpc deployment test to include subnet that h…
084b83a
fix(tests): bump collision subnet host to small to save me money ;)
9076eaf
fix(api): update log message in normalization function to be more clear
d5919e2
fix(api): update logical id utility function to be more clear when ra…
a2b71cd
fix(tests): update api deploy tests to use tenacity for retrying SSH …
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,63 @@ | ||
| import tempfile | ||
| from collections import Counter, defaultdict | ||
|
|
||
| from .name_utils import normalize_name | ||
|
|
||
|
|
||
| def create_cdktf_dir() -> str: | ||
| """Create temp dir for CDKTF.""" | ||
| # /tmp/.openlabs-cdktf-XXXX | ||
| return tempfile.mkdtemp(prefix=".openlabs-cdktf-") | ||
|
|
||
|
|
||
| def gen_resource_logical_ids(resource_names: list[str]) -> dict[str, str]: | ||
| """Generate deterministic, normalized, and unique logical IDs from a list of resource names. | ||
|
|
||
| This function handles collisions that occur after normalization by appending | ||
| a numeric suffix. | ||
|
|
||
| Args: | ||
| resource_names: A list of user-supplied resource names. | ||
|
|
||
| Returns: | ||
| A dictionary mapping each original resource name to its unique logical ID. | ||
|
|
||
| Example: | ||
| >>> names = ["Web Server", "Database", "web-server", "Auth Service"] | ||
| >>> gen_resource_logical_ids(names) | ||
| { | ||
| 'Auth Service': 'auth-service', | ||
| 'Database': 'database', | ||
| 'Web Server': 'web-server', | ||
| 'web-server': 'web-server-1' | ||
| } | ||
|
|
||
| """ | ||
| if len(set(resource_names)) != len(resource_names): | ||
| counts = Counter(resource_names) | ||
| duplicates = [name for name, count in counts.items() if count > 1] | ||
| msg = f"Input list contains duplicate names: {', '.join(duplicates)}" | ||
| raise ValueError(msg) | ||
|
|
||
| logical_ids: dict[str, str] = {} | ||
| seen_counts: defaultdict[str, int] = defaultdict(int) | ||
|
|
||
| # Sorted to ensure deterministic ID generation | ||
| sorted_names = sorted(resource_names) | ||
|
|
||
| for name in sorted_names: | ||
| base_id = normalize_name(name) | ||
|
|
||
| # The first time we see a base_id, its ID is just itself | ||
| # each time we see it again we add a suffix | ||
| if seen_counts[base_id] > 0: | ||
| logical_id = f"{base_id}-{seen_counts[base_id]}" | ||
| else: | ||
| logical_id = base_id | ||
|
|
||
| logical_ids[name] = logical_id | ||
|
|
||
| # Increment the count for the next potential collision | ||
| seen_counts[base_id] += 1 | ||
|
|
||
| return logical_ids | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.