diff --git a/lib/ansible/modules/network/fortimanager/fmgr_device.py b/lib/ansible/modules/network/fortimanager/fmgr_device.py index 54c85c9bd3d..5f40144f2ff 100644 --- a/lib/ansible/modules/network/fortimanager/fmgr_device.py +++ b/lib/ansible/modules/network/fortimanager/fmgr_device.py @@ -17,6 +17,7 @@ # from __future__ import absolute_import, division, print_function + __metaclass__ = type ANSIBLE_METADATA = { @@ -29,11 +30,13 @@ DOCUMENTATION = ''' --- module: fmgr_device version_added: "2.8" +notes: + - Full Documentation at U(https://ftnt-ansible-docs.readthedocs.io/en/latest/). author: - Luke Weighall (@lweighall) - Andrew Welsh (@Ghilli3) - Jim Huber (@p4r4n0y1ng) -short_description: Add or remove device +short_description: Add or remove device from FortiManager. description: - Add or remove a device or list of devices from FortiManager Device Manager using JSON RPC API. @@ -43,76 +46,69 @@ options: - The ADOM the configuration should belong to. required: true default: root - host: - description: - - The FortiManager's address. - required: true - username: - description: - - The username used to authenticate with the FortiManager. - required: false - password: + + mode: description: - - The password associated with the username account. + - The desired mode of the specified object. required: false - state: + default: add + choices: ["add", "delete"] + + blind_add: description: - - The desired state of the specified object. - - absent will delete the object if it exists. - - present will create the configuration if needed. + - When adding a device, module will check if it exists, and skip if it does. + - If enabled, this option will stop the module from checking if it already exists, and blindly add the device. required: false - default: present - choices: ["absent", "present"] + default: "disable" + choices: ["enable", "disable"] device_username: description: - The username of the device being added to FortiManager. required: false + device_password: description: - The password of the device being added to FortiManager. required: false + device_ip: description: - The IP of the device being added to FortiManager. Supports both IPv4 and IPv6. required: false + device_unique_name: description: - The desired "friendly" name of the device being added to FortiManager. required: false + device_serial: description: - The serial number of the device being added to FortiManager. required: false ''' - EXAMPLES = ''' - name: DISCOVER AND ADD DEVICE FGT1 fmgr_device: - host: "{{inventory_hostname}}" - username: "{{ username }}" - password: "{{ password }}" adom: "root" device_username: "admin" device_password: "admin" device_ip: "10.10.24.201" device_unique_name: "FGT1" device_serial: "FGVM000000117994" - state: "present" + mode: "add" + blind_add: "enable" - name: DISCOVER AND ADD DEVICE FGT2 fmgr_device: - host: "{{inventory_hostname}}" - username: "{{ username }}" - password: "{{ password }}" adom: "root" device_username: "admin" device_password: "admin" device_ip: "10.10.24.202" device_unique_name: "FGT2" device_serial: "FGVM000000117992" - state: "absent" + mode: "delete" ''' RETURN = """ @@ -122,20 +118,27 @@ api_result: type: str """ -from ansible.module_utils.basic import AnsibleModule, env_fallback -from ansible.module_utils.network.fortimanager.fortimanager import AnsibleFortiManager +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.connection import Connection +from ansible.module_utils.network.fortimanager.fortimanager import FortiManagerHandler +from ansible.module_utils.network.fortimanager.common import FMGBaseException +from ansible.module_utils.network.fortimanager.common import FMGRCommon +from ansible.module_utils.network.fortimanager.common import FMGRMethods +from ansible.module_utils.network.fortimanager.common import DEFAULT_RESULT_OBJ +from ansible.module_utils.network.fortimanager.common import FAIL_SOCKET_MSG -# check for pyFMG lib -try: - from pyFMG.fortimgr import FortiManager - HAS_PYFMGR = True -except ImportError: - HAS_PYFMGR = False - -def discover_device(fmg, paramgram): +def discover_device(fmgr, paramgram): """ This method is used to discover devices before adding them to FMGR + + :param fmgr: The fmgr object instance from fmgr_utils.py + :type fmgr: class object + :param paramgram: The formatted dictionary of options to process + :type paramgram: dict + + :return: The response from the FortiManager + :rtype: dict """ datagram = { @@ -146,13 +149,22 @@ def discover_device(fmg, paramgram): } url = '/dvm/cmd/discover/device/' - response = fmg.execute(url, datagram) + + response = fmgr.process_request(url, datagram, FMGRMethods.EXEC) return response -def add_device(fmg, paramgram): +def add_device(fmgr, paramgram): """ This method is used to add devices to the FMGR + + :param fmgr: The fmgr object instance from fmgr_utils.py + :type fmgr: class object + :param paramgram: The formatted dictionary of options to process + :type paramgram: dict + + :return: The response from the FortiManager + :rtype: dict """ datagram = { @@ -165,72 +177,61 @@ def add_device(fmg, paramgram): } url = '/dvm/cmd/add/device/' - response = fmg.execute(url, datagram) + response = fmgr.process_request(url, datagram, FMGRMethods.EXEC) return response -def delete_device(fmg, paramgram): +def delete_device(fmgr, paramgram): """ This method deletes a device from the FMGR + + :param fmgr: The fmgr object instance from fmgr_utils.py + :type fmgr: class object + :param paramgram: The formatted dictionary of options to process + :type paramgram: dict + + :return: The response from the FortiManager + :rtype: dict """ datagram = { "adom": paramgram["adom"], "flags": ["create_task", "nonblocking"], - "odd_request_form": "True", "device": paramgram["device_unique_name"], } url = '/dvm/cmd/del/device/' - response = fmg.execute(url, datagram) + response = fmgr.process_request(url, datagram, FMGRMethods.EXEC) return response -# FUNCTION/METHOD FOR LOGGING OUT AND ANALYZING ERROR CODES -def fmgr_logout(fmg, module, msg="NULL", results=(), good_codes=(0,), logout_on_fail=True, logout_on_success=False): +def get_device(fmgr, paramgram): """ - THIS METHOD CONTROLS THE LOGOUT AND ERROR REPORTING AFTER AN METHOD OR FUNCTION RUNS + This method attempts to find the firewall on FortiManager to see if it already exists. + + :param fmgr: The fmgr object instance from fmgr_utils.py + :type fmgr: class object + :param paramgram: The formatted dictionary of options to process + :type paramgram: dict + + :return: The response from the FortiManager + :rtype: dict """ + datagram = { + "adom": paramgram["adom"], + "filter": ["name", "==", paramgram["device_unique_name"]], + } - # VALIDATION ERROR (NO RESULTS, JUST AN EXIT) - if msg != "NULL" and len(results) == 0: - try: - fmg.logout() - except Exception: - pass - module.fail_json(msg=msg) - - # SUBMISSION ERROR - if len(results) > 0: - if msg == "NULL": - try: - msg = results[1]['status']['message'] - except Exception: - msg = "No status message returned from pyFMG. Possible that this was a GET with a tuple result." - - if results[0] not in good_codes: - if logout_on_fail: - fmg.logout() - module.fail_json(msg=msg, **results[1]) - else: - return_msg = msg + " -- LOGOUT ON FAIL IS OFF, MOVING ON" - return return_msg - else: - if logout_on_success: - fmg.logout() - module.exit_json(msg=msg, **results[1]) - else: - return_msg = msg + " -- LOGOUT ON SUCCESS IS OFF, MOVING ON TO REST OF CODE" - return return_msg + url = '/dvmdb/adom/{adom}/device/{name}'.format(adom=paramgram["adom"], + name=paramgram["device_unique_name"]) + response = fmgr.process_request(url, datagram, FMGRMethods.GET) + return response def main(): argument_spec = dict( adom=dict(required=False, type="str", default="root"), - host=dict(required=True, type="str"), - username=dict(fallback=(env_fallback, ["ANSIBLE_NET_USERNAME"])), - password=dict(fallback=(env_fallback, ["ANSIBLE_NET_PASSWORD"]), no_log=True), - state=dict(choices=["absent", "present"], type="str", default="present"), - + mode=dict(choices=["add", "delete"], type="str", default="add"), + blind_add=dict(choices=["enable", "disable"], type="str", default="disable"), device_ip=dict(required=False, type="str"), device_username=dict(required=False, type="str"), device_password=dict(required=False, type="str", no_log=True), @@ -238,9 +239,10 @@ def main(): device_serial=dict(required=False, type="str") ) - module = AnsibleModule(argument_spec, supports_check_mode=True,) + # BUILD MODULE OBJECT SO WE CAN BUILD THE PARAMGRAM + module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False, ) - # handle params passed via provider and insure they are represented as the data type expected by fortimanagerd + # BUILD THE PARAMGRAM paramgram = { "device_ip": module.params["device_ip"], "device_username": module.params["device_username"], @@ -248,44 +250,52 @@ def main(): "device_unique_name": module.params["device_unique_name"], "device_serial": module.params["device_serial"], "adom": module.params["adom"], - "state": module.params["state"] + "mode": module.params["mode"] } - # validate required arguments are passed; not used in argument_spec to allow params to be called from provider - # check if params are set - if module.params["host"] is None or module.params["username"] is None or module.params["password"] is None: - module.fail_json(msg="Host and username are required for connection") - - # CHECK IF LOGIN FAILED - fmg = AnsibleFortiManager(module, module.params["host"], module.params["username"], module.params["password"]) - response = fmg.login() + # INSERT THE PARAMGRAM INTO THE MODULE SO WHEN WE PASS IT TO MOD_UTILS.FortiManagerHandler IT HAS THAT INFO + module.paramgram = paramgram - if response[1]['status']['code'] != 0: - module.fail_json(msg="Connection to FortiManager Failed") + # TRY TO INIT THE CONNECTION SOCKET PATH AND FortiManagerHandler OBJECT AND TOOLS + fmgr = None + if module._socket_path: + connection = Connection(module._socket_path) + fmgr = FortiManagerHandler(connection, module) + fmgr.tools = FMGRCommon() else: - # START SESSION LOGIC - results = (-100000, {"msg": "Nothing Happened."}) - if paramgram["state"] == "present": - # add device - results = discover_device(fmg, paramgram) - if results[0] != 0: - if results[0] == -20042: - fmgr_logout(fmg, module, msg="Couldn't contact device on network", results=results, good_codes=[0]) - else: - fmgr_logout(fmg, module, msg="Discovering Device Failed", results=results, good_codes=[0]) - - if results[0] == 0: - results = add_device(fmg, paramgram) - if results[0] != 0 and results[0] != -20010: - fmgr_logout(fmg, module, msg="Adding Device Failed", results=results, good_codes=[0]) - - if paramgram["state"] == "absent": - # remove device - results = delete_device(fmg, paramgram) - if results[0] != 0: - fmgr_logout(fmg, module, msg="Deleting Device Failed", results=results, good_codes=[0]) - - fmg.logout() + module.fail_json(**FAIL_SOCKET_MSG) + + # BEGIN MODULE-SPECIFIC LOGIC -- THINGS NEED TO HAPPEN DEPENDING ON THE ENDPOINT AND OPERATION + results = DEFAULT_RESULT_OBJ + try: + if paramgram["mode"] == "add": + # CHECK IF DEVICE EXISTS + if module.params["blind_add"] == "disable": + exists_results = get_device(fmgr, paramgram) + fmgr.govern_response(module=module, results=exists_results, good_codes=(0, -3), changed=False, + ansible_facts=fmgr.construct_ansible_facts(exists_results, + module.params, paramgram)) + + discover_results = discover_device(fmgr, paramgram) + fmgr.govern_response(module=module, results=discover_results, stop_on_success=False, + ansible_facts=fmgr.construct_ansible_facts(discover_results, + module.params, paramgram)) + + if discover_results[0] == 0: + results = add_device(fmgr, paramgram) + fmgr.govern_response(module=module, results=discover_results, stop_on_success=True, + changed_if_success=True, + ansible_facts=fmgr.construct_ansible_facts(discover_results, + module.params, paramgram)) + + if paramgram["mode"] == "delete": + results = delete_device(fmgr, paramgram) + fmgr.govern_response(module=module, results=results, + ansible_facts=fmgr.construct_ansible_facts(results, module.params, paramgram)) + + except Exception as err: + raise FMGBaseException(err) + return module.exit_json(**results[1]) diff --git a/test/units/modules/network/fortimanager/fixtures/test_fmgr_device.json b/test/units/modules/network/fortimanager/fixtures/test_fmgr_device.json index 4cfe482de48..90dba75f0e5 100644 --- a/test/units/modules/network/fortimanager/fixtures/test_fmgr_device.json +++ b/test/units/modules/network/fortimanager/fixtures/test_fmgr_device.json @@ -1,526 +1,934 @@ { - "add_device": [ - { - "url": "/dvm/cmd/add/device/", - "raw_response": { - "device": { - "adm_pass": "fortinet", - "ip": "10.7.220.151", - "mgmt.__data[6]": 1, - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "vm_cpu_limit": 1, - "vm_cpu": 1, - "branch_pt": 163, - "hostname": "FGVM010000122995", - "source": 1, - "mgmt_id": 312304802, - "version": 600, - "build": 163, - "mgmt_mode": 3, - "adm_usr": "admin", - "av_ver": "61.00026(2018-07-27 11:28)", - "oid": 378, - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410598, - "patch": 2, - "vm_mem_limit": 2048, - "mgmt.__data[0]": 3870643, - "name": "FGT1", - "tab_status": "", - "mgmt.__data[4]": 2103046144, - "platform_id": 111, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097169, - "sn": "FGVM010000122995", - "mr": 0, - "os_type": 0, - "os_ver": 6 - } - }, - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.151", - "state": "present", - "device_unique_name": "FGT1", - "device_serial": null, - "device_password": "fortinet" - }, - "post_method": "execute" - }, - { - "url": "/dvm/cmd/add/device/", - "raw_response": { - "device": { - "adm_pass": "fortinet", - "ip": "10.7.220.152", - "mgmt.__data[6]": 1, - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "vm_cpu_limit": 1, - "vm_cpu": 1, - "branch_pt": 163, - "hostname": "FGVM010000123005", - "source": 1, - "mgmt_id": 2084190718, - "version": 600, - "build": 163, - "mgmt_mode": 3, - "adm_usr": "admin", - "av_ver": "61.00026(2018-07-27 11:28)", - "oid": 390, - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410631, - "patch": 2, - "vm_mem_limit": 2048, - "mgmt.__data[0]": 3870643, - "name": "FGT2", - "tab_status": "", - "mgmt.__data[4]": 2103046144, - "platform_id": 111, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097169, - "sn": "FGVM010000123005", - "mr": 0, - "os_type": 0, - "os_ver": 6 - } - }, - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.152", - "state": "present", - "device_unique_name": "FGT2", - "device_serial": null, - "device_password": "fortinet" - }, - "post_method": "execute" - }, - { - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.151", - "state": "present", - "device_unique_name": "FGT1", - "device_serial": null, - "device_password": "fortinet" - }, - "raw_response": { - "status": { - "message": "Serial number already in use", - "code": -20010 - }, - "url": "/dvm/cmd/add/device/" - }, - "post_method": "execute" - }, - { - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.152", - "state": "present", - "device_unique_name": "FGT2", - "device_serial": null, - "device_password": "fortinet" - }, - "raw_response": { - "status": { - "message": "Serial number already in use", - "code": -20010 - }, - "url": "/dvm/cmd/add/device/" - }, - "post_method": "execute" - }, - { - "url": "/dvm/cmd/add/device/", - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.153", - "state": "present", - "device_unique_name": "FGT3", - "device_serial": null, - "device_password": "fortinet" - }, - "raw_response": { - "device": { - "adm_pass": "fortinet", - "os_ver": 6, - "ip": "10.7.220.153", - "mgmt.__data[6]": 1, - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "platform_id": 111, - "branch_pt": 163, - "hostname": "FGVM010000123017", - "source": 1, - "mgmt_id": 501253209, - "version": 600, - "build": 163, - "mgmt_mode": 3, - "adm_usr": "admin", - "av_ver": "62.00278(2018-09-17 13:28)", - "mgmt.__data[4]": 2103046144, - "oid": 402, - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410728, - "vm_mem_limit": 2048, - "mgmt.__data[0]": 3870643, - "name": "FGT3", - "tab_status": "", - "patch": 2, - "vm_cpu_limit": 1, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097169, - "sn": "FGVM010000123017", - "mr": 0, - "os_type": 0, - "vm_cpu": 1 - } - }, - "post_method": "execute" - } - ], - "discover_device": [ - { - "url": "/dvm/cmd/discover/device/", - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.151", - "state": "present", - "device_unique_name": "FGT1", - "device_serial": null, - "device_password": "fortinet" - }, - "raw_response": { - "device": { - "adm_pass": "fortinet", - "os_ver": 6, - "ip": "10.7.220.151", - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "platform_id": 111, - "branch_pt": 163, - "hostname": "FGVM010000122995", - "source": 1, - "version": 600, - "build": 163, - "adm_usr": "admin", - "av_ver": "61.00026(2018-07-27 11:28)", - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410595, - "vm_mem_limit": 2048, - "name": "FGVM010000122995", - "tab_status": "", - "patch": 2, - "vm_cpu_limit": 1, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097153, - "sn": "FGVM010000122995", - "mr": 0, - "os_type": 0, - "vm_cpu": 1 - } - }, - "post_method": "execute" - }, - { - "url": "/dvm/cmd/discover/device/", - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.152", - "state": "present", - "device_unique_name": "FGT2", - "device_serial": null, - "device_password": "fortinet" - }, - "raw_response": { - "device": { - "adm_pass": "fortinet", - "os_ver": 6, - "ip": "10.7.220.152", - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "platform_id": 111, - "branch_pt": 163, - "hostname": "FGVM010000123005", - "source": 1, - "version": 600, - "build": 163, - "adm_usr": "admin", - "av_ver": "61.00026(2018-07-27 11:28)", - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410627, - "vm_mem_limit": 2048, - "name": "FGVM010000123005", - "tab_status": "", - "patch": 2, - "vm_cpu_limit": 1, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097153, - "sn": "FGVM010000123005", - "mr": 0, - "os_type": 0, - "vm_cpu": 1 - } - }, - "post_method": "execute" - }, - { - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.153", - "state": "present", - "device_unique_name": "FGT3", - "device_serial": null, - "device_password": "fortinet" - }, - "raw_response": { - "status": { - "message": "Probe failed: network", - "code": -20042 - }, - "url": "/dvm/cmd/discover/device/" - }, - "post_method": "execute" - }, - { - "url": "/dvm/cmd/discover/device/", - "raw_response": { - "device": { - "adm_pass": "fortinet", - "ip": "10.7.220.151", - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "vm_cpu_limit": 1, - "vm_cpu": 1, - "branch_pt": 163, - "hostname": "FGVM010000122995", - "source": 1, - "version": 600, - "build": 163, - "managed_sn": "FMG-VM0A17004505", - "adm_usr": "admin", - "av_ver": "61.00026(2018-07-27 11:28)", - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410707, - "vm_mem_limit": 2048, - "name": "FGVM010000122995", - "tab_status": "", - "patch": 2, - "platform_id": 111, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097153, - "sn": "FGVM010000122995", - "mr": 0, - "os_type": 0, - "os_ver": 6 - } - }, - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.151", - "state": "present", - "device_unique_name": "FGT1", - "device_serial": null, - "device_password": "fortinet" - }, - "post_method": "execute" - }, - { - "url": "/dvm/cmd/discover/device/", - "raw_response": { - "device": { - "adm_pass": "fortinet", - "ip": "10.7.220.152", - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "vm_cpu_limit": 1, - "vm_cpu": 1, - "branch_pt": 163, - "hostname": "FGVM010000123005", - "source": 1, - "version": 600, - "build": 163, - "managed_sn": "FMG-VM0A17004505", - "adm_usr": "admin", - "av_ver": "61.00026(2018-07-27 11:28)", - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410713, - "vm_mem_limit": 2048, - "name": "FGVM010000123005", - "tab_status": "", - "patch": 2, - "platform_id": 111, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097153, - "sn": "FGVM010000123005", - "mr": 0, - "os_type": 0, - "os_ver": 6 - } - }, - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.152", - "state": "present", - "device_unique_name": "FGT2", - "device_serial": null, - "device_password": "fortinet" - }, - "post_method": "execute" - }, - { - "url": "/dvm/cmd/discover/device/", - "raw_response": { - "device": { - "adm_pass": "fortinet", - "ip": "10.7.220.153", - "vm_mem": 2005, - "maxvdom": 10, - "conn_mode": 1, - "vm_cpu_limit": 1, - "vm_cpu": 1, - "branch_pt": 163, - "hostname": "FGVM010000123017", - "source": 1, - "version": 600, - "build": 163, - "adm_usr": "admin", - "av_ver": "62.00278(2018-09-17 13:28)", - "conn_status": 1, - "beta": -1, - "dev_status": 1, - "platform_str": "FortiGate-VM64", - "last_checked": 1537410723, - "vm_mem_limit": 2048, - "name": "FGVM010000123017", - "tab_status": "", - "patch": 2, - "platform_id": 111, - "vm_status": 3, - "ips_ver": "6.00741(2015-12-01 02:30)", - "flags": 2097153, - "sn": "FGVM010000123017", - "mr": 0, - "os_type": 0, - "os_ver": 6 - } - }, - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.153", - "state": "present", - "device_unique_name": "FGT3", - "device_serial": null, - "device_password": "fortinet" - }, - "post_method": "execute" - } - ], - "delete_device": [ - { - "raw_response": { - "status": { - "message": "OK", - "code": 0 - }, - "url": "/dvm/cmd/del/device/" - }, - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.151", - "state": "absent", - "device_unique_name": "FGT1", - "device_serial": null, - "device_password": "fortinet" - }, - "post_method": "execute" - }, - { - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.152", - "state": "absent", - "device_unique_name": "FGT2", - "device_serial": null, - "device_password": "fortinet" - }, - "raw_response": { - "status": { - "message": "OK", - "code": 0 - }, - "url": "/dvm/cmd/del/device/" - }, - "post_method": "execute" - }, - { - "raw_response": { - "status": { - "message": "OK", - "code": 0 - }, - "url": "/dvm/cmd/del/device/" - }, - "paramgram_used": { - "device_username": "admin", - "adom": "ansible", - "device_ip": "10.7.220.153", - "state": "absent", - "device_unique_name": "FGT3", - "device_serial": null, - "device_password": "fortinet" - }, - "post_method": "execute" - } - ] + "add_device": [ + { + "url": "/dvm/cmd/add/device/", + "raw_response": { + "device": { + "adm_pass": "fortinet", + "os_ver": 6, + "ip": "10.7.220.151", + "mgmt.__data[6]": 1, + "vm_mem": 1003, + "maxvdom": 10, + "conn_mode": 1, + "platform_id": 112, + "branch_pt": 231, + "hostname": "ansible-fgt01", + "source": 1, + "mgmt_id": 1014939351, + "version": 600, + "build": 231, + "mgmt_mode": 3, + "adm_usr": "admin", + "av_ver": "1.00000(2018-04-09 18:07)", + "mgmt.__data[4]": 1052262400, + "oid": 403, + "conn_status": 1, + "beta": -1, + "dev_status": 1, + "platform_str": "FortiGate-VM64", + "last_checked": 1550698141, + "vm_mem_limit": 6144, + "mgmt.__data[0]": 3870643, + "name": "FGT1", + "tab_status": "", + "patch": 4, + "vm_cpu_limit": 4, + "vm_status": 3, + "ips_ver": "6.00741(2015-12-01 02:30)", + "flags": 2097169, + "sn": "FGVM04TM18000391", + "mr": 0, + "os_type": 0, + "vm_cpu": 1 + } + }, + "datagram_sent": { + "device": { + "adm_pass": "fortinet", + "name": "FGT1", + "ip": "10.7.220.151", + "flags": 24, + "sn": null, + "mgmt_mode": "fmgfaz", + "adm_usr": "admin" + }, + "flags": [ + "create_task", + "nonblocking" + ], + "odd_request_form": "True", + "adom": "ansible" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.151", + "device_unique_name": "FGT1", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "exec" + }, + { + "url": "/dvm/cmd/add/device/", + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.152", + "device_unique_name": "FGT2", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "datagram_sent": { + "device": { + "adm_pass": "fortinet", + "name": "FGT2", + "ip": "10.7.220.152", + "flags": 24, + "sn": null, + "mgmt_mode": "fmgfaz", + "adm_usr": "admin" + }, + "flags": [ + "create_task", + "nonblocking" + ], + "odd_request_form": "True", + "adom": "ansible" + }, + "raw_response": { + "device": { + "adm_pass": "fortinet", + "ip": "10.7.220.152", + "mgmt.__data[6]": 1, + "vm_mem": 1003, + "maxvdom": 10, + "conn_mode": 1, + "vm_cpu_limit": 4, + "vm_cpu": 1, + "branch_pt": 231, + "hostname": "ansible-fgt02", + "source": 1, + "mgmt_id": 1879100317, + "version": 600, + "build": 231, + "mgmt_mode": 3, + "adm_usr": "admin", + "av_ver": "1.00000(2018-04-09 18:07)", + "oid": 415, + "conn_status": 1, + "beta": -1, + "dev_status": 1, + "platform_str": "FortiGate-VM64", + "last_checked": 1550698177, + "patch": 4, + "vm_mem_limit": 6144, + "mgmt.__data[0]": 3870643, + "name": "FGT2", + "tab_status": "", + "mgmt.__data[4]": 1052262400, + "platform_id": 112, + "vm_status": 3, + "ips_ver": "6.00741(2015-12-01 02:30)", + "flags": 2097169, + "sn": "FGVM04TM18000392", + "mr": 0, + "os_type": 0, + "os_ver": 6 + } + }, + "post_method": "exec" + }, + { + "url": "/dvm/cmd/add/device/", + "raw_response": { + "device": { + "adm_pass": "fortinet", + "os_ver": 6, + "ip": "10.7.220.153", + "mgmt.__data[6]": 1, + "vm_mem": 1003, + "maxvdom": 10, + "conn_mode": 1, + "platform_id": 112, + "branch_pt": 231, + "hostname": "ansible-fgt03", + "source": 1, + "mgmt_id": 104863251, + "version": 600, + "build": 231, + "mgmt_mode": 3, + "adm_usr": "admin", + "av_ver": "1.00000(2018-04-09 18:07)", + "mgmt.__data[4]": 1052262400, + "oid": 427, + "conn_status": 1, + "beta": -1, + "dev_status": 1, + "platform_str": "FortiGate-VM64", + "last_checked": 1550698204, + "vm_mem_limit": 6144, + "mgmt.__data[0]": 3870643, + "name": "FGT3", + "tab_status": "", + "patch": 4, + "vm_cpu_limit": 4, + "vm_status": 3, + "ips_ver": "6.00741(2015-12-01 02:30)", + "flags": 2097169, + "sn": "FGVM04TM18000393", + "mr": 0, + "os_type": 0, + "vm_cpu": 1 + } + }, + "datagram_sent": { + "device": { + "adm_pass": "fortinet", + "name": "FGT3", + "ip": "10.7.220.153", + "flags": 24, + "sn": null, + "mgmt_mode": "fmgfaz", + "adm_usr": "admin" + }, + "flags": [ + "create_task", + "nonblocking" + ], + "odd_request_form": "True", + "adom": "ansible" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.153", + "device_unique_name": "FGT3", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "exec" + } + ], + "discover_device": [ + { + "url": "/dvm/cmd/discover/device/", + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.151", + "device_unique_name": "FGT1", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "datagram_sent": { + "device": { + "adm_pass": "fortinet", + "ip": "10.7.220.151", + "adm_usr": "admin" + }, + "odd_request_form": "True" + }, + "raw_response": { + "device": { + "adm_pass": "fortinet", + "ip": "10.7.220.151", + "vm_mem": 1003, + "maxvdom": 10, + "conn_mode": 1, + "vm_cpu_limit": 4, + "vm_cpu": 1, + "branch_pt": 231, + "hostname": "ansible-fgt01", + "source": 1, + "version": 600, + "build": 231, + "adm_usr": "admin", + "av_ver": "1.00000(2018-04-09 18:07)", + "conn_status": 1, + "beta": -1, + "dev_status": 1, + "platform_str": "FortiGate-VM64", + "last_checked": 1550698136, + "vm_mem_limit": 6144, + "name": "ansible-fgt01", + "tab_status": "", + "patch": 4, + "platform_id": 112, + "vm_status": 3, + "ips_ver": "6.00741(2015-12-01 02:30)", + "flags": 2097153, + "sn": "FGVM04TM18000391", + "mr": 0, + "os_type": 0, + "os_ver": 6 + } + }, + "post_method": "exec" + }, + { + "url": "/dvm/cmd/discover/device/", + "raw_response": { + "device": { + "adm_pass": "fortinet", + "os_ver": 6, + "ip": "10.7.220.152", + "vm_mem": 1003, + "maxvdom": 10, + "conn_mode": 1, + "platform_id": 112, + "branch_pt": 231, + "hostname": "ansible-fgt02", + "source": 1, + "version": 600, + "build": 231, + "adm_usr": "admin", + "av_ver": "1.00000(2018-04-09 18:07)", + "conn_status": 1, + "beta": -1, + "dev_status": 1, + "platform_str": "FortiGate-VM64", + "last_checked": 1550698173, + "vm_mem_limit": 6144, + "name": "ansible-fgt02", + "tab_status": "", + "patch": 4, + "vm_cpu_limit": 4, + "vm_status": 3, + "ips_ver": "6.00741(2015-12-01 02:30)", + "flags": 2097153, + "sn": "FGVM04TM18000392", + "mr": 0, + "os_type": 0, + "vm_cpu": 1 + } + }, + "datagram_sent": { + "device": { + "adm_pass": "fortinet", + "ip": "10.7.220.152", + "adm_usr": "admin" + }, + "odd_request_form": "True" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.152", + "device_unique_name": "FGT2", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "exec" + }, + { + "url": "/dvm/cmd/discover/device/", + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.153", + "device_unique_name": "FGT3", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "datagram_sent": { + "device": { + "adm_pass": "fortinet", + "ip": "10.7.220.153", + "adm_usr": "admin" + }, + "odd_request_form": "True" + }, + "raw_response": { + "device": { + "adm_pass": "fortinet", + "ip": "10.7.220.153", + "vm_mem": 1003, + "maxvdom": 10, + "conn_mode": 1, + "vm_cpu_limit": 4, + "vm_cpu": 1, + "branch_pt": 231, + "hostname": "ansible-fgt03", + "source": 1, + "version": 600, + "build": 231, + "adm_usr": "admin", + "av_ver": "1.00000(2018-04-09 18:07)", + "conn_status": 1, + "beta": -1, + "dev_status": 1, + "platform_str": "FortiGate-VM64", + "last_checked": 1550698200, + "vm_mem_limit": 6144, + "name": "ansible-fgt03", + "tab_status": "", + "patch": 4, + "platform_id": 112, + "vm_status": 3, + "ips_ver": "6.00741(2015-12-01 02:30)", + "flags": 2097153, + "sn": "FGVM04TM18000393", + "mr": 0, + "os_type": 0, + "os_ver": 6 + } + }, + "post_method": "exec" + } + ], + "get_device": [ + { + "url": "/dvmdb/adom/ansible/device/FGT1", + "raw_response": { + "adm_pass": [ + "ENC", + "tUEPOPpQM6XsNwOPcWyrWoPoKo2DMjtFqOYEzLfF+99FpTkDmKa+GTmwBMLV1ns0OYrNgWnk6RPbRjSZSvu2LPYvCcWfQONLEZ1HlczZ00kEtDRCvRxG6l7FGtcj1Pl7QO9khy2lKWx4/lbPmLNqCzwCmlkAO5fGXR3nCbWPXH5BrRwO" + ], + "faz.perm": 0, + "foslic_ram": 0, + "foslic_type": "temporary", + "last_checked": 1550635232, + "psk": "", + "opts": 0, + "ip": "10.7.220.151", + "foslic_utm": null, + "logdisk_size": 30235, + "mgmt.__data[6]": 1, + "foslic_last_sync": 0, + "app_ver": "", + "ips_ext": 0, + "vm_mem": 1003, + "mgmt.__data[4]": 1052262400, + "maxvdom": 10, + "conn_mode": "passive", + "location_from": "GUI(10.0.0.151)", + "mgmt.__data[1]": 0, + "mgmt.__data[2]": 0, + "faz.full_act": 0, + "os_ver": "6.0", + "node_flags": 0, + "hostname": "ansible-fgt01", + "mgmt.__data[5]": 0, + "mgmt_id": 2076985412, + "hw_rev_minor": 0, + "mgmt_if": "port1", + "source": "faz", + "ha_mode": "standalone", + "version": 600, + "build": 231, + "latitude": "47.473991", + "foslic_cpu": 0, + "last_resync": 1550634702, + "desc": "", + "adm_usr": "admin", + "vm_lic_expire": 0, + "ha_slave": null, + "av_ver": "1.00000(2018-04-09 18:07)", + "fsw_cnt": 0, + "tunnel_cookie": "", + "foslic_inst_time": 0, + "lic_flags": 0, + "checksum": "89 1f b7 b7 2a a6 af 54 c5 a5 aa e3 32 92 c7 55", + "oid": 366, + "conn_status": "up", + "fex_cnt": 0, + "mgmt.__data[3]": 0, + "beta": -1, + "ha_group_name": "", + "dev_status": "installed", + "platform_str": "FortiGate-VM64", + "mgmt.__data[7]": 0, + "faz.used": 0, + "fap_cnt": 0, + "foslic_dr_site": "disable", + "mgmt_mode": "fmgfaz", + "vdom": [ + { + "status": null, + "oid": 3, + "name": "root", + "node_flags": 0, + "devid": "FGT1", + "tab_status": null, + "comments": "", + "flags": null, + "opmode": "nat", + "ext_flags": 1, + "rtm_prof_id": 0 + } + ], + "hdisk_size": 30720, + "vm_mem_limit": 6144, + "mgmt.__data[0]": 3870643, + "ha_group_id": 0, + "name": "FGT1", + "faz.quota": 0, + "mgt_vdom": "root", + "tab_status": "", + "tunnel_ip": "169.254.0.5", + "longitude": "-122.260963", + "patch": 4, + "vm_cpu_limit": 4, + "vm_status": 3, + "lic_region": "", + "hw_rev_major": 0, + "flags": [ + "has_hdd", + "reload" + ], + "sn": "FGVM04TM18000391", + "mr": 0, + "conf_status": "insync", + "os_type": "fos", + "ips_ver": "6.00741(2015-12-01 02:30)", + "db_status": "nomod", + "branch_pt": 231, + "vm_cpu": 1 + }, + "datagram_sent": { + "filter": [ + "name", + "==", + "FGT1" + ], + "adom": "ansible" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.151", + "device_unique_name": "FGT1", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "get" + }, + { + "url": "/dvmdb/adom/ansible/device/FGT2", + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.152", + "device_unique_name": "FGT2", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "datagram_sent": { + "filter": [ + "name", + "==", + "FGT2" + ], + "adom": "ansible" + }, + "raw_response": { + "adm_pass": [ + "ENC", + "F27zJSIl5O8O5rlXIi7BzHIUO5d3ZAuNxoniR42zOxGHyqZCx1OyA81b7v6dNwE30nBhjqfD+IDRmSPEW6qxKIQ2UV5eh8zgDNj8i5lj5gTvbLN5A4BR4CMLQo7nYTTomHUJQrGPfYskuxm74JGik+di9TrqOhvpZL8d1zj3XHx5pq+d" + ], + "faz.perm": 0, + "hostname": "ansible-fgt02", + "foslic_type": "temporary", + "mgmt.__data[7]": 0, + "av_ver": "1.00000(2018-04-09 18:07)", + "ip": "10.7.220.152", + "foslic_utm": null, + "logdisk_size": 30235, + "mgmt.__data[6]": 1, + "fsw_cnt": 0, + "app_ver": "", + "ips_ext": 0, + "vm_mem": 1003, + "maxvdom": 10, + "conn_mode": "passive", + "mgt_vdom": "root", + "mgmt.__data[1]": 0, + "hw_rev_major": 0, + "name": "FGT2", + "node_flags": 0, + "foslic_ram": 0, + "mgmt.__data[5]": 0, + "ha_mode": "standalone", + "hw_rev_minor": 0, + "mgmt_if": "port1", + "source": "faz", + "mgmt_id": 1555154046, + "version": 600, + "build": 231, + "latitude": "47.473991", + "foslic_cpu": 0, + "last_resync": 1550634728, + "hdisk_size": 30720, + "adm_usr": "admin", + "vm_lic_expire": 0, + "sn": "FGVM04TM18000392", + "ha_slave": null, + "psk": "", + "foslic_last_sync": 0, + "tunnel_cookie": "", + "vm_mem_limit": 6144, + "mr": 0, + "lic_flags": 0, + "oid": 378, + "conn_status": "up", + "fex_cnt": 0, + "vm_cpu": 1, + "beta": -1, + "ha_group_name": "", + "dev_status": "retrieved", + "platform_str": "FortiGate-VM64", + "last_checked": 1550634728, + "branch_pt": 231, + "faz.used": 0, + "patch": 4, + "fap_cnt": 0, + "foslic_dr_site": "disable", + "mgmt_mode": "fmgfaz", + "vdom": [ + { + "status": null, + "oid": 3, + "name": "root", + "node_flags": 4, + "devid": "FGT2", + "tab_status": null, + "comments": "", + "flags": null, + "opmode": "nat", + "ext_flags": 1, + "rtm_prof_id": 0 + } + ], + "desc": "", + "foslic_inst_time": 0, + "mgmt.__data[0]": 3870643, + "ha_group_id": 0, + "location_from": "GUI(10.0.0.151)", + "faz.quota": 0, + "faz.full_act": 0, + "tab_status": "", + "tunnel_ip": "169.254.0.3", + "longitude": "-122.260963", + "mgmt.__data[4]": 1052262400, + "vm_cpu_limit": 4, + "vm_status": 3, + "lic_region": "", + "mgmt.__data[2]": 0, + "flags": [ + "has_hdd", + "reload" + ], + "opts": 0, + "checksum": "56 e9 a7 14 e2 61 05 f9 ec 2b 00 1e 36 bc af c8", + "conf_status": "insync", + "os_type": "fos", + "ips_ver": "6.00741(2015-12-01 02:30)", + "db_status": "mod", + "mgmt.__data[3]": 0, + "os_ver": "6.0" + }, + "post_method": "get" + }, + { + "url": "/dvmdb/adom/ansible/device/FGT3", + "raw_response": { + "adm_pass": [ + "ENC", + "F27zJSIl5O8O5rlXIi7BzHIUO5d3ZAuNxoniR42zOxGHyqZCx1OyA81b7v6dNwE30nBhjqfD+IDRmSPEW6qxKIQ2UV5eh8zgDNj8i5lj5gTvbLN5A4BR4CMLQo7nYTTomHUJQrGPfYskuxm74JGik+di9TrqOhvpZL8d1zj3XHx5pq+d" + ], + "faz.perm": 0, + "foslic_ram": 0, + "foslic_type": "temporary", + "last_checked": 1550634754, + "psk": "", + "opts": 0, + "ip": "10.7.220.153", + "foslic_utm": null, + "logdisk_size": 30235, + "mgmt.__data[6]": 1, + "foslic_last_sync": 0, + "app_ver": "", + "ips_ext": 0, + "vm_mem": 1003, + "mgmt.__data[4]": 1052262400, + "desc": "", + "maxvdom": 10, + "conn_mode": "passive", + "location_from": "GUI(10.0.0.151)", + "mgmt.__data[1]": 0, + "os_ver": "6.0", + "faz.full_act": 0, + "node_flags": 0, + "hostname": "ansible-fgt03", + "mgmt.__data[5]": 0, + "mgmt_id": 1175062219, + "hw_rev_minor": 0, + "mgmt_if": "port1", + "source": "faz", + "ha_mode": "standalone", + "version": 600, + "build": 231, + "latitude": "47.473991", + "foslic_cpu": 0, + "last_resync": 1550634754, + "hdisk_size": 30720, + "adm_usr": "admin", + "vm_lic_expire": 0, + "conf_status": "insync", + "ha_slave": null, + "av_ver": "1.00000(2018-04-09 18:07)", + "fsw_cnt": 0, + "tunnel_cookie": "", + "foslic_inst_time": 0, + "lic_flags": 0, + "oid": 390, + "conn_status": "up", + "fex_cnt": 0, + "mgmt.__data[3]": 0, + "beta": -1, + "ha_group_name": "", + "dev_status": "retrieved", + "platform_str": "FortiGate-VM64", + "mgmt.__data[7]": 0, + "faz.used": 0, + "fap_cnt": 0, + "foslic_dr_site": "disable", + "mgmt_mode": "fmgfaz", + "vdom": [ + { + "status": null, + "oid": 3, + "name": "root", + "node_flags": 4, + "devid": "FGT3", + "tab_status": null, + "comments": "", + "flags": null, + "opmode": "nat", + "ext_flags": 1, + "rtm_prof_id": 0 + } + ], + "name": "FGT3", + "vm_mem_limit": 6144, + "mgmt.__data[0]": 3870643, + "ha_group_id": 0, + "mgmt.__data[2]": 0, + "faz.quota": 0, + "checksum": "30 fc af f5 58 e4 1e 2d 46 c0 07 4b b6 4b c2 1b", + "tab_status": "", + "tunnel_ip": "169.254.0.4", + "longitude": "-122.260963", + "patch": 4, + "vm_cpu_limit": 4, + "vm_status": 3, + "lic_region": "", + "mgt_vdom": "root", + "flags": [ + "has_hdd", + "reload" + ], + "sn": "FGVM04TM18000393", + "mr": 0, + "hw_rev_major": 0, + "os_type": "fos", + "ips_ver": "6.00741(2015-12-01 02:30)", + "db_status": "mod", + "branch_pt": 231, + "vm_cpu": 1 + }, + "datagram_sent": { + "filter": [ + "name", + "==", + "FGT3" + ], + "adom": "ansible" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.153", + "device_unique_name": "FGT3", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "get" + }, + { + "raw_response": { + "status": { + "message": "Object does not exist", + "code": -3 + }, + "url": "/dvmdb/adom/ansible/device/FGT1" + }, + "datagram_sent": { + "filter": [ + "name", + "==", + "FGT1" + ], + "adom": "ansible" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.151", + "device_unique_name": "FGT1", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "get" + }, + { + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.152", + "device_unique_name": "FGT2", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "datagram_sent": { + "filter": [ + "name", + "==", + "FGT2" + ], + "adom": "ansible" + }, + "raw_response": { + "status": { + "message": "Object does not exist", + "code": -3 + }, + "url": "/dvmdb/adom/ansible/device/FGT2" + }, + "post_method": "get" + }, + { + "raw_response": { + "status": { + "message": "Object does not exist", + "code": -3 + }, + "url": "/dvmdb/adom/ansible/device/FGT3" + }, + "datagram_sent": { + "filter": [ + "name", + "==", + "FGT3" + ], + "adom": "ansible" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.153", + "device_unique_name": "FGT3", + "mode": "add", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "get" + } + ], + "delete_device": [ + { + "paramgram_used": { + "device_username": "admin", + "adom": "root", + "device_ip": "10.7.220.151", + "device_unique_name": "FGT1", + "mode": "delete", + "device_serial": null, + "device_password": "fortinet" + }, + "datagram_sent": { + "device": "FGT1", + "flags": [ + "create_task", + "nonblocking" + ], + "adom": "root" + }, + "raw_response": { + "status": { + "message": "OK", + "code": 0 + }, + "url": "/dvm/cmd/del/device/" + }, + "post_method": "exec" + }, + { + "raw_response": { + "status": { + "message": "OK", + "code": 0 + }, + "url": "/dvm/cmd/del/device/" + }, + "datagram_sent": { + "device": "FGT2", + "flags": [ + "create_task", + "nonblocking" + ], + "adom": "ansible" + }, + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.152", + "device_unique_name": "FGT2", + "mode": "delete", + "device_serial": null, + "device_password": "fortinet" + }, + "post_method": "exec" + }, + { + "paramgram_used": { + "device_username": "admin", + "adom": "ansible", + "device_ip": "10.7.220.153", + "device_unique_name": "FGT3", + "mode": "delete", + "device_serial": null, + "device_password": "fortinet" + }, + "datagram_sent": { + "device": "FGT3", + "flags": [ + "create_task", + "nonblocking" + ], + "adom": "ansible" + }, + "raw_response": { + "status": { + "message": "OK", + "code": 0 + }, + "url": "/dvm/cmd/del/device/" + }, + "post_method": "exec" + } + ] } diff --git a/test/units/modules/network/fortimanager/test_fmgr_device.py b/test/units/modules/network/fortimanager/test_fmgr_device.py index 01fb6b8b4b6..64f9b3765c0 100644 --- a/test/units/modules/network/fortimanager/test_fmgr_device.py +++ b/test/units/modules/network/fortimanager/test_fmgr_device.py @@ -1,19 +1,17 @@ -# (c) 2016 Red Hat Inc. +# Copyright 2018 Fortinet, Inc. # -# This file is part of Ansible -# -# Ansible is free software: you can redistribute it and/or modify +# This program 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, +# This program 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 . +# along with Ansible. If not, see . # Make coding more python3-ish from __future__ import (absolute_import, division, print_function) @@ -21,17 +19,13 @@ __metaclass__ = type import os import json -from pyFMG.fortimgr import FortiManager +from ansible.module_utils.network.fortimanager.fortimanager import FortiManagerHandler import pytest try: from ansible.modules.network.fortimanager import fmgr_device except ImportError: - pytest.skip( - "Could not load required modules for testing", - allow_module_level=True) - -fmg_instance = FortiManager("1.1.1.1", "admin", "") + pytest.skip("Could not load required modules for testing", allow_module_level=True) def load_fixtures(): @@ -45,277 +39,234 @@ def load_fixtures(): return [fixture_data] +@pytest.fixture(autouse=True) +def module_mock(mocker): + connection_class_mock = mocker.patch('ansible.module_utils.basic.AnsibleModule') + return connection_class_mock + + +@pytest.fixture(autouse=True) +def connection_mock(mocker): + connection_class_mock = mocker.patch('ansible.modules.network.fortimanager.fmgr_device.Connection') + return connection_class_mock + + @pytest.fixture(scope="function", params=load_fixtures()) def fixture_data(request): func_name = request.function.__name__.replace("test_", "") return request.param.get(func_name, None) +fmg_instance = FortiManagerHandler(connection_mock, module_mock) + + def test_discover_device(fixture_data, mocker): - mocker.patch( - "pyFMG.fortimgr.FortiManager._post_request", - side_effect=fixture_data) - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.151', 'state': 'present', - 'device_unique_name': 'FGT1', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.discover_device(fmg_instance, paramgram_used) - # + mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request", + side_effect=fixture_data) + # Fixture sets used:########################### + + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.151 - # state: present # device_unique_name: FGT1 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # - assert isinstance(output['raw_response'], dict) is True - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.152', 'state': 'present', - 'device_unique_name': 'FGT2', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.discover_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.152 - # state: present # device_unique_name: FGT2 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # - assert isinstance(output['raw_response'], dict) is True - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.153', 'state': 'present', - 'device_unique_name': 'FGT3', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.discover_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.153 - # state: present # device_unique_name: FGT3 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # - assert output['raw_response']['status']['code'] == -20042 - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.151', 'state': 'present', - 'device_unique_name': 'FGT1', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.discover_device(fmg_instance, paramgram_used) - # + ################################################## + + # Test using fixture 1 # + output = fmgr_device.discover_device(fmg_instance, fixture_data[0]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + # Test using fixture 2 # + output = fmgr_device.discover_device(fmg_instance, fixture_data[1]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + # Test using fixture 3 # + output = fmgr_device.discover_device(fmg_instance, fixture_data[2]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + + +def test_add_device(fixture_data, mocker): + mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request", + side_effect=fixture_data) + # Fixture sets used:########################### + + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.151 - # state: present # device_unique_name: FGT1 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # - assert isinstance(output['raw_response'], dict) is True - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.152', 'state': 'present', - 'device_unique_name': 'FGT2', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.discover_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.152 - # state: present # device_unique_name: FGT2 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # - assert isinstance(output['raw_response'], dict) is True - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.153', 'state': 'present', - 'device_unique_name': 'FGT3', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.discover_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.153 - # state: present # device_unique_name: FGT3 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # + ################################################## + + # Test using fixture 1 # + output = fmgr_device.add_device(fmg_instance, fixture_data[0]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + # Test using fixture 2 # + output = fmgr_device.add_device(fmg_instance, fixture_data[1]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + # Test using fixture 3 # + output = fmgr_device.add_device(fmg_instance, fixture_data[2]['paramgram_used']) assert isinstance(output['raw_response'], dict) is True -def test_add_device(fixture_data, mocker): - mocker.patch( - "pyFMG.fortimgr.FortiManager._post_request", - side_effect=fixture_data) - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.151', 'state': 'present', - 'device_unique_name': 'FGT1', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.add_device(fmg_instance, paramgram_used) - # +def test_delete_device(fixture_data, mocker): + mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request", + side_effect=fixture_data) + # Fixture sets used:########################### + + ################################################## # device_username: admin - # adom: ansible + # adom: root # device_ip: 10.7.220.151 - # state: present # device_unique_name: FGT1 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # - assert isinstance(output['raw_response'], dict) is True - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.152', 'state': 'present', - 'device_unique_name': 'FGT2', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.add_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.152 - # state: present # device_unique_name: FGT2 + # mode: exec # device_serial: None # device_password: fortinet - # mode: execute - # - assert isinstance(output['raw_response'], dict) is True - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.151', 'state': 'present', - 'device_unique_name': 'FGT1', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.add_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## + # device_username: admin + # adom: ansible + # device_ip: 10.7.220.153 + # device_unique_name: FGT3 + # mode: exec + # device_serial: None + # device_password: fortinet + ################################################## + + # Test using fixture 1 # + output = fmgr_device.delete_device(fmg_instance, fixture_data[0]['paramgram_used']) + assert output['raw_response']['status']['code'] == 0 + # Test using fixture 2 # + output = fmgr_device.delete_device(fmg_instance, fixture_data[1]['paramgram_used']) + assert output['raw_response']['status']['code'] == 0 + # Test using fixture 3 # + output = fmgr_device.delete_device(fmg_instance, fixture_data[2]['paramgram_used']) + assert output['raw_response']['status']['code'] == 0 + + +def test_get_device(fixture_data, mocker): + mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request", + side_effect=fixture_data) + # Fixture sets used:########################### + + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.151 - # state: present # device_unique_name: FGT1 + # mode: get # device_serial: None # device_password: fortinet - # mode: execute - # - assert output['raw_response']['status']['code'] == -20010 - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.152', 'state': 'present', - 'device_unique_name': 'FGT2', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.add_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.152 - # state: present # device_unique_name: FGT2 + # mode: get # device_serial: None # device_password: fortinet - # mode: execute - # - assert output['raw_response']['status']['code'] == -20010 - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.153', 'state': 'present', - 'device_unique_name': 'FGT3', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.add_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.153 - # state: present # device_unique_name: FGT3 + # mode: get # device_serial: None # device_password: fortinet - # mode: execute - # - assert isinstance(output['raw_response'], dict) is True - - -def test_delete_device(fixture_data, mocker): - mocker.patch( - "pyFMG.fortimgr.FortiManager._post_request", - side_effect=fixture_data) - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.151', 'state': 'absent', - 'device_unique_name': 'FGT1', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.delete_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.151 - # state: absent # device_unique_name: FGT1 + # mode: get # device_serial: None # device_password: fortinet - # mode: execute - # - assert output['raw_response']['status']['code'] == 0 - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.152', 'state': 'absent', - 'device_unique_name': 'FGT2', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.delete_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.152 - # state: absent # device_unique_name: FGT2 + # mode: get # device_serial: None # device_password: fortinet - # mode: execute - # - assert output['raw_response']['status']['code'] == 0 - paramgram_used = { - 'device_username': 'admin', 'adom': 'ansible', - 'device_ip': '10.7.220.153', 'state': 'absent', - 'device_unique_name': 'FGT3', 'device_serial': - None, 'device_password': 'fortinet', - 'mode': 'execute'} - output = fmgr_device.delete_device(fmg_instance, paramgram_used) - # + ################################################## + ################################################## # device_username: admin # adom: ansible # device_ip: 10.7.220.153 - # state: absent # device_unique_name: FGT3 + # mode: get # device_serial: None # device_password: fortinet - # mode: execute - # - assert output['raw_response']['status']['code'] == 0 + ################################################## + + # Test using fixture 1 # + output = fmgr_device.get_device(fmg_instance, fixture_data[0]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + # Test using fixture 2 # + output = fmgr_device.get_device(fmg_instance, fixture_data[1]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + # Test using fixture 3 # + output = fmgr_device.get_device(fmg_instance, fixture_data[2]['paramgram_used']) + assert isinstance(output['raw_response'], dict) is True + # Test using fixture 4 # + output = fmgr_device.get_device(fmg_instance, fixture_data[3]['paramgram_used']) + assert output['raw_response']['status']['code'] == -3 + # Test using fixture 5 # + output = fmgr_device.get_device(fmg_instance, fixture_data[4]['paramgram_used']) + assert output['raw_response']['status']['code'] == -3 + # Test using fixture 6 # + output = fmgr_device.get_device(fmg_instance, fixture_data[5]['paramgram_used']) + assert output['raw_response']['status']['code'] == -3