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:
ftntcorecse 2019-03-04 00:55:40 -05:00 committed by Nilashish Chakraborty
parent 785c567f72
commit 13517fe822
3 changed files with 333 additions and 430 deletions

View file

@ -1,173 +1,204 @@
{
"update_device_interface": [
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/pm/config/device/FGT1/global/system/interface/port2"
},
"paramgram_used": {
"adom": "ansible",
"install_config": "disable",
"device_unique_name": "FGT1",
"interface": "port2",
"device_hostname": null,
"interface_ip": "10.1.1.1/24",
"interface_allow_access": "ping, telnet, https, http"
},
"post_method": "update"
},
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/pm/config/device/FGT2/global/system/interface/port2"
},
"paramgram_used": {
"adom": "ansible",
"install_config": "disable",
"device_unique_name": "FGT2",
"interface": "port2",
"device_hostname": null,
"interface_ip": "10.1.2.1/24",
"interface_allow_access": "ping, telnet, https, http"
},
"post_method": "update"
},
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/pm/config/device/FGT3/global/system/interface/port2"
},
"paramgram_used": {
"adom": "ansible",
"install_config": "disable",
"device_unique_name": "FGT3",
"interface": "port2",
"device_hostname": null,
"interface_ip": "10.1.3.1/24",
"interface_allow_access": "ping, telnet, https, http"
},
"post_method": "update"
}
],
"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": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT1",
"install_config": "disable",
"device_hostname": "ansible-fgt01",
"interface_ip": null,
"interface_allow_access": null
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "pm/config/device/FGT1/global/system/global"
},
"post_method": "update"
},
{
"paramgram_used": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT2",
"install_config": "disable",
"device_hostname": "ansible-fgt02",
"interface_ip": null,
"interface_allow_access": null
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "pm/config/device/FGT2/global/system/global"
},
"post_method": "update"
},
{
"paramgram_used": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT3",
"install_config": "disable",
"device_hostname": "ansible-fgt03",
"interface_ip": null,
"interface_allow_access": null
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "pm/config/device/FGT3/global/system/global"
},
"post_method": "update"
}
],
"exec_config": [
{
"url": "/securityconsole/install/device",
"paramgram_used": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT1",
"install_config": "enable",
"device_hostname": null,
"interface_ip": null,
"interface_allow_access": null
},
"raw_response": {
"task": 352
},
"post_method": "execute"
},
{
"url": "/securityconsole/install/device",
"raw_response": {
"task": 353
},
"paramgram_used": {
"adom": "ansible",
"install_config": "enable",
"device_unique_name": "FGT2, FGT3",
"interface": null,
"device_hostname": null,
"interface_ip": null,
"interface_allow_access": null
},
"post_method": "execute"
}
]
"update_device_interface": [
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/pm/config/device/FGT1/global/system/interface/port2"
},
"datagram_sent": {
"ip": "10.1.1.1/24",
"allowaccess": [
"ping",
"telnet",
"https",
"http"
]
},
"paramgram_used": {
"adom": "ansible",
"install_config": "disable",
"device_unique_name": "FGT1",
"interface": "port2",
"device_hostname": null,
"interface_ip": "10.1.1.1/24",
"interface_allow_access": "ping, telnet, https, http"
},
"post_method": "update"
},
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/pm/config/device/FGT2/global/system/interface/port2"
},
"datagram_sent": {
"ip": "10.1.2.1/24",
"allowaccess": [
"ping",
"telnet",
"https",
"http"
]
},
"paramgram_used": {
"adom": "ansible",
"install_config": "disable",
"device_unique_name": "FGT2",
"interface": "port2",
"device_hostname": null,
"interface_ip": "10.1.2.1/24",
"interface_allow_access": "ping, telnet, https, http"
},
"post_method": "update"
},
{
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "/pm/config/device/FGT3/global/system/interface/port2"
},
"datagram_sent": {
"ip": "10.1.3.1/24",
"allowaccess": [
"ping",
"telnet",
"https",
"http"
]
},
"paramgram_used": {
"adom": "ansible",
"install_config": "disable",
"device_unique_name": "FGT3",
"interface": "port2",
"device_hostname": null,
"interface_ip": "10.1.3.1/24",
"interface_allow_access": "ping, telnet, https, http"
},
"post_method": "update"
}
],
"update_device_hostname": [
{
"paramgram_used": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT1",
"install_config": "disable",
"device_hostname": "ansible-fgt01",
"interface_ip": null,
"interface_allow_access": null
},
"datagram_sent": {
"hostname": "ansible-fgt01"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "pm/config/device/FGT1/global/system/global"
},
"post_method": "update"
},
{
"paramgram_used": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT2",
"install_config": "disable",
"device_hostname": "ansible-fgt02",
"interface_ip": null,
"interface_allow_access": null
},
"datagram_sent": {
"hostname": "ansible-fgt02"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "pm/config/device/FGT2/global/system/global"
},
"post_method": "update"
},
{
"paramgram_used": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT3",
"install_config": "disable",
"device_hostname": "ansible-fgt03",
"interface_ip": null,
"interface_allow_access": null
},
"datagram_sent": {
"hostname": "ansible-fgt03"
},
"raw_response": {
"status": {
"message": "OK",
"code": 0
},
"url": "pm/config/device/FGT3/global/system/global"
},
"post_method": "update"
}
],
"exec_config": [
{
"url": "/securityconsole/install/device",
"paramgram_used": {
"adom": "ansible",
"interface": null,
"device_unique_name": "FGT1",
"install_config": "enable",
"device_hostname": null,
"interface_ip": null,
"interface_allow_access": null
},
"datagram_sent": {
"scope": {
"name": "FGT1"
},
"flags": "none",
"adom": "ansible"
},
"raw_response": {
"task": 243
},
"post_method": "exec"
},
{
"url": "/securityconsole/install/device",
"raw_response": {
"task": 244
},
"datagram_sent": {
"scope": {
"name": "FGT2, FGT3"
},
"flags": "none",
"adom": "ansible"
},
"paramgram_used": {
"adom": "ansible",
"install_config": "enable",
"device_unique_name": "FGT2, FGT3",
"interface": null,
"device_hostname": null,
"interface_ip": null,
"interface_allow_access": null
},
"post_method": "exec"
}
]
}

View file

@ -19,17 +19,13 @@ __metaclass__ = type
import os
import json
from pyFMG.fortimgr import FortiManager
from ansible.module_utils.network.fortimanager.fortimanager import FortiManagerHandler
import pytest
try:
from ansible.modules.network.fortimanager import fmgr_device_config
except ImportError:
pytest.skip(
"Could not load required modules for testing",
allow_module_level=True)
fmg_instance = FortiManager("1.1.1.1", "admin", "")
pytest.skip("Could not load required modules for testing", allow_module_level=True)
def load_fixtures():
@ -43,51 +39,33 @@ def load_fixtures():
return [fixture_data]
@pytest.fixture(autouse=True)
def module_mock(mocker):
connection_class_mock = mocker.patch('ansible.module_utils.basic.AnsibleModule')
return connection_class_mock
@pytest.fixture(autouse=True)
def connection_mock(mocker):
connection_class_mock = mocker.patch('ansible.modules.network.fortimanager.fmgr_device_config.Connection')
return connection_class_mock
@pytest.fixture(scope="function", params=load_fixtures())
def fixture_data(request):
func_name = request.function.__name__.replace("test_", "")
return request.param.get(func_name, None)
def test_update_device_hostname(fixture_data, mocker):
mocker.patch(
"pyFMG.fortimgr.FortiManager._post_request",
side_effect=fixture_data)
fmg_instance = FortiManagerHandler(connection_mock, module_mock)
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'}
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)
#
def test_update_device_hostname(fixture_data, mocker):
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
side_effect=fixture_data)
# Fixture sets used:###########################
##################################################
# adom: ansible
# interface: None
# device_unique_name: FGT1
@ -96,20 +74,8 @@ def test_update_device_hostname(fixture_data, mocker):
# 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': '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
# interface: None
# device_unique_name: FGT2
@ -118,20 +84,8 @@ def test_update_device_hostname(fixture_data, mocker):
# 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': '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
# interface: None
# device_unique_name: FGT3
@ -140,27 +94,25 @@ def test_update_device_hostname(fixture_data, mocker):
# interface_ip: None
# interface_allow_access: None
# 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
def test_update_device_interface(fixture_data, mocker):
mocker.patch(
"pyFMG.fortimgr.FortiManager._post_request",
side_effect=fixture_data)
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
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
# install_config: disable
# device_unique_name: FGT1
@ -169,20 +121,8 @@ def test_update_device_interface(fixture_data, mocker):
# interface_ip: 10.1.1.1/24
# interface_allow_access: ping, telnet, https, http
# 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
# install_config: disable
# device_unique_name: FGT2
@ -191,20 +131,8 @@ def test_update_device_interface(fixture_data, mocker):
# interface_ip: 10.1.2.1/24
# interface_allow_access: ping, telnet, https, http
# 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
# install_config: disable
# device_unique_name: FGT3
@ -213,26 +141,25 @@ def test_update_device_interface(fixture_data, mocker):
# interface_ip: 10.1.3.1/24
# interface_allow_access: ping, telnet, https, http
# 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
def test_exec_config(fixture_data, mocker):
mocker.patch(
"pyFMG.fortimgr.FortiManager._post_request",
side_effect=fixture_data)
mocker.patch("ansible.module_utils.network.fortimanager.fortimanager.FortiManagerHandler.process_request",
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
# interface: None
# device_unique_name: FGT1
@ -240,20 +167,9 @@ def test_exec_config(fixture_data, mocker):
# device_hostname: None
# interface_ip: None
# interface_allow_access: None
# mode: execute
#
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)
#
# mode: exec
##################################################
##################################################
# adom: ansible
# install_config: enable
# device_unique_name: FGT2, FGT3
@ -261,6 +177,12 @@ def test_exec_config(fixture_data, mocker):
# device_hostname: None
# interface_ip: 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