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

refactor: extract copy-pasted code into a separate function #240

Merged
1 commit merged into from
Jun 24, 2021
Merged
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
74 changes: 35 additions & 39 deletions splunk_add_on_ucc_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,37 @@ def version_tuple(version_str):
filled.append(point.zfill(8))
return tuple(filled)


def _handle_biased_terms(conf_entities: dict) -> dict:
for entity in conf_entities:
entity_option = entity.get("options")
if entity_option and "whiteList" in entity_option:
entity_option["allowList"] = entity_option.get("whiteList")
del entity_option["whiteList"]
if entity_option and "blackList" in entity_option:
entity_option["denyList"] = entity_option.get("blackList")
del entity_option["blackList"]
return conf_entities


def handle_biased_terms_update(schema_content: dict) -> dict:
pages = schema_content.get("pages")
ta_tabs = pages.get("configuration", {}).get("tabs", {})

for tab in ta_tabs:
conf_entities = tab.get("entity")
tab["entity"] = _handle_biased_terms(conf_entities)

if "inputs" in pages:
services = pages.get("inputs", {}).get("services", {})
for service in services:
conf_entities = service.get("entity")
service["entity"] = _handle_biased_terms(conf_entities)

schema_content["meta"]["schemaVersion"] = "0.0.1"
return schema_content


def handle_update(config_path):
"""
handle changes in globalConfig.json
Expand All @@ -142,46 +173,13 @@ def handle_update(config_path):
"""
with open(config_path, "r") as config_file:
schema_content = json.load(config_file)
# check for schemaVersion in meta, if not availble then set default 0.0.0
version = schema_content.get("meta").get("schemaVersion","0.0.0")

# check for schemaVersion, if it's less than 0.0.1 then updating globalConfig.json
if version_tuple(version) < version_tuple("0.0.1"):
ta_tabs = schema_content.get("pages").get("configuration",{}).get("tabs",{})
version = schema_content.get("meta").get("schemaVersion", "0.0.0")

# check in every Account tab for biased term
for tab in ta_tabs:
conf_entitties= tab.get("entity")
for entity in conf_entitties:
entity_option = entity.get("options")
if entity_option and "whiteList" in entity_option:
entity_option["allowList"] = entity_option.get("whiteList")
del entity_option["whiteList"]
if entity_option and "blackList" in entity_option:
entity_option["denyList"] = entity_option.get("blackList")
del entity_option["blackList"]

is_inputs = ("inputs" in schema_content.get("pages"))
if is_inputs:
services = schema_content.get("pages").get("inputs",{}).get("services",{})
# check in every Input service for biased term
for service in services:
conf_entitties= service.get("entity")
for entity in conf_entitties:
entity_option = entity.get("options")
if entity_option and "whiteList" in entity_option:
entity_option["allowList"] = entity_option.get("whiteList")
del entity_option["whiteList"]
if entity_option and "blackList" in entity_option:
entity_option["denyList"] = entity_option.get("blackList")
del entity_option["blackList"]

# set schemaVersion to 0.0.1 as updated globalConfig.json according to new update
schema_content["meta"]["schemaVersion"]="0.0.1"

# upadating new changes in globalConfig.json
if version_tuple(version) < version_tuple("0.0.1"):
schema_content = handle_biased_terms_update(schema_content)
with open(config_path, "w") as config_file:
json.dump(schema_content,config_file, ensure_ascii=False, indent=4)
json.dump(schema_content, config_file, ensure_ascii=False, indent=4)

# check for schemaVersion, if it's less than 0.0.2 then updating globalConfig.json
if version_tuple(version) < version_tuple("0.0.2"):
Expand Down Expand Up @@ -214,7 +212,6 @@ def handle_update(config_path):
is_inputs = ("inputs" in schema_content.get("pages"))
if is_inputs:
services = schema_content.get("pages").get("inputs",{}).get("services",{})
# check in every Input service for onSave and onLoad options
for service in services:
service_options = service.get("options", {})
if service_options.get("onChange"):
Expand All @@ -225,7 +222,6 @@ def handle_update(config_path):
del service_options["onLoad"]

schema_content["meta"]["schemaVersion"] = "0.0.2"
# upadating new changes in globalConfig.json
with open(config_path, "w") as config_file:
json.dump(schema_content,config_file, ensure_ascii=False, indent=4)

Expand Down
26 changes: 26 additions & 0 deletions tests/unit/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright 2021 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import json
import os


def get_config(config_name: str) -> dict:
config_path = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "testdata", config_name
)
with open(config_path, "r") as f_config:
valid_config_raw = f_config.read()
return json.loads(valid_config_raw)
49 changes: 49 additions & 0 deletions tests/unit/test_config_file_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Copyright 2021 Splunk Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import unittest

import tests.unit.helpers as helpers
from splunk_add_on_ucc_framework import handle_biased_terms_update


class ConfigFileUpdateTest(unittest.TestCase):
def test_handle_biased_terms_update(self):
config = helpers.get_config("config_with_biased_terms.json")
updated_config = handle_biased_terms_update(config)
expected_schema_version = "0.0.1"
self.assertEqual(
expected_schema_version, updated_config["meta"]["schemaVersion"]
)
input_entity_1_options_keys = updated_config["pages"]["inputs"]["services"][0][
"entity"
][0]["options"].keys()
self.assertIn("denyList", input_entity_1_options_keys)
self.assertNotIn("blackList", input_entity_1_options_keys)
input_entity_2_options_keys = updated_config["pages"]["inputs"]["services"][0][
"entity"
][1]["options"].keys()
self.assertIn("allowList", input_entity_2_options_keys)
self.assertNotIn("whiteList", input_entity_2_options_keys)
configuration_entity_1_options_keys = updated_config["pages"]["configuration"][
"tabs"
][0]["entity"][0]["options"].keys()
self.assertIn("denyList", configuration_entity_1_options_keys)
self.assertNotIn("blackList", configuration_entity_1_options_keys)
configuration_entity_2_options_keys = updated_config["pages"]["configuration"][
"tabs"
][0]["entity"][1]["options"].keys()
self.assertIn("allowList", configuration_entity_2_options_keys)
self.assertNotIn("whiteList", configuration_entity_2_options_keys)
6 changes: 3 additions & 3 deletions tests/unit/test_config_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import unittest

import jsonschema

import tests.unit.helpers as helpers
from splunk_add_on_ucc_framework import validate_config_against_schema


Expand All @@ -33,10 +33,10 @@ def _get_config(config_name: str) -> dict:

class ConfigValidationTest(unittest.TestCase):
def test_config_validation_when_valid_config_then_no_exception(self):
config = _get_config("valid_config.json")
config = helpers.get_config("valid_config.json")
validate_config_against_schema(config)

def test_config_validation_when_invalid_config_then_exception(self):
config = _get_config("invalid_config_no_configuration_tabs.json")
config = helpers.get_config("invalid_config_no_configuration_tabs.json")
with self.assertRaises(jsonschema.ValidationError):
validate_config_against_schema(config)
131 changes: 131 additions & 0 deletions tests/unit/testdata/config_with_biased_terms.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"pages": {
"configuration": {
"tabs": [
{
"name": "configuration_tab",
"entity": [
{
"type": "singleSelect",
"label": "Index 1",
"help": "An index is a type of data repository. Select the index in which you want to collect the events.",
"defaultValue": "main",
"required": true,
"validators": [
{
"type": "string",
"maxLength": 80,
"errorMsg": "Maximum length allowed for index is 80",
"minLength": 1
}
],
"field": "index_1",
"options": {
"createSearchChoice": true,
"blackList": "^_.*$",
"endpointUrl": "data/indexes"
}
},
{
"type": "singleSelect",
"label": "Index 2",
"help": "An index is a type of data repository. Select the index in which you want to collect the events.",
"defaultValue": "main",
"required": true,
"validators": [
{
"type": "string",
"maxLength": 80,
"errorMsg": "Maximum length allowed for index is 80",
"minLength": 1
}
],
"field": "index_2",
"options": {
"createSearchChoice": true,
"whiteList": "^_.*$",
"endpointUrl": "data/indexes"
}
}
],
"title": "Configuration tab"
}
],
"title": "Configuration",
"description": "Set up your add-on"
},
"inputs": {
"services": [
{
"name": "example_input_one",
"entity": [
{
"type": "singleSelect",
"label": "Index 1",
"validators": [
{
"type": "string",
"errorMsg": "Length of index name should be between 1 and 80.",
"minLength": 1,
"maxLength": 80
}
],
"defaultValue": "default",
"options": {
"endpointUrl": "data/indexes",
"blackList": "^_.*$",
"createSearchChoice": true
},
"field": "index_1",
"required": true
},
{
"type": "singleSelect",
"label": "Index 2",
"validators": [
{
"type": "string",
"errorMsg": "Length of index name should be between 1 and 80.",
"minLength": 1,
"maxLength": 80
}
],
"defaultValue": "default",
"options": {
"endpointUrl": "data/indexes",
"whiteList": "^_.*$",
"createSearchChoice": true
},
"field": "index_2",
"required": true
}
],
"title": "Example Input One"
}
],
"title": "Inputs",
"description": "Manage your data inputs",
"table": {
"actions": [
"edit",
"enable",
"delete",
"clone"
],
"header": [
{
"label": "Name",
"field": "name"
}
]
}
}
},
"meta": {
"apiVersion": "3.2.0",
"name": "Splunk_TA_UCCExample",
"restRoot": "splunk_ta_uccexample",
"version": "1.0.0",
"displayName": "Splunk UCC test Add-on"
}
}