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 an accesible
Hostname
* 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>
---
lnst/Common/Parameters.py | 49 +++++++++++++++++++++++++++++++++++----
1 file changed, 44 insertions(+), 5 deletions(-)
diff --git a/lnst/Common/Parameters.py b/lnst/Common/Parameters.py
index 43faf6d..0a331e3 100644
--- a/lnst/Common/Parameters.py
+++ b/lnst/Common/Parameters.py
@@ -15,8 +15,9 @@ olichtne(a)redhat.com (Ondrej Lichtner)
"""
import copy
+import socket
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,31 @@ 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):
+ 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
+
try:
- return ipaddress(value)
- except ValueError:
- raise ParamError("Value must be a BaseIpAddress, string ip address or a
Device object. Not {}"
- .format(type(value)))
+ #TODO check by regex?
+ socket.getaddrinfo(value)
+ return value
+ except:
+ raise ParamError("Value must be a BaseIpAddress object or a valid
hostname")
class DeviceParam(Param):
def type_check(self, value):
@@ -71,6 +90,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