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) |
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
|
Connect/read inactivity timeout per transport attempt, 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: Union[dict[str, Any], List[dict[str, Any]]]) -> AsyncFMGResponse
Add operation
Notes
Multiple requests in one call is supported
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Union[dict[str, Any], List[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
clone
async
#
clone(request: Union[dict[str, Any], List[dict[str, Any]]], create_task: bool = False) -> AsyncFMGResponse
Clone operation
Notes
Multiple requests in one call is supported
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
dict
|
Clone operation's data structure |
required |
create_task
|
bool
|
Wheter to create task |
False
|
Examples:
>>> import asyncio
>>> settings = {...}
>>> clone_request = {
... "url": "/pm/config/global/obj/firewall/address/test-address", # source object
... "data": {
.... "name": "clone-address", # destination object
... }
... }
>>> async def clone_address(request):
... async with AsyncFMGBase(**settings) as fmg:
... await fmg.clone(request)
>>> asyncio.run(clone_address(clone_request))
Returns:
| Type | Description |
|---|---|
FMGResponse
|
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: Union[dict[str, Any], List[dict[str, Any]]]) -> AsyncFMGResponse
Delete operation
Notes
Multiple requests in one call is supported
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Union[dict[str, Any], List[dict[str, Any]]]
|
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(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
This method currently does not support multiple requests in one call!
Source code in pyfortinet/fmg_api/async_fmgbase.py
get
async
#
get(request: dict[str, Any]) -> AsyncFMGResponse
Get info from FMG
This method currently does not support multiple requests in one call!
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
lock_adom
async
#
Locks specified ADOM explicitly
The usual error based locking not always work. For example FMG replies authentication error while running a job to create new Device in device database. This makes error handling unusable and therefore this context manager can be used to lock ADOM for the job run duration.
If adom was already locked, this function won't lock it again and won't unlock it either.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adom
|
Optional[str]
|
ADOM to be locked as string. If not specified, currently used ADOM will be used. |
None
|
keep_locked
|
bool
|
If True, the lock is not released after operation. (default: False) |
False
|
Yields:
| Type | Description |
|---|---|
str
|
adom name (not used normally) |
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: Union[dict[str, Any], List[dict[str, Any]]]) -> AsyncFMGResponse
Set operation
Notes
Multiple requests in one call is supported
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Union[dict[str, Any], List[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
unset
async
#
unset(request: Union[dict[str, Any], list[dict[str, Any]]]) -> AsyncFMGResponse
Unset the tables identified by low-level request dictionaries.
Source code in pyfortinet/fmg_api/async_fmgbase.py
update
async
#
update(request: Union[dict[str, Any], List[dict[str, Any]]]) -> AsyncFMGResponse
Update operation
Notes
Multiple requests in one call is supported
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Union[dict[str, Any], List[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: Union[Coroutine[Any, Any, None], Callable[[int, str], None]] = None,
timeout: int = 60,
loop_interval: int = 2,
) -> Union[str, None]
Poll a FortiManager task until it reaches a terminal state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
task_res
|
Union[int, AsyncFMGResponse]
|
Task ID or a response containing task metadata. |
required |
callback
|
Union[Coroutine[Any, Any, None], Callable[[int, str], None]]
|
Optional callable receiving the completion percentage and latest log line. |
None
|
timeout
|
int
|
Polling timeout in seconds. |
60
|
loop_interval
|
int
|
Delay between status reads in seconds. |
2
|
Returns:
| Type | Description |
|---|---|
Union[str, None]
|
str or None: Terminal task state, or None if no task can be polled or a status read fails. |
Union[str, None]
|
Check for "done" before treating the task as completed successfully. |
Examples:
Inside an open connection, use a task ID returned by an earlier execution request:
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
#
Discover the effective workspace mode for the requested or selected ADOM.
Source code in pyfortinet/fmg_api/async_fmgbase.py
commit_changes
async
#
commit_changes(adoms: Optional[list] = None, aux: bool = False) -> list[AsyncFMGResponse]
Commit workspace changes in FortiManager's database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adoms
|
Optional[list]
|
ADOM names to commit. If omitted or empty, use the ADOMs locked by this client. |
None
|
aux
|
bool
|
Use the policy/object database workspace commit route. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
list |
list[AsyncFMGResponse]
|
Responses for the commit operations. This does not install configuration onto devices. |
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(fmg: AsyncFMGBase = None)
Bases: FMGResponse
Response to a request
Attributes:
| Name | Type | Description |
|---|---|---|
data |
dict | List[FMGObject]
|
response data |
success |
bool
|
True on success |
fmg |
AsyncFMGBase
|
FMG object tied to this response |
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
error_handling #
Decorator to provide error handling for the methods
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 |