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: make configuration page as optional #1521

Open
wants to merge 13 commits into
base: develop
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
2 changes: 1 addition & 1 deletion docs/generated_files.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ The following table describes the files generated by UCC framework.
| _settings.conf | output/&lt;YOUR_ADDON_NAME&gt;/README | Generates `<YOUR_ADDON_NAME>_settings.conf.spec` file for the Proxy, Logging or Custom Tab mentioned in globalConfig |
| configuration.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/views | Generates configuration.xml file in `default/data/ui/views/` folder if globalConfig is present. |
| dashboard.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/views | Generates dashboard.xml file based on dashboard configuration present in globalConfig in `default/data/ui/views` folder. |
| default.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/nav | Generates default.xml file based on configs present in globalConfigin in `default/data/ui/nav` folder. |
| default.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/nav | Generates default.xml file based on configs present in globalConfig, in `default/data/ui/nav` folder. |
| inputs.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/views | Generates inputs.xml based on inputs configuration present in globalConfig, in `default/data/ui/views/inputs.xml` folder |
| _redirect.xml | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/views | Generates ta_name_redirect.xml file, if oauth is mentioned in globalConfig,in `default/data/ui/views/` folder. |
| _.html | output/&lt;YOUR_ADDON_NAME&gt;/default/data/ui/alerts | Generates `alert_name.html` file based on alerts configuration present in globalConfig in `default/data/ui/alerts` folder. |
Expand Down
3 changes: 2 additions & 1 deletion splunk_add_on_ucc_framework/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ def generate(
f"Updated and saved add-on version in the globalConfig file to {addon_version}"
)
global_config.add_ucc_version(__version__)
global_config.expand()
if global_config.has_configuration():
global_config.expand()
if ta_name != global_config.product:
logger.error(
"Add-on name mentioned in globalConfig meta tag and that app.manifest are not same,"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,17 @@ def __add_schemas_object(
) -> OpenAPIObject:
if open_api_object.components is not None:
open_api_object.components.schemas = {}
for tab in global_config.pages.configuration.tabs: # type: ignore[attr-defined]
schema_name, schema_object = __get_schema_object(
name=tab.name, entities=tab.entity
)
open_api_object.components.schemas[schema_name] = schema_object
schema_name, schema_object = __get_schema_object(
name=tab.name, entities=tab.entity, without=["name"]
)
open_api_object.components.schemas[schema_name] = schema_object
pages = getattr(global_config, "pages", None)
if hasattr(pages, "configuration"):
for tab in global_config.pages.configuration.tabs: # type: ignore[attr-defined]
schema_name, schema_object = __get_schema_object(
name=tab.name, entities=tab.entity
)
open_api_object.components.schemas[schema_name] = schema_object
schema_name, schema_object = __get_schema_object(
name=tab.name, entities=tab.entity, without=["name"]
)
open_api_object.components.schemas[schema_name] = schema_object
if hasattr(global_config.pages, "inputs") and hasattr( # type: ignore[attr-defined]
global_config.pages.inputs, "services" # type: ignore[attr-defined]
):
Expand Down Expand Up @@ -378,18 +380,20 @@ def __assign_ta_paths(
def __add_paths(
open_api_object: OpenAPIObject, global_config: DataClasses
) -> OpenAPIObject:
for tab in global_config.pages.configuration.tabs: # type: ignore[attr-defined]
open_api_object = __assign_ta_paths(
open_api_object=open_api_object,
path=f"/{global_config.meta.restRoot}_{tab.name}" # type: ignore[attr-defined]
if hasattr(tab, "table")
else f"/{global_config.meta.restRoot}_settings/{tab.name}", # type: ignore[attr-defined]
path_name=tab.name,
actions=tab.table.actions
if hasattr(tab, "table") and hasattr(tab.table, "actions")
else None,
page=GloblaConfigPages.CONFIGURATION,
)
pages = getattr(global_config, "pages", None)
if hasattr(pages, "configuration"):
for tab in global_config.pages.configuration.tabs: # type: ignore[attr-defined]
open_api_object = __assign_ta_paths(
open_api_object=open_api_object,
path=f"/{global_config.meta.restRoot}_{tab.name}" # type: ignore[attr-defined]
if hasattr(tab, "table")
else f"/{global_config.meta.restRoot}_settings/{tab.name}", # type: ignore[attr-defined]
path_name=tab.name,
actions=tab.table.actions
if hasattr(tab, "table") and hasattr(tab.table, "actions")
else None,
page=GloblaConfigPages.CONFIGURATION,
)
if hasattr(global_config.pages, "inputs") and hasattr( # type: ignore[attr-defined]
global_config.pages.inputs, "services" # type: ignore[attr-defined]
):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def _parse_builder_schema(self) -> None:
self._builder_inputs()

def _builder_configs(self) -> None:
if not self.global_config.has_configuration():
return
for config in self.global_config.configs:
name = config["name"]
endpoint = SingleModelEndpointBuilder(
Expand Down
21 changes: 15 additions & 6 deletions splunk_add_on_ucc_framework/data_ui_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def _pretty_print_xml(string: str) -> str:


def generate_nav_default_xml(
include_inputs: bool, include_dashboard: bool, default_view: str
include_inputs: bool,
include_dashboard: bool,
include_configuration: bool,
default_view: str,
) -> str:
"""
Generates `default/data/ui/nav/default.xml` file.
Expand All @@ -40,14 +43,20 @@ def generate_nav_default_xml(
"""
nav = ET.Element("nav")
if include_inputs:
if default_view == "inputs":
if (
not (include_configuration) and default_view == "configuration"
) or default_view == "inputs":
ET.SubElement(nav, "view", attrib={"name": "inputs", "default": "true"})
else:
ET.SubElement(nav, "view", attrib={"name": "inputs"})
if default_view == "configuration":
ET.SubElement(nav, "view", attrib={"name": "configuration", "default": "true"})
else:
ET.SubElement(nav, "view", attrib={"name": "configuration"})

if include_configuration:
if default_view == "configuration":
ET.SubElement(
nav, "view", attrib={"name": "configuration", "default": "true"}
)
else:
ET.SubElement(nav, "view", attrib={"name": "configuration"})
if include_dashboard:
if default_view == "dashboard":
ET.SubElement(nav, "view", attrib={"name": "dashboard", "default": "true"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@
# limitations under the License.
#
from splunk_add_on_ucc_framework.generators.xml_files import XMLGenerator
from typing import Any, Dict
from typing import Any, Dict, Union
from splunk_add_on_ucc_framework import data_ui_generator


class ConfigurationXml(XMLGenerator):
__description__ = "Generates configuration.xml file in `default/data/ui/views/` folder if globalConfig is present."

def _set_attributes(self, **kwargs: Any) -> None:
self.configuration_xml_content = (
data_ui_generator.generate_views_configuration_xml(
self._addon_name,
if self._global_config and self._global_config.has_configuration():
self.configuration_xml_content = (
data_ui_generator.generate_views_configuration_xml(
self._addon_name,
)
)
)

def generate_xml(self) -> Dict[str, str]:
def generate_xml(self) -> Union[Dict[str, str], None]:
if self._global_config and not self._global_config.has_configuration():
return None
file_path = self.get_file_output_path(
["default", "data", "ui", "views", "configuration.xml"]
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

class DefaultXml(XMLGenerator):
__description__ = (
"Generates default.xml file based on configs present in globalConfig"
"in in `default/data/ui/nav` folder."
"Generates default.xml file based on configs present in globalConfig, "
"in `default/data/ui/nav` folder."
)

def _set_attributes(self, **kwargs: Any) -> None:
Expand All @@ -45,6 +45,7 @@ def _set_attributes(self, **kwargs: Any) -> None:
self.default_xml_content = data_ui_generator.generate_nav_default_xml(
include_inputs=self._global_config.has_inputs(),
include_dashboard=self._global_config.has_dashboard(),
include_configuration=self._global_config.has_configuration(),
default_view=self._global_config.meta.get(
"default_view", data_ui_generator.DEFAULT_VIEW
),
Expand Down
16 changes: 12 additions & 4 deletions splunk_add_on_ucc_framework/global_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,14 @@ def expand(self) -> None:
self.expand_entities()

def expand_tabs(self) -> None:
for i, tab in enumerate(self._content["pages"]["configuration"]["tabs"]):
self._content["pages"]["configuration"]["tabs"][i] = resolve_tab(tab)
if self.has_configuration():
for i, tab in enumerate(self._content["pages"]["configuration"]["tabs"]):
self._content["pages"]["configuration"]["tabs"][i] = resolve_tab(tab)

def expand_entities(self) -> None:
self._expand_entities(self._content["pages"]["configuration"]["tabs"])
self._expand_entities(
self._content["pages"].get("configuration", {}).get("tabs")
)
self._expand_entities(self._content["pages"].get("inputs", {}).get("services"))
self._expand_entities(self._content.get("alerts"))

Expand All @@ -117,7 +120,9 @@ def inputs(self) -> List[Any]:

@property
def tabs(self) -> List[Any]:
return self._content["pages"]["configuration"]["tabs"]
if "configuration" in self._content["pages"]:
return self._content["pages"]["configuration"]["tabs"]
return []

@property
def dashboard(self) -> Dict[str, Any]:
Expand Down Expand Up @@ -207,6 +212,9 @@ def add_ucc_version(self, version: str) -> None:
def has_inputs(self) -> bool:
return bool(self.inputs)

def has_configuration(self) -> bool:
return bool(self.tabs)

def has_alerts(self) -> bool:
return bool(self.alerts)

Expand Down
7 changes: 4 additions & 3 deletions splunk_add_on_ucc_framework/global_config_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,10 @@ def _dump_with_migrated_entities(
_collapse_entities(
global_config.content["pages"].get("inputs", {}).get("services"), entity_type
)
_collapse_entities(
global_config.content["pages"]["configuration"].get("tabs"), entity_type
)
if global_config.has_configuration():
_collapse_entities(
global_config.content["pages"]["configuration"].get("tabs"), entity_type
)
_collapse_entities(global_config.content.get("alerts"), entity_type)

_dump(global_config.content, path, global_config._is_global_config_yaml)
Expand Down
29 changes: 17 additions & 12 deletions splunk_add_on_ucc_framework/global_config_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,13 @@ def __init__(self, source_dir: str, global_config: global_config_lib.GlobalConfi
self._config = global_config.content

@property
def config_tabs(self) -> List[Tab]:
return [
resolve_tab(tab) for tab in self._config["pages"]["configuration"]["tabs"]
]
def config_tabs(self) -> List[Any]:
if self._global_config.has_configuration():
return [
resolve_tab(tab)
for tab in self._config["pages"]["configuration"]["tabs"]
]
return []

def _validate_config_against_schema(self) -> None:
"""
Expand Down Expand Up @@ -249,10 +252,11 @@ def _validate_validators(self) -> None:
number and regex are supported.
"""
pages = self._config["pages"]
for tab in self.config_tabs:
entities = tab["entity"]
for entity in entities:
self._validate_entity_validators(entity)
if pages.get("configuration"):
for tab in self.config_tabs:
entities = tab["entity"]
for entity in entities:
self._validate_entity_validators(entity)

inputs = pages.get("inputs")
if inputs is None:
Expand Down Expand Up @@ -430,8 +434,8 @@ def _validate_duplicates(self) -> None:
not required in schema, so this checks if globalConfig has inputs
"""
pages = self._config["pages"]

self._validate_tabs_duplicates(self.config_tabs)
if self._config["pages"].get("configuration"):
self._validate_tabs_duplicates(self.config_tabs)

inputs = pages.get("inputs")
if inputs:
Expand Down Expand Up @@ -711,9 +715,10 @@ def _validate_meta_default_view(self) -> None:

def validate(self) -> None:
self._validate_config_against_schema()
self._validate_configuration_tab_table_has_name_field()
if self._config["pages"].get("configuration"):
self._validate_configuration_tab_table_has_name_field()
self._validate_file_type_entity()
self._validate_custom_rest_handlers()
self._validate_file_type_entity()
self._validate_validators()
self._validate_multilevel_menu()
self._validate_duplicates()
Expand Down
3 changes: 0 additions & 3 deletions splunk_add_on_ucc_framework/schema/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -2550,9 +2550,6 @@
"$ref": "#/definitions/DashboardPage"
}
},
"required": [
"configuration"
],
"additionalProperties": false
},
"RegexValidator": {
Expand Down
69 changes: 69 additions & 0 deletions tests/smoke/test_ucc_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,75 @@ def test_ucc_generate_with_configuration():
assert path.exists(actual_file_path)


def test_ucc_generate_with_no_configuration():
with tempfile.TemporaryDirectory() as temp_dir:
package_folder = path.join(
path.dirname(path.realpath(__file__)),
"..",
"testdata",
"test_addons",
"package_global_config_no_configuration",
"package",
)
build.generate(
source=package_folder, output_directory=temp_dir, addon_version="1.1.1"
)

expected_folder = path.join(
path.dirname(__file__),
"..",
"testdata",
"expected_addons",
"expected_addon_no_configuration",
"Splunk_TA_UCCExample",
)
actual_folder = path.join(temp_dir, "Splunk_TA_UCCExample")

# app.manifest and appserver/static/js/build/globalConfig.json
# should be included too, but they may introduce flaky tests as
# their content depends on the git commit.
_compare_app_conf(expected_folder, actual_folder)
# Expected add-on package folder does not have "lib" in it.
files_to_be_equal = [
("README.txt",),
("default", "restmap.conf"),
("default", "inputs.conf"),
("default", "web.conf"),
("default", "data", "ui", "nav", "default.xml"),
("default", "data", "ui", "views", "inputs.xml"),
("bin", "example_input_one.py"),
("bin", "example_input_two.py"),
("bin", "import_declare_test.py"),
("bin", "splunk_ta_uccexample_rh_example_input_one.py"),
("bin", "splunk_ta_uccexample_rh_example_input_two.py"),
("README", "inputs.conf.spec"),
("metadata", "default.meta"),
]
helpers.compare_file_content(
files_to_be_equal,
expected_folder,
actual_folder,
)
files_to_exist = [
("static", "appIcon.png"),
("static", "appIcon_2x.png"),
("static", "appIconAlt.png"),
("static", "appIconAlt_2x.png"),
]
for f in files_to_exist:
actual_file_path = path.join(actual_folder, *f)
assert path.exists(actual_file_path)

files_should_be_absent = [
("default", "data", "ui", "views", "configuration.xml"),
("README", "splunk_ta_uccexample_account.conf.spec"),
("README", "splunk_ta_uccexample_settings.conf.spec"),
]
for af in files_should_be_absent:
actual_file_path = path.join(actual_folder, *af)
assert not path.exists(actual_file_path)


def test_ucc_generate_with_configuration_files_only():
with tempfile.TemporaryDirectory() as temp_dir:
package_folder = path.join(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Just a readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[example_input_one://<name>]
interval = Time interval of the data input, in seconds.

[example_input_two://<name>]
interval = Time interval of the data input, in seconds.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5.55.0+2b3a9e8d9
5.55.0+2b3a9e8d9
Loading
Loading