mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-06-28 03:00:23 -07:00
FortiManager Plugin Module Conversion: fmgr_device (#52767)
* Auto Commit for: fmgr_device * Auto Commit for: fmgr_device
This commit is contained in:
parent
a8f1ad8602
commit
785c567f72
3 changed files with 1221 additions and 852 deletions
|
@ -17,6 +17,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
|
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
ANSIBLE_METADATA = {
|
ANSIBLE_METADATA = {
|
||||||
|
@ -29,11 +30,13 @@ DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: fmgr_device
|
module: fmgr_device
|
||||||
version_added: "2.8"
|
version_added: "2.8"
|
||||||
|
notes:
|
||||||
|
- Full Documentation at U(https://ftnt-ansible-docs.readthedocs.io/en/latest/).
|
||||||
author:
|
author:
|
||||||
- Luke Weighall (@lweighall)
|
- Luke Weighall (@lweighall)
|
||||||
- Andrew Welsh (@Ghilli3)
|
- Andrew Welsh (@Ghilli3)
|
||||||
- Jim Huber (@p4r4n0y1ng)
|
- Jim Huber (@p4r4n0y1ng)
|
||||||
short_description: Add or remove device
|
short_description: Add or remove device from FortiManager.
|
||||||
description:
|
description:
|
||||||
- Add or remove a device or list of devices from FortiManager Device Manager using JSON RPC API.
|
- 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.
|
- The ADOM the configuration should belong to.
|
||||||
required: true
|
required: true
|
||||||
default: root
|
default: root
|
||||||
host:
|
|
||||||
|
mode:
|
||||||
description:
|
description:
|
||||||
- The FortiManager's address.
|
- The desired mode of the specified object.
|
||||||
required: true
|
|
||||||
username:
|
|
||||||
description:
|
|
||||||
- The username used to authenticate with the FortiManager.
|
|
||||||
required: false
|
required: false
|
||||||
password:
|
default: add
|
||||||
|
choices: ["add", "delete"]
|
||||||
|
|
||||||
|
blind_add:
|
||||||
description:
|
description:
|
||||||
- The password associated with the username account.
|
- 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
|
required: false
|
||||||
state:
|
default: "disable"
|
||||||
description:
|
choices: ["enable", "disable"]
|
||||||
- The desired state of the specified object.
|
|
||||||
- absent will delete the object if it exists.
|
|
||||||
- present will create the configuration if needed.
|
|
||||||
required: false
|
|
||||||
default: present
|
|
||||||
choices: ["absent", "present"]
|
|
||||||
|
|
||||||
device_username:
|
device_username:
|
||||||
description:
|
description:
|
||||||
- The username of the device being added to FortiManager.
|
- The username of the device being added to FortiManager.
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
device_password:
|
device_password:
|
||||||
description:
|
description:
|
||||||
- The password of the device being added to FortiManager.
|
- The password of the device being added to FortiManager.
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
device_ip:
|
device_ip:
|
||||||
description:
|
description:
|
||||||
- The IP of the device being added to FortiManager. Supports both IPv4 and IPv6.
|
- The IP of the device being added to FortiManager. Supports both IPv4 and IPv6.
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
device_unique_name:
|
device_unique_name:
|
||||||
description:
|
description:
|
||||||
- The desired "friendly" name of the device being added to FortiManager.
|
- The desired "friendly" name of the device being added to FortiManager.
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
device_serial:
|
device_serial:
|
||||||
description:
|
description:
|
||||||
- The serial number of the device being added to FortiManager.
|
- The serial number of the device being added to FortiManager.
|
||||||
required: false
|
required: false
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
- name: DISCOVER AND ADD DEVICE FGT1
|
- name: DISCOVER AND ADD DEVICE FGT1
|
||||||
fmgr_device:
|
fmgr_device:
|
||||||
host: "{{inventory_hostname}}"
|
|
||||||
username: "{{ username }}"
|
|
||||||
password: "{{ password }}"
|
|
||||||
adom: "root"
|
adom: "root"
|
||||||
device_username: "admin"
|
device_username: "admin"
|
||||||
device_password: "admin"
|
device_password: "admin"
|
||||||
device_ip: "10.10.24.201"
|
device_ip: "10.10.24.201"
|
||||||
device_unique_name: "FGT1"
|
device_unique_name: "FGT1"
|
||||||
device_serial: "FGVM000000117994"
|
device_serial: "FGVM000000117994"
|
||||||
state: "present"
|
mode: "add"
|
||||||
|
blind_add: "enable"
|
||||||
|
|
||||||
- name: DISCOVER AND ADD DEVICE FGT2
|
- name: DISCOVER AND ADD DEVICE FGT2
|
||||||
fmgr_device:
|
fmgr_device:
|
||||||
host: "{{inventory_hostname}}"
|
|
||||||
username: "{{ username }}"
|
|
||||||
password: "{{ password }}"
|
|
||||||
adom: "root"
|
adom: "root"
|
||||||
device_username: "admin"
|
device_username: "admin"
|
||||||
device_password: "admin"
|
device_password: "admin"
|
||||||
device_ip: "10.10.24.202"
|
device_ip: "10.10.24.202"
|
||||||
device_unique_name: "FGT2"
|
device_unique_name: "FGT2"
|
||||||
device_serial: "FGVM000000117992"
|
device_serial: "FGVM000000117992"
|
||||||
state: "absent"
|
mode: "delete"
|
||||||
'''
|
'''
|
||||||
|
|
||||||
RETURN = """
|
RETURN = """
|
||||||
|
@ -122,20 +118,27 @@ api_result:
|
||||||
type: str
|
type: str
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, env_fallback
|
from ansible.module_utils.basic import AnsibleModule
|
||||||
from ansible.module_utils.network.fortimanager.fortimanager import AnsibleFortiManager
|
from ansible.module_utils.connection import Connection
|
||||||
|
from ansible.module_utils.network.fortimanager.fortimanager import FortiManagerHandler
|
||||||
# check for pyFMG lib
|
from ansible.module_utils.network.fortimanager.common import FMGBaseException
|
||||||
try:
|
from ansible.module_utils.network.fortimanager.common import FMGRCommon
|
||||||
from pyFMG.fortimgr import FortiManager
|
from ansible.module_utils.network.fortimanager.common import FMGRMethods
|
||||||
HAS_PYFMGR = True
|
from ansible.module_utils.network.fortimanager.common import DEFAULT_RESULT_OBJ
|
||||||
except ImportError:
|
from ansible.module_utils.network.fortimanager.common import FAIL_SOCKET_MSG
|
||||||
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
|
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 = {
|
datagram = {
|
||||||
|
@ -146,13 +149,22 @@ def discover_device(fmg, paramgram):
|
||||||
}
|
}
|
||||||
|
|
||||||
url = '/dvm/cmd/discover/device/'
|
url = '/dvm/cmd/discover/device/'
|
||||||
response = fmg.execute(url, datagram)
|
|
||||||
|
response = fmgr.process_request(url, datagram, FMGRMethods.EXEC)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def add_device(fmg, paramgram):
|
def add_device(fmgr, paramgram):
|
||||||
"""
|
"""
|
||||||
This method is used to add devices to the FMGR
|
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 = {
|
datagram = {
|
||||||
|
@ -165,72 +177,61 @@ def add_device(fmg, paramgram):
|
||||||
}
|
}
|
||||||
|
|
||||||
url = '/dvm/cmd/add/device/'
|
url = '/dvm/cmd/add/device/'
|
||||||
response = fmg.execute(url, datagram)
|
response = fmgr.process_request(url, datagram, FMGRMethods.EXEC)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def delete_device(fmg, paramgram):
|
def delete_device(fmgr, paramgram):
|
||||||
"""
|
"""
|
||||||
This method deletes a device from the FMGR
|
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 = {
|
datagram = {
|
||||||
"adom": paramgram["adom"],
|
"adom": paramgram["adom"],
|
||||||
"flags": ["create_task", "nonblocking"],
|
"flags": ["create_task", "nonblocking"],
|
||||||
"odd_request_form": "True",
|
|
||||||
"device": paramgram["device_unique_name"],
|
"device": paramgram["device_unique_name"],
|
||||||
}
|
}
|
||||||
|
|
||||||
url = '/dvm/cmd/del/device/'
|
url = '/dvm/cmd/del/device/'
|
||||||
response = fmg.execute(url, datagram)
|
response = fmgr.process_request(url, datagram, FMGRMethods.EXEC)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
# FUNCTION/METHOD FOR LOGGING OUT AND ANALYZING ERROR CODES
|
def get_device(fmgr, paramgram):
|
||||||
def fmgr_logout(fmg, module, msg="NULL", results=(), good_codes=(0,), logout_on_fail=True, logout_on_success=False):
|
|
||||||
"""
|
"""
|
||||||
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)
|
url = '/dvmdb/adom/{adom}/device/{name}'.format(adom=paramgram["adom"],
|
||||||
if msg != "NULL" and len(results) == 0:
|
name=paramgram["device_unique_name"])
|
||||||
try:
|
response = fmgr.process_request(url, datagram, FMGRMethods.GET)
|
||||||
fmg.logout()
|
return response
|
||||||
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
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
adom=dict(required=False, type="str", default="root"),
|
adom=dict(required=False, type="str", default="root"),
|
||||||
host=dict(required=True, type="str"),
|
mode=dict(choices=["add", "delete"], type="str", default="add"),
|
||||||
username=dict(fallback=(env_fallback, ["ANSIBLE_NET_USERNAME"])),
|
blind_add=dict(choices=["enable", "disable"], type="str", default="disable"),
|
||||||
password=dict(fallback=(env_fallback, ["ANSIBLE_NET_PASSWORD"]), no_log=True),
|
|
||||||
state=dict(choices=["absent", "present"], type="str", default="present"),
|
|
||||||
|
|
||||||
device_ip=dict(required=False, type="str"),
|
device_ip=dict(required=False, type="str"),
|
||||||
device_username=dict(required=False, type="str"),
|
device_username=dict(required=False, type="str"),
|
||||||
device_password=dict(required=False, type="str", no_log=True),
|
device_password=dict(required=False, type="str", no_log=True),
|
||||||
|
@ -238,9 +239,10 @@ def main():
|
||||||
device_serial=dict(required=False, type="str")
|
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 = {
|
paramgram = {
|
||||||
"device_ip": module.params["device_ip"],
|
"device_ip": module.params["device_ip"],
|
||||||
"device_username": module.params["device_username"],
|
"device_username": module.params["device_username"],
|
||||||
|
@ -248,44 +250,52 @@ def main():
|
||||||
"device_unique_name": module.params["device_unique_name"],
|
"device_unique_name": module.params["device_unique_name"],
|
||||||
"device_serial": module.params["device_serial"],
|
"device_serial": module.params["device_serial"],
|
||||||
"adom": module.params["adom"],
|
"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
|
# INSERT THE PARAMGRAM INTO THE MODULE SO WHEN WE PASS IT TO MOD_UTILS.FortiManagerHandler IT HAS THAT INFO
|
||||||
# check if params are set
|
module.paramgram = paramgram
|
||||||
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
|
# TRY TO INIT THE CONNECTION SOCKET PATH AND FortiManagerHandler OBJECT AND TOOLS
|
||||||
fmg = AnsibleFortiManager(module, module.params["host"], module.params["username"], module.params["password"])
|
fmgr = None
|
||||||
response = fmg.login()
|
if module._socket_path:
|
||||||
|
connection = Connection(module._socket_path)
|
||||||
if response[1]['status']['code'] != 0:
|
fmgr = FortiManagerHandler(connection, module)
|
||||||
module.fail_json(msg="Connection to FortiManager Failed")
|
fmgr.tools = FMGRCommon()
|
||||||
else:
|
else:
|
||||||
# START SESSION LOGIC
|
module.fail_json(**FAIL_SOCKET_MSG)
|
||||||
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:
|
# BEGIN MODULE-SPECIFIC LOGIC -- THINGS NEED TO HAPPEN DEPENDING ON THE ENDPOINT AND OPERATION
|
||||||
results = add_device(fmg, paramgram)
|
results = DEFAULT_RESULT_OBJ
|
||||||
if results[0] != 0 and results[0] != -20010:
|
try:
|
||||||
fmgr_logout(fmg, module, msg="Adding Device Failed", results=results, good_codes=[0])
|
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))
|
||||||
|
|
||||||
if paramgram["state"] == "absent":
|
discover_results = discover_device(fmgr, paramgram)
|
||||||
# remove device
|
fmgr.govern_response(module=module, results=discover_results, stop_on_success=False,
|
||||||
results = delete_device(fmg, paramgram)
|
ansible_facts=fmgr.construct_ansible_facts(discover_results,
|
||||||
if results[0] != 0:
|
module.params, paramgram))
|
||||||
fmgr_logout(fmg, module, msg="Deleting Device Failed", results=results, good_codes=[0])
|
|
||||||
|
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)
|
||||||
|
|
||||||
fmg.logout()
|
|
||||||
return module.exit_json(**results[1])
|
return module.exit_json(**results[1])
|
||||||
|
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,19 +1,17 @@
|
||||||
# (c) 2016 Red Hat Inc.
|
# Copyright 2018 Fortinet, Inc.
|
||||||
#
|
#
|
||||||
# This file is part of Ansible
|
# This program is free software: you can redistribute it and/or modify
|
||||||
#
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
# it under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (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
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
# GNU General Public License for more details.
|
# GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
# along with Ansible. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
# Make coding more python3-ish
|
# Make coding more python3-ish
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
@ -21,17 +19,13 @@ __metaclass__ = type
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
from pyFMG.fortimgr import FortiManager
|
from ansible.module_utils.network.fortimanager.fortimanager import FortiManagerHandler
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from ansible.modules.network.fortimanager import fmgr_device
|
from ansible.modules.network.fortimanager import fmgr_device
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pytest.skip(
|
pytest.skip("Could not load required modules for testing", allow_module_level=True)
|
||||||
"Could not load required modules for testing",
|
|
||||||
allow_module_level=True)
|
|
||||||
|
|
||||||
fmg_instance = FortiManager("1.1.1.1", "admin", "")
|
|
||||||
|
|
||||||
|
|
||||||
def load_fixtures():
|
def load_fixtures():
|
||||||
|
@ -45,277 +39,234 @@ def load_fixtures():
|
||||||
return [fixture_data]
|
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())
|
@pytest.fixture(scope="function", params=load_fixtures())
|
||||||
def fixture_data(request):
|
def fixture_data(request):
|
||||||
func_name = request.function.__name__.replace("test_", "")
|
func_name = request.function.__name__.replace("test_", "")
|
||||||
return request.param.get(func_name, None)
|
return request.param.get(func_name, None)
|
||||||
|
|
||||||
|
|
||||||
|
fmg_instance = FortiManagerHandler(connection_mock, module_mock)
|
||||||
|
|
||||||
|
|
||||||
def test_discover_device(fixture_data, mocker):
|
def test_discover_device(fixture_data, mocker):
|
||||||
mocker.patch(
|
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
|
||||||
"pyFMG.fortimgr.FortiManager._post_request",
|
|
||||||
side_effect=fixture_data)
|
side_effect=fixture_data)
|
||||||
paramgram_used = {
|
# Fixture sets 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)
|
|
||||||
#
|
|
||||||
# device_username: admin
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.151
|
# device_ip: 10.7.220.151
|
||||||
# state: present
|
|
||||||
# device_unique_name: FGT1
|
# device_unique_name: FGT1
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.152
|
# device_ip: 10.7.220.152
|
||||||
# state: present
|
|
||||||
# device_unique_name: FGT2
|
# device_unique_name: FGT2
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.153
|
# device_ip: 10.7.220.153
|
||||||
# state: present
|
|
||||||
# device_unique_name: FGT3
|
# device_unique_name: FGT3
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# device_password: fortinet
|
||||||
# mode: execute
|
##################################################
|
||||||
#
|
|
||||||
assert output['raw_response']['status']['code'] == -20042
|
# Test using fixture 1 #
|
||||||
paramgram_used = {
|
output = fmgr_device.discover_device(fmg_instance, fixture_data[0]['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)
|
|
||||||
#
|
|
||||||
# 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
|
|
||||||
#
|
|
||||||
assert isinstance(output['raw_response'], dict) is True
|
assert isinstance(output['raw_response'], dict) is True
|
||||||
paramgram_used = {
|
# Test using fixture 2 #
|
||||||
'device_username': 'admin', 'adom': 'ansible',
|
output = fmgr_device.discover_device(fmg_instance, fixture_data[1]['paramgram_used'])
|
||||||
'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
|
|
||||||
# device_serial: None
|
|
||||||
# device_password: fortinet
|
|
||||||
# mode: execute
|
|
||||||
#
|
|
||||||
assert isinstance(output['raw_response'], dict) is True
|
assert isinstance(output['raw_response'], dict) is True
|
||||||
paramgram_used = {
|
# Test using fixture 3 #
|
||||||
'device_username': 'admin', 'adom': 'ansible',
|
output = fmgr_device.discover_device(fmg_instance, fixture_data[2]['paramgram_used'])
|
||||||
'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
|
|
||||||
# device_serial: None
|
|
||||||
# device_password: fortinet
|
|
||||||
# mode: execute
|
|
||||||
#
|
|
||||||
assert isinstance(output['raw_response'], dict) is True
|
assert isinstance(output['raw_response'], dict) is True
|
||||||
|
|
||||||
|
|
||||||
def test_add_device(fixture_data, mocker):
|
def test_add_device(fixture_data, mocker):
|
||||||
mocker.patch(
|
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
|
||||||
"pyFMG.fortimgr.FortiManager._post_request",
|
|
||||||
side_effect=fixture_data)
|
side_effect=fixture_data)
|
||||||
paramgram_used = {
|
# Fixture sets 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.151
|
# device_ip: 10.7.220.151
|
||||||
# state: present
|
|
||||||
# device_unique_name: FGT1
|
# device_unique_name: FGT1
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.152
|
# device_ip: 10.7.220.152
|
||||||
# state: present
|
|
||||||
# device_unique_name: FGT2
|
# device_unique_name: FGT2
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# 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.151
|
|
||||||
# state: present
|
|
||||||
# device_unique_name: FGT1
|
|
||||||
# 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
|
|
||||||
# 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.153
|
# device_ip: 10.7.220.153
|
||||||
# state: present
|
|
||||||
# device_unique_name: FGT3
|
# device_unique_name: FGT3
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# 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
|
assert isinstance(output['raw_response'], dict) is True
|
||||||
|
|
||||||
|
|
||||||
def test_delete_device(fixture_data, mocker):
|
def test_delete_device(fixture_data, mocker):
|
||||||
mocker.patch(
|
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
|
||||||
"pyFMG.fortimgr.FortiManager._post_request",
|
|
||||||
side_effect=fixture_data)
|
side_effect=fixture_data)
|
||||||
paramgram_used = {
|
# Fixture sets 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: root
|
||||||
# device_ip: 10.7.220.151
|
# device_ip: 10.7.220.151
|
||||||
# state: absent
|
|
||||||
# device_unique_name: FGT1
|
# device_unique_name: FGT1
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.152
|
# device_ip: 10.7.220.152
|
||||||
# state: absent
|
|
||||||
# device_unique_name: FGT2
|
# device_unique_name: FGT2
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# 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
|
# device_username: admin
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# device_ip: 10.7.220.153
|
# device_ip: 10.7.220.153
|
||||||
# state: absent
|
|
||||||
# device_unique_name: FGT3
|
# device_unique_name: FGT3
|
||||||
|
# mode: exec
|
||||||
# device_serial: None
|
# device_serial: None
|
||||||
# device_password: fortinet
|
# device_password: fortinet
|
||||||
# mode: execute
|
##################################################
|
||||||
#
|
|
||||||
|
# Test using fixture 1 #
|
||||||
|
output = fmgr_device.delete_device(fmg_instance, fixture_data[0]['paramgram_used'])
|
||||||
assert output['raw_response']['status']['code'] == 0
|
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
|
||||||
|
# device_unique_name: FGT1
|
||||||
|
# mode: get
|
||||||
|
# device_serial: None
|
||||||
|
# device_password: fortinet
|
||||||
|
##################################################
|
||||||
|
##################################################
|
||||||
|
# device_username: admin
|
||||||
|
# adom: ansible
|
||||||
|
# device_ip: 10.7.220.152
|
||||||
|
# device_unique_name: FGT2
|
||||||
|
# mode: get
|
||||||
|
# device_serial: None
|
||||||
|
# device_password: fortinet
|
||||||
|
##################################################
|
||||||
|
##################################################
|
||||||
|
# device_username: admin
|
||||||
|
# adom: ansible
|
||||||
|
# device_ip: 10.7.220.153
|
||||||
|
# device_unique_name: FGT3
|
||||||
|
# mode: get
|
||||||
|
# device_serial: None
|
||||||
|
# device_password: fortinet
|
||||||
|
##################################################
|
||||||
|
##################################################
|
||||||
|
# device_username: admin
|
||||||
|
# adom: ansible
|
||||||
|
# device_ip: 10.7.220.151
|
||||||
|
# device_unique_name: FGT1
|
||||||
|
# mode: get
|
||||||
|
# device_serial: None
|
||||||
|
# device_password: fortinet
|
||||||
|
##################################################
|
||||||
|
##################################################
|
||||||
|
# device_username: admin
|
||||||
|
# adom: ansible
|
||||||
|
# device_ip: 10.7.220.152
|
||||||
|
# device_unique_name: FGT2
|
||||||
|
# mode: get
|
||||||
|
# device_serial: None
|
||||||
|
# device_password: fortinet
|
||||||
|
##################################################
|
||||||
|
##################################################
|
||||||
|
# device_username: admin
|
||||||
|
# adom: ansible
|
||||||
|
# device_ip: 10.7.220.153
|
||||||
|
# device_unique_name: FGT3
|
||||||
|
# mode: get
|
||||||
|
# device_serial: None
|
||||||
|
# device_password: fortinet
|
||||||
|
##################################################
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue