#2514 Provide more friendly messages on validation failure
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
# flake8: noqa
|
# flake8: noqa
|
||||||
"""Core of the PrimAITE Simulator."""
|
"""Core of the PrimAITE Simulator."""
|
||||||
|
import warnings
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from typing import Callable, Dict, List, Literal, Optional, Union
|
from typing import Callable, Dict, List, Literal, Optional, Union
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
@@ -26,6 +27,12 @@ class RequestPermissionValidator(BaseModel):
|
|||||||
"""Use the request and context parameters to decide whether the request should be permitted."""
|
"""Use the request and context parameters to decide whether the request should be permitted."""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@property
|
||||||
|
@abstractmethod
|
||||||
|
def fail_message(self) -> str:
|
||||||
|
"""Message that is reported when a request is rejected by this validator."""
|
||||||
|
return "request rejected"
|
||||||
|
|
||||||
|
|
||||||
class AllowAllValidator(RequestPermissionValidator):
|
class AllowAllValidator(RequestPermissionValidator):
|
||||||
"""Always allows the request."""
|
"""Always allows the request."""
|
||||||
@@ -34,6 +41,16 @@ class AllowAllValidator(RequestPermissionValidator):
|
|||||||
"""Always allow the request."""
|
"""Always allow the request."""
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fail_message(self) -> str:
|
||||||
|
"""
|
||||||
|
Message that is reported when a request is rejected by this validator.
|
||||||
|
|
||||||
|
This method should really never be called because this validator never rejects requests.
|
||||||
|
"""
|
||||||
|
warnings.warn("Something went wrong - AllowAllValidator rejected a request.")
|
||||||
|
return super().fail_message
|
||||||
|
|
||||||
|
|
||||||
class RequestType(BaseModel):
|
class RequestType(BaseModel):
|
||||||
"""
|
"""
|
||||||
@@ -99,7 +116,7 @@ class RequestManager(BaseModel):
|
|||||||
|
|
||||||
if not request_type.validator(request_options, context):
|
if not request_type.validator(request_options, context):
|
||||||
_LOGGER.debug(f"Request {request} was denied due to insufficient permissions")
|
_LOGGER.debug(f"Request {request} was denied due to insufficient permissions")
|
||||||
return RequestResponse(status="failure", data={"reason": "request validation failed"})
|
return RequestResponse(status="failure", data={"reason": request_type.validator.fail_message})
|
||||||
|
|
||||||
return request_type.func(request_options, context)
|
return request_type.func(request_options, context)
|
||||||
|
|
||||||
|
|||||||
@@ -57,6 +57,11 @@ class GroupMembershipValidator(RequestPermissionValidator):
|
|||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fail_message(self) -> str:
|
||||||
|
"""Message that is reported when a request is rejected by this validator."""
|
||||||
|
return "User does not belong to group"
|
||||||
|
|
||||||
|
|
||||||
class DomainController(SimComponent):
|
class DomainController(SimComponent):
|
||||||
"""Main object for controlling the domain."""
|
"""Main object for controlling the domain."""
|
||||||
|
|||||||
@@ -798,6 +798,11 @@ class Node(SimComponent):
|
|||||||
"""Return whether the node is on or off."""
|
"""Return whether the node is on or off."""
|
||||||
return self.node.operating_state == NodeOperatingState.ON
|
return self.node.operating_state == NodeOperatingState.ON
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fail_message(self) -> str:
|
||||||
|
"""Message that is reported when a request is rejected by this validator."""
|
||||||
|
return f"Cannot perform request on node '{self.node.hostname}' because it is not turned on."
|
||||||
|
|
||||||
def _init_request_manager(self) -> RequestManager:
|
def _init_request_manager(self) -> RequestManager:
|
||||||
"""
|
"""
|
||||||
Initialise the request manager.
|
Initialise the request manager.
|
||||||
|
|||||||
Reference in New Issue
Block a user