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

feat: extended rh_templates to pass user validators to resthandler #1343

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@
class RestEntityBuilder:
_title_template = "[{}]"
_rh_template = """
special_fields = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If special_fields are not present, can we omit this variable declaration?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can, but how do we benefit from it? I think it's not worth adding extra logic and template just to avoid declaring an empty list. What do you think about it?

{special_fields}
]

fields{name_rh} = [
{fields}
]
model{name_rh} = RestModel(fields{name_rh}, name={name})
model{name_rh} = RestModel(fields{name_rh}, name={name}, special_fields=special_fields)
"""
_disabled_field_template = """
field.RestField(
Expand All @@ -47,10 +51,15 @@ class RestEntityBuilder:
"""

def __init__(
self, name: Optional[str], fields: List["RestFieldBuilder"], **kwargs: Any
self,
name: Optional[str],
fields: List["RestFieldBuilder"],
special_fields: List["RestFieldBuilder"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In splunktaucclib you had made this parameter of type Optional[List[RestField]] should we mention the same thing here.

**kwargs: Any,
) -> None:
self._name = name
self._fields = fields
self._special_fields = special_fields
self._conf_name = kwargs.get("conf_name")

@property
Expand Down Expand Up @@ -79,9 +88,13 @@ def generate_conf_with_default_values(self) -> str:

def generate_rh(self) -> str:
fields = []
special_fields = []
for field in self._fields:
field_line = field.generate_rh()
fields.append(field_line)
for special_field in self._special_fields:
field_line = special_field.generate_rh()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think here we can use list comprehension, or we can remove redundant variable declaration.

special_fields.append(field_line)
# add disabled field for data input
entity_builder = self.__class__.__name__
if (
Expand All @@ -91,7 +104,9 @@ def generate_rh(self) -> str:
):
fields.append(self._disabled_field_template)
fields_lines = ", \n".join(fields)
special_fields_lines = ", \n".join(special_fields)
return self._rh_template.format(
special_fields=indent(special_fields_lines),
fields=indent(fields_lines),
name_rh=self.name_rh,
name=quote_string(self._name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#
import json
from typing import Dict, List, Set, Any
from typing import Dict, List, Set, Any, Tuple

from splunk_add_on_ucc_framework import global_config as global_config_lib

Expand Down Expand Up @@ -105,10 +105,11 @@ def _builder_configs(self) -> None:
)
self._endpoints[name] = endpoint
content = self._get_oauth_enitities(config["entity"])
fields = self._parse_fields(content)
fields, special_fields = self._parse_fields(content)
entity = SingleModelEntityBuilder(
None,
fields,
special_fields=special_fields,
conf_name=config.get("conf"),
)
endpoint.add_entity(entity)
Expand Down Expand Up @@ -140,10 +141,11 @@ def _builder_settings(self) -> None:
self._endpoints["settings"] = endpoint
for setting in self.global_config.settings:
content = self._get_oauth_enitities(setting["entity"])
fields = self._parse_fields(content)
fields, special_fields = self._parse_fields(content)
entity = MultipleModelEntityBuilder(
setting["name"],
fields,
special_fields=special_fields,
)
endpoint.add_entity(entity)
self._settings_conf_file_names.add(endpoint.conf_name)
Expand All @@ -170,10 +172,11 @@ def _builder_inputs(self) -> None:
)
self._endpoints[name] = single_model_endpoint
content = self._get_oauth_enitities(input_item["entity"])
fields = self._parse_fields(content)
fields, special_fields = self._parse_fields(content)
single_model_entity = SingleModelEntityBuilder(
None,
fields,
special_fields=special_fields,
conf_name=input_item["conf"],
)
single_model_endpoint.add_entity(single_model_entity)
Expand All @@ -189,28 +192,42 @@ def _builder_inputs(self) -> None:
)
self._endpoints[name] = data_input_endpoint
content = self._get_oauth_enitities(input_item["entity"])
fields = self._parse_fields(content)
fields, special_fields = self._parse_fields(content)
data_input_entity = DataInputEntityBuilder(
None,
fields,
special_fields=special_fields,
input_type=input_item["name"],
)
data_input_endpoint.add_entity(data_input_entity)

def _parse_fields(
self, fields_content: List[Dict[str, Any]]
) -> List[RestFieldBuilder]:
return [
RestFieldBuilder(
field["field"],
_is_true(field.get("required")),
_is_true(field.get("encrypted")),
field.get("defaultValue"),
ValidatorBuilder().build(field.get("validators")),
)
for field in fields_content
if field["field"] != "name"
]
) -> Tuple[List[RestFieldBuilder], List[RestFieldBuilder]]:
fields = []
special_fields = []
for field in fields_content:
if field["field"] != "name":
fields.append(
RestFieldBuilder(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull building object up to for loop

field["field"],
_is_true(field.get("required")),
_is_true(field.get("encrypted")),
field.get("defaultValue"),
ValidatorBuilder().build(field.get("validators")),
)
)
else:
special_fields.append(
RestFieldBuilder(
field["field"],
_is_true(field.get("required")),
_is_true(field.get("encrypted")),
field.get("defaultValue"),
ValidatorBuilder().build(field.get("validators")),
)
)
return fields, special_fields

"""
If the entity contains type oauth then we need to alter the content to generate proper entities to generate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _set_attributes(self, **kwargs: Any) -> None:
if account["name"] == "oauth":
continue
content = self._gc_schema._get_oauth_enitities(account["entity"])
fields = self._gc_schema._parse_fields(content)
fields, special_fields = self._gc_schema._parse_fields(content)
self.account_fields.append(
("<name>", [f"{f._name} = " for f in fields])
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def _set_attributes(self, **kwargs: Any) -> None:
self.conf_spec_file = f"{self.conf_file}.spec"
for setting in self._global_config.settings:
content = self._gc_schema._get_oauth_enitities(setting["entity"])
fields = self._gc_schema._parse_fields(content)
fields, special_fields = self._gc_schema._parse_fields(content)
self.settings_stanzas.append(
(setting["name"], [f"{f._name} = " for f in fields])
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
util.remove_http_proxy_env_vars()


special_fields = [
field.RestField(
'name',
required=True,
encrypted=False,
default=None,
validator=validator.AllOf(
validator.String(
max_len=50,
min_len=1,
),
validator.Pattern(
regex=r"""^[a-zA-Z]\w*$""",
)
)
)
]

fields = [
field.RestField(
'custom_endpoint',
Expand Down Expand Up @@ -128,7 +146,7 @@
validator=None
)
]
model = RestModel(fields, name=None)
model = RestModel(fields, name=None, special_fields=special_fields)


endpoint = SingleModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
util.remove_http_proxy_env_vars()


special_fields = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we skip the declaration of special_fields if it is an empty list?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like above: "we can, but how do we benefit from it? I think it's not worth adding extra logic and template just to avoid declaring an empty list. What do you think about it?"


]

fields_proxy = [
field.RestField(
'proxy_enabled',
Expand Down Expand Up @@ -78,9 +82,13 @@
validator=None
)
]
model_proxy = RestModel(fields_proxy, name='proxy')
model_proxy = RestModel(fields_proxy, name='proxy', special_fields=special_fields)


special_fields = [

]

fields_logging = [
field.RestField(
'loglevel',
Expand All @@ -90,8 +98,12 @@
validator=None
)
]
model_logging = RestModel(fields_logging, name='logging')
model_logging = RestModel(fields_logging, name='logging', special_fields=special_fields)


special_fields = [

]

fields_custom_abc = [
field.RestField(
Expand Down Expand Up @@ -160,7 +172,7 @@
)
)
]
model_custom_abc = RestModel(fields_custom_abc, name='custom_abc')
model_custom_abc = RestModel(fields_custom_abc, name='custom_abc', special_fields=special_fields)


endpoint = MultipleModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
util.remove_http_proxy_env_vars()


special_fields = [
field.RestField(
'name',
required=True,
encrypted=False,
default=None,
validator=validator.AllOf(
validator.String(
max_len=50,
min_len=1,
),
validator.Pattern(
regex=r"""^[a-zA-Z]\w*$""",
)
)
)
]

fields = [
field.RestField(
'custom_endpoint',
Expand Down Expand Up @@ -179,7 +197,7 @@
validator=None
)
]
model = RestModel(fields, name=None)
model = RestModel(fields, name=None, special_fields=special_fields)


endpoint = SingleModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
util.remove_http_proxy_env_vars()


special_fields = [
field.RestField(
'name',
required=True,
encrypted=False,
default=None,
validator=validator.AllOf(
validator.Pattern(
regex=r"""^[a-zA-Z]\w*$""",
),
validator.String(
max_len=100,
min_len=1,
)
)
)
]

fields = [
field.RestField(
'interval',
Expand All @@ -32,7 +50,7 @@
)

]
model = RestModel(fields, name=None)
model = RestModel(fields, name=None, special_fields=special_fields)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
util.remove_http_proxy_env_vars()


special_fields = [
field.RestField(
'name',
required=True,
encrypted=False,
default=None,
validator=validator.AllOf(
validator.Pattern(
regex=r"""^[a-zA-Z]\w*$""",
),
validator.String(
max_len=100,
min_len=1,
)
)
)
]

fields = [
field.RestField(
'input_one_checkbox',
Expand Down Expand Up @@ -161,7 +179,7 @@
)

]
model = RestModel(fields, name=None)
model = RestModel(fields, name=None, special_fields=special_fields)



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@
util.remove_http_proxy_env_vars()


special_fields = [
field.RestField(
'name',
required=True,
encrypted=False,
default=None,
validator=validator.AllOf(
validator.Pattern(
regex=r"""^[a-zA-Z]\w*$""",
),
validator.String(
max_len=100,
min_len=1,
)
)
)
]

fields = [
field.RestField(
'interval',
Expand Down Expand Up @@ -119,7 +137,7 @@
)

]
model = RestModel(fields, name=None)
model = RestModel(fields, name=None, special_fields=special_fields)



Expand Down
Loading
Loading