Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
ansible
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ansible
Commits
94cfbe7e
Commit
94cfbe7e
authored
Sep 30, 2015
by
Trond Hindenes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Get-AnsibleParam
parent
dcb8caf9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
3 deletions
+29
-3
lib/ansible/module_utils/powershell.ps1
+29
-3
No files found.
lib/ansible/module_utils/powershell.ps1
View file @
94cfbe7e
...
...
@@ -101,9 +101,11 @@ Function Fail-Json($obj, $message = $null)
# Helper function to get an "attribute" from a psobject instance in powershell.
# This is a convenience to make getting Members from an object easier and
# slightly more pythonic
# Example: $attr = Get-Attr $response "code" -default "1"
# Example: $attr = Get-AnsibleParam $response "code" -default "1"
#Get-AnsibleParam also supports Parameter validation to save you from coding that manually:
#Example: Get-AnsibleParam -obj $params -name "State" -default "Present" -ValidateSet "Present","Absent" -resultobj $resultobj -failifempty $true
#Note that if you use the failifempty option, you do need to specify resultobject as well.
Function
Get-A
ttr
(
$obj
,
$name
,
$default
=
$null
,
$resultobj
,
$failifempty
=
$false
,
$emptyattributefailm
essage
)
Function
Get-A
nsibleParam
(
$obj
,
$name
,
$default
=
$null
,
$resultobj
,
$failifempty
=
$false
,
$emptyattributefailmessage
,
$ValidateSet
,
$ValidateSetErrorM
essage
)
{
# Check if the provided Member $name exists in $obj and return it or the default.
Try
...
...
@@ -112,7 +114,28 @@ Function Get-Attr($obj, $name, $default = $null, $resultobj, $failifempty=$false
{
throw
}
$obj
.
$name
if
(
$ValidateSet
)
{
if
(
$ValidateSet
-contains
(
$obj
.
$name
))
{
$obj
.
$name
}
Else
{
if
(
$ValidateSetErrorMessage
-eq
$null
)
{
#Auto-generated error should be sufficient in most use cases
$ValidateSetErrorMessage
=
"Argument
$name
needs to be one of
$(
$ValidateSet
-join
","
)
but was
$(
$obj
.
$name
)
."
}
Fail-Json -obj
$resultobj
-message
$ValidateSetErrorMessage
}
}
Else
{
$obj
.
$name
}
}
Catch
{
...
...
@@ -131,6 +154,9 @@ Function Get-Attr($obj, $name, $default = $null, $resultobj, $failifempty=$false
}
}
#Alias Get-attr-->Get-AnsibleParam for backwards compat.
New-Alias
-Name Get-attr -Value Get-AnsibleParam
# Helper filter/pipeline function to convert a value to boolean following current
# Ansible practices
# Example: $is_true = "true" | ConvertTo-Bool
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment