-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add dynamic random string generator for modules #1287
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
base: master
Are you sure you want to change the base?
Changes from all commits
0cc045d
2a32fe2
4090ae3
2a0b83e
b1ffe4e
6db5ee7
6fd226a
2d7ae21
29b52f5
cebef4f
807cd64
1a908d2
561ac67
11ac571
09788d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,10 @@ | ||
| import copy | ||
| import re | ||
|
|
||
| import yaml | ||
|
|
||
| from nettacker.config import Config | ||
| from nettacker.core.utils import common as common_utils | ||
|
|
||
|
|
||
| class TemplateLoader: | ||
|
|
@@ -27,6 +29,26 @@ def parse(module_content, module_inputs): | |
|
|
||
| return module_content | ||
|
|
||
| def _apply_dynamic_placeholders(self, content: str) -> str: | ||
| """ | ||
| Handle runtime placeholders like: | ||
| - {rand_str(10)} | ||
| """ | ||
|
|
||
| def _rand_str_replacer(match): | ||
| length = min( | ||
| int(match.group(1)), | ||
| 256, # maximum length allowed in rand_str | ||
| ) | ||
| return common_utils.generate_random_token(length) | ||
|
|
||
| content = re.sub( | ||
| r"\{rand_str\((\d+)\)\}", | ||
| _rand_str_replacer, | ||
| content, | ||
| ) | ||
| return content | ||
|
|
||
| def open(self): | ||
| module_name_parts = self.name.split("_") | ||
| action = module_name_parts[-1] | ||
|
|
@@ -36,7 +58,9 @@ def open(self): | |
| return yaml_file.read() | ||
|
|
||
| def format(self): | ||
| return self.open().format(**self.inputs) | ||
| content = self.open() | ||
| content = self._apply_dynamic_placeholders(content) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This introduces a new template-preprocessing path without any test coverage, so regressions involving replacement, the 256-character cap, multiple placeholders, or coexistence with ordinary placeholders such as AGENTS.md reference: AGENTS.md:L27-L30 Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add tests once this is merged |
||
| return content.format(**self.inputs) | ||
|
|
||
| def load(self): | ||
| return self.parse(yaml.safe_load(self.format()), self.inputs) | ||
Uh oh!
There was an error while loading. Please reload this page.