Repository navigation
Identifier Utils Module #126
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
Merged
ammokhov
merged 4 commits into
aws-cloudformation:master
from
ammokhov:identifier-utils
Oct 15, 2020
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import hashlib | ||
| import re | ||
| from typing import Optional | ||
|
|
||
| STACK_ARN_PATTERN = "^[a-z0-9-:]*stack/[-a-z0-9A-Z]*/[-a-z0-9A-Z]*" | ||
|
|
||
| MIN_PHYSICAL_RESOURCE_ID_LENGTH = 15 | ||
| MIN_PREFERRED_LENGTH = 17 | ||
| HASH_LENGTH = 12 | ||
|
|
||
|
|
||
| def _get_hash(client_request_token: str) -> str: | ||
| return hashlib.sha1(str.encode(client_request_token)).hexdigest() # nosec | ||
|
|
||
|
|
||
| def generate_resource_identifier( | ||
|
ammokhov marked this conversation as resolved.
|
||
| stack_id_or_name: Optional[str], | ||
| logical_resource_id: Optional[str], | ||
| client_request_token: Optional[str], | ||
| max_length: int, | ||
| ) -> str: | ||
| if max_length < MIN_PHYSICAL_RESOURCE_ID_LENGTH: | ||
| raise Exception( | ||
| f"Cannot generate resource IDs shorter than\ | ||
| {MIN_PHYSICAL_RESOURCE_ID_LENGTH} characters." | ||
| ) | ||
|
|
||
| strinct_logical_resource_id: str = logical_resource_id or "" | ||
| strinct_client_request_token: str = client_request_token or "" | ||
|
|
||
| stack_name: str = stack_id_or_name or "" | ||
|
|
||
| pattern = re.compile(STACK_ARN_PATTERN) | ||
|
|
||
| if pattern.match(stack_name): | ||
| stack_name = stack_name.split("/")[1] | ||
|
|
||
| separate: bool = max_length > MIN_PREFERRED_LENGTH | ||
|
|
||
| clean_stack_name: str = stack_name.replace("^-+", "", 1).replace("--", "-") | ||
| free_chars: int = max_length - (HASH_LENGTH + 1) - (1 if separate else 0) | ||
|
|
||
| chars_for_resource_name: int = min( | ||
| free_chars // 2, len(strinct_logical_resource_id) | ||
| ) | ||
| chars_for_stack_name: int = min( | ||
| free_chars - chars_for_resource_name, len(clean_stack_name) | ||
| ) | ||
|
|
||
| hash_value: str = _get_hash(strinct_client_request_token) | ||
|
|
||
| identifier: str = ( | ||
| clean_stack_name[:chars_for_stack_name] | ||
| + ("-" if separate else "") | ||
| + strinct_logical_resource_id[:chars_for_resource_name] | ||
| + "-" | ||
| + hash_value[:HASH_LENGTH] | ||
| ) | ||
| return identifier | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import pytest | ||
| from cloudformation_cli_python_lib.identifier_utils import generate_resource_identifier | ||
|
|
||
|
|
||
| def test_generated_name_with_stack_name_and_long_logical_id(): | ||
| result: str = generate_resource_identifier( | ||
| stack_id_or_name="my-custom-stack-name", | ||
| logical_resource_id="my-long-long-long-long-long-logical-id-name", | ||
| client_request_token="123456789", | ||
| max_length=36, | ||
| ) | ||
| assert len(result) == 36 | ||
| assert result.startswith("my-custom-s-my-long-lon-") | ||
|
|
||
|
|
||
| def test_generated_name_with_stack_id_and_long_logical_id(): | ||
| result: str = generate_resource_identifier( | ||
| stack_id_or_name="arn:aws:cloudformation:us-east-1:123456789012:stack/my-stack-name/084c0bd1-082b-11eb-afdc-0a2fadfa68a5", # noqa: B950 # pylint: disable=line-too-long | ||
| logical_resource_id="my-long-long-long-long-long-logical-id-name", | ||
| client_request_token="123456789", | ||
| max_length=36, | ||
| ) | ||
| assert len(result) == 36 | ||
| assert result.startswith("my-stack-na-my-long-lon-") | ||
|
|
||
|
|
||
| def test_generated_name_with_short_stack_name_and_short_logical_id(): | ||
| result: str = generate_resource_identifier( | ||
| stack_id_or_name="abc", | ||
| logical_resource_id="abc", | ||
| client_request_token="123456789", | ||
| max_length=255, | ||
| ) | ||
| assert len(result) == 20 # "abc" + "-" + "abc" + "-" + 12 char hash | ||
| assert result.startswith("abc-abc-") | ||
|
|
||
|
|
||
| def test_generated_name_with_max_len_shorter_than_preferred(): | ||
| result: str = generate_resource_identifier( | ||
| stack_id_or_name="abc", | ||
| logical_resource_id="abc", | ||
| client_request_token="123456789", | ||
| max_length=16, | ||
| ) | ||
| assert len(result) == 16 | ||
| assert result.startswith("aba-f7c3bc1d808e") | ||
|
|
||
|
|
||
| def test_generated_name_with_invalid_len(): | ||
| with pytest.raises(Exception) as excinfo: | ||
| generate_resource_identifier( | ||
| stack_id_or_name="my-stack-name", | ||
| logical_resource_id="my-logical-id", | ||
| client_request_token="123456789", | ||
| max_length=13, | ||
| ) | ||
| assert "Cannot generate resource IDs shorter than" in str(excinfo.value) |
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.