summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Bocahu <sbu@reportlinker.com>2015-03-27 11:11:25 +0100
committerSébastien Bocahu <sbu@reportlinker.com>2015-03-27 11:11:25 +0100
commit6b20c3890f92093373c0ed50ff8489786abc1982 (patch)
tree882f55cc2e4b46607c151552854f16c9713ccf5a
parenteacf4e42395689893478c16fea66ed7129b82403 (diff)
downloadansible-modules-core-6b20c3890f92093373c0ed50ff8489786abc1982.tar.gz
Add body_format for handling of JSON and YAML body
body_format is a new optional argument that enables handling of JSON or YAML serialization format for the body argument. When set to either 'json' or 'yaml', the body argument can be a dict or list. The body will be encoded, and the Content-Type HTTP header will be set, accordingly to the body_format argument. Example: - name: Facette - Create memory graph uri: method: POST url: http://facette/api/v1/library/graphs status_code: 201 body_format: json body: name: "{{ ansible_fqdn }} - Memory usage" attributes: Source": "{{ ansible_fqdn }}" link: "1947a490-8ac6-4bf2-47c1-ff74272f8b32"
-rw-r--r--network/basics/uri.py29
1 files changed, 23 insertions, 6 deletions
diff --git a/network/basics/uri.py b/network/basics/uri.py
index 9929316d..e3449ccb 100644
--- a/network/basics/uri.py
+++ b/network/basics/uri.py
@@ -64,6 +64,11 @@ options:
- The body of the http request/response to the web service.
required: false
default: null
+ body_format:
+ description:
+ - The serialization format of the body. Either raw, json, or yaml. When set to json or yaml, encodes the body argument and automatically sets the Content-Type header accordingly.
+ required: false
+ default: raw
method:
description:
- The HTTP method of the request or response.
@@ -238,11 +243,11 @@ def url_filename(url):
return fn
-def uri(module, url, dest, user, password, body, method, headers, redirects, socket_timeout):
+def uri(module, url, dest, user, password, body, body_format, method, headers, redirects, socket_timeout):
# To debug
#httplib2.debug = 4
- # Handle Redirects
+ # Handle Redirects
if redirects == "all" or redirects == "yes":
follow_redirects = True
follow_all_redirects = True
@@ -335,6 +340,7 @@ def main():
user = dict(required=False, default=None),
password = dict(required=False, default=None),
body = dict(required=False, default=None),
+ body_format = dict(required=False, default='raw', choices=['raw', 'json', 'yaml']),
method = dict(required=False, default='GET', choices=['GET', 'POST', 'PUT', 'HEAD', 'DELETE', 'OPTIONS', 'PATCH']),
return_content = dict(required=False, default='no', type='bool'),
force_basic_auth = dict(required=False, default='no', type='bool'),
@@ -357,6 +363,7 @@ def main():
user = module.params['user']
password = module.params['password']
body = module.params['body']
+ body_format = module.params['body_format']
method = module.params['method']
dest = module.params['dest']
return_content = module.params['return_content']
@@ -367,14 +374,24 @@ def main():
status_code = [int(x) for x in list(module.params['status_code'])]
socket_timeout = module.params['timeout']
- # Grab all the http headers. Need this hack since passing multi-values is currently a bit ugly. (e.g. headers='{"Content-Type":"application/json"}')
dict_headers = {}
+
+ # If body_format is json or yaml, encore the body (wich can be a dict or a list) and automatically sets the Content-Type header
+ if body_format == 'json':
+ body = json.dumps(body)
+ dict_headers['Content-Type'] = 'application/json'
+ elif body_format == 'yaml':
+ body = yaml.dump(body)
+ dict_headers['Content-Type'] = 'application/yaml'
+
+
+ # Grab all the http headers. Need this hack since passing multi-values is currently a bit ugly. (e.g. headers='{"Content-Type":"application/json"}')
for key, value in module.params.iteritems():
if key.startswith("HEADER_"):
skey = key.replace("HEADER_", "")
dict_headers[skey] = value
-
+
if creates is not None:
# do not run the command if the line contains creates=filename
# and the filename already exists. This allows idempotence
@@ -400,7 +417,7 @@ def main():
# Make the request
- resp, content, dest = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout)
+ resp, content, dest = uri(module, url, dest, user, password, body, body_format, method, dict_headers, redirects, socket_timeout)
resp['status'] = int(resp['status'])
# Write the file out if requested
@@ -422,7 +439,7 @@ def main():
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
uresp = {}
for key, value in resp.iteritems():
- ukey = key.replace("-", "_")
+ ukey = key.replace("-", "_")
uresp[ukey] = value
if 'content_type' in uresp: