mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-26 22:51:23 -07:00
FortiManager Plugin Module Conversion: fmgr_device_config (#52765)
* Auto Commit for: fmgr_device_config * autopep8 changes * Auto Commit for: fmgr_device_config * Auto Commit for: fmgr_device_config
This commit is contained in:
parent
785c567f72
commit
13517fe822
3 changed files with 333 additions and 430 deletions
|
@ -29,6 +29,8 @@ DOCUMENTATION = '''
|
||||||
---
|
---
|
||||||
module: fmgr_device_config
|
module: fmgr_device_config
|
||||||
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)
|
||||||
|
@ -43,23 +45,12 @@ options:
|
||||||
- The ADOM the configuration should belong to.
|
- The ADOM the configuration should belong to.
|
||||||
required: false
|
required: false
|
||||||
default: root
|
default: root
|
||||||
host:
|
|
||||||
description:
|
|
||||||
- The FortiManager's address.
|
|
||||||
required: true
|
|
||||||
username:
|
|
||||||
description:
|
|
||||||
- The username used to authenticate with the FortiManager.
|
|
||||||
required: false
|
|
||||||
password:
|
|
||||||
description:
|
|
||||||
- The password associated with the username account.
|
|
||||||
required: false
|
|
||||||
|
|
||||||
device_unique_name:
|
device_unique_name:
|
||||||
description:
|
description:
|
||||||
- The unique device's name that you are editing. A.K.A. Friendly name of the device in FortiManager.
|
- The unique device's name that you are editing. A.K.A. Friendly name of the device in FortiManager.
|
||||||
required: True
|
required: True
|
||||||
|
|
||||||
device_hostname:
|
device_hostname:
|
||||||
description:
|
description:
|
||||||
- The device's new hostname.
|
- The device's new hostname.
|
||||||
|
@ -70,35 +61,31 @@ options:
|
||||||
- Tells FMGR to attempt to install the config after making it.
|
- Tells FMGR to attempt to install the config after making it.
|
||||||
required: false
|
required: false
|
||||||
default: disable
|
default: disable
|
||||||
|
|
||||||
interface:
|
interface:
|
||||||
description:
|
description:
|
||||||
- The interface/port number you are editing.
|
- The interface/port number you are editing.
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
interface_ip:
|
interface_ip:
|
||||||
description:
|
description:
|
||||||
- The IP and subnet of the interface/port you are editing.
|
- The IP and subnet of the interface/port you are editing.
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
interface_allow_access:
|
interface_allow_access:
|
||||||
description:
|
description:
|
||||||
- Specify what protocols are allowed on the interface, comma-sepeareted list (see examples).
|
- Specify what protocols are allowed on the interface, comma-separated list (see examples).
|
||||||
required: false
|
required: false
|
||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
EXAMPLES = '''
|
EXAMPLES = '''
|
||||||
- name: CHANGE HOSTNAME
|
- name: CHANGE HOSTNAME
|
||||||
fmgr_device_config:
|
fmgr_device_config:
|
||||||
host: "{{inventory_hostname}}"
|
|
||||||
username: "{{ username }}"
|
|
||||||
password: "{{ password }}"
|
|
||||||
device_hostname: "ChangedbyAnsible"
|
device_hostname: "ChangedbyAnsible"
|
||||||
device_unique_name: "FGT1"
|
device_unique_name: "FGT1"
|
||||||
|
|
||||||
- name: EDIT INTERFACE INFORMATION
|
- name: EDIT INTERFACE INFORMATION
|
||||||
fmgr_device_config:
|
fmgr_device_config:
|
||||||
host: "{{inventory_hostname}}"
|
|
||||||
username: "{{ username }}"
|
|
||||||
password: "{{ password }}"
|
|
||||||
adom: "root"
|
adom: "root"
|
||||||
device_unique_name: "FGT2"
|
device_unique_name: "FGT2"
|
||||||
interface: "port3"
|
interface: "port3"
|
||||||
|
@ -107,9 +94,6 @@ EXAMPLES = '''
|
||||||
|
|
||||||
- name: INSTALL CONFIG
|
- name: INSTALL CONFIG
|
||||||
fmgr_device_config:
|
fmgr_device_config:
|
||||||
host: "{{inventory_hostname}}"
|
|
||||||
username: "{{ username }}"
|
|
||||||
password: "{{ password }}"
|
|
||||||
adom: "root"
|
adom: "root"
|
||||||
device_unique_name: "FGT1"
|
device_unique_name: "FGT1"
|
||||||
install_config: "enable"
|
install_config: "enable"
|
||||||
|
@ -122,33 +106,42 @@ 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 DEFAULT_RESULT_OBJ
|
||||||
HAS_PYFMGR = True
|
from ansible.module_utils.network.fortimanager.common import FAIL_SOCKET_MSG
|
||||||
except ImportError:
|
from ansible.module_utils.network.fortimanager.common import FMGRMethods
|
||||||
HAS_PYFMGR = False
|
|
||||||
|
|
||||||
|
|
||||||
def update_device_hostname(fmg, paramgram):
|
def update_device_hostname(fmgr, paramgram):
|
||||||
"""
|
"""
|
||||||
Change a device's hostname
|
:param fmgr: The fmgr object instance from fortimanager.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 = {
|
||||||
"hostname": paramgram["device_hostname"]
|
"hostname": paramgram["device_hostname"]
|
||||||
}
|
}
|
||||||
|
|
||||||
url = "pm/config/device/{device_name}/global/system/global".format(device_name=paramgram["device_unique_name"])
|
url = "pm/config/device/{device_name}/global/system/global".format(device_name=paramgram["device_unique_name"])
|
||||||
response = fmg.update(url, datagram)
|
response = fmgr.process_request(url, datagram, FMGRMethods.UPDATE)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def update_device_interface(fmg, paramgram):
|
def update_device_interface(fmgr, paramgram):
|
||||||
"""
|
"""
|
||||||
Update a device interface IP and allow access
|
:param fmgr: The fmgr object instance from fortimanager.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
|
||||||
"""
|
"""
|
||||||
access_list = list()
|
access_list = list()
|
||||||
allow_access_list = paramgram["interface_allow_access"].replace(' ', '')
|
allow_access_list = paramgram["interface_allow_access"].replace(' ', '')
|
||||||
|
@ -161,13 +154,18 @@ def update_device_interface(fmg, paramgram):
|
||||||
|
|
||||||
url = "/pm/config/device/{device_name}/global/system/interface" \
|
url = "/pm/config/device/{device_name}/global/system/interface" \
|
||||||
"/{interface}".format(device_name=paramgram["device_unique_name"], interface=paramgram["interface"])
|
"/{interface}".format(device_name=paramgram["device_unique_name"], interface=paramgram["interface"])
|
||||||
response = fmg.update(url, datagram)
|
response = fmgr.process_request(url, datagram, FMGRMethods.UPDATE)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
|
||||||
def exec_config(fmg, paramgram):
|
def exec_config(fmgr, paramgram):
|
||||||
"""
|
"""
|
||||||
Update a device interface IP and allow access
|
:param fmgr: The fmgr object instance from fortimanager.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 = {
|
||||||
"scope": {
|
"scope": {
|
||||||
|
@ -178,55 +176,13 @@ def exec_config(fmg, paramgram):
|
||||||
}
|
}
|
||||||
|
|
||||||
url = "/securityconsole/install/device"
|
url = "/securityconsole/install/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 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
|
|
||||||
"""
|
|
||||||
|
|
||||||
# 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
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
host=dict(required=True, type="str"),
|
|
||||||
adom=dict(required=False, type="str", default="root"),
|
adom=dict(required=False, type="str", default="root"),
|
||||||
password=dict(fallback=(env_fallback, ["ANSIBLE_NET_PASSWORD"]), no_log=True),
|
|
||||||
username=dict(fallback=(env_fallback, ["ANSIBLE_NET_USERNAME"])),
|
|
||||||
|
|
||||||
device_unique_name=dict(required=True, type="str"),
|
device_unique_name=dict(required=True, type="str"),
|
||||||
device_hostname=dict(required=False, type="str"),
|
device_hostname=dict(required=False, type="str"),
|
||||||
interface=dict(required=False, type="str"),
|
interface=dict(required=False, type="str"),
|
||||||
|
@ -235,9 +191,7 @@ def main():
|
||||||
install_config=dict(required=False, type="str", default="disable"),
|
install_config=dict(required=False, type="str", default="disable"),
|
||||||
)
|
)
|
||||||
|
|
||||||
module = AnsibleModule(argument_spec, supports_check_mode=True,)
|
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 fortimanager
|
|
||||||
paramgram = {
|
paramgram = {
|
||||||
"device_unique_name": module.params["device_unique_name"],
|
"device_unique_name": module.params["device_unique_name"],
|
||||||
"device_hostname": module.params["device_hostname"],
|
"device_hostname": module.params["device_hostname"],
|
||||||
|
@ -247,40 +201,36 @@ def main():
|
||||||
"install_config": module.params["install_config"],
|
"install_config": module.params["install_config"],
|
||||||
"adom": module.params["adom"]
|
"adom": module.params["adom"]
|
||||||
}
|
}
|
||||||
|
module.paramgram = paramgram
|
||||||
# check if params are set
|
fmgr = None
|
||||||
if module.params["host"] is None or module.params["username"] is None or module.params["password"] is None:
|
if module._socket_path:
|
||||||
module.fail_json(msg="Host and username are required for connection")
|
connection = Connection(module._socket_path)
|
||||||
|
fmgr = FortiManagerHandler(connection, module)
|
||||||
# CHECK IF LOGIN FAILED
|
fmgr.tools = FMGRCommon()
|
||||||
fmg = AnsibleFortiManager(module, module.params["host"], module.params["username"], module.params["password"])
|
|
||||||
response = fmg.login()
|
|
||||||
if response[1]['status']['code'] != 0:
|
|
||||||
module.fail_json(msg="Connection to FortiManager Failed")
|
|
||||||
else:
|
else:
|
||||||
|
module.fail_json(**FAIL_SOCKET_MSG)
|
||||||
|
|
||||||
# START SESSION LOGIC
|
# BEGIN MODULE-SPECIFIC LOGIC -- THINGS NEED TO HAPPEN DEPENDING ON THE ENDPOINT AND OPERATION
|
||||||
|
results = DEFAULT_RESULT_OBJ
|
||||||
# if the device_hostname isn't null, then attempt the api call via method call, store results in variable
|
try:
|
||||||
if paramgram["device_hostname"] is not None:
|
if paramgram["device_hostname"] is not None:
|
||||||
# add device
|
results = update_device_hostname(fmgr, paramgram)
|
||||||
results = update_device_hostname(fmg, paramgram)
|
fmgr.govern_response(module=module, results=results,
|
||||||
if results[0] != 0:
|
ansible_facts=fmgr.construct_ansible_facts(results, module.params, paramgram))
|
||||||
fmgr_logout(fmg, module, msg="Failed to set Hostname", results=results, good_codes=[0])
|
|
||||||
|
|
||||||
if paramgram["interface_ip"] is not None or paramgram["interface_allow_access"] is not None:
|
if paramgram["interface_ip"] is not None or paramgram["interface_allow_access"] is not None:
|
||||||
results = update_device_interface(fmg, paramgram)
|
results = update_device_interface(fmgr, paramgram)
|
||||||
if results[0] != 0:
|
fmgr.govern_response(module=module, results=results,
|
||||||
fmgr_logout(fmg, module, msg="Failed to Update Device Interface", results=results, good_codes=[0])
|
ansible_facts=fmgr.construct_ansible_facts(results, module.params, paramgram))
|
||||||
|
|
||||||
if paramgram["install_config"] == "enable":
|
if paramgram["install_config"] == "enable":
|
||||||
# attempt to install the config
|
results = exec_config(fmgr, paramgram)
|
||||||
results = exec_config(fmg, paramgram)
|
fmgr.govern_response(module=module, results=results,
|
||||||
if results[0] != 0:
|
ansible_facts=fmgr.construct_ansible_facts(results, module.params, paramgram))
|
||||||
fmgr_logout(fmg, module, msg="Failed to Update Device Interface", results=results, good_codes=[0])
|
|
||||||
|
except Exception as err:
|
||||||
|
raise FMGBaseException(err)
|
||||||
|
|
||||||
# logout, build in check for future logging capabilities
|
|
||||||
fmg.logout()
|
|
||||||
return module.exit_json(**results[1])
|
return module.exit_json(**results[1])
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,15 @@
|
||||||
},
|
},
|
||||||
"url": "/pm/config/device/FGT1/global/system/interface/port2"
|
"url": "/pm/config/device/FGT1/global/system/interface/port2"
|
||||||
},
|
},
|
||||||
|
"datagram_sent": {
|
||||||
|
"ip": "10.1.1.1/24",
|
||||||
|
"allowaccess": [
|
||||||
|
"ping",
|
||||||
|
"telnet",
|
||||||
|
"https",
|
||||||
|
"http"
|
||||||
|
]
|
||||||
|
},
|
||||||
"paramgram_used": {
|
"paramgram_used": {
|
||||||
"adom": "ansible",
|
"adom": "ansible",
|
||||||
"install_config": "disable",
|
"install_config": "disable",
|
||||||
|
@ -27,6 +36,15 @@
|
||||||
},
|
},
|
||||||
"url": "/pm/config/device/FGT2/global/system/interface/port2"
|
"url": "/pm/config/device/FGT2/global/system/interface/port2"
|
||||||
},
|
},
|
||||||
|
"datagram_sent": {
|
||||||
|
"ip": "10.1.2.1/24",
|
||||||
|
"allowaccess": [
|
||||||
|
"ping",
|
||||||
|
"telnet",
|
||||||
|
"https",
|
||||||
|
"http"
|
||||||
|
]
|
||||||
|
},
|
||||||
"paramgram_used": {
|
"paramgram_used": {
|
||||||
"adom": "ansible",
|
"adom": "ansible",
|
||||||
"install_config": "disable",
|
"install_config": "disable",
|
||||||
|
@ -46,6 +64,15 @@
|
||||||
},
|
},
|
||||||
"url": "/pm/config/device/FGT3/global/system/interface/port2"
|
"url": "/pm/config/device/FGT3/global/system/interface/port2"
|
||||||
},
|
},
|
||||||
|
"datagram_sent": {
|
||||||
|
"ip": "10.1.3.1/24",
|
||||||
|
"allowaccess": [
|
||||||
|
"ping",
|
||||||
|
"telnet",
|
||||||
|
"https",
|
||||||
|
"http"
|
||||||
|
]
|
||||||
|
},
|
||||||
"paramgram_used": {
|
"paramgram_used": {
|
||||||
"adom": "ansible",
|
"adom": "ansible",
|
||||||
"install_config": "disable",
|
"install_config": "disable",
|
||||||
|
@ -59,25 +86,6 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"update_device_hostname": [
|
"update_device_hostname": [
|
||||||
{
|
|
||||||
"raw_response": {
|
|
||||||
"status": {
|
|
||||||
"message": "OK",
|
|
||||||
"code": 0
|
|
||||||
},
|
|
||||||
"url": "pm/config/device/FGT1/global/system/global"
|
|
||||||
},
|
|
||||||
"paramgram_used": {
|
|
||||||
"adom": "ansible",
|
|
||||||
"install_config": "disable",
|
|
||||||
"device_unique_name": "FGT1",
|
|
||||||
"interface": null,
|
|
||||||
"device_hostname": "ansible-fgt01",
|
|
||||||
"interface_ip": null,
|
|
||||||
"interface_allow_access": null
|
|
||||||
},
|
|
||||||
"post_method": "update"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"paramgram_used": {
|
"paramgram_used": {
|
||||||
"adom": "ansible",
|
"adom": "ansible",
|
||||||
|
@ -88,6 +96,9 @@
|
||||||
"interface_ip": null,
|
"interface_ip": null,
|
||||||
"interface_allow_access": null
|
"interface_allow_access": null
|
||||||
},
|
},
|
||||||
|
"datagram_sent": {
|
||||||
|
"hostname": "ansible-fgt01"
|
||||||
|
},
|
||||||
"raw_response": {
|
"raw_response": {
|
||||||
"status": {
|
"status": {
|
||||||
"message": "OK",
|
"message": "OK",
|
||||||
|
@ -107,6 +118,9 @@
|
||||||
"interface_ip": null,
|
"interface_ip": null,
|
||||||
"interface_allow_access": null
|
"interface_allow_access": null
|
||||||
},
|
},
|
||||||
|
"datagram_sent": {
|
||||||
|
"hostname": "ansible-fgt02"
|
||||||
|
},
|
||||||
"raw_response": {
|
"raw_response": {
|
||||||
"status": {
|
"status": {
|
||||||
"message": "OK",
|
"message": "OK",
|
||||||
|
@ -126,6 +140,9 @@
|
||||||
"interface_ip": null,
|
"interface_ip": null,
|
||||||
"interface_allow_access": null
|
"interface_allow_access": null
|
||||||
},
|
},
|
||||||
|
"datagram_sent": {
|
||||||
|
"hostname": "ansible-fgt03"
|
||||||
|
},
|
||||||
"raw_response": {
|
"raw_response": {
|
||||||
"status": {
|
"status": {
|
||||||
"message": "OK",
|
"message": "OK",
|
||||||
|
@ -148,15 +165,29 @@
|
||||||
"interface_ip": null,
|
"interface_ip": null,
|
||||||
"interface_allow_access": null
|
"interface_allow_access": null
|
||||||
},
|
},
|
||||||
"raw_response": {
|
"datagram_sent": {
|
||||||
"task": 352
|
"scope": {
|
||||||
|
"name": "FGT1"
|
||||||
},
|
},
|
||||||
"post_method": "execute"
|
"flags": "none",
|
||||||
|
"adom": "ansible"
|
||||||
|
},
|
||||||
|
"raw_response": {
|
||||||
|
"task": 243
|
||||||
|
},
|
||||||
|
"post_method": "exec"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"url": "/securityconsole/install/device",
|
"url": "/securityconsole/install/device",
|
||||||
"raw_response": {
|
"raw_response": {
|
||||||
"task": 353
|
"task": 244
|
||||||
|
},
|
||||||
|
"datagram_sent": {
|
||||||
|
"scope": {
|
||||||
|
"name": "FGT2, FGT3"
|
||||||
|
},
|
||||||
|
"flags": "none",
|
||||||
|
"adom": "ansible"
|
||||||
},
|
},
|
||||||
"paramgram_used": {
|
"paramgram_used": {
|
||||||
"adom": "ansible",
|
"adom": "ansible",
|
||||||
|
@ -167,7 +198,7 @@
|
||||||
"interface_ip": null,
|
"interface_ip": null,
|
||||||
"interface_allow_access": null
|
"interface_allow_access": null
|
||||||
},
|
},
|
||||||
"post_method": "execute"
|
"post_method": "exec"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,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_config
|
from ansible.modules.network.fortimanager import fmgr_device_config
|
||||||
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():
|
||||||
|
@ -43,51 +39,33 @@ 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_config.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)
|
||||||
|
|
||||||
|
|
||||||
def test_update_device_hostname(fixture_data, mocker):
|
fmg_instance = FortiManagerHandler(connection_mock, module_mock)
|
||||||
mocker.patch(
|
|
||||||
"pyFMG.fortimgr.FortiManager._post_request",
|
|
||||||
side_effect=fixture_data)
|
|
||||||
|
|
||||||
paramgram_used = {
|
|
||||||
'adom': 'ansible',
|
def test_update_device_hostname(fixture_data, mocker):
|
||||||
'install_config': 'disable',
|
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
|
||||||
'device_unique_name': 'FGT1',
|
side_effect=fixture_data)
|
||||||
'interface': None,
|
# Fixture sets used:###########################
|
||||||
'device_hostname': 'ansible-fgt01',
|
|
||||||
'interface_ip': None,
|
##################################################
|
||||||
'interface_allow_access': None,
|
|
||||||
'mode': 'update'}
|
|
||||||
output = fmgr_device_config.update_device_hostname(
|
|
||||||
fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
|
||||||
# install_config: disable
|
|
||||||
# device_unique_name: FGT1
|
|
||||||
# interface: None
|
|
||||||
# device_hostname: ansible-fgt01
|
|
||||||
# interface_ip: None
|
|
||||||
# interface_allow_access: None
|
|
||||||
# mode: update
|
|
||||||
#
|
|
||||||
assert output['raw_response']['status']['code'] == 0
|
|
||||||
paramgram_used = {
|
|
||||||
'adom': 'ansible',
|
|
||||||
'interface': None,
|
|
||||||
'device_unique_name': 'FGT1',
|
|
||||||
'install_config': 'disable',
|
|
||||||
'device_hostname': 'ansible-fgt01',
|
|
||||||
'interface_ip': None,
|
|
||||||
'interface_allow_access': None,
|
|
||||||
'mode': 'update'}
|
|
||||||
output = fmgr_device_config.update_device_hostname(
|
|
||||||
fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# interface: None
|
# interface: None
|
||||||
# device_unique_name: FGT1
|
# device_unique_name: FGT1
|
||||||
|
@ -96,20 +74,8 @@ def test_update_device_hostname(fixture_data, mocker):
|
||||||
# interface_ip: None
|
# interface_ip: None
|
||||||
# interface_allow_access: None
|
# interface_allow_access: None
|
||||||
# mode: update
|
# mode: update
|
||||||
#
|
##################################################
|
||||||
assert output['raw_response']['status']['code'] == 0
|
##################################################
|
||||||
paramgram_used = {
|
|
||||||
'adom': 'ansible',
|
|
||||||
'interface': None,
|
|
||||||
'device_unique_name': 'FGT2',
|
|
||||||
'install_config': 'disable',
|
|
||||||
'device_hostname': 'ansible-fgt02',
|
|
||||||
'interface_ip': None,
|
|
||||||
'interface_allow_access': None,
|
|
||||||
'mode': 'update'}
|
|
||||||
output = fmgr_device_config.update_device_hostname(
|
|
||||||
fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# interface: None
|
# interface: None
|
||||||
# device_unique_name: FGT2
|
# device_unique_name: FGT2
|
||||||
|
@ -118,20 +84,8 @@ def test_update_device_hostname(fixture_data, mocker):
|
||||||
# interface_ip: None
|
# interface_ip: None
|
||||||
# interface_allow_access: None
|
# interface_allow_access: None
|
||||||
# mode: update
|
# mode: update
|
||||||
#
|
##################################################
|
||||||
assert output['raw_response']['status']['code'] == 0
|
##################################################
|
||||||
paramgram_used = {
|
|
||||||
'adom': 'ansible',
|
|
||||||
'interface': None,
|
|
||||||
'device_unique_name': 'FGT3',
|
|
||||||
'install_config': 'disable',
|
|
||||||
'device_hostname': 'ansible-fgt03',
|
|
||||||
'interface_ip': None,
|
|
||||||
'interface_allow_access': None,
|
|
||||||
'mode': 'update'}
|
|
||||||
output = fmgr_device_config.update_device_hostname(
|
|
||||||
fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# interface: None
|
# interface: None
|
||||||
# device_unique_name: FGT3
|
# device_unique_name: FGT3
|
||||||
|
@ -140,27 +94,25 @@ def test_update_device_hostname(fixture_data, mocker):
|
||||||
# interface_ip: None
|
# interface_ip: None
|
||||||
# interface_allow_access: None
|
# interface_allow_access: None
|
||||||
# mode: update
|
# mode: update
|
||||||
#
|
##################################################
|
||||||
|
|
||||||
|
# Test using fixture 1 #
|
||||||
|
output = fmgr_device_config.update_device_hostname(fmg_instance, fixture_data[0]['paramgram_used'])
|
||||||
|
assert output['raw_response']['status']['code'] == 0
|
||||||
|
# Test using fixture 2 #
|
||||||
|
output = fmgr_device_config.update_device_hostname(fmg_instance, fixture_data[1]['paramgram_used'])
|
||||||
|
assert output['raw_response']['status']['code'] == 0
|
||||||
|
# Test using fixture 3 #
|
||||||
|
output = fmgr_device_config.update_device_hostname(fmg_instance, fixture_data[2]['paramgram_used'])
|
||||||
assert output['raw_response']['status']['code'] == 0
|
assert output['raw_response']['status']['code'] == 0
|
||||||
|
|
||||||
|
|
||||||
def test_update_device_interface(fixture_data, mocker):
|
def test_update_device_interface(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)
|
||||||
|
# Fixture sets used:###########################
|
||||||
|
|
||||||
paramgram_used = {
|
##################################################
|
||||||
'adom': 'ansible',
|
|
||||||
'install_config': 'disable',
|
|
||||||
'device_unique_name': 'FGT1',
|
|
||||||
'interface': 'port2',
|
|
||||||
'device_hostname': None,
|
|
||||||
'interface_ip': '10.1.1.1/24',
|
|
||||||
'interface_allow_access': 'ping, telnet, https, http',
|
|
||||||
'mode': 'update'}
|
|
||||||
output = fmgr_device_config.update_device_interface(
|
|
||||||
fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# install_config: disable
|
# install_config: disable
|
||||||
# device_unique_name: FGT1
|
# device_unique_name: FGT1
|
||||||
|
@ -169,20 +121,8 @@ def test_update_device_interface(fixture_data, mocker):
|
||||||
# interface_ip: 10.1.1.1/24
|
# interface_ip: 10.1.1.1/24
|
||||||
# interface_allow_access: ping, telnet, https, http
|
# interface_allow_access: ping, telnet, https, http
|
||||||
# mode: update
|
# mode: update
|
||||||
#
|
##################################################
|
||||||
assert output['raw_response']['status']['code'] == 0
|
##################################################
|
||||||
paramgram_used = {
|
|
||||||
'adom': 'ansible',
|
|
||||||
'install_config': 'disable',
|
|
||||||
'device_unique_name': 'FGT2',
|
|
||||||
'interface': 'port2',
|
|
||||||
'device_hostname': None,
|
|
||||||
'interface_ip': '10.1.2.1/24',
|
|
||||||
'interface_allow_access': 'ping, telnet, https, http',
|
|
||||||
'mode': 'update'}
|
|
||||||
output = fmgr_device_config.update_device_interface(
|
|
||||||
fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# install_config: disable
|
# install_config: disable
|
||||||
# device_unique_name: FGT2
|
# device_unique_name: FGT2
|
||||||
|
@ -191,20 +131,8 @@ def test_update_device_interface(fixture_data, mocker):
|
||||||
# interface_ip: 10.1.2.1/24
|
# interface_ip: 10.1.2.1/24
|
||||||
# interface_allow_access: ping, telnet, https, http
|
# interface_allow_access: ping, telnet, https, http
|
||||||
# mode: update
|
# mode: update
|
||||||
#
|
##################################################
|
||||||
assert output['raw_response']['status']['code'] == 0
|
##################################################
|
||||||
paramgram_used = {
|
|
||||||
'adom': 'ansible',
|
|
||||||
'install_config': 'disable',
|
|
||||||
'device_unique_name': 'FGT3',
|
|
||||||
'interface': 'port2',
|
|
||||||
'device_hostname': None,
|
|
||||||
'interface_ip': '10.1.3.1/24',
|
|
||||||
'interface_allow_access': 'ping, telnet, https, http',
|
|
||||||
'mode': 'update'}
|
|
||||||
output = fmgr_device_config.update_device_interface(
|
|
||||||
fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# install_config: disable
|
# install_config: disable
|
||||||
# device_unique_name: FGT3
|
# device_unique_name: FGT3
|
||||||
|
@ -213,26 +141,25 @@ def test_update_device_interface(fixture_data, mocker):
|
||||||
# interface_ip: 10.1.3.1/24
|
# interface_ip: 10.1.3.1/24
|
||||||
# interface_allow_access: ping, telnet, https, http
|
# interface_allow_access: ping, telnet, https, http
|
||||||
# mode: update
|
# mode: update
|
||||||
#
|
##################################################
|
||||||
|
|
||||||
|
# Test using fixture 1 #
|
||||||
|
output = fmgr_device_config.update_device_interface(fmg_instance, fixture_data[0]['paramgram_used'])
|
||||||
|
assert output['raw_response']['status']['code'] == 0
|
||||||
|
# Test using fixture 2 #
|
||||||
|
output = fmgr_device_config.update_device_interface(fmg_instance, fixture_data[1]['paramgram_used'])
|
||||||
|
assert output['raw_response']['status']['code'] == 0
|
||||||
|
# Test using fixture 3 #
|
||||||
|
output = fmgr_device_config.update_device_interface(fmg_instance, fixture_data[2]['paramgram_used'])
|
||||||
assert output['raw_response']['status']['code'] == 0
|
assert output['raw_response']['status']['code'] == 0
|
||||||
|
|
||||||
|
|
||||||
def test_exec_config(fixture_data, mocker):
|
def test_exec_config(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)
|
||||||
|
# Fixture sets used:###########################
|
||||||
|
|
||||||
paramgram_used = {
|
##################################################
|
||||||
'adom': 'ansible',
|
|
||||||
'interface': None,
|
|
||||||
'device_unique_name': 'FGT1',
|
|
||||||
'install_config': 'enable',
|
|
||||||
'device_hostname': None,
|
|
||||||
'interface_ip': None,
|
|
||||||
'interface_allow_access': None,
|
|
||||||
'mode': 'execute'}
|
|
||||||
output = fmgr_device_config.exec_config(fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# interface: None
|
# interface: None
|
||||||
# device_unique_name: FGT1
|
# device_unique_name: FGT1
|
||||||
|
@ -240,20 +167,9 @@ def test_exec_config(fixture_data, mocker):
|
||||||
# device_hostname: None
|
# device_hostname: None
|
||||||
# interface_ip: None
|
# interface_ip: None
|
||||||
# interface_allow_access: None
|
# interface_allow_access: None
|
||||||
# mode: execute
|
# mode: exec
|
||||||
#
|
##################################################
|
||||||
assert isinstance(output['raw_response'], dict) is True
|
##################################################
|
||||||
paramgram_used = {
|
|
||||||
'adom': 'ansible',
|
|
||||||
'install_config': 'enable',
|
|
||||||
'device_unique_name': 'FGT2, FGT3',
|
|
||||||
'interface': None,
|
|
||||||
'device_hostname': None,
|
|
||||||
'interface_ip': None,
|
|
||||||
'interface_allow_access': None,
|
|
||||||
'mode': 'execute'}
|
|
||||||
output = fmgr_device_config.exec_config(fmg_instance, paramgram_used)
|
|
||||||
#
|
|
||||||
# adom: ansible
|
# adom: ansible
|
||||||
# install_config: enable
|
# install_config: enable
|
||||||
# device_unique_name: FGT2, FGT3
|
# device_unique_name: FGT2, FGT3
|
||||||
|
@ -261,6 +177,12 @@ def test_exec_config(fixture_data, mocker):
|
||||||
# device_hostname: None
|
# device_hostname: None
|
||||||
# interface_ip: None
|
# interface_ip: None
|
||||||
# interface_allow_access: None
|
# interface_allow_access: None
|
||||||
# mode: execute
|
# mode: exec
|
||||||
#
|
##################################################
|
||||||
|
|
||||||
|
# Test using fixture 1 #
|
||||||
|
output = fmgr_device_config.exec_config(fmg_instance, fixture_data[0]['paramgram_used'])
|
||||||
|
assert isinstance(output['raw_response'], dict) is True
|
||||||
|
# Test using fixture 2 #
|
||||||
|
output = fmgr_device_config.exec_config(fmg_instance, fixture_data[1]['paramgram_used'])
|
||||||
assert isinstance(output['raw_response'], dict) is True
|
assert isinstance(output['raw_response'], dict) is True
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue