Skip to content
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

Merged
merged 18 commits into from
Oct 3, 2022
Merged

Msg filter #1144

merged 18 commits into from
Oct 3, 2022

Conversation

grogsaxle
Copy link
Contributor

No description provided.

Comment on lines 36 to 63
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)
Copy link
Contributor

@vladmonea vladmonea Sep 2, 2022

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.

Suggested change
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

Copy link
Contributor Author

@grogsaxle grogsaxle left a 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!

@grogsaxle grogsaxle marked this pull request as ready for review September 6, 2022 14:53
@grogsaxle grogsaxle requested a review from vladmonea October 3, 2022 13:49
@grogsaxle grogsaxle merged commit 5126963 into hubblestack:develop Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants