Argspec default fix (#12)
* Return data updated with default values from aav.validate() * Update aav docs Co-authored-by: cidrblock <brad@thethorntons.net>pull/16/head
parent
3011554520
commit
024daa9dfe
|
@ -14,7 +14,7 @@ def _check_argspec(self):
|
||||||
other_args={},
|
other_args={},
|
||||||
name=self._task.action,
|
name=self._task.action,
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, updated_data = aav.validate()
|
||||||
if not valid:
|
if not valid:
|
||||||
raise AnsibleActionFail(errors)
|
raise AnsibleActionFail(errors)
|
||||||
|
|
||||||
|
@ -127,9 +127,11 @@ class MonkeyModule(AnsibleModule):
|
||||||
:rtype valid: bool
|
:rtype valid: bool
|
||||||
:return errors: errors reported during validation
|
:return errors: errors reported during validation
|
||||||
:rtype errors: str
|
:rtype errors: str
|
||||||
|
:return params: The original data updated with defaults
|
||||||
|
:rtype params: dict
|
||||||
"""
|
"""
|
||||||
super(MonkeyModule, self).__init__(**self._schema)
|
super(MonkeyModule, self).__init__(**self._schema)
|
||||||
return self._valid, self._errors
|
return self._valid, self._errors, self.params
|
||||||
|
|
||||||
|
|
||||||
class AnsibleArgSpecValidator:
|
class AnsibleArgSpecValidator:
|
||||||
|
@ -202,6 +204,13 @@ class AnsibleArgSpecValidator:
|
||||||
def _validate(self):
|
def _validate(self):
|
||||||
"""Validate the data gainst the schema
|
"""Validate the data gainst the schema
|
||||||
convert doc string in argspec if necessary
|
convert doc string in argspec if necessary
|
||||||
|
|
||||||
|
:return valid: if the data passed
|
||||||
|
:rtype valid: bool
|
||||||
|
:return errors: errors reported during validation
|
||||||
|
:rtype errors: str
|
||||||
|
:return params: The original data updated with defaults
|
||||||
|
:rtype params: dict
|
||||||
"""
|
"""
|
||||||
if self._schema_format == "doc":
|
if self._schema_format == "doc":
|
||||||
self._convert_doc_to_schema()
|
self._convert_doc_to_schema()
|
||||||
|
@ -217,12 +226,13 @@ class AnsibleArgSpecValidator:
|
||||||
errors = "Invalid schema. Invalid keys found: {ikeys}".format(
|
errors = "Invalid schema. Invalid keys found: {ikeys}".format(
|
||||||
ikeys=",".join(invalid_keys)
|
ikeys=",".join(invalid_keys)
|
||||||
)
|
)
|
||||||
|
updated_data = {}
|
||||||
else:
|
else:
|
||||||
mm = MonkeyModule(
|
mm = MonkeyModule(
|
||||||
data=self._data, schema=self._schema, name=self._name
|
data=self._data, schema=self._schema, name=self._name
|
||||||
)
|
)
|
||||||
valid, errors = mm.validate()
|
valid, errors, updated_data = mm.validate()
|
||||||
return valid, errors
|
return valid, errors, updated_data
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
"""The public validate method
|
"""The public validate method
|
||||||
|
|
|
@ -32,4 +32,7 @@ options:
|
||||||
type: dict
|
type: dict
|
||||||
description:
|
description:
|
||||||
- A dict suboption
|
- A dict suboption
|
||||||
|
param_default:
|
||||||
|
type: bool
|
||||||
|
default: True
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -25,10 +25,30 @@ class TestSortList(unittest.TestCase):
|
||||||
schema_conditionals={},
|
schema_conditionals={},
|
||||||
name="test_action",
|
name="test_action",
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertTrue(valid)
|
self.assertTrue(valid)
|
||||||
self.assertEqual(errors, None)
|
self.assertEqual(errors, None)
|
||||||
|
|
||||||
|
def test_simple_defaults(self):
|
||||||
|
data = {"param_str": "string"}
|
||||||
|
aav = AnsibleArgSpecValidator(
|
||||||
|
data=data,
|
||||||
|
schema=DOCUMENTATION,
|
||||||
|
schema_format="doc",
|
||||||
|
schema_conditionals={},
|
||||||
|
name="test_action",
|
||||||
|
)
|
||||||
|
expected = {
|
||||||
|
"param_str": "string",
|
||||||
|
"param_default": True,
|
||||||
|
"params_bool": None,
|
||||||
|
"params_dict": None,
|
||||||
|
}
|
||||||
|
valid, errors, updated_data = aav.validate()
|
||||||
|
self.assertTrue(valid)
|
||||||
|
self.assertEqual(errors, None)
|
||||||
|
self.assertEqual(expected, updated_data)
|
||||||
|
|
||||||
def test_simple_fail(self):
|
def test_simple_fail(self):
|
||||||
data = {}
|
data = {}
|
||||||
aav = AnsibleArgSpecValidator(
|
aav = AnsibleArgSpecValidator(
|
||||||
|
@ -38,7 +58,7 @@ class TestSortList(unittest.TestCase):
|
||||||
schema_conditionals={},
|
schema_conditionals={},
|
||||||
name="test_action",
|
name="test_action",
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertFalse(valid)
|
self.assertFalse(valid)
|
||||||
self.assertIn("missing required arguments: param_str", errors)
|
self.assertIn("missing required arguments: param_str", errors)
|
||||||
|
|
||||||
|
@ -50,7 +70,7 @@ class TestSortList(unittest.TestCase):
|
||||||
schema_format="doc",
|
schema_format="doc",
|
||||||
schema_conditionals={},
|
schema_conditionals={},
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertFalse(valid)
|
self.assertFalse(valid)
|
||||||
self.assertIn("missing required arguments: param_str", errors)
|
self.assertIn("missing required arguments: param_str", errors)
|
||||||
|
|
||||||
|
@ -62,7 +82,7 @@ class TestSortList(unittest.TestCase):
|
||||||
schema_format="argspec",
|
schema_format="argspec",
|
||||||
name="test_action",
|
name="test_action",
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertTrue(valid)
|
self.assertTrue(valid)
|
||||||
self.assertEqual(errors, None)
|
self.assertEqual(errors, None)
|
||||||
|
|
||||||
|
@ -77,7 +97,7 @@ class TestSortList(unittest.TestCase):
|
||||||
},
|
},
|
||||||
name="test_action",
|
name="test_action",
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertFalse(valid)
|
self.assertFalse(valid)
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
"parameters are required together: param_str, param_bool", errors
|
"parameters are required together: param_str, param_bool", errors
|
||||||
|
@ -92,7 +112,7 @@ class TestSortList(unittest.TestCase):
|
||||||
name="test_action",
|
name="test_action",
|
||||||
# other_args={'bypass_checks': True},
|
# other_args={'bypass_checks': True},
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertFalse(valid)
|
self.assertFalse(valid)
|
||||||
self.assertIn(
|
self.assertIn(
|
||||||
"Unsupported parameters for 'test_action' module: not_valid",
|
"Unsupported parameters for 'test_action' module: not_valid",
|
||||||
|
@ -108,7 +128,7 @@ class TestSortList(unittest.TestCase):
|
||||||
name="test_action",
|
name="test_action",
|
||||||
other_args={"bypass_checks": True},
|
other_args={"bypass_checks": True},
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertTrue(valid)
|
self.assertTrue(valid)
|
||||||
self.assertIsNone(errors)
|
self.assertIsNone(errors)
|
||||||
|
|
||||||
|
@ -121,6 +141,6 @@ class TestSortList(unittest.TestCase):
|
||||||
name="test_action",
|
name="test_action",
|
||||||
other_args={"bypass_checks": True},
|
other_args={"bypass_checks": True},
|
||||||
)
|
)
|
||||||
valid, errors = aav.validate()
|
valid, errors, _updated_data = aav.validate()
|
||||||
self.assertFalse(valid)
|
self.assertFalse(valid)
|
||||||
self.assertIn("Invalid keys found: not_valid", errors)
|
self.assertIn("Invalid keys found: not_valid", errors)
|
||||||
|
|
Loading…
Reference in New Issue