-
Notifications
You must be signed in to change notification settings - Fork 87
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
Msg filter #1144
Msg filter #1144
Conversation
hubblestack/filter/filter_chain.py
Outdated
self.config = {} | ||
self.chain = [] | ||
self._load_config() | ||
|
||
def _load_config(self): | ||
config_path = "salt://filter_chain.yaml" | ||
config_path = __mods__["cp.cache_file"](config_path) | ||
try: | ||
with open(config_path, 'r') as handle: | ||
self.config = yaml.safe_load(handle) | ||
except Exception as e: | ||
self.config = {"default": {"filter": { "default": { | ||
"sequence_id": { "label": "seq", "type": "hubblestack.filter.seq_id" }, | ||
"hubble_version": { "label": "hubble_version", "type": "hubblestack.hubble_version"}}}}} | ||
raise CommandExecutionError(f"Could not load filter config: {e}") | ||
|
||
if not isinstance(self.config, dict) or \ | ||
"filter" not in self.config or \ | ||
not(isinstance(self.config["filter"], dict)) or \ | ||
self.config_label not in self.config["filter"].keys() or \ | ||
not(isinstance(self.config["filter"][self.config_label], dict)): | ||
raise CommandExecutionError("FilterChain config not formatted correctly") | ||
|
||
self.config = self.config['filter'][self.config_label] | ||
|
||
for filter_name in self.config: | ||
new_fltr = self._get_filter_class(self.config[filter_name]["type"])(filter_name, self.config[filter_tag]) | ||
self.chain.append(new_fltr) |
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.
Follow-up on my property
suggestion. It was missing the return
, as @mzainea said, and now config
can be used as a variable inside the method's scope. Nevertheless, when we do FilterChain.config
in some other code, it will return the config proper.
self.config = {} | |
self.chain = [] | |
self._load_config() | |
def _load_config(self): | |
config_path = "salt://filter_chain.yaml" | |
config_path = __mods__["cp.cache_file"](config_path) | |
try: | |
with open(config_path, 'r') as handle: | |
self.config = yaml.safe_load(handle) | |
except Exception as e: | |
self.config = {"default": {"filter": { "default": { | |
"sequence_id": { "label": "seq", "type": "hubblestack.filter.seq_id" }, | |
"hubble_version": { "label": "hubble_version", "type": "hubblestack.hubble_version"}}}}} | |
raise CommandExecutionError(f"Could not load filter config: {e}") | |
if not isinstance(self.config, dict) or \ | |
"filter" not in self.config or \ | |
not(isinstance(self.config["filter"], dict)) or \ | |
self.config_label not in self.config["filter"].keys() or \ | |
not(isinstance(self.config["filter"][self.config_label], dict)): | |
raise CommandExecutionError("FilterChain config not formatted correctly") | |
self.config = self.config['filter'][self.config_label] | |
for filter_name in self.config: | |
new_fltr = self._get_filter_class(self.config[filter_name]["type"])(filter_name, self.config[filter_tag]) | |
self.chain.append(new_fltr) | |
self._config = self.config | |
self.chain = [] | |
@property | |
def config(self): | |
config_path = "salt://filter_chain.yaml" | |
config_path = __mods__["cp.cache_file"](config_path) | |
try: | |
with open(config_path, 'r') as handle: | |
config = yaml.safe_load(handle) | |
except Exception as e: | |
config = {"default": {"filter": { "default": { | |
"sequence_id": { "label": "seq", "type": "hubblestack.filter.seq_id" }, | |
"hubble_version": { "label": "hubble_version", "type": "hubblestack.hubble_version"}}}}} | |
raise CommandExecutionError(f"Could not load filter config: {e}") | |
if not isinstance(config, dict) or \ | |
"filter" not in config or \ | |
not(isinstance(config.get("filter"), dict)) or \ | |
config_label not in config["filter"].keys() or \ | |
not(isinstance(config.get("filter", {}).get(self.config_label), dict)): | |
raise CommandExecutionError("FilterChain config not formatted correctly") | |
config = config['filter'][config_label] | |
for filter_name in self.config: | |
new_fltr = self._get_filter_class(config[filter_name]["type"])(filter_name, config[filter_tag]) | |
self.chain.append(new_fltr) | |
return config |
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.
LGTM. Using property per @vladmonea 's suggestion for config and chain. Thanks!
No description provided.