async_fmgbase
Async FMGBase connection
AsyncFMGBase #
Fortimanager connection class
This can be used as a connection handler for the FortiManager. It maintains state of operation and provides functions to communicate with the FMG.
Attributes:
Name | Type | Description |
---|---|---|
lock |
AsyncFMGLockContext
|
Workspace lock handler |
Examples:
Possible arguments to initialize: FMGSettings
Using as context manager#
>>> import asyncio
>>> settings = {...}
>>> async def get_version(**settings):
... async with AsyncFMGBase(**settings) as conn:
... print(await conn.get_version())
>>> asyncio.run(get_version())
Using as function:#
>>> import asyncio
>>> from pyfortinet.exceptions import FMGException
>>> settings = {...}
>>> async def get_version(**settings):
... conn = AsyncFMGBase(**settings)
... try:
... await conn.open()
... print(await conn.get_version())
... except FMGException as err:
... print(f"Error: {err}")
... finally:
... await conn.close()
>>> asyncio.run(get_version())
Parameters:
Name | Type | Description | Default |
---|---|---|---|
settings |
Settings
|
FortiManager settings |
None
|
Other Parameters:
Name | Type | Description |
---|---|---|
base_url |
str
|
Base URL to access FMG (e.g.: https://myfmg/jsonrpc) |
username |
str
|
User to authenticate |
password |
str
|
Password for authentication |
adom |
str
|
ADOM to use for this connection |
verify |
bool
|
Verify SSL certificate (REQUESTS_CA_BUNDLE can set accepted CA cert) |
timeout |
float
|
Connection timeout for requests in seconds |
raise_on_error |
bool
|
Raise exception on error |
discard_on_close |
bool
|
Discard changes after connection close (workspace mode) |
discard_on_error |
bool
|
Discard changes when exception occurs (workspace mode) |
Source code in pyfortinet/fmg_api/async_fmgbase.py
add
async
#
add(request: dict[str, Any]) -> AsyncFMGResponse
Add operation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
dict[str, Any]
|
Add operation's data structure |
required |
Examples:
>>> import asyncio
>>> settings = {...}
>>> address_request = {
... "url": "/pm/config/global/obj/firewall/address",
... "data": {
... "name": "test-address",
... "associated-interface": "inside",
... "obj-type": "ip",
... "type": "ipmask",
... "start-ip": "10.0.0.1/24"
... }
... }
>>> async def add_request(request: dict[str,Any]):
... async with AsyncFMGBase(**settings) as fmg:
... return await fmg.add(address_request)
>>> asyncio.run(add_request(address_request))
Returns:
Type | Description |
---|---|
AsyncFMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/async_fmgbase.py
close
async
#
close connection
Source code in pyfortinet/fmg_api/async_fmgbase.py
delete
async
#
delete(request: dict[str, str]) -> AsyncFMGResponse
Delete operation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
dict[str, str]
|
Update operation's data structure |
required |
Examples:
>>> import asyncio
>>> settings = {...}
>>> address_request = {
... "url": "/pm/config/global/obj/firewall/address/test-address",
... }
>>> async def delete_address(request):
... async with AsyncFMGBase(**settings) as fmg:
... await fmg.delete(address_request)
>>> asyncio.run(delete_address(address_request))
Returns:
Type | Description |
---|---|
FMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/async_fmgbase.py
exec
async
#
exec(request: dict[str, str]) -> AsyncFMGResponse
Execute on FMG
Source code in pyfortinet/fmg_api/async_fmgbase.py
get
async
#
get(request: dict[str, Any]) -> AsyncFMGResponse
Get info from FMG
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
dict[str, Any]
|
Get operation's param structure |
required |
Examples:
>>> import asyncio
>>> address_request = {
... "url": "/pm/config/global/obj/firewall/address",
... "filter": [ ["name", "==", "test-address"] ],
... "fields": [ "name", "subnet" ]
... }
>>> settings = {...}
>>> async def get_address(request: dict[str, Any]):
... async with AsyncFMGBase(**settings) as fmg:
... return await fmg.get(address_request)
>>> asyncio.run(get_address())
Returns:
Type | Description |
---|---|
AsyncFMGResponse
|
response object with data |
Source code in pyfortinet/fmg_api/async_fmgbase.py
get_version
async
#
Gather FMG version
Source code in pyfortinet/fmg_api/async_fmgbase.py
open
async
#
open() -> AsyncFMGBase
open connection
Source code in pyfortinet/fmg_api/async_fmgbase.py
set
async
#
set(request: dict[str, Any]) -> AsyncFMGResponse
Set operation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
dict[str, Any]
|
Set operation's data structure |
required |
Examples:
>>> import asyncio
>>> settings = {...}
>>> address_request = {
... "url": "/pm/config/global/obj/firewall/address",
... "data": {
... "name": "test-address",
... "associated-interface": "inside",
... "obj-type": "ip",
... "type": "ipmask",
... "start-ip": "10.0.0.1/24"
... }
... }
>>> async def set_address(request):
... async with AsyncFMGBase(**settings) as fmg:
... return await fmg.set(address_request)
>>> asyncio.run(set_address())
Returns:
Type | Description |
---|---|
AsyncFMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/async_fmgbase.py
update
async
#
update(request: dict[str, Any]) -> AsyncFMGResponse
Update operation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request |
dict[str, Any]
|
Update operation's data structure |
required |
Examples:
>>> import asyncio
>>> settings = {...}
>>> address_request = {
... "url": "/pm/config/global/obj/firewall/address",
... "data": {
... "name": "test-address",
... "associated-interface": "inside",
... "obj-type": "ip",
... "type": "ipmask",
... "start-ip": "10.0.0.1/24"
... }
... }
>>> async def update_address(request):
... async with AsyncFMGBase(**settings) as fmg:
... return await fmg.update(address_request)
>>> asyncio.run(update_address())
Returns:
Type | Description |
---|---|
AsyncFMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/async_fmgbase.py
wait_for_task
async
#
wait_for_task(task_res: Union[int, AsyncFMGResponse], callback: Callable[[int, str], None] = None, timeout: int = 60, loop_interval: int = 2) -> Union[str, None]
Wait for task to finish
Parameters:
Name | Type | Description | Default |
---|---|---|---|
task_res |
Union[int, AsyncFMGResponse]
|
(int, AsyncFMGResponse): Task or task ID to check |
required |
callback |
Callable[[int, str], None]
|
(Callable[[int, str], None]): function to call in each iteration. It must accept 2 args which are the current percentage and latest log line |
None
|
timeout |
int
|
(int): timeout for waiting in seconds |
60
|
loop_interval |
int
|
(int): interval between task status updates in seconds |
2
|
Example
>>> import asyncio
>>> from pyfortinet.fmg_api.dvmcmd import DeviceTask
>>> from pyfortinet.fmg_api.dvmdb import RealDevice
>>> from rich.progress import Progress
>>> settings = {...}
>>> test_device = RealDevice(name="test", ip="1.1.1.1", adm_usr="test", adm_pass="<PASSWORD>")
>>> async def add_device(device: Device):
... async with AsyncFMGBase(**settings) as fmg:
... task = DeviceTask(adom=fmg.adom, device=device)
... result = await fmg.exec(task)
... with Progress() as progress:
... prog_task = progress.add_task(f"Adding device {device.name}", total=100)
... update_progress = lambda percent, log: progress.update(prog_task, percent)
... await result.wait_for_task(task, callback=update_progress)
>>> asyncio.run(add_device(test_device))
Source code in pyfortinet/fmg_api/async_fmgbase.py
AsyncFMGLockContext #
AsyncFMGLockContext(fmg: AsyncFMGBase)
Lock FMG workspace
Source code in pyfortinet/fmg_api/async_fmgbase.py
check_mode
async
#
Get workspace-mode from config
Source code in pyfortinet/fmg_api/async_fmgbase.py
commit_changes
async
#
commit_changes(adoms: Optional[list] = None, aux: bool = False) -> list[AsyncFMGResponse]
Apply workspace changes in the DB
Parameters:
Name | Type | Description | Default |
---|---|---|---|
adoms |
Optional[list]
|
list of ADOMs to commit. If empty, commit ALL ADOMs. |
None
|
aux |
bool
|
|
False
|
Returns:
Type | Description |
---|---|
list[FMGResponse]
|
List of response of operations |
Source code in pyfortinet/fmg_api/async_fmgbase.py
lock_adoms
async
#
lock_adoms(*adoms: str) -> AsyncFMGResponse
Lock adom list
If no adom specified, global workspace will be locked
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*adoms |
str
|
list of adom names |
()
|
Returns:
Type | Description |
---|---|
AsyncFMGResponse
|
Response object |
Source code in pyfortinet/fmg_api/async_fmgbase.py
unlock_adoms
async
#
unlock_adoms(*adoms) -> AsyncFMGResponse
unlock ADOMs
Source code in pyfortinet/fmg_api/async_fmgbase.py
AsyncFMGResponse
dataclass
#
AsyncFMGResponse(data: Union[dict, List[FMGObject]] = dict(), status: int = 0, success: bool = False, fmg: AsyncFMGBase = None)
Response to a request
Attributes:
Name | Type | Description |
---|---|---|
data |
dict | List[FMGObject]
|
response data |
status |
int
|
status code |
success |
bool
|
True on success |
first #
Return first data or None if result is empty
Source code in pyfortinet/fmg_api/async_fmgbase.py
auth_required #
Decorator to provide authentication for the method
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
function to handle authentication errors |
required |
Returns:
Type | Description |
---|---|
Callable
|
function with authentication handling enabled |
Source code in pyfortinet/fmg_api/async_fmgbase.py
lock #
Decorator to provide ADOM locking if needed
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func |
Callable
|
function to handle errors complaining about no locking |
required |
Returns:
Type | Description |
---|---|
Callable
|
function with lock handling enabled |