Skip to content

Commit

Permalink
[networking_mapper] Add the network to ranges validation message
Browse files Browse the repository at this point in the history
  • Loading branch information
pablintino authored and openshift-merge-bot[bot] committed Sep 25, 2024
1 parent 86c92f5 commit 5b499ca
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
13 changes: 8 additions & 5 deletions plugins/module_utils/net_map/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@


class NetworkMappingError(Exception, ansible_encoding.RawConvertibleObject):
def __init__(self, message) -> None:
def __init__(self, message, **kwargs) -> None:
super().__init__(message)
self.message = message
self.details = kwargs

def to_raw(self) -> typing.Dict[str, typing.Any]:
return ansible_encoding.decode_ansible_raw(vars(self))
Expand All @@ -22,17 +23,18 @@ def __init__(
invalid_value=None,
parent_name=None,
parent_type=None,
**kwargs,
) -> None:
super().__init__(message)
super().__init__(message, **kwargs)
self.field = field
self.invalid_value = invalid_value
self.parent_name = parent_name
self.parent_type = parent_type


class HostNetworkRangeCollisionValidationError(NetworkMappingValidationError):
def __init__(self, message, range_1=None, range_2=None) -> None:
super().__init__(message)
def __init__(self, message, range_1=None, range_2=None, **kwargs) -> None:
super().__init__(message, **kwargs)
self.range_1 = range_1
self.range_2 = range_2

Expand All @@ -48,8 +50,9 @@ def __init__(
invalid_value=None,
parent_name=None,
parent_type=None,
**kwargs,
) -> None:
super().__init__(message)
super().__init__(message, **kwargs)
self.field = field
self.invalid_value = invalid_value
self.parent_name = parent_name
Expand Down
4 changes: 4 additions & 0 deletions plugins/module_utils/net_map/networking_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def _validate_fields_one_of(
parent_type: str = None,
alone_field: str = None,
mandatory: bool = False,
**kwargs,
) -> bool:
fields_present = any(
field_name in raw_definition.keys() for field_name in fields_list
Expand All @@ -183,6 +184,7 @@ def _validate_fields_one_of(
f"at least one of {mandatory_fields} must be provided",
parent_name=parent_name,
parent_type=parent_type,
**kwargs,
)
if alone_field and alone_field in raw_definition:
rest = [
Expand All @@ -198,6 +200,7 @@ def _validate_fields_one_of(
invalid_value=raw_definition[alone_field],
parent_name=parent_name,
parent_type=parent_type,
**kwargs,
)

return True
Expand Down Expand Up @@ -2100,6 +2103,7 @@ def __parse_raw_net_ranges(
parent_name=self.__group_name,
parent_type=self.__OBJECT_TYPE_NAME,
alone_field=self.__FIELD_NETWORK_RANGE,
network_name=network_definition.name,
)
if not ranges_present:
return None, None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@ def test_group_template_definition_parse_invalid_range_fail():
assert exc_info.value.field == "length"
assert exc_info.value.invalid_value == 0

# Test mix of single and dual-stack fields
invalid_range = {"start": 10, "length": 10}
group_template_definition_raw = {
"networks": {
first_net.name: {
"range": invalid_range,
"range-v4": {"start": 100, "length": 0},
},
},
}

with pytest.raises(exceptions.NetworkMappingValidationError) as exc_info:
networking_definition.GroupTemplateDefinition(
inventory_group, group_template_definition_raw, networks_definitions
)
assert exc_info.value.field == "range"
assert exc_info.value.invalid_value == invalid_range
assert "network_name" in exc_info.value.details
assert exc_info.value.details["network_name"] == first_net.name
assert exc_info.value.parent_name == inventory_group
assert exc_info.value.parent_type == "host-template"


def test_group_template_definition_parse_invalid_net_fail():
# Test invalid start value
Expand Down

0 comments on commit 5b499ca

Please sign in to comment.