Skip to content

Commit

Permalink
feat(generate):generation of html files for alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
hetangmodi-crest committed Aug 2, 2024
1 parent f0c4044 commit 4db6be8
Show file tree
Hide file tree
Showing 22 changed files with 117 additions and 155 deletions.
18 changes: 1 addition & 17 deletions splunk_add_on_ucc_framework/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
import configparser
import glob
import json
import logging
Expand Down Expand Up @@ -158,19 +157,6 @@ def _add_modular_input(
with open(helper_filename, "w") as helper_file:
helper_file.write(content)

input_default = os.path.join(outputdir, ta_name, "default", "inputs.conf")
config = configparser.ConfigParser()
if os.path.exists(input_default):
config.read(input_default)

if config.has_section(input_name):
config[input_name]["python.version"] = "python3"
else:
config[input_name] = {"python.version": "python3"}

with open(input_default, "w") as configfile:
config.write(configfile)


def _get_ignore_list(
addon_name: str, ucc_ignore_path: str, output_directory: str
Expand Down Expand Up @@ -536,9 +522,7 @@ def generate(
_add_modular_input(ta_name, global_config, output_directory)
if global_config.has_alerts():
logger.info("Generating alerts code")
alert_builder.generate_alerts(
global_config, ta_name, internal_root_dir, output_directory
)
alert_builder.generate_alerts(global_config, ta_name, output_directory)

conf_file_names = []
conf_file_names.extend(list(scheme.settings_conf_file_names))
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
from splunk_add_on_ucc_framework.commands.modular_alert_builder import (
normalize,
)
from splunk_add_on_ucc_framework.commands.modular_alert_builder import (
alert_actions_html_gen,
)
from splunk_add_on_ucc_framework.commands.modular_alert_builder import (
alert_actions_py_gen,
)
Expand All @@ -33,7 +30,6 @@
def generate_alerts(
global_config: global_config_lib.GlobalConfig,
addon_name: str,
internal_source_dir: str,
output_dir: str,
) -> None:
envs = normalize.normalize(
Expand All @@ -43,12 +39,6 @@ def generate_alerts(
package_dir = os.path.join(output_dir, addon_name)
schema_content = envs["schema.content"]

html_gen = alert_actions_html_gen.AlertHtmlGenerator(
input_setting=schema_content,
package_path=package_dir,
)
html_gen.handle()

py_gen = alert_actions_py_gen.AlertActionsPyGenerator(
addon_name=addon_name,
input_setting=schema_content,
Expand Down
9 changes: 9 additions & 0 deletions splunk_add_on_ucc_framework/generators/file_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
RedirectXml,
)

from splunk_add_on_ucc_framework.generators.html_files import AlertHtml

__all__ = ["FileClass", "GEN_FILE_LIST"]


Expand All @@ -57,6 +59,7 @@ class FileClass(NamedTuple):
DefaultXml,
InputsXml,
RedirectXml,
AlertHtml,
]
]
file_path: Union[str, List[str]]
Expand Down Expand Up @@ -111,4 +114,10 @@ class FileClass(NamedTuple):
["default", "data", "ui", "views"],
RedirectXml.__description__,
),
FileClass(
"_.html",
AlertHtml,
["default", "data", "ui", "alerts"],
AlertHtml.__description__,
),
]
2 changes: 1 addition & 1 deletion splunk_add_on_ucc_framework/generators/file_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def set_template_and_render(
loader=FileSystemLoader(sep.join(self._template_dir + template_file_path)),
trim_blocks=True,
lstrip_blocks=True,
keep_trailing_newline=False,
keep_trailing_newline=True,
)
self._template = self._template.get_template(file_name)

Expand Down
4 changes: 4 additions & 0 deletions splunk_add_on_ucc_framework/generators/html_files/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .html_generator import HTMLGenerator
from .create_alert_html import AlertHtml

__all__ = ["HTMLGenerator", "AlertHtml"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from splunk_add_on_ucc_framework.generators.html_files import HTMLGenerator
from splunk_add_on_ucc_framework.global_config import GlobalConfig
from splunk_add_on_ucc_framework.commands.modular_alert_builder import (
arf_consts as ac,
normalize,
)
from typing import Dict, Any
from os import linesep
from re import search


class AlertHtml(HTMLGenerator):
__description__ = (
" Generates `alert_name.html` file based on alerts configuration present in globalConfig"
" in `default/data/ui/alerts` folder."
)

def __init__(
self,
global_config: GlobalConfig,
input_dir: str,
output_dir: str,
**kwargs: Dict[str, Any],
) -> None:
super().__init__(global_config, input_dir, output_dir, **kwargs)

def _set_attributes(self, **kwargs: Dict[str, Any]) -> None:
if self._global_config and self._global_config.has_alerts():
self._html_home = "alert_html_skeleton.template"
envs = normalize.normalize(
self._global_config.alerts,
self._global_config.namespace,
)
schema_content = envs["schema.content"]
self._alert_settings = schema_content["modular_alerts"]
for self.alert in self._alert_settings:
self.generate_html()

def generate_html(self) -> Dict[str, str]:
if self._global_config and not self._global_config.has_alerts():
return super().generate_html()
self.set_template_and_render(
template_file_path=["html_templates"], file_name="mod_alert.html.template"
)
rendered_content = self._template.render(
mod_alert=self.alert, home_page=self._html_home
)
text = linesep.join(
[s for s in rendered_content.splitlines() if not search(r"^\s*$", s)]
)
file_name = f"{self.alert[ac.SHORT_NAME] + '.html'}"
file_path = self.get_file_output_path(
[
"default",
"data",
"ui",
"alerts",
file_name,
]
)
self.writer(
file_name=file_name,
file_path=file_path,
content=text,
)
return {file_name: file_path}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from ..file_generator import FileGenerator
from typing import Dict, Any, Union, NoReturn
from splunk_add_on_ucc_framework.global_config import GlobalConfig


class HTMLGenerator(FileGenerator):
__description__ = "DESCRIBE THE HTML FILE THAT IS GENERATED"

def __init__(
self,
global_config: GlobalConfig,
input_dir: str,
output_dir: str,
**kwargs: Dict[str, Any]
) -> None:
super().__init__(global_config, input_dir, output_dir, **kwargs)

def generate(self) -> Dict[str, str]:
html_files: Dict[str, str] = {}
html_files.update(self.generate_html())
return html_files

def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]:
# parse self._global_config and set the require attributes for self
raise NotImplementedError()

def generate_html(self) -> Dict[str, str]:
# uses the attributes set in _set_attributes method to set the required attributes
# uses set_template_and_render to load and render the HTML template.
# use self.writer function to create the html file.
return {"": ""}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def __init__(
super().__init__(global_config, input_dir, output_dir, **kwargs)

def generate(self) -> Dict[str, str]:
conf_files: Dict[str, str] = {}
conf_files.update(self.generate_xml())
return conf_files
xml_files: Dict[str, str] = {}
xml_files.update(self.generate_xml())
return xml_files

@abstractmethod
def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]:
Expand All @@ -43,5 +43,5 @@ def _set_attributes(self, **kwargs: Any) -> Union[NoReturn, None]:

def generate_xml(self) -> Dict[str, str]:
# uses the attributes set in _set_attributes method to set the required attributes
# use self.writer function to create the xml file to get the output file to create the file.
# use self.writer function to create the xml file.
return {"": ""}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from splunk_add_on_ucc_framework.commands.modular_alert_builder import (
from splunk_add_on_ucc_framework.commands.modular_alert_builder import ( # type: ignore[attr-defined]
alert_actions_html_gen,
)
from tests.unit.helpers import get_testdata_file
Expand Down
2 changes: 0 additions & 2 deletions tests/unit/commands/modular_alert_builder/test_builder.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from splunk_add_on_ucc_framework.commands.build import internal_root_dir
from splunk_add_on_ucc_framework.commands.modular_alert_builder import builder
from tests.unit.helpers import get_testdata_file

Expand All @@ -17,7 +16,6 @@ def test_builder(global_config_all_json, tmp_path):
builder.generate_alerts(
global_config_all_json,
"Splunk_TA_UCCExample",
internal_root_dir,
str(tmp_path),
)

Expand Down

0 comments on commit 4db6be8

Please sign in to comment.