ACI module_utils library for ACI modules (#27070)

* ACI module_utils library for ACI modules

This PR includes:
- the ACI argument_spec
- an aci_login function
- an experimental aci_request function
- an aci_response function
- included the ACI team

* New prototype using ACIModule

This PR includes:
- A new ACIModule object with various useful methods
This commit is contained in:
Dag Wieers
2017-07-31 13:44:27 +02:00
committed by John R Barker
parent 82a0a05406
commit 97aaf103e8
6 changed files with 287 additions and 152 deletions

View File

@@ -21,7 +21,7 @@
import sys
from ansible.compat.tests import unittest
from ansible.modules.network.aci.aci_rest import aci_response
from ansible.module_utils.aci import aci_response_json, aci_response_xml
from nose.plugins.skip import SkipTest
@@ -53,7 +53,8 @@ class AciRest(unittest.TestCase):
}
json_response = '{"totalCount":"1","imdata":[{"error":{"attributes":{"code":"401","text":"Username or password is incorrect - FAILED local authentication"}}}]}' # NOQA
json_result = aci_response(json_response, 'json')
json_result = dict()
aci_response_json(json_result, json_response)
self.assertEqual(expected_result, json_result)
# Python 2.7+ is needed for xmljson
@@ -64,7 +65,8 @@ class AciRest(unittest.TestCase):
<error code="401" text="Username or password is incorrect - FAILED local authentication"/>
</imdata>
'''
xml_result = aci_response(xml_response, 'xml')
xml_result = dict()
aci_response_xml(xml_result, xml_response)
self.assertEqual(json_result, xml_result)
def test_valid_aci_login(self):
@@ -149,16 +151,17 @@ class AciRest(unittest.TestCase):
}
json_response = '{"totalCount":"1","imdata":[{"aaaLogin":{"attributes":{"token":"ZldYAsoO9d0FfAQM8xaEVWvQPSOYwpnqzhwpIC1r4MaToknJjlIuAt9+TvXqrZ8lWYIGPj6VnZkWiS8nJfaiaX/AyrdD35jsSxiP3zydh+849xym7ALCw/fFNsc7b5ik1HaMuSUtdrN8fmCEUy7Pq/QNpGEqkE8m7HaxAuHpmvXgtdW1bA+KKJu2zY1c/tem","siteFingerprint":"NdxD72K/uXaUK0wn","refreshTimeoutSeconds":"600","maximumLifetimeSeconds":"86400","guiIdleTimeoutSeconds":"1200","restTimeoutSeconds":"90","creationTime":"1500134817","firstLoginTime":"1500134817","userName":"admin","remoteUser":"false","unixUserId":"15374","sessionId":"o7hObsqNTfCmDGcZI5c4ng==","lastName":"","firstName":"","version":"2.0(2f)","buildTime":"Sat Aug 20 23:07:07 PDT 2016","node":"topology/pod-1/node-1"},"children":[{"aaaUserDomain":{"attributes":{"name":"all","rolesR":"admin","rolesW":"admin"},"children":[{"aaaReadRoles":{"attributes":{}}},{"aaaWriteRoles":{"attributes":{},"children":[{"role":{"attributes":{"name":"admin"}}}]}}]}},{"DnDomainMapEntry":{"attributes":{"dn":"uni/tn-common","readPrivileges":"admin","writePrivileges":"admin"}}},{"DnDomainMapEntry":{"attributes":{"dn":"uni/tn-infra","readPrivileges":"admin","writePrivileges":"admin"}}},{"DnDomainMapEntry":{"attributes":{"dn":"uni/tn-mgmt","readPrivileges":"admin","writePrivileges":"admin"}}}]}}]}' # NOQA
json_result = aci_response(json_response, 'json')
json_result = dict()
aci_response_json(json_result, json_response)
self.assertEqual(expected_result, json_result)
# Python 2.7+ is needed for xmljson
if sys.version_info < (2, 7):
return
xml_response = '<?xml version="1.0" encoding="UTF-8"?><imdata totalCount="1">\n<aaaLogin token="ZldYAsoO9d0FfAQM8xaEVWvQPSOYwpnqzhwpIC1r4MaToknJjlIuAt9+TvXqrZ8lWYIGPj6VnZkWiS8nJfaiaX/AyrdD35jsSxiP3zydh+849xym7ALCw/fFNsc7b5ik1HaMuSUtdrN8fmCEUy7Pq/QNpGEqkE8m7HaxAuHpmvXgtdW1bA+KKJu2zY1c/tem" siteFingerprint="NdxD72K/uXaUK0wn" refreshTimeoutSeconds="600" maximumLifetimeSeconds="86400" guiIdleTimeoutSeconds="1200" restTimeoutSeconds="90" creationTime="1500134817" firstLoginTime="1500134817" userName="admin" remoteUser="false" unixUserId="15374" sessionId="o7hObsqNTfCmDGcZI5c4ng==" lastName="" firstName="" version="2.0(2f)" buildTime="Sat Aug 20 23:07:07 PDT 2016" node="topology/pod-1/node-1">\n<aaaUserDomain name="all" rolesR="admin" rolesW="admin">\n<aaaReadRoles/>\n<aaaWriteRoles>\n<role name="admin"/>\n</aaaWriteRoles>\n</aaaUserDomain>\n<DnDomainMapEntry dn="uni/tn-common" readPrivileges="admin" writePrivileges="admin"/>\n<DnDomainMapEntry dn="uni/tn-infra" readPrivileges="admin" writePrivileges="admin"/>\n<DnDomainMapEntry dn="uni/tn-mgmt" readPrivileges="admin" writePrivileges="admin"/>\n</aaaLogin></imdata>\n''' # NOQA
xml_result = aci_response(xml_response, 'xml')
self.assertEqual(expected_result, json_result)
xml_result = dict()
aci_response_xml(xml_result, xml_response)
self.assertEqual(json_result, xml_result)
def test_invalid_input(self):
@@ -179,7 +182,9 @@ class AciRest(unittest.TestCase):
}
json_response = '{"totalCount":"1","imdata":[{"error":{"attributes":{"code":"401","text":"Username or password is incorrect - FAILED local authentication"}}}]}' # NOQA
json_result = aci_response(json_response, 'json')
json_result = dict()
aci_response_json(json_result, json_response)
self.assertEqual(expected_result, json_result)
# Python 2.7+ is needed for xmljson
if sys.version_info < (2, 7):
@@ -189,9 +194,8 @@ class AciRest(unittest.TestCase):
<error code="401" text="Username or password is incorrect - FAILED local authentication"/>
</imdata>
'''
xml_result = aci_response(xml_response, 'xml')
self.assertEqual(expected_result, json_result)
xml_result = dict()
aci_response_xml(xml_result, xml_response)
self.assertEqual(json_result, xml_result)
def test_empty_response(self):
@@ -212,7 +216,8 @@ class AciRest(unittest.TestCase):
}
json_response = ''
json_result = aci_response(json_response, 'json')
json_result = dict()
aci_response_json(json_result, json_response)
self.assertEqual(expected_json_result, json_result)
# Python 2.7+ is needed for xmljson
@@ -241,7 +246,8 @@ class AciRest(unittest.TestCase):
}
xml_response = ''
xml_result = aci_response(xml_response, 'xml')
xml_result = dict()
aci_response_xml(xml_result, xml_response)
self.assertEqual(expected_xml_result, xml_result)
def test_invalid_response(self):
@@ -269,7 +275,8 @@ class AciRest(unittest.TestCase):
}
json_response = '{ "aaa":'
json_result = aci_response(json_response, 'json')
json_result = dict()
aci_response_json(json_result, json_response)
self.assertEqual(expected_json_result, json_result)
# Python 2.7+ is needed for xmljson
@@ -298,5 +305,6 @@ class AciRest(unittest.TestCase):
}
xml_response = '<aaa '
xml_result = aci_response(xml_response, 'xml')
xml_result = dict()
aci_response_xml(xml_result, xml_response)
self.assertEqual(expected_xml_result, xml_result)