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
a5dfb353
Commit
a5dfb353
authored
Jun 29, 2015
by
Toshio Kuratomi
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #11420 from ansible/argspec-path-and-refactor
Refactor the argspec type checking and add path as a type
parents
8a1a8679
be6db1a7
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
84 additions
and
50 deletions
+84
-50
lib/ansible/module_utils/basic.py
+84
-50
No files found.
lib/ansible/module_utils/basic.py
View file @
a5dfb353
...
@@ -380,6 +380,16 @@ class AnsibleModule(object):
...
@@ -380,6 +380,16 @@ class AnsibleModule(object):
self
.
_set_defaults
(
pre
=
True
)
self
.
_set_defaults
(
pre
=
True
)
self
.
_CHECK_ARGUMENT_TYPES_DISPATCHER
=
{
'str'
:
self
.
_check_type_str
,
'list'
:
self
.
_check_type_list
,
'dict'
:
self
.
_check_type_dict
,
'bool'
:
self
.
_check_type_bool
,
'int'
:
self
.
_check_type_int
,
'float'
:
self
.
_check_type_float
,
'path'
:
self
.
_check_type_path
,
}
if
not
bypass_checks
:
if
not
bypass_checks
:
self
.
_check_required_arguments
()
self
.
_check_required_arguments
()
self
.
_check_argument_values
()
self
.
_check_argument_values
()
...
@@ -1021,72 +1031,96 @@ class AnsibleModule(object):
...
@@ -1021,72 +1031,96 @@ class AnsibleModule(object):
return
(
str
,
e
)
return
(
str
,
e
)
return
str
return
str
def
_check_argument_types
(
self
):
def
_check_type_str
(
self
,
value
):
''' ensure all arguments have the requested type '''
if
isinstance
(
value
,
basestring
):
for
(
k
,
v
)
in
self
.
argument_spec
.
iteritems
():
return
value
wanted
=
v
.
get
(
'type'
,
None
)
# Note: This could throw a unicode error if value's __str__() method
if
wanted
is
None
:
# returns non-ascii. Have to port utils.to_bytes() if that happens
continue
return
str
(
value
)
if
k
not
in
self
.
params
:
continue
value
=
self
.
params
[
k
]
def
_check_type_list
(
self
,
value
):
is_invalid
=
False
if
isinstance
(
value
,
list
):
return
value
try
:
if
wanted
==
'str'
:
if
not
isinstance
(
value
,
basestring
):
self
.
params
[
k
]
=
str
(
value
)
elif
wanted
==
'list'
:
if
not
isinstance
(
value
,
list
):
if
isinstance
(
value
,
basestring
):
if
isinstance
(
value
,
basestring
):
self
.
params
[
k
]
=
value
.
split
(
","
)
return
value
.
split
(
","
)
elif
isinstance
(
value
,
int
)
or
isinstance
(
value
,
float
):
elif
isinstance
(
value
,
int
)
or
isinstance
(
value
,
float
):
self
.
params
[
k
]
=
[
str
(
value
)
]
return
[
str
(
value
)
]
else
:
is_invalid
=
True
raise
TypeError
(
'
%
s cannot be converted to a list'
%
type
(
value
))
elif
wanted
==
'dict'
:
if
not
isinstance
(
value
,
dict
):
def
_check_type_dict
(
self
,
value
):
if
isinstance
(
value
,
dict
):
return
value
if
isinstance
(
value
,
basestring
):
if
isinstance
(
value
,
basestring
):
if
value
.
startswith
(
"{"
):
if
value
.
startswith
(
"{"
):
try
:
try
:
self
.
params
[
k
]
=
json
.
loads
(
value
)
return
json
.
loads
(
value
)
except
:
except
:
(
result
,
exc
)
=
self
.
safe_eval
(
value
,
dict
(),
include_exceptions
=
True
)
(
result
,
exc
)
=
self
.
safe_eval
(
value
,
dict
(),
include_exceptions
=
True
)
if
exc
is
not
None
:
if
exc
is
not
None
:
self
.
fail_json
(
msg
=
"unable to evaluate dictionary for
%
s"
%
k
)
raise
TypeError
(
'unable to evaluate string as dictionary'
)
self
.
params
[
k
]
=
result
return
result
elif
'='
in
value
:
elif
'='
in
value
:
self
.
params
[
k
]
=
dict
([
x
.
strip
()
.
split
(
"="
,
1
)
for
x
in
value
.
split
(
","
)])
return
dict
([
x
.
strip
()
.
split
(
"="
,
1
)
for
x
in
value
.
split
(
","
)])
else
:
self
.
fail_json
(
msg
=
"dictionary requested, could not parse JSON or key=value"
)
else
:
else
:
is_invalid
=
True
raise
TypeError
(
"dictionary requested, could not parse JSON or key=value"
)
elif
wanted
==
'bool'
:
if
not
isinstance
(
value
,
bool
):
raise
TypeError
(
'
%
s cannot be converted to a dict'
%
type
(
value
))
def
_check_type_bool
(
self
,
value
):
if
isinstance
(
value
,
bool
):
return
value
if
isinstance
(
value
,
basestring
):
if
isinstance
(
value
,
basestring
):
self
.
params
[
k
]
=
self
.
boolean
(
value
)
return
self
.
boolean
(
value
)
else
:
is_invalid
=
True
raise
TypeError
(
'
%
s cannot be converted to a bool'
%
type
(
value
))
elif
wanted
==
'int'
:
if
not
isinstance
(
value
,
int
):
def
_check_type_int
(
self
,
value
):
if
isinstance
(
value
,
int
):
return
value
if
isinstance
(
value
,
basestring
):
if
isinstance
(
value
,
basestring
):
self
.
params
[
k
]
=
int
(
value
)
return
int
(
value
)
else
:
is_invalid
=
True
raise
TypeError
(
'
%
s cannot be converted to an int'
%
type
(
value
))
elif
wanted
==
'float'
:
if
not
isinstance
(
value
,
float
):
def
_check_type_float
(
self
,
value
):
if
isinstance
(
value
,
float
):
return
value
if
isinstance
(
value
,
basestring
):
if
isinstance
(
value
,
basestring
):
self
.
params
[
k
]
=
float
(
value
)
return
float
(
value
)
else
:
is_invalid
=
True
else
:
self
.
fail_json
(
msg
=
"implementation error: unknown type
%
s requested for
%
s"
%
(
wanted
,
k
))
if
is_invalid
:
raise
TypeError
(
'
%
s cannot be converted to a float'
%
type
(
value
))
self
.
fail_json
(
msg
=
"argument
%
s is of invalid type:
%
s, required:
%
s"
%
(
k
,
type
(
value
),
wanted
))
except
ValueError
:
def
_check_type_path
(
self
,
value
):
self
.
fail_json
(
msg
=
"value of argument
%
s is not of type
%
s and we were unable to automatically convert"
%
(
k
,
wanted
))
value
=
self
.
_check_type_str
(
value
)
return
os
.
path
.
expanduser
(
os
.
path
.
expandvars
(
value
))
def
_check_argument_types
(
self
):
''' ensure all arguments have the requested type '''
for
(
k
,
v
)
in
self
.
argument_spec
.
iteritems
():
wanted
=
v
.
get
(
'type'
,
None
)
if
wanted
is
None
:
continue
if
k
not
in
self
.
params
:
continue
value
=
self
.
params
[
k
]
is_invalid
=
False
try
:
type_checker
=
self
.
_CHECK_ARGUMENT_TYPES_DISPATCHER
[
wanted
]
except
KeyError
:
self
.
fail_json
(
msg
=
"implementation error: unknown type
%
s requested for
%
s"
%
(
wanted
,
k
))
try
:
self
.
params
[
k
]
=
type_checker
(
value
)
except
(
TypeError
,
ValueError
):
self
.
fail_json
(
msg
=
"argument
%
s is of type
%
s and we were unable to convert to
%
s"
%
(
k
,
type
(
value
),
wanted
))
def
_set_defaults
(
self
,
pre
=
True
):
def
_set_defaults
(
self
,
pre
=
True
):
for
(
k
,
v
)
in
self
.
argument_spec
.
iteritems
():
for
(
k
,
v
)
in
self
.
argument_spec
.
iteritems
():
...
...
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