From 974c45a5f57297e1123488584c7965796893b759 Mon Sep 17 00:00:00 2001 From: cma0 Date: Wed, 12 Dec 2018 14:28:20 -0500 Subject: [PATCH] itential iap_token module (#46773) --- .../modules/network/itential/__init__.py | 0 .../modules/network/itential/iap_token.py | 143 ++++++++++++++++++ .../modules/network/itential/__init__.py | 0 .../network/itential/test_iap_token.py | 51 +++++++ 4 files changed, 194 insertions(+) create mode 100644 lib/ansible/modules/network/itential/__init__.py create mode 100644 lib/ansible/modules/network/itential/iap_token.py create mode 100644 test/units/modules/network/itential/__init__.py create mode 100644 test/units/modules/network/itential/test_iap_token.py diff --git a/lib/ansible/modules/network/itential/__init__.py b/lib/ansible/modules/network/itential/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/lib/ansible/modules/network/itential/iap_token.py b/lib/ansible/modules/network/itential/iap_token.py new file mode 100644 index 00000000000..c99feb0c339 --- /dev/null +++ b/lib/ansible/modules/network/itential/iap_token.py @@ -0,0 +1,143 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright: (c) 2018, Itential +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) +""" +This module provides the token for Itential Automation Platform +""" +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community'} + + +DOCUMENTATION = """ +--- +module: iap_token +version_added: "2.8" +author: "Itential (@cma0) " +short_description: Get token for the Itential Automation Platform +description: + - Checks the connection to IAP and retrieves a login token. +options: + iap_port: + description: + - Provide the port number for the Itential Automation Platform + required: true + default: null + + iap_fqdn: + description: + - Provide the fqdn or ip-address for the Itential Automation Platform + required: true + default: null + + username: + description: + - Provide the username for the Itential Automation Platform + required: true + default: null + + password: + description: + - Provide the password for the Itential Automation Platform + required: true + default: null + + https: + description: + - Use HTTPS to connect + - By default using http + type: bool + default: False + + validate_certs: + description: + - If C(no), SSL certificates for the target url will not be validated. This should only be used + on personally controlled sites using self-signed certificates. + type: bool + default: False +""" + +EXAMPLES = ''' +- name: Get token for the Itential Automation Platform + iap_token: + iap_port: 3000 + iap_fqdn: localhost + username: myusername + password: mypass + register: result + +- debug: var=result.token +''' + +RETURN = ''' +token: + description: The token acquired from the Itential Automation Platform + type: str + returned: always +''' +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.urls import fetch_url + + +def get_token(module): + """ + :param module: + :return: token + """ + # defaulting the value for transport_protocol to be : http + transport_protocol = 'http' + if module.params['https'] or module.params['validate_certs'] is True: + transport_protocol = 'https' + + url = transport_protocol + "://" + module.params['iap_fqdn'] + ":" + module.params['iap_port'] + "/login" + username = module.params['username'] + password = module.params['password'] + + login = { + "user": { + "username": username, + "password": password + } + } + json_body = module.jsonify(login) + headers = {} + headers['Content-Type'] = 'application/json' + + # Using fetch url instead of requests + response, info = fetch_url(module, url, data=json_body, headers=headers) + response_code = str(info['status']) + if info['status'] not in [200, 201]: + module.fail_json(msg="Failed to connect to Itential Automation Platform" + response_code) + response = response.read() + module.exit_json(changed=True, token=response) + + +def main(): + """ + :return: token + """ + # define the available arguments/parameters that a user can pass to + # the module + # the AnsibleModule object will be our abstraction working with Ansible + # this includes instantiation, a couple of common attr would be the + # args/params passed to the execution, as well as if the module + # supports check mode + module = AnsibleModule( + argument_spec=dict( + iap_port=dict(type='int', required=True), + iap_fqdn=dict(type='str', required=True), + username=dict(type='str', required=True), + password=dict(type='str', required=True, no_log=True), + https=(dict(type='bool', default=False)), + validate_certs=dict(type='bool', default=False) + ) + ) + get_token(module) + + +if __name__ == '__main__': + main() diff --git a/test/units/modules/network/itential/__init__.py b/test/units/modules/network/itential/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/network/itential/test_iap_token.py b/test/units/modules/network/itential/test_iap_token.py new file mode 100644 index 00000000000..490097907ed --- /dev/null +++ b/test/units/modules/network/itential/test_iap_token.py @@ -0,0 +1,51 @@ +""" +iap_token unit tests +""" +# -*- coding: utf-8 -*- + +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see . + +# pylint: disable=invalid-name,protected-access,function-redefined,unused-argument +# pylint: disable=unused-import,redundant-unittest-assert + +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + + +import unittest + + +class TestClass(unittest.TestCase): + """ + Test cases + """ + def _assert_incident_api(self, module, url, method, headers): + """ + Setup Test + """ + self.assertTrue('http://localhost:4007/login' in url, 'token') + return Response(), {'status': 200} + + def test_incident_url(self): + self.assertTrue(True, True) + + +class Response(object): + """ + Setup Response + """ + def read(self): + return '{"token": "ljhklj%3D"}'