Skip to content
Permalink
Browse files

Fix get_ruleset/set_ruleset API calls

* The folder macro is not exposed to the Web API because the folder
  information is held by the first dictionary level keys
* Add basic integration tests for both API calls

CMK-2172

Change-Id: Ib4ab3d8356f1f96c395a23b47fefdb2a308ff6c5
  • Loading branch information
LarsMichelsen committed Jun 3, 2019
1 parent 6ea3d2b commit 9663f23bfeada6929a1ee8cdf032fab0768dc50b
Showing with 62 additions and 2 deletions.
  1. +1 −1 cmk/gui/plugins/webapi/webapi.py
  2. +10 −1 cmk/gui/watolib/rulesets.py
  3. +51 −0 tests/integration/cmk/gui/test_webapi.py
@@ -635,7 +635,7 @@ def _get_ruleset_configuration(self, ruleset_name):

ruleset_dict = {}
for folder, _rule_index, rule in ruleset.get_rules():
ruleset_dict.setdefault(folder.path(), []).append(rule.to_config())
ruleset_dict.setdefault(folder.path(), []).append(rule.to_web_api())

return ruleset_dict

@@ -101,6 +101,9 @@ def to_config_with_folder(self):
cfg["host_folder"] = self.host_folder
return cfg

def to_config_without_folder(self):
return self._to_config()

def _to_config(self):
cfg = {}

@@ -757,9 +760,15 @@ def _parse_dict_rule(self, rule_config):
self.conditions.from_config(conditions)

def to_config(self):
return self._to_config(self.conditions.to_config_with_folder_macro())

def to_web_api(self):
return self._to_config(self.conditions.to_config_without_folder())

def _to_config(self, conditions):
result = {
"value": self.value,
"condition": self.conditions.to_config_with_folder_macro(),
"condition": conditions,
}

rule_options = self._rule_options_to_config()
@@ -214,6 +214,57 @@ def test_get_all_hosts_effective_attributes(web):
web.delete_host("test-host")


def test_get_ruleset(web):
response = web.get_ruleset("extra_host_conf:notification_options")
assert response == {
'ruleset': {
'': [{
'value': 'd,r,f,s',
'condition': {}
}]
},
'configuration_hash': 'b76f205bbe674300f677a282d9ccd71f',
}


def test_set_ruleset(web):
orig_ruleset = web.get_ruleset("bulkwalk_hosts")
assert orig_ruleset == {
'ruleset': {
'': [{
'value': True,
'condition': {
'host_tags': {
'snmp': 'snmp',
'snmp_ds': {
'$ne': 'snmp-v1'
}
}
},
'options': {
'description': u'Hosts with the tag "snmp-v1" must not use bulkwalk'
}
}]
},
'configuration_hash': '0cca93426feb558f7c9f09631340c63c',
}

# Now modify something
ruleset = copy.deepcopy(orig_ruleset)
ruleset["ruleset"][""][0]["value"] = False
response = web.set_ruleset("bulkwalk_hosts", ruleset)
assert response is None

try:
changed = web.get_ruleset("bulkwalk_hosts")
assert changed["ruleset"][""][0]["value"] is False
finally:
# revert it back
del orig_ruleset["configuration_hash"]
response = web.set_ruleset("bulkwalk_hosts", orig_ruleset)
assert response is None


def test_get_site(web, site):
response = web.get_site(site.id)
assert "site_config" in response

0 comments on commit 9663f23

Please sign in to comment.
You can’t perform that action at this time.