FortiManager Plugin Module Conversion: fmgr_ha (#52768)

* Auto Commit for: fmgr_ha

* Auto Commit for: fmgr_ha

* Removed about 50 lines of validation in favor of "required_if" clauses.
This commit is contained in:
ftntcorecse 2019-03-06 23:01:17 -08:00 committed by Nilashish Chakraborty
parent 16571ec8a6
commit be3fe8f364
3 changed files with 449 additions and 776 deletions

View file

@ -29,6 +29,8 @@ DOCUMENTATION = '''
--- ---
module: fmgr_ha module: fmgr_ha
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)
@ -37,59 +39,56 @@ short_description: Manages the High-Availability State of FortiManager Clusters
description: Change HA state or settings of FortiManager nodes (Standalone/Master/Slave). description: Change HA state or settings of FortiManager nodes (Standalone/Master/Slave).
options: options:
host:
description:
- The FortiManager's address.
required: true
username:
description:
- The username to log into the FortiManager.
required: true
password:
description:
- The password associated with the username account.
required: false
fmgr_ha_mode: fmgr_ha_mode:
description: description:
- Sets the role of the FortiManager host for HA. - Sets the role of the FortiManager host for HA.
required: false required: false
choices: ["standalone", "master", "slave"] choices: ["standalone", "master", "slave"]
fmgr_ha_peer_ipv4: fmgr_ha_peer_ipv4:
description: description:
- Sets the IPv4 address of a HA peer. - Sets the IPv4 address of a HA peer.
required: false required: false
fmgr_ha_peer_ipv6: fmgr_ha_peer_ipv6:
description: description:
- Sets the IPv6 address of a HA peer. - Sets the IPv6 address of a HA peer.
required: false required: false
fmgr_ha_peer_sn: fmgr_ha_peer_sn:
description: description:
- Sets the HA Peer Serial Number. - Sets the HA Peer Serial Number.
required: false required: false
fmgr_ha_peer_status: fmgr_ha_peer_status:
description: description:
- Sets the peer status to enable or disable. - Sets the peer status to enable or disable.
required: false required: false
choices: ["enable", "disable"] choices: ["enable", "disable"]
fmgr_ha_cluster_pw: fmgr_ha_cluster_pw:
description: description:
- Sets the password for the HA cluster. Only required once. System remembers between HA mode switches. - Sets the password for the HA cluster. Only required once. System remembers between HA mode switches.
required: false required: false
fmgr_ha_cluster_id: fmgr_ha_cluster_id:
description: description:
- Sets the ID number of the HA cluster. Defaults to 1. - Sets the ID number of the HA cluster. Defaults to 1.
required: false required: false
default: 1 default: 1
fmgr_ha_hb_threshold: fmgr_ha_hb_threshold:
description: description:
- Sets heartbeat lost threshold (1-255). - Sets heartbeat lost threshold (1-255).
required: false required: false
default: 3 default: 3
fmgr_ha_hb_interval: fmgr_ha_hb_interval:
description: description:
- Sets the heartbeat interval (1-255). - Sets the heartbeat interval (1-255).
required: false required: false
default: 5 default: 5
fmgr_ha_file_quota: fmgr_ha_file_quota:
description: description:
- Sets the File quota in MB (2048-20480). - Sets the File quota in MB (2048-20480).
@ -101,39 +100,28 @@ options:
EXAMPLES = ''' EXAMPLES = '''
- name: SET FORTIMANAGER HA NODE TO MASTER - name: SET FORTIMANAGER HA NODE TO MASTER
fmgr_ha: fmgr_ha:
host: "{{inventory_hostname}}"
username: "{{ username }}"
password: "{{ password }}"
fmgr_ha_mode: "master" fmgr_ha_mode: "master"
fmgr_ha_cluster_pw: "fortinet"
fmgr_ha_cluster_id: "1"
- name: SET FORTIMANAGER HA NODE TO SLAVE - name: SET FORTIMANAGER HA NODE TO SLAVE
fmgr_ha: fmgr_ha:
host: "{{inventory_hostname}}"
username: "{{ username }}"
password: "{{ password }}"
fmgr_ha_mode: "slave" fmgr_ha_mode: "slave"
fmgr_ha_cluster_pw: "fortinet"
fmgr_ha_cluster_id: "1"
- name: SET FORTIMANAGER HA NODE TO STANDALONE - name: SET FORTIMANAGER HA NODE TO STANDALONE
fmgr_ha: fmgr_ha:
host: "{{inventory_hostname}}"
username: "{{ username }}"
password: "{{ password }}"
fmgr_ha_mode: "standalone" fmgr_ha_mode: "standalone"
- name: ADD FORTIMANAGER HA PEER - name: ADD FORTIMANAGER HA PEER
fmgr_ha: fmgr_ha:
host: "{{ inventory_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
fmgr_ha_peer_ipv4: "192.168.1.254" fmgr_ha_peer_ipv4: "192.168.1.254"
fmgr_ha_peer_sn: "FMG-VM1234567890" fmgr_ha_peer_sn: "FMG-VM1234567890"
fmgr_ha_peer_status: "enable" fmgr_ha_peer_status: "enable"
- name: CREATE CLUSTER ON MASTER - name: CREATE CLUSTER ON MASTER
fmgr_ha: fmgr_ha:
host: "{{ inventory_hostname }}"
username: "{{ username }}"
password: "{{ password }}"
fmgr_ha_mode: "master" fmgr_ha_mode: "master"
fmgr_ha_cluster_pw: "fortinet" fmgr_ha_cluster_pw: "fortinet"
fmgr_ha_cluster_id: "1" fmgr_ha_cluster_id: "1"
@ -148,23 +136,31 @@ 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 fmgr_set_ha_mode(fmg, paramgram): def fmgr_set_ha_mode(fmgr, paramgram):
""" """
This method is used set the HA mode of a FortiManager Node :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
""" """
# INIT A BASIC OBJECTS
response = DEFAULT_RESULT_OBJ
url = ""
datagram = {}
if paramgram["fmgr_ha_cluster_pw"] is not None and str.lower(paramgram["fmgr_ha_mode"]) != "standalone": if paramgram["fmgr_ha_cluster_pw"] is not None and str(paramgram["fmgr_ha_mode"].lower()) != "standalone":
datagram = { datagram = {
"mode": paramgram["fmgr_ha_mode"], "mode": paramgram["fmgr_ha_mode"],
"file-quota": paramgram["fmgr_ha_file_quota"], "file-quota": paramgram["fmgr_ha_file_quota"],
@ -173,7 +169,7 @@ def fmgr_set_ha_mode(fmg, paramgram):
"password": paramgram["fmgr_ha_cluster_pw"], "password": paramgram["fmgr_ha_cluster_pw"],
"clusterid": paramgram["fmgr_ha_cluster_id"] "clusterid": paramgram["fmgr_ha_cluster_id"]
} }
elif str.lower(paramgram["fmgr_ha_mode"]) == "standalone": elif str(paramgram["fmgr_ha_mode"].lower()) == "standalone":
datagram = { datagram = {
"mode": paramgram["fmgr_ha_mode"], "mode": paramgram["fmgr_ha_mode"],
"file-quota": paramgram["fmgr_ha_file_quota"], "file-quota": paramgram["fmgr_ha_file_quota"],
@ -183,27 +179,38 @@ def fmgr_set_ha_mode(fmg, paramgram):
} }
url = '/cli/global/system/ha' url = '/cli/global/system/ha'
response = fmg.set(url, datagram) response = fmgr.process_request(url, datagram, FMGRMethods.SET)
return response return response
def fmgr_get_ha_peer_list(fmg): def fmgr_get_ha_peer_list(fmgr):
""" """
This method is used GET the HA PEERS of a FortiManager Node :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
""" """
# INIT A BASIC OBJECTS
response = DEFAULT_RESULT_OBJ
datagram = { datagram = {}
"method": "get" paramgram = {}
}
url = '/cli/global/system/ha/peer/' url = '/cli/global/system/ha/peer/'
response = fmg.get(url, datagram) response = fmgr.process_request(url, datagram, FMGRMethods.GET)
return response return response
def fmgr_set_ha_peer(fmg, paramgram): def fmgr_set_ha_peer(fmgr, paramgram):
""" """
This method is used GET the HA PEERS of a FortiManager Node :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 = {
@ -215,52 +222,12 @@ def fmgr_set_ha_peer(fmg, paramgram):
} }
url = '/cli/global/system/ha/peer/' url = '/cli/global/system/ha/peer/'
response = fmg.set(url, datagram) response = fmgr.process_request(url, datagram, FMGRMethods.SET)
return response return response
# ADDITIONAL COMMON FUNCTIONS
# 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
else:
if logout_on_success:
fmg.logout()
module.exit_json(msg=msg, **results[1])
else:
return
def main(): def main():
argument_spec = dict( argument_spec = dict(
host=dict(required=True, type="str"),
password=dict(fallback=(env_fallback, ["ANSIBLE_NET_PASSWORD"]), no_log=True),
username=dict(fallback=(env_fallback, ["ANSIBLE_NET_USERNAME"]), no_log=True),
fmgr_ha_mode=dict(required=False, type="str", choices=["standalone", "master", "slave"]), fmgr_ha_mode=dict(required=False, type="str", choices=["standalone", "master", "slave"]),
fmgr_ha_cluster_pw=dict(required=False, type="str", no_log=True), fmgr_ha_cluster_pw=dict(required=False, type="str", no_log=True),
fmgr_ha_peer_status=dict(required=False, type="str", choices=["enable", "disable"]), fmgr_ha_peer_status=dict(required=False, type="str", choices=["enable", "disable"]),
@ -273,9 +240,14 @@ def main():
fmgr_ha_cluster_id=dict(required=False, type="int", default=1) fmgr_ha_cluster_id=dict(required=False, type="int", default=1)
) )
module = AnsibleModule(argument_spec, supports_check_mode=True,) required_if = [
['fmgr_ha_peer_ipv4', 'present', ['fmgr_ha_peer_sn', 'fmgr_ha_peer_status']],
['fmgr_ha_peer_ipv6', 'present', ['fmgr_ha_peer_sn', 'fmgr_ha_peer_status']],
['fmgr_ha_mode', 'master', ['fmgr_ha_cluster_pw', 'fmgr_ha_cluster_id']],
['fmgr_ha_mode', 'slave', ['fmgr_ha_cluster_pw', 'fmgr_ha_cluster_id']],
]
# VALIDATE PARAMS BEFORE ATTEMPTING TO CONNECT module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False, required_if=required_if)
paramgram = { paramgram = {
"fmgr_ha_mode": module.params["fmgr_ha_mode"], "fmgr_ha_mode": module.params["fmgr_ha_mode"],
"fmgr_ha_cluster_pw": module.params["fmgr_ha_cluster_pw"], "fmgr_ha_cluster_pw": module.params["fmgr_ha_cluster_pw"],
@ -288,68 +260,45 @@ def main():
"fmgr_ha_file_quota": module.params["fmgr_ha_file_quota"], "fmgr_ha_file_quota": module.params["fmgr_ha_file_quota"],
"fmgr_ha_cluster_id": module.params["fmgr_ha_cluster_id"], "fmgr_ha_cluster_id": module.params["fmgr_ha_cluster_id"],
} }
module.paramgram = paramgram
fmgr = None
if module._socket_path:
connection = Connection(module._socket_path)
fmgr = FortiManagerHandler(connection, module)
fmgr.tools = FMGRCommon()
else:
module.fail_json(**FAIL_SOCKET_MSG)
# INIT FLAGS AND COUNTERS # INIT FLAGS AND COUNTERS
get_ha_peers = 0 get_ha_peers = 0
# validate required arguments are passed; not used in argument_spec to allow params to be called from provider results = DEFAULT_RESULT_OBJ
# check if params are set try:
if module.params["host"] is None or module.params["username"] is None: if any(v is not None for v in (paramgram["fmgr_ha_peer_sn"], paramgram["fmgr_ha_peer_ipv4"],
module.fail_json(msg="Host and username are required for connection") paramgram["fmgr_ha_peer_ipv6"], paramgram["fmgr_ha_peer_status"])):
# CHECK IF LOGIN FAILED
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:
# START SESSION LOGIC
# IF THE PEER SN DEFINED, BUT THE IPS ARE NOT, THEN QUIT
if paramgram["fmgr_ha_peer_sn"] is not None:
# CHANGE GET_HA_PEERS TO SHOW INTENT TO EDIT PEERS
get_ha_peers = 1 get_ha_peers = 1
# DOUBLE CHECK THAT THE REST OF THE NEEDED PARAMETERS ARE THERE except Exception as err:
if paramgram["fmgr_ha_peer_ipv4"] is None and paramgram["fmgr_ha_peer_ipv6"] is None: raise FMGBaseException(err)
fmgr_logout(fmg, module, msg="HA Peer Serial Number is defined but the " try:
"IPv4 and IPv6 fields are empty."
" Fill in the IPv4 or v6 parameters in the playbook")
# IF THE PEER IPS ARE DEFINED, BUT NOT THE SERIAL NUMBER, THEN QUIT
if paramgram["fmgr_ha_peer_ipv4"] is not None or paramgram["fmgr_ha_peer_ipv6"] is not None:
# CHANGE GET_HA_PEERS TO SHOW INTENT TO EDIT PEERS
get_ha_peers = 1
# DOUBLE CHECK THAT THE REST OF THE NEEDED PARAMETERS ARE THERE
if paramgram["fmgr_ha_peer_sn"] is None:
fmgr_logout(fmg, module, msg="HA Peer IP Address is defined, but not the Peer Serial Number. "
"Fill in the SN parameter in the playbook.")
# IF THE PEER STATUS IS SET, BUT THE SERIAL NUMBER OR IP FIELDS AREN'T THERE, THEN EXIT
if paramgram["fmgr_ha_peer_status"] is not None:
# CHANGE GET_HA_PEERS TO SHOW INTENT TO EDIT PEERS
get_ha_peers = 1
# DOUBLE CHECK THAT THE REST OF THE NEEDED PARAMETERS ARE THERE
if paramgram["fmgr_ha_peer_ipv4"] is None and paramgram["fmgr_ha_peer_sn"] is None:
if paramgram["fmgr_ha_peer_sn"] is None and paramgram["fmgr_ha_peer_ipv6"] is None:
fmgr_logout(fmg, module, msg="HA Peer Status was defined, but nothing "
"to identify the peer was set. "
"Fill in one of"
" three parameters peer_ipv4 or v6 or serial_num")
# IF HA MODE IS NOT NULL, SWITCH THAT # IF HA MODE IS NOT NULL, SWITCH THAT
if paramgram["fmgr_ha_mode"] is not None: if paramgram["fmgr_ha_mode"] is not None:
if (str.lower(paramgram["fmgr_ha_mode"]) != "standalone" and paramgram["fmgr_ha_cluster_pw"] is not None)\ if (str.lower(paramgram["fmgr_ha_mode"]) != "standalone" and paramgram["fmgr_ha_cluster_pw"] is not None)\
or str.lower(paramgram["fmgr_ha_mode"]) == "standalone": or str.lower(paramgram["fmgr_ha_mode"]) == "standalone":
results = fmgr_set_ha_mode(fmg, paramgram) results = fmgr_set_ha_mode(fmgr, paramgram)
if results[0] != 0: fmgr.govern_response(module=module, results=results, stop_on_success=False,
fmgr_logout(fmg, module, results=results, good_codes=[0], ansible_facts=fmgr.construct_ansible_facts(results, module.params, paramgram))
msg="Failed to edit HA configuration the HA Peer")
elif str.lower(paramgram["fmgr_ha_mode"]) != "standalone" and \
paramgram["fmgr_ha_mode"] is not None and paramgram["fmgr_ha_cluster_pw"] is None:
fmgr_logout(fmg, module, msg="If setting HA Mode of MASTER or SLAVE, "
"you must specify a cluster password")
elif str.lower(paramgram["fmgr_ha_mode"]) != "standalone" and\
paramgram["fmgr_ha_mode"] is not None and\
paramgram["fmgr_ha_cluster_pw"] is None:
module.exit_json(msg="If setting HA Mode of MASTER or SLAVE, you must specify a cluster password")
except Exception as err:
raise FMGBaseException(err)
# IF GET_HA_PEERS IS ENABLED, LETS PROCESS THE PEERS # IF GET_HA_PEERS IS ENABLED, LETS PROCESS THE PEERS
try:
if get_ha_peers == 1: if get_ha_peers == 1:
# GET THE CURRENT LIST OF PEERS FROM THE NODE # GET THE CURRENT LIST OF PEERS FROM THE NODE
peers = fmgr_get_ha_peer_list(fmg) peers = fmgr_get_ha_peer_list(fmgr)
# GET LENGTH OF RETURNED PEERS LIST AND ADD ONE FOR THE NEXT ID # GET LENGTH OF RETURNED PEERS LIST AND ADD ONE FOR THE NEXT ID
paramgram["next_peer_id"] = len(peers[1]) + 1 paramgram["next_peer_id"] = len(peers[1]) + 1
# SET THE ACTUAL NUMBER OF PEERS # SET THE ACTUAL NUMBER OF PEERS
@ -374,8 +323,8 @@ def main():
if sn_compare == paramgram["fmgr_ha_peer_sn"]: if sn_compare == paramgram["fmgr_ha_peer_sn"]:
paramgram["peer_id"] = peer_loopcount paramgram["peer_id"] = peer_loopcount
paramgram["next_peer_id"] = paramgram["peer_id"] paramgram["next_peer_id"] = paramgram["peer_id"]
except Exception: except Exception as err:
pass raise FMGBaseException(err)
# ADVANCE THE LOOP AND REPEAT UNTIL DONE # ADVANCE THE LOOP AND REPEAT UNTIL DONE
peer_loopcount += 1 peer_loopcount += 1
@ -385,19 +334,22 @@ def main():
# IF THE PEER STATUS IS ENABLE, USE THE next_peer_id IN THE API CALL FOR THE ID # IF THE PEER STATUS IS ENABLE, USE THE next_peer_id IN THE API CALL FOR THE ID
if paramgram["fmgr_ha_peer_status"] == "enable": if paramgram["fmgr_ha_peer_status"] == "enable":
results = fmgr_set_ha_peer(fmg, paramgram) results = fmgr_set_ha_peer(fmgr, paramgram)
if results[0] != 0: fmgr.govern_response(module=module, results=results, stop_on_success=True,
fmgr_logout(fmg, module, results=results, good_codes=[0], msg="Failed to Enable the HA Peer") ansible_facts=fmgr.construct_ansible_facts(results,
module.params, paramgram))
# IF THE PEER STATUS IS DISABLE, WE HAVE TO HANDLE THAT A BIT DIFFERENTLY # IF THE PEER STATUS IS DISABLE, WE HAVE TO HANDLE THAT A BIT DIFFERENTLY
# JUST USING TWO DIFFERENT peer_id 's HERE # JUST USING TWO DIFFERENT peer_id 's HERE
if paramgram["fmgr_ha_peer_status"] == "disable": if paramgram["fmgr_ha_peer_status"] == "disable":
results = fmgr_set_ha_peer(fmg, paramgram) results = fmgr_set_ha_peer(fmgr, paramgram)
if results[0] != 0: fmgr.govern_response(module=module, results=results, stop_on_success=True,
fmgr_logout(fmg, module, results=results, good_codes=[0], msg="Failed to Disable the HA Peer") ansible_facts=fmgr.construct_ansible_facts(results, module.params, paramgram))
fmg.logout() except Exception as err:
return module.exit_json(**results[1]) raise FMGBaseException(err)
return module.exit_json(**results[1])
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,312 +1,241 @@
{ {
"fmgr_set_ha_peer": [ "fmgr_set_ha_peer": [
{ {
"raw_response": { "raw_response": {
"status": { "status": {
"message": "OK", "message": "OK",
"code": 0 "code": 0
},
"url": "/cli/global/system/ha/peer/"
}, },
"url": "/cli/global/system/ha/peer/" "datagram_sent": {
}, "status": "enable",
"paramgram_used": { "ip": "10.7.220.36",
"fmgr_ha_peer_sn": "FMG-VM0A17004505", "serial-number": "FMG-VMTM18001882",
"next_peer_id": 1, "ip6": null,
"fmgr_ha_hb_threshold": 3, "id": 1
"fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_status": "enable",
"fmgr_ha_file_quota": 4096,
"fmgr_ha_cluster_id": 2,
"peer_id": 1,
"fmgr_ha_peer_ipv4": "10.7.220.35",
"fmgr_ha_hb_interval": 5,
"fmgr_ha_mode": "slave"
},
"post_method": "set"
},
{
"paramgram_used": {
"fmgr_ha_peer_sn": "FMG-VM0A17005528",
"next_peer_id": 1,
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_cluster_pw": null,
"fmgr_ha_hb_interval": 5,
"fmgr_ha_cluster_id": 1,
"fmgr_ha_file_quota": 4096,
"fmgr_ha_peer_status": "enable",
"peer_id": 1,
"fmgr_ha_peer_ipv4": "10.7.220.36",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_mode": null
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha/peer/" "paramgram_used": {
}, "fmgr_ha_peer_sn": "FMG-VMTM18001882",
"post_method": "set" "next_peer_id": 2,
}, "fmgr_ha_hb_threshold": 3,
{ "fmgr_ha_cluster_pw": null,
"paramgram_used": { "fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_sn": "FMG-VM0A17005528", "fmgr_ha_peer_status": "enable",
"next_peer_id": 1, "fmgr_ha_file_quota": 4096,
"fmgr_ha_hb_threshold": 3, "fmgr_ha_cluster_id": 1,
"fmgr_ha_cluster_pw": null, "peer_id": 1,
"fmgr_ha_hb_interval": 5, "fmgr_ha_peer_ipv4": "10.7.220.36",
"fmgr_ha_cluster_id": 1, "fmgr_ha_hb_interval": 5,
"fmgr_ha_file_quota": 4096, "fmgr_ha_mode": null
"fmgr_ha_peer_status": "enable",
"peer_id": 1,
"fmgr_ha_peer_ipv4": "10.7.220.36",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_mode": null
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha/peer/" "post_method": "set"
}, },
"post_method": "set" {
}, "paramgram_used": {
{ "fmgr_ha_peer_sn": "FMG-VMTM18001881",
"raw_response": { "next_peer_id": 1,
"status": { "fmgr_ha_hb_threshold": 3,
"message": "OK", "fmgr_ha_cluster_pw": "fortinet",
"code": 0 "fmgr_ha_hb_interval": 5,
"fmgr_ha_cluster_id": 2,
"fmgr_ha_file_quota": 4096,
"fmgr_ha_peer_status": "enable",
"peer_id": 1,
"fmgr_ha_peer_ipv4": "10.7.220.35",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_mode": "slave"
}, },
"url": "/cli/global/system/ha/peer/" "datagram_sent": {
}, "status": "enable",
"paramgram_used": { "ip": "10.7.220.35",
"fmgr_ha_file_quota": 4096, "serial-number": "FMG-VMTM18001881",
"fmgr_ha_peer_status": "enable", "ip6": null,
"fmgr_ha_peer_sn": "FMG-VM0A17004505", "id": 1
"next_peer_id": 1,
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_mode": "slave",
"fmgr_ha_cluster_id": 2,
"peer_id": 1,
"fmgr_ha_peer_ipv4": "10.7.220.35",
"fmgr_ha_hb_interval": 5
},
"post_method": "set"
}
],
"fmgr_get_ha_peer_list": [
{
"url": "/cli/global/system/ha/peer/",
"raw_response": [
{
"status": 1,
"ip": "10.7.220.36",
"serial-number": "FMG-VM0A17005528",
"ip6": "::",
"id": 1
}
],
"paramgram_used": {
"method": "get"
},
"post_method": "get"
},
{
"url": "/cli/global/system/ha/peer/",
"paramgram_used": {
"method": "get"
},
"raw_response": [
{
"status": 1,
"ip": "10.7.220.35",
"serial-number": "FMG-VM0A17004505",
"ip6": "::",
"id": 1
}
],
"post_method": "get"
}
],
"fmgr_set_ha_mode": [
{
"paramgram_used": {
"fmgr_ha_peer_sn": null,
"fmgr_ha_hb_threshold": 10,
"fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_status": null,
"fmgr_ha_file_quota": 2048,
"fmgr_ha_cluster_id": 2,
"fmgr_ha_peer_ipv4": null,
"fmgr_ha_hb_interval": 15,
"fmgr_ha_mode": "master"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha" "raw_response": {
}, "status": {
"post_method": "set" "message": "OK",
}, "code": 0
{ },
"raw_response": { "url": "/cli/global/system/ha/peer/"
"status": {
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha" "post_method": "set"
}, }
"paramgram_used": { ],
"fmgr_ha_peer_sn": null, "fmgr_get_ha_peer_list": [
"fmgr_ha_hb_threshold": 3, {
"fmgr_ha_cluster_pw": "fortinet", "url": "/cli/global/system/ha/peer/",
"fmgr_ha_hb_interval": 5, "paramgram_used": {
"fmgr_ha_cluster_id": 2, "fmgr_ha_peer_sn": "FMG-VMTM18001882",
"fmgr_ha_file_quota": 4096, "fmgr_ha_hb_threshold": 3,
"fmgr_ha_peer_status": null, "fmgr_ha_cluster_pw": null,
"fmgr_ha_peer_ipv4": null, "fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_ipv6": null, "fmgr_ha_peer_status": "enable",
"fmgr_ha_mode": "slave" "fmgr_ha_file_quota": 4096,
}, "fmgr_ha_cluster_id": 1,
"post_method": "set" "fmgr_ha_peer_ipv4": "10.7.220.36",
}, "fmgr_ha_hb_interval": 5,
{ "fmgr_ha_mode": null
"paramgram_used": {
"fmgr_ha_peer_sn": "FMG-VM0A17004505",
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_status": "enable",
"fmgr_ha_file_quota": 4096,
"fmgr_ha_cluster_id": 2,
"fmgr_ha_peer_ipv4": "10.7.220.35",
"fmgr_ha_hb_interval": 5,
"fmgr_ha_mode": "slave"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha" "datagram_sent": {},
}, "raw_response": [
"post_method": "set" {
}, "status": "enable",
{ "ip": "10.7.220.140",
"raw_response": { "serial-number": "FMG-VM0A17005535",
"status": { "ip6": "::",
"message": "OK", "id": 1
"code": 0 }
],
"post_method": "get"
},
{
"url": "/cli/global/system/ha/peer/",
"raw_response": [
{
"status": "enable",
"ip": "10.7.220.35",
"serial-number": "FMG-VMTM18001881",
"ip6": "::",
"id": 1
}
],
"datagram_sent": {},
"paramgram_used": {
"fmgr_ha_peer_sn": "FMG-VMTM18001881",
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_hb_interval": 5,
"fmgr_ha_cluster_id": 2,
"fmgr_ha_file_quota": 4096,
"fmgr_ha_peer_status": "enable",
"fmgr_ha_peer_ipv4": "10.7.220.35",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_mode": "slave"
}, },
"url": "/cli/global/system/ha" "post_method": "get"
}, }
"paramgram_used": { ],
"fmgr_ha_peer_sn": null, "fmgr_set_ha_mode": [
"fmgr_ha_hb_threshold": 3, {
"fmgr_ha_cluster_pw": null, "paramgram_used": {
"fmgr_ha_hb_interval": 5, "fmgr_ha_peer_sn": null,
"fmgr_ha_cluster_id": 1, "fmgr_ha_hb_threshold": 10,
"fmgr_ha_file_quota": 4096, "fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_peer_status": null, "fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_ipv4": null, "fmgr_ha_peer_status": null,
"fmgr_ha_peer_ipv6": null, "fmgr_ha_file_quota": 2048,
"fmgr_ha_mode": "standalone" "fmgr_ha_cluster_id": 2,
}, "fmgr_ha_peer_ipv4": null,
"post_method": "set" "fmgr_ha_hb_interval": 15,
}, "fmgr_ha_mode": "master"
{
"paramgram_used": {
"fmgr_ha_peer_sn": null,
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_cluster_pw": null,
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_status": null,
"fmgr_ha_file_quota": 4096,
"fmgr_ha_cluster_id": 1,
"fmgr_ha_peer_ipv4": null,
"fmgr_ha_hb_interval": 5,
"fmgr_ha_mode": "standalone"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha" "datagram_sent": {
}, "file-quota": 2048,
"post_method": "set" "clusterid": 2,
}, "hb-lost-threshold": 10,
{ "mode": "master",
"raw_response": { "hb-interval": 15,
"status": { "password": "fortinet"
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha" "raw_response": {
}, "status": {
"paramgram_used": { "message": "OK",
"fmgr_ha_peer_sn": null, "code": 0
"fmgr_ha_hb_threshold": 10, },
"fmgr_ha_cluster_pw": "fortinet", "url": "/cli/global/system/ha"
"fmgr_ha_hb_interval": 15,
"fmgr_ha_cluster_id": 2,
"fmgr_ha_file_quota": 2048,
"fmgr_ha_peer_status": null,
"fmgr_ha_peer_ipv4": null,
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_mode": "master"
},
"post_method": "set"
},
{
"paramgram_used": {
"fmgr_ha_peer_sn": null,
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_status": null,
"fmgr_ha_file_quota": 4096,
"fmgr_ha_cluster_id": 2,
"fmgr_ha_peer_ipv4": null,
"fmgr_ha_hb_interval": 5,
"fmgr_ha_mode": "slave"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
}, },
"url": "/cli/global/system/ha" "post_method": "set"
}, },
"post_method": "set" {
}, "raw_response": {
{ "status": {
"raw_response": { "message": "OK",
"status": { "code": 0
"message": "OK", },
"code": 0 "url": "/cli/global/system/ha"
}, },
"url": "/cli/global/system/ha" "datagram_sent": {
}, "file-quota": 4096,
"paramgram_used": { "clusterid": 2,
"fmgr_ha_peer_sn": "FMG-VM0A17004505", "hb-lost-threshold": 3,
"fmgr_ha_hb_threshold": 3, "mode": "slave",
"fmgr_ha_cluster_pw": "fortinet", "hb-interval": 5,
"fmgr_ha_hb_interval": 5, "password": "fortinet"
"fmgr_ha_cluster_id": 2, },
"fmgr_ha_file_quota": 4096, "paramgram_used": {
"fmgr_ha_peer_status": "enable", "fmgr_ha_peer_sn": null,
"fmgr_ha_peer_ipv4": "10.7.220.35", "fmgr_ha_hb_threshold": 3,
"fmgr_ha_peer_ipv6": null, "fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_mode": "slave" "fmgr_ha_hb_interval": 5,
}, "fmgr_ha_cluster_id": 2,
"post_method": "set" "fmgr_ha_file_quota": 4096,
} "fmgr_ha_peer_status": null,
] "fmgr_ha_peer_ipv4": null,
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_mode": "slave"
},
"post_method": "set"
},
{
"paramgram_used": {
"fmgr_ha_peer_sn": "FMG-VMTM18001881",
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_cluster_pw": "fortinet",
"fmgr_ha_peer_ipv6": null,
"fmgr_ha_peer_status": "enable",
"fmgr_ha_file_quota": 4096,
"fmgr_ha_cluster_id": 2,
"fmgr_ha_peer_ipv4": "10.7.220.35",
"fmgr_ha_hb_interval": 5,
"fmgr_ha_mode": "slave"
},
"datagram_sent": {
"file-quota": 4096,
"clusterid": 2,
"hb-lost-threshold": 3,
"mode": "slave",
"hb-interval": 5,
"password": "fortinet"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/cli/global/system/ha"
},
"post_method": "set"
},
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/cli/global/system/ha"
},
"datagram_sent": {
"hb-lost-threshold": 3,
"hb-interval": 5,
"clusterid": 1,
"mode": "standalone",
"file-quota": 4096
},
"paramgram_used": {
"fmgr_ha_file_quota": 4096,
"fmgr_ha_cluster_pw": null,
"fmgr_ha_peer_sn": null,
"fmgr_ha_hb_interval": 5,
"fmgr_ha_cluster_id": 1,
"fmgr_ha_mode": "standalone",
"fmgr_ha_peer_status": null,
"fmgr_ha_hb_threshold": 3,
"fmgr_ha_peer_ipv4": null,
"fmgr_ha_peer_ipv6": null
},
"post_method": "set"
}
]
} }

View file

@ -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_ha from ansible.modules.network.fortimanager import fmgr_ha
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,31 +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_ha.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_fmgr_set_ha_mode(fixture_data, mocker): fmg_instance = FortiManagerHandler(connection_mock, module_mock)
mocker.patch(
"pyFMG.fortimgr.FortiManager._post_request",
side_effect=fixture_data)
paramgram_used = {
'fmgr_ha_peer_sn': None, def test_fmgr_set_ha_mode(fixture_data, mocker):
'fmgr_ha_hb_threshold': 10, mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
'fmgr_ha_cluster_pw': 'fortinet', side_effect=fixture_data)
'fmgr_ha_peer_ipv6': None, # Fixture sets used:###########################
'fmgr_ha_peer_status': None,
'fmgr_ha_file_quota': 2048, ##################################################
'fmgr_ha_cluster_id': 2,
'fmgr_ha_peer_ipv4': None,
'fmgr_ha_hb_interval': 15,
'fmgr_ha_mode': 'master',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: None # fmgr_ha_peer_sn: None
# fmgr_ha_hb_threshold: 10 # fmgr_ha_hb_threshold: 10
# fmgr_ha_cluster_pw: fortinet # fmgr_ha_cluster_pw: fortinet
@ -79,22 +77,8 @@ def test_fmgr_set_ha_mode(fixture_data, mocker):
# fmgr_ha_hb_interval: 15 # fmgr_ha_hb_interval: 15
# fmgr_ha_mode: master # fmgr_ha_mode: master
# mode: set # mode: set
# ##################################################
assert output['raw_response']['status']['code'] == 0 ##################################################
paramgram_used = {
'fmgr_ha_peer_sn': None,
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': 'fortinet',
'fmgr_ha_hb_interval': 5,
'fmgr_ha_cluster_id': 2,
'fmgr_ha_file_quota': 4096,
'fmgr_ha_peer_status': None,
'fmgr_ha_peer_ipv4': None,
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_mode': 'slave',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: None # fmgr_ha_peer_sn: None
# fmgr_ha_hb_threshold: 3 # fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: fortinet # fmgr_ha_cluster_pw: fortinet
@ -106,23 +90,9 @@ def test_fmgr_set_ha_mode(fixture_data, mocker):
# fmgr_ha_peer_ipv6: None # fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: slave # fmgr_ha_mode: slave
# mode: set # mode: set
# ##################################################
assert output['raw_response']['status']['code'] == 0 ##################################################
paramgram_used = { # fmgr_ha_peer_sn: FMG-VMTM18001881
'fmgr_ha_peer_sn': 'FMG-VM0A17004505',
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': 'fortinet',
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_peer_status': 'enable',
'fmgr_ha_file_quota': 4096,
'fmgr_ha_cluster_id': 2,
'fmgr_ha_peer_ipv4': '10.7.220.35',
'fmgr_ha_hb_interval': 5,
'fmgr_ha_mode': 'slave',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: FMG-VM0A17004505
# fmgr_ha_hb_threshold: 3 # fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: fortinet # fmgr_ha_cluster_pw: fortinet
# fmgr_ha_peer_ipv6: None # fmgr_ha_peer_ipv6: None
@ -133,292 +103,114 @@ def test_fmgr_set_ha_mode(fixture_data, mocker):
# fmgr_ha_hb_interval: 5 # fmgr_ha_hb_interval: 5
# fmgr_ha_mode: slave # fmgr_ha_mode: slave
# mode: set # mode: set
# ##################################################
assert output['raw_response']['status']['code'] == 0 ##################################################
paramgram_used = { # fmgr_ha_file_quota: 4096
'fmgr_ha_peer_sn': None,
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': None,
'fmgr_ha_hb_interval': 5,
'fmgr_ha_cluster_id': 1,
'fmgr_ha_file_quota': 4096,
'fmgr_ha_peer_status': None,
'fmgr_ha_peer_ipv4': None,
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_mode': 'standalone',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: None
# fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: None # fmgr_ha_cluster_pw: None
# fmgr_ha_peer_sn: None
# fmgr_ha_hb_interval: 5 # fmgr_ha_hb_interval: 5
# fmgr_ha_cluster_id: 1 # fmgr_ha_cluster_id: 1
# fmgr_ha_file_quota: 4096
# fmgr_ha_peer_status: None
# fmgr_ha_peer_ipv4: None
# fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: standalone # fmgr_ha_mode: standalone
# mode: set # fmgr_ha_peer_status: None
#
assert output['raw_response']['status']['code'] == 0
paramgram_used = {
'fmgr_ha_peer_sn': None,
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': None,
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_peer_status': None,
'fmgr_ha_file_quota': 4096,
'fmgr_ha_cluster_id': 1,
'fmgr_ha_peer_ipv4': None,
'fmgr_ha_hb_interval': 5,
'fmgr_ha_mode': 'standalone',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: None
# fmgr_ha_hb_threshold: 3 # fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: None
# fmgr_ha_peer_ipv6: None
# fmgr_ha_peer_status: None
# fmgr_ha_file_quota: 4096
# fmgr_ha_cluster_id: 1
# fmgr_ha_peer_ipv4: None
# fmgr_ha_hb_interval: 5
# fmgr_ha_mode: standalone
# mode: set
#
assert output['raw_response']['status']['code'] == 0
paramgram_used = {
'fmgr_ha_peer_sn': None,
'fmgr_ha_hb_threshold': 10,
'fmgr_ha_cluster_pw': 'fortinet',
'fmgr_ha_hb_interval': 15,
'fmgr_ha_cluster_id': 2,
'fmgr_ha_file_quota': 2048,
'fmgr_ha_peer_status': None,
'fmgr_ha_peer_ipv4': None,
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_mode': 'master',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: None
# fmgr_ha_hb_threshold: 10
# fmgr_ha_cluster_pw: fortinet
# fmgr_ha_hb_interval: 15
# fmgr_ha_cluster_id: 2
# fmgr_ha_file_quota: 2048
# fmgr_ha_peer_status: None
# fmgr_ha_peer_ipv4: None # fmgr_ha_peer_ipv4: None
# fmgr_ha_peer_ipv6: None # fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: master
# mode: set # mode: set
# ##################################################
# Test using fixture 1 #
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, fixture_data[0]['paramgram_used'])
assert output['raw_response']['status']['code'] == 0 assert output['raw_response']['status']['code'] == 0
paramgram_used = { # Test using fixture 2 #
'fmgr_ha_peer_sn': None, output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, fixture_data[1]['paramgram_used'])
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': 'fortinet',
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_peer_status': None,
'fmgr_ha_file_quota': 4096,
'fmgr_ha_cluster_id': 2,
'fmgr_ha_peer_ipv4': None,
'fmgr_ha_hb_interval': 5,
'fmgr_ha_mode': 'slave',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: None
# fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: fortinet
# fmgr_ha_peer_ipv6: None
# fmgr_ha_peer_status: None
# fmgr_ha_file_quota: 4096
# fmgr_ha_cluster_id: 2
# fmgr_ha_peer_ipv4: None
# fmgr_ha_hb_interval: 5
# fmgr_ha_mode: slave
# mode: set
#
assert output['raw_response']['status']['code'] == 0 assert output['raw_response']['status']['code'] == 0
paramgram_used = { # Test using fixture 3 #
'fmgr_ha_peer_sn': 'FMG-VM0A17004505', output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, fixture_data[2]['paramgram_used'])
'fmgr_ha_hb_threshold': 3, assert output['raw_response']['status']['code'] == 0
'fmgr_ha_cluster_pw': 'fortinet', # Test using fixture 4 #
'fmgr_ha_hb_interval': 5, output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, fixture_data[3]['paramgram_used'])
'fmgr_ha_cluster_id': 2,
'fmgr_ha_file_quota': 4096,
'fmgr_ha_peer_status': 'enable',
'fmgr_ha_peer_ipv4': '10.7.220.35',
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_mode': 'slave',
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_mode(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: FMG-VM0A17004505
# fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: fortinet
# fmgr_ha_hb_interval: 5
# fmgr_ha_cluster_id: 2
# fmgr_ha_file_quota: 4096
# fmgr_ha_peer_status: enable
# fmgr_ha_peer_ipv4: 10.7.220.35
# fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: slave
# mode: set
#
assert output['raw_response']['status']['code'] == 0 assert output['raw_response']['status']['code'] == 0
def test_fmgr_get_ha_peer_list(fixture_data, mocker): def test_fmgr_get_ha_peer_list(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 = {'method': 'get', 'mode': 'get'} ##################################################
output = fmgr_ha.fmgr_get_ha_peer_list(fmg_instance) # fmgr_ha_peer_sn: FMG-VMTM18001882
# # fmgr_ha_hb_threshold: 3
# method: get # fmgr_ha_cluster_pw: None
# fmgr_ha_peer_ipv6: None
# fmgr_ha_peer_status: enable
# fmgr_ha_file_quota: 4096
# fmgr_ha_cluster_id: 1
# fmgr_ha_peer_ipv4: 10.7.220.36
# fmgr_ha_hb_interval: 5
# fmgr_ha_mode: None
# mode: get # mode: get
# ##################################################
##################################################
# fmgr_ha_peer_sn: FMG-VMTM18001881
# fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: fortinet
# fmgr_ha_hb_interval: 5
# fmgr_ha_cluster_id: 2
# fmgr_ha_file_quota: 4096
# fmgr_ha_peer_status: enable
# fmgr_ha_peer_ipv4: 10.7.220.35
# fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: slave
# mode: get
##################################################
# Test using fixture 1 #
output = fmgr_ha.fmgr_get_ha_peer_list(fmg_instance)
assert isinstance(output['raw_response'], list) is True assert isinstance(output['raw_response'], list) is True
paramgram_used = {'method': 'get', 'mode': 'get'} # Test using fixture 2 #
output = fmgr_ha.fmgr_get_ha_peer_list(fmg_instance) output = fmgr_ha.fmgr_get_ha_peer_list(fmg_instance)
#
# method: get
# mode: get
#
assert isinstance(output['raw_response'], list) is True assert isinstance(output['raw_response'], list) is True
def test_fmgr_set_ha_peer(fixture_data, mocker): def test_fmgr_set_ha_peer(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 = { ##################################################
'fmgr_ha_peer_sn': 'FMG-VM0A17004505', # fmgr_ha_peer_sn: FMG-VMTM18001882
'next_peer_id': 1, # next_peer_id: 2
'fmgr_ha_hb_threshold': 3, # fmgr_ha_hb_threshold: 3
'fmgr_ha_cluster_pw': 'fortinet', # fmgr_ha_cluster_pw: None
'fmgr_ha_peer_ipv6': None, # fmgr_ha_peer_ipv6: None
'fmgr_ha_peer_status': 'enable', # fmgr_ha_peer_status: enable
'fmgr_ha_file_quota': 4096, # fmgr_ha_file_quota: 4096
'fmgr_ha_cluster_id': 2, # fmgr_ha_cluster_id: 1
'peer_id': 1, # peer_id: 1
'fmgr_ha_peer_ipv4': '10.7.220.35', # fmgr_ha_peer_ipv4: 10.7.220.36
'fmgr_ha_hb_interval': 5, # fmgr_ha_hb_interval: 5
'fmgr_ha_mode': 'slave', # fmgr_ha_mode: None
'mode': 'set'} # mode: set
output = fmgr_ha.fmgr_set_ha_peer(fmg_instance, paramgram_used) ##################################################
# ##################################################
# fmgr_ha_peer_sn: FMG-VM0A17004505 # fmgr_ha_peer_sn: FMG-VMTM18001881
# next_peer_id: 1 # next_peer_id: 1
# fmgr_ha_hb_threshold: 3 # fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: fortinet # fmgr_ha_cluster_pw: fortinet
# fmgr_ha_peer_ipv6: None # fmgr_ha_hb_interval: 5
# fmgr_ha_peer_status: enable
# fmgr_ha_file_quota: 4096
# fmgr_ha_cluster_id: 2 # fmgr_ha_cluster_id: 2
# fmgr_ha_file_quota: 4096
# fmgr_ha_peer_status: enable
# peer_id: 1 # peer_id: 1
# fmgr_ha_peer_ipv4: 10.7.220.35 # fmgr_ha_peer_ipv4: 10.7.220.35
# fmgr_ha_hb_interval: 5
# fmgr_ha_mode: slave
# mode: set
#
assert output['raw_response']['status']['code'] == 0
paramgram_used = {
'fmgr_ha_peer_sn': 'FMG-VM0A17005528',
'next_peer_id': 1,
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': None,
'fmgr_ha_hb_interval': 5,
'fmgr_ha_cluster_id': 1,
'fmgr_ha_file_quota': 4096,
'fmgr_ha_peer_status': 'enable',
'peer_id': 1,
'fmgr_ha_peer_ipv4': '10.7.220.36',
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_mode': None,
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_peer(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: FMG-VM0A17005528
# next_peer_id: 1
# fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: None
# fmgr_ha_hb_interval: 5
# fmgr_ha_cluster_id: 1
# fmgr_ha_file_quota: 4096
# fmgr_ha_peer_status: enable
# peer_id: 1
# fmgr_ha_peer_ipv4: 10.7.220.36
# fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: None
# mode: set
#
assert output['raw_response']['status']['code'] == 0
paramgram_used = {
'fmgr_ha_peer_sn': 'FMG-VM0A17005528',
'next_peer_id': 1,
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': None,
'fmgr_ha_hb_interval': 5,
'fmgr_ha_cluster_id': 1,
'fmgr_ha_file_quota': 4096,
'fmgr_ha_peer_status': 'enable',
'peer_id': 1,
'fmgr_ha_peer_ipv4': '10.7.220.36',
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_mode': None,
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_peer(fmg_instance, paramgram_used)
#
# fmgr_ha_peer_sn: FMG-VM0A17005528
# next_peer_id: 1
# fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: None
# fmgr_ha_hb_interval: 5
# fmgr_ha_cluster_id: 1
# fmgr_ha_file_quota: 4096
# fmgr_ha_peer_status: enable
# peer_id: 1
# fmgr_ha_peer_ipv4: 10.7.220.36
# fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: None
# mode: set
#
assert output['raw_response']['status']['code'] == 0
paramgram_used = {
'fmgr_ha_file_quota': 4096,
'fmgr_ha_peer_status': 'enable',
'fmgr_ha_peer_sn': 'FMG-VM0A17004505',
'next_peer_id': 1,
'fmgr_ha_hb_threshold': 3,
'fmgr_ha_cluster_pw': 'fortinet',
'fmgr_ha_peer_ipv6': None,
'fmgr_ha_mode': 'slave',
'fmgr_ha_cluster_id': 2,
'peer_id': 1,
'fmgr_ha_peer_ipv4': '10.7.220.35',
'fmgr_ha_hb_interval': 5,
'mode': 'set'}
output = fmgr_ha.fmgr_set_ha_peer(fmg_instance, paramgram_used)
#
# fmgr_ha_file_quota: 4096
# fmgr_ha_peer_status: enable
# fmgr_ha_peer_sn: FMG-VM0A17004505
# next_peer_id: 1
# fmgr_ha_hb_threshold: 3
# fmgr_ha_cluster_pw: fortinet
# fmgr_ha_peer_ipv6: None # fmgr_ha_peer_ipv6: None
# fmgr_ha_mode: slave # fmgr_ha_mode: slave
# fmgr_ha_cluster_id: 2
# peer_id: 1
# fmgr_ha_peer_ipv4: 10.7.220.35
# fmgr_ha_hb_interval: 5
# mode: set # mode: set
# ##################################################
# Test using fixture 1 #
output = fmgr_ha.fmgr_set_ha_peer(fmg_instance, fixture_data[0]['paramgram_used'])
assert output['raw_response']['status']['code'] == 0
# Test using fixture 2 #
output = fmgr_ha.fmgr_set_ha_peer(fmg_instance, fixture_data[1]['paramgram_used'])
assert output['raw_response']['status']['code'] == 0 assert output['raw_response']['status']['code'] == 0