Ansible.Basic - add required_by to module spec (#51407)

* Ansible.Basic - add required_by to module spec

* fix typo in docs
This commit is contained in:
Jordan Borean 2019-02-15 13:00:25 +10:00 committed by GitHub
parent 994063bbf9
commit de118734e9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 231 additions and 2 deletions

View file

@ -83,6 +83,7 @@ namespace Ansible.Basic
{ "options", new List<object>() { typeof(Hashtable), typeof(Hashtable) } },
{ "removed_in_version", new List<object>() { null, typeof(string) } },
{ "required", new List<object>() { false, typeof(bool) } },
{ "required_by", new List<object>() { typeof(Hashtable), typeof(Hashtable) } },
{ "required_if", new List<object>() { typeof(List<List<object>>), null } },
{ "required_one_of", new List<object>() { typeof(List<List<string>>), null } },
{ "required_together", new List<object>() { typeof(List<List<string>>), null } },
@ -792,6 +793,7 @@ namespace Ansible.Basic
CheckRequiredTogether(param, (IList)spec["required_together"]);
CheckRequiredOneOf(param, (IList)spec["required_one_of"]);
CheckRequiredIf(param, (IList)spec["required_if"]);
CheckRequiredBy(param, (IDictionary)spec["required_by"]);
// finally ensure all missing parameters are set to null and handle sub options
foreach (DictionaryEntry entry in optionSpec)
@ -1012,6 +1014,28 @@ namespace Ansible.Basic
}
}
private void CheckRequiredBy(IDictionary param, IDictionary requiredBy)
{
foreach (DictionaryEntry entry in requiredBy)
{
string key = (string)entry.Key;
if (!param.Contains(key))
continue;
List<string> missing = new List<string>();
List<string> requires = ParseList(entry.Value).Cast<string>().ToList();
foreach (string required in requires)
if (!param.Contains(required))
missing.Add(required);
if (missing.Count > 0)
{
string msg = String.Format("missing parameter(s) required by '{0}': {1}", key, String.Join(", ", missing));
FailJson(FormatOptionsContext(msg));
}
}
}
private void CheckSubOption(IDictionary param, string key, IDictionary spec)
{
string type;