fmgbase
FMGBase connection
FMGBase #
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 |
FMGLockContext
|
Workspace lock handler |
Examples:
Possible arguments to initialize: FMGSettings
Using as context manager#
Using as function:#
>>> from pyfortinet.exceptions import FMGException
>>> settings = {...}
>>> conn = FMGBase(**settings)
>>> try:
... conn.open()
... print(conn.get_version())
... except FMGException as err:
... print(f"Error: {err}")
... finally:
... conn.close()
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/fmgbase.py
add #
add(request: Union[dict[str, Any], List[dict[str, Any]]]) -> FMGResponse
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:
>>> 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"
... }
... }
>>> with FMGBase(**settings) as fmg:
... fmg.add(address_request)
Returns:
| Type | Description |
|---|---|
FMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/fmgbase.py
clone #
clone(request: Union[dict[str, Any], List[dict[str, Any]]], create_task: bool = False) -> FMGResponse
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:
>>> settings = {...}
>>> clone_request = {
... "url": "/pm/config/global/obj/firewall/address/test-address", # source object
... "data": {
.... "name": "clone-address", # destination object
... }
... }
>>> with FMGBase(**settings) as fmg:
... fmg.clone(clone_request)
Returns:
| Type | Description |
|---|---|
FMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/fmgbase.py
close #
close connection
Source code in pyfortinet/fmg_api/fmgbase.py
delete #
delete(request: Union[dict[str, Any], List[dict[str, Any]]]) -> FMGResponse
Delete operation
Notes
Multiple requests in one call is supported
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Union[dict[str, Any], List[dict[str, Any]]]
|
Delete operation's data structure |
required |
Examples:
>>> settings = {...}
>>> address_request = {
... "url": "/pm/config/global/obj/firewall/address/test-address",
... }
>>> with FMGBase(**settings) as fmg:
... fmg.delete(address_request)
Returns:
| Type | Description |
|---|---|
FMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/fmgbase.py
exec #
exec(request: dict[str, str]) -> FMGResponse
Execute on FMG
This method currently does not support multiple requests in one call!
Source code in pyfortinet/fmg_api/fmgbase.py
get #
get(request: dict[str, Any]) -> FMGResponse
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:
>>> address_request = {
... "url": "/pm/config/global/obj/firewall/address",
... "filter": [ ["name", "==", "test-address"] ],
... "fields": [ "name", "subnet" ]
... }
>>> settings = {...}
>>> with FMGBase(**settings) as fmg:
... fmg.get(address_request)
Returns:
| Type | Description |
|---|---|
FMGResponse
|
response object with data |
Source code in pyfortinet/fmg_api/fmgbase.py
get_version #
Gather FMG version
Source code in pyfortinet/fmg_api/fmgbase.py
lock_adom #
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/fmgbase.py
open #
open() -> FMGBase
open connection
Source code in pyfortinet/fmg_api/fmgbase.py
set #
set(request: Union[dict[str, Any], List[dict[str, Any]]]) -> FMGResponse
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:
>>> 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"
... }
... }
>>> with FMGBase(**settings) as fmg:
>>> fmg.set(address_request)
Returns:
| Type | Description |
|---|---|
FMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/fmgbase.py
unset #
unset(request: Union[dict[str, Any], list[dict[str, Any]]]) -> FMGResponse
Unset the tables identified by low-level request dictionaries.
Source code in pyfortinet/fmg_api/fmgbase.py
update #
update(request: Union[dict[str, Any], List[dict[str, Any]]]) -> FMGResponse
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:
>>> 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"
... }
... }
>>> with FMGBase(**settings) as fmg:
>>> fmg.update(address_request)
Returns:
| Type | Description |
|---|---|
FMGResponse
|
Result of operation |
Source code in pyfortinet/fmg_api/fmgbase.py
wait_for_task #
wait_for_task(
task_res: Union[int, FMGResponse],
callback: 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, FMGResponse]
|
Task ID or a response containing task metadata. |
required |
callback
|
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/fmgbase.py
FMGLockContext #
FMGLockContext(fmg: FMGBase)
Lock FMG workspace
Source code in pyfortinet/fmg_api/fmgbase.py
check_mode #
Discover the effective workspace mode for the requested or selected ADOM.
Source code in pyfortinet/fmg_api/fmgbase.py
commit_changes #
commit_changes(adoms: Optional[list] = None, aux: bool = False) -> list[FMGResponse]
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[FMGResponse]
|
Responses for the commit operations. This does not install configuration onto devices. |
Source code in pyfortinet/fmg_api/fmgbase.py
lock_adoms #
lock_adoms(*adoms: str) -> FMGResponse
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 |
|---|---|
FMGResponse
|
Response object |
Source code in pyfortinet/fmg_api/fmgbase.py
unlock_adoms #
unlock_adoms(*adoms) -> FMGResponse
unlock ADOMs
Source code in pyfortinet/fmg_api/fmgbase.py
FMGResponse
dataclass
#
FMGResponse(
data: Union[Dict[str, str], List[dict], List[FMGObject]] = dict(), success: bool = False, fmg: FMGBase = None
)
Response to a request
Attributes:
| Name | Type | Description |
|---|---|---|
data |
dict | List[FMGObject]
|
response data |
success |
bool
|
True on success |
fmg |
FMGBase
|
FMG object tied to this response |
first #
Return first data or None if result is empty
Source code in pyfortinet/fmg_api/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/fmgbase.py
error_handling #
Decorator to provide error handling for the methods
Source code in pyfortinet/fmg_api/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 |