Skip to content

Commit bf8b801

Browse files
Validate value but not attribute and attribute but not value
Signed-off-by: Stew Francis <[email protected]>
1 parent 92717d3 commit bf8b801

File tree

2 files changed

+91
-11
lines changed

2 files changed

+91
-11
lines changed

plugins/module_utils/cmci.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def parameters_argument(name): # type: (str) -> Dict[str, Any]
7575
AND = 'and'
7676
OR = 'or'
7777
OPERATOR = 'operator'
78-
VALUE = 'value'
7978

8079
RESOURCES_ARGUMENT = {
8180
RESOURCES: {
@@ -634,6 +633,15 @@ def _get_filter(self, list_of_filters, complex_filter_string, joiner, path):
634633
% (OPERATOR, ATTRIBUTE)
635634
)
636635

636+
value = i.get(VALUE)
637+
638+
if (value and not attribute) or (attribute and not value):
639+
self._fail(
640+
'parameters are required together: %s, %s found in '
641+
'resources -> complex_filter%s'
642+
% (ATTRIBUTE, VALUE, path)
643+
)
644+
637645
# Validate mutually exclusive parameters
638646
if (and_item and or_item) or (and_item and attribute) or \
639647
(or_item and attribute):
@@ -675,15 +683,6 @@ def _get_filter(self, list_of_filters, complex_filter_string, joiner, path):
675683
% (ATTRIBUTE, type(attribute), path)
676684
)
677685

678-
value = i.get(VALUE)
679-
680-
if value is None:
681-
self._fail(
682-
'parameters are required together: %s, %s found in '
683-
'resources -> complex_filter%s'
684-
% (ATTRIBUTE, VALUE, path)
685-
)
686-
687686
# Validate value type
688687
if not isinstance(value, str):
689688
self._fail(

tests/unit/modules/test_cmci_filters.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,88 @@ def test_value_type_or(cmci_module):
11211121
})
11221122

11231123

1124-
# value type
1124+
def test_value_no_attribute_root(cmci_module):
1125+
# type: (CMCITestHelper) -> None
1126+
cmci_module.expect({
1127+
'msg': "parameters are required together: attribute, value found in "
1128+
"resources -> complex_filter",
1129+
'failed': True
1130+
})
1131+
1132+
cmci_module.run(cmci_get, {
1133+
'cmci_host': HOST,
1134+
'cmci_port': PORT,
1135+
'context': CONTEXT,
1136+
'scope': 'IYCWEMW2',
1137+
'type': 'cicslocalfile',
1138+
'resources': {
1139+
'complex_filter': {
1140+
'value': '456a',
1141+
'and': [{
1142+
'attribute': '123',
1143+
'value': '678a'
1144+
}]
1145+
}
1146+
}
1147+
})
1148+
1149+
1150+
def test_value_no_attribute_and(cmci_module):
1151+
# type: (CMCITestHelper) -> None
1152+
cmci_module.expect({
1153+
'msg': "parameters are required together: attribute, value found in "
1154+
"resources -> complex_filter -> and",
1155+
'failed': True,
1156+
'changed': False
1157+
})
1158+
1159+
cmci_module.run(cmci_get, {
1160+
'cmci_host': HOST,
1161+
'cmci_port': PORT,
1162+
'context': CONTEXT,
1163+
'scope': 'IYCWEMW2',
1164+
'type': 'cicslocalfile',
1165+
'resources': {
1166+
'complex_filter': {
1167+
'and': [{
1168+
'value': '456',
1169+
'and': [{
1170+
'attribute': '123',
1171+
'value': '678'
1172+
}]
1173+
}]
1174+
}
1175+
}
1176+
})
1177+
1178+
1179+
def test_value_no_attribute_or(cmci_module):
1180+
# type: (CMCITestHelper) -> None
1181+
cmci_module.expect({
1182+
'msg': "parameters are required together: attribute, value found in "
1183+
"resources -> complex_filter -> or",
1184+
'failed': True,
1185+
'changed': False
1186+
})
1187+
1188+
cmci_module.run(cmci_get, {
1189+
'cmci_host': HOST,
1190+
'cmci_port': PORT,
1191+
'context': CONTEXT,
1192+
'scope': 'IYCWEMW2',
1193+
'type': 'cicslocalfile',
1194+
'resources': {
1195+
'complex_filter': {
1196+
'or': [{
1197+
'value': '456',
1198+
'or': [{
1199+
'attribute': '123',
1200+
'value': '678'
1201+
}]
1202+
}]
1203+
}
1204+
}
1205+
})
11251206

11261207

11271208
def result(url, records, http_status='OK', http_status_code=200):

0 commit comments

Comments
 (0)