From 147857d7ac111a61dfe48ef367c76c4cd3c8af91 Mon Sep 17 00:00:00 2001 From: Eric Chou Date: Fri, 1 Jul 2016 13:49:28 -0700 Subject: [PATCH 1/2] Added axapi_authenticate_v3 and axapi_call_v3 for new AXAPIv3 that is not backward compatible --- lib/ansible/module_utils/a10.py | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/lib/ansible/module_utils/a10.py b/lib/ansible/module_utils/a10.py index 45fda0eb39..e9eb7f10e6 100644 --- a/lib/ansible/module_utils/a10.py +++ b/lib/ansible/module_utils/a10.py @@ -88,6 +88,43 @@ def axapi_authenticate(module, base_url, username, password): sessid = result['session_id'] return base_url + '&session_id=' + sessid +def axapi_authenticate_v3(module, base_url, username, password): + url = base_url + auth_payload = {"credentials": {"username": username, "password": password}} + result = axapi_call_v3(module, url, post=auth_payload) + if axapi_failure(result): + return module.fail_json(msg=result['response']['err']['msg']) + signature = result['authresponse']['signature'] + return signature + +def axapi_call_v3(module, url, post=None, signature=''): + ''' + Returns a datastructure based on the result of the API call + ''' + if signature: + headers = {'content-type': 'application/json', 'signature': signature} + else: + headers = {'content-type': 'application/json'} + rsp, info = fetch_url(module, url, method='POST', data=json.dumps(post), headers=headers) + if not rsp or info['status'] >= 400: + module.fail_json(msg="failed to connect (status code %s), error was %s" % (info['status'], info.get('msg', 'no error given'))) + try: + raw_data = rsp.read() + data = json.loads(raw_data) + except ValueError: + # at least one API call (system.action.write_config) returns + # XML even when JSON is requested, so do some minimal handling + # here to prevent failing even when the call succeeded + if 'status="ok"' in raw_data.lower(): + data = {"response": {"status": "OK"}} + else: + data = {"response": {"status": "fail", "err": {"msg": raw_data}}} + except: + module.fail_json(msg="could not read the result from the host") + finally: + rsp.close() + return data + def axapi_enabled_disabled(flag): ''' The axapi uses 0/1 integer values for flags, rather than strings From b6c30e7985308b3daa0c0b9785bf43a1fcb47f2b Mon Sep 17 00:00:00 2001 From: Eric Chou Date: Fri, 1 Jul 2016 17:59:49 -0700 Subject: [PATCH 2/2] Added axapi_authenticate_v3 and axapi_call_v3 for new AXAPIv3 that is not backward compatible --- lib/ansible/module_utils/a10.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/ansible/module_utils/a10.py b/lib/ansible/module_utils/a10.py index e9eb7f10e6..737919b463 100644 --- a/lib/ansible/module_utils/a10.py +++ b/lib/ansible/module_utils/a10.py @@ -97,15 +97,15 @@ def axapi_authenticate_v3(module, base_url, username, password): signature = result['authresponse']['signature'] return signature -def axapi_call_v3(module, url, post=None, signature=''): +def axapi_call_v3(module, url, method=None, body=None, signature=None): ''' Returns a datastructure based on the result of the API call ''' if signature: - headers = {'content-type': 'application/json', 'signature': signature} + headers = {'content-type': 'application/json', 'Authorization': 'A10 %s' % signature} else: headers = {'content-type': 'application/json'} - rsp, info = fetch_url(module, url, method='POST', data=json.dumps(post), headers=headers) + rsp, info = fetch_url(module, url, method=method, data=json.dumps(body), headers=headers) if not rsp or info['status'] >= 400: module.fail_json(msg="failed to connect (status code %s), error was %s" % (info['status'], info.get('msg', 'no error given'))) try: