-
-
Notifications
You must be signed in to change notification settings - Fork 777
Add support for overridding whether resources are enabled #5506
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
Changes from 5 commits
f5a6aa8
af455f6
6592a7c
8f4f4f8
d7a1435
e3c8352
20fe84f
6200478
2415056
9ce0662
df920ab
e8ea7a4
7938dce
27da722
3635f4c
70035fc
3810862
0f1d233
455f890
ef1b2c2
03671cf
d485fc4
45c6bc9
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 |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ | |
| from yaml.parser import ParserError | ||
| import six | ||
|
|
||
| from oslo_config import cfg | ||
| from st2common import log as logging | ||
| from st2common.constants.meta import ALLOWED_EXTS | ||
| from st2common.constants.meta import PARSER_FUNCS | ||
|
|
@@ -28,7 +29,7 @@ | |
| if six.PY2: | ||
| from io import open | ||
|
|
||
| __all__ = ["ContentPackLoader", "MetaLoader"] | ||
| __all__ = ["ContentPackLoader", "MetaLoader", "OverrideLoader"] | ||
|
|
||
| LOG = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -268,3 +269,124 @@ def _load(self, parser_func, file_path): | |
| except ParserError: | ||
| LOG.exception("Failed loading content from %s.", file_path) | ||
| raise | ||
|
|
||
|
|
||
| class OverrideLoader(object): | ||
| """ | ||
| Class for loading pack override data | ||
| """ | ||
|
|
||
| # Mapping of permitted override types to resource name | ||
| ALLOWED_OVERRIDE_TYPES = { | ||
| "sensors": "class_name", | ||
| "actions": "name", | ||
| "rules": "name", | ||
| "aliases": "name", | ||
| } | ||
|
|
||
| ALLOWED_OVERRIDE_NAMES = [ | ||
| "enabled", | ||
| ] | ||
|
|
||
| def override(self, pack_name, type, content): | ||
|
|
||
| """ | ||
| Loads override content for pack, and updates content | ||
|
|
||
| :param pack_name: Name of pack | ||
| :type pack_name: ``str`` | ||
| :param type: Type of resource loading | ||
| :type type: ``str`` | ||
| :param content: Content as loaded from meta information | ||
| :type content: ``object`` | ||
|
|
||
| """ | ||
|
|
||
| if type not in self.ALLOWED_OVERRIDE_TYPES.keys(): | ||
| raise ValueError( | ||
| f"Invalid override type of {type} attempted for pack {pack_name}" | ||
| ) | ||
|
|
||
| override_dir = os.path.join(cfg.CONF.system.base_path, "configs/overrides") | ||
| # Apply global overrides | ||
| global_file = os.path.join(override_dir, "global.yaml") | ||
|
||
| self._apply_override_file(global_file, pack_name, type, content, True) | ||
|
|
||
| # Apply pack overrides | ||
| override_file = os.path.join(override_dir, f"{pack_name}.yaml") | ||
| self._apply_override_file(override_file, pack_name, type, content, False) | ||
|
|
||
| return content | ||
|
|
||
| def _apply_override_file( | ||
| self, override_file, pack_name, type, content, global_file | ||
| ): | ||
|
|
||
| """ | ||
| Loads override content from override file | ||
|
|
||
| :param override_file: Override filename | ||
| :type override_file: ``str`` | ||
| :param pack_name: Name of pack | ||
| :type pack_name: ``str`` | ||
| :param type: Type of resource loading | ||
| :type type: ``str`` | ||
| :param content: Content as loaded from meta information | ||
| :type content: ``object`` | ||
| :param global_file: Whether global file | ||
| :type global_file: ``bool`` | ||
| """ | ||
|
|
||
| if not os.path.exists(override_file): | ||
| # No override file for pack | ||
| LOG.info(f"No override file {override_file} found") | ||
| return content | ||
|
|
||
| # Read override file | ||
| file_name, file_ext = os.path.splitext(override_file) | ||
| overrides = self._load(PARSER_FUNCS[file_ext], override_file) | ||
| # Apply overrides | ||
| if type in overrides.keys(): | ||
| type_override = overrides[type] | ||
| name = content[self.ALLOWED_OVERRIDE_TYPES[type]] | ||
| if "defaults" in type_override.keys(): | ||
| for key in type_override["defaults"].keys(): | ||
| if key in self.ALLOWED_OVERRIDE_NAMES: | ||
| content[key] = type_override["defaults"][key] | ||
| LOG.info( | ||
| f"Overridden {type} {pack_name}.{name} {key} to default value of {content[key]} from {override_file}" | ||
| ) | ||
| else: | ||
| raise ValueError( | ||
| f"Override attempted with invalid default key {key} in pack {pack_name}" | ||
| ) | ||
|
|
||
| if global_file: | ||
| # No exceptions required in global content file | ||
| return content | ||
|
|
||
| if "exceptions" in type_override.keys(): | ||
| if name in type_override["exceptions"]: | ||
| for key in type_override["exceptions"][name].keys(): | ||
amanda11 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if key in self.ALLOWED_OVERRIDE_NAMES: | ||
| content[key] = type_override["exceptions"][name][key] | ||
| LOG.info( | ||
| f"Overridden {type} {pack_name}.{name} {key} to exception value of {content[key]} from {override_file}" | ||
| ) | ||
| else: | ||
| raise ValueError( | ||
| f"Override attempted with invalid exceptions key {key} in pack {pack_name}" | ||
| ) | ||
|
|
||
| return content | ||
|
|
||
| def _load(self, parser_func, file_path): | ||
| with open(file_path, "r", encoding="utf-8") as fd: | ||
| try: | ||
| return parser_func(fd) | ||
| except ValueError: | ||
| LOG.exception("Failed loading content from %s.", file_path) | ||
| raise | ||
| except ParserError: | ||
| LOG.exception("Failed loading content from %s.", file_path) | ||
| raise | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| sensors: | ||
| defaults: | ||
| enabled: false |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| actions: | ||
| defaults: | ||
| enabled: False | ||
| exceptions: | ||
| action2: | ||
| enabled: True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| actions: | ||
| defaults: | ||
| enabled: False | ||
| rubbish: False | ||
| exceptions: | ||
| action2: | ||
| enabled: True | ||
| sensors: | ||
| defaults: | ||
| enabled: True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| actions: | ||
| defaults: | ||
| enabled: False | ||
| exceptions: | ||
| action2: | ||
| rubbish: True | ||
| sensors: | ||
| exceptions: | ||
| sensor1: | ||
| enabled: True |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| actions: | ||
| defaults: | ||
| enabled: False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to avoid hijacking the
typekeyword and useresource_typeinstead?