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

Fix bug: Directives will be error on deserialize #299

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions hyperglass/models/directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .fields import Action

StringOrArray = t.Union[str, t.List[str]]
Condition = t.Union[IPvAnyNetwork, str]
Condition = t.Union[str, None]
RuleValidation = t.Union[t.Literal["ipv4", "ipv6", "pattern"], None]
PassedValidation = t.Union[bool, None]
IPFamily = t.Literal["ipv4", "ipv6"]
Expand Down Expand Up @@ -264,7 +264,7 @@ class Directive(HyperglassUniqueModel, unique_by=("id", "table_output")):
id: str
name: str
rules: t.List[RuleType] = [RuleWithoutValidation()]
field: t.Union[Text, Select]
field: t.Union[Text, Select, None]
info: t.Optional[FilePath] = None
plugins: t.List[str] = []
table_output: t.Optional[str] = None
Expand All @@ -282,15 +282,16 @@ def validate_rules(cls, rules: t.List[t.Dict[str, t.Any]]):
condition = rule.get("condition")
if condition is None:
out_rules.append(RuleWithoutValidation(**rule))
try:
condition_net = ip_network(condition)
if condition_net.version == 4:
out_rules.append(RuleWithIPv4(**rule))
if condition_net.version == 6:
out_rules.append(RuleWithIPv6(**rule))
except ValueError:
out_rules.append(RuleWithPattern(**rule))
if isinstance(rule, Rule):
else:
try:
condition_net = ip_network(condition)
if condition_net.version == 4:
out_rules.append(RuleWithIPv4(**rule))
if condition_net.version == 6:
out_rules.append(RuleWithIPv6(**rule))
except ValueError:
out_rules.append(RuleWithPattern(**rule))
elif isinstance(rule, Rule):
out_rules.append(rule)
return out_rules

Expand All @@ -306,7 +307,8 @@ def validate_target(self, target: str) -> bool:
@property
def field_type(self) -> t.Literal["text", "select", None]:
"""Get the linked field type."""

if self.field is None:
return None
if self.field.is_select:
return "select"
if self.field.is_text or self.field.is_ip:
Expand Down Expand Up @@ -337,15 +339,15 @@ def frontend(self: "Directive") -> t.Dict[str, t.Any]:
"name": self.name,
"field_type": self.field_type,
"groups": self.groups,
"description": self.field.description,
"description": self.field.description if self.field is not None else '',
"info": None,
}

if self.info is not None:
with self.info.open() as md:
value["info"] = md.read()

if self.field.is_select:
if self.field is not None and self.field.is_select:
value["options"] = [o.export_dict() for o in self.field.options if o is not None]

return value
Expand Down
2 changes: 1 addition & 1 deletion hyperglass/models/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UIDirective(HyperglassModel):

id: str
name: str
field_type: str
field_type: t.Union[str, None]
groups: t.List[str]
description: str
info: t.Optional[str] = None
Expand Down
11 changes: 9 additions & 2 deletions hyperglass/ui/components/looking-glass-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ export const LookingGlassForm = (): JSX.Element => {
);

const directive = useMemo<Directive | null>(
() => getDirective(),
() => {
const tmp = getDirective();
if (tmp !== null && tmp.fieldType === null) {
setFormValue('queryTarget', ['null']);
setValue('queryTarget', ['null']);
}
return tmp;
},
[form.queryType, form.queryLocation, getDirective],
);

Expand Down Expand Up @@ -200,7 +207,7 @@ export const LookingGlassForm = (): JSX.Element => {
<QueryType onChange={handleChange} label={web.text.queryType} />
</FormField>
</SlideFade>
<SlideFade offsetX={100} in={directive !== null} unmountOnExit>
<SlideFade offsetX={100} in={directive !== null && directive.fieldType !== null} unmountOnExit>
{directive !== null && (
<FormField name="queryTarget" label={web.text.queryTarget}>
<QueryTarget
Expand Down