Ansible.Basic - Support a delegate type for option elements key (#52951)

pull/4420/head
Jordan Borean 2019-02-26 08:14:10 +10:00 committed by GitHub
parent fe7cbc2554
commit 13f8f1481f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 3 deletions

View File

@ -1039,13 +1039,26 @@ namespace Ansible.Basic
private void CheckSubOption(IDictionary param, string key, IDictionary spec)
{
object value = param[key];
string type;
if (spec["type"].GetType() == typeof(string))
type = (string)spec["type"];
else
type = "delegate";
string elements = (string)spec["elements"];
object value = param[key];
string elements = null;
Delegate typeConverter = null;
if (spec["elements"] != null && spec["elements"].GetType() == typeof(string))
{
elements = (string)spec["elements"];
typeConverter = optionTypes[elements];
}
else if (spec["elements"] != null)
{
elements = "delegate";
typeConverter = (Delegate)spec["elements"];
}
if (!(type == "dict" || (type == "list" && elements != null)))
// either not a dict, or list with the elements set, so continue
@ -1057,7 +1070,6 @@ namespace Ansible.Basic
return;
List<object> newValue = new List<object>();
Delegate typeConverter = optionTypes[elements];
foreach (object element in (List<object>)value)
{
if (elements == "dict")

View File

@ -503,6 +503,47 @@ $tests = @{
$actual.invocation | Assert-DictionaryEquals -Expected @{module_args = $expected_module_args}
}
"Parse module args with list elements and delegate type" = {
$spec = @{
options = @{
list_delegate_type = @{
type = "list"
elements = [Func[[Object], [UInt16]]]{ [System.UInt16]::Parse($args[0]) }
}
}
}
$complex_args = @{
list_delegate_type = @(
"1234",
4321
)
}
$m = [Ansible.Basic.AnsibleModule]::Create(@(), $spec)
$m.Params.list_delegate_type.GetType().Name | Assert-Equals -Expected 'List`1'
$m.Params.list_delegate_type[0].GetType().FullName | Assert-Equals -Expected "System.UInt16"
$m.Params.list_delegate_Type[1].GetType().FullName | Assert-Equals -Expected "System.UInt16"
$failed = $false
try {
$m.ExitJson()
} catch [System.Management.Automation.RuntimeException] {
$failed = $true
$_.Exception.Message | Assert-Equals -Expected "exit: 0"
$actual = [Ansible.Basic.AnsibleModule]::FromJson($_test_out)
}
$failed | Assert-Equals -Expected $true
$expected_module_args = @{
list_delegate_type = @(
1234,
4321
)
}
$actual.Keys.Count | Assert-Equals -Expected 2
$actual.changed | Assert-Equals -Expected $false
$actual.invocation | Assert-DictionaryEquals -Expected @{module_args = $expected_module_args}
}
"Parse module args with case insensitive input" = {
$spec = @{
options = @{