-
Notifications
You must be signed in to change notification settings - Fork 24
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
Changes from 2 commits
b5ebf84
a5f5a98
aeb06f4
8dc06bf
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 |
---|---|---|
|
@@ -33,10 +33,14 @@ | |
class RestEntityBuilder: | ||
_title_template = "[{}]" | ||
_rh_template = """ | ||
special_fields = [ | ||
{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( | ||
|
@@ -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"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In splunktaucclib you had made this parameter of type |
||
**kwargs: Any, | ||
) -> None: | ||
self._name = name | ||
self._fields = fields | ||
self._special_fields = special_fields | ||
self._conf_name = kwargs.get("conf_name") | ||
|
||
@property | ||
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( | ||
|
@@ -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), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,10 @@ | |
util.remove_http_proxy_env_vars() | ||
|
||
|
||
special_fields = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', | ||
|
@@ -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', | ||
|
@@ -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( | ||
|
@@ -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( | ||
|
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.
If special_fields are not present, can we omit this variable declaration?
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.
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?