From: Ondrej Lichtner <olichtne(a)redhat.com>
Parameter types added:
* BoolParam - checks if the value is a boolean
* HostnameParam - checks if the value is an ip address or a valid Hostname
* validity checked by regex based on RFC 1123
* DictParam - checks if the value is a dictionary
* DeviceOrIpParam - checks if the value is a Device object/reference or
an ip address
Modified IpParam - type check is based on isinstance BaseIpAddress
rather than ipaddress factory method
Signed-off-by: Ondrej Lichtner <olichtne(a)redhat.com>
---
v2: Hostname checked by regex instead of getaddrinfo. Thanks jurbanov!
---
lnst/Common/Parameters.py | 54 ++++++++++++++++++++++++++++++++++-----
1 file changed, 48 insertions(+), 6 deletions(-)
diff --git a/lnst/Common/Parameters.py b/lnst/Common/Parameters.py
index 43faf6d..66380d2 100644
--- a/lnst/Common/Parameters.py
+++ b/lnst/Common/Parameters.py
@@ -15,8 +15,9 @@ olichtne(a)redhat.com (Ondrej Lichtner)
"""
import copy
+import re
from lnst.Common.DeviceRef import DeviceRef
-from lnst.Common.IpAddress import ipaddress
+from lnst.Common.IpAddress import BaseIpAddress
from lnst.Common.LnstError import LnstError
class ParamError(LnstError):
@@ -52,13 +53,34 @@ class StrParam(Param):
except ValueError:
raise ParamError("Value must be a string")
+class BoolParam(Param):
+ def type_check(self, value):
+ if isinstance(value, bool):
+ return value
+ else:
+ raise ParamError("Value must be a boolean")
+
class IpParam(Param):
def type_check(self, value):
- try:
- return ipaddress(value)
- except ValueError:
- raise ParamError("Value must be a BaseIpAddress, string ip address or a
Device object. Not {}"
- .format(type(value)))
+ if isinstance(value, BaseIpAddress):
+ return value
+ else:
+ raise ParamError("Value must be a BaseIpAddress object")
+
+class HostnameParam(Param):
+ def type_check(self, value):
+ if isinstance(value, BaseIpAddress):
+ return value
+
+ if len(value) > 255:
+ raise ParamError("Value must be a BaseIpAddress object or a valid
hostname")
+
+ hostname_re = ("^([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9])"
+ "(\.([A-Z0-9]|[A-Z0-9][A-Z0-9\-]{0,61}[A-Z0-9]))*$")
+ if re.match(hostname_re, value, re.IGNORECASE):
+ return value
+ else:
+ raise ParamError("Value must be a BaseIpAddress object or a valid
hostname")
class DeviceParam(Param):
def type_check(self, value):
@@ -71,6 +93,26 @@ class DeviceParam(Param):
raise ParamError("Value must be a Device or DeviceRef object."
" Not {}".format(type(value)))
+class DeviceOrIpParam(Param):
+ def type_check(self, value):
+ #runtime import this because the Device class arrives on the Slave
+ #during recipe execution, not during Slave init
+ from lnst.Devices.Device import Device
+ if (isinstance(value, Device) or isinstance(value, DeviceRef) or
+ isinstance(value, BaseIpAddress)):
+ return value
+ else:
+ raise ParamError("Value must be a Device, DeviceRef or BaseIpAddress
object."
+ " Not {}".format(type(value)))
+
+class DictParam(Param):
+ def type_check(self, value):
+ if not isinstance(value, dict):
+ raise ParamError("Value must be a Dictionary. Not {}"
+ .format(type(value)))
+ else:
+ return value
+
class Parameters(object):
def __init__(self):
self._attrs = {}
--
2.17.0