FortiGateAPI.cmdb.system_dhcp.server
- class fortigate_api.cmdb.system_dhcp.ServerSdC(fortigate: FortiGate, **kwargs)
Web UI
API
Data
- uid: str = 'id'
Unique identifier of fortigate-object.
- create(data: Dict[str, Any]) Response
Create the fortigate-object in the Fortigate.
- Parameters:
data (dict) – Data of the fortigate-object. More details can be found at https://fndn.fortinet.net for related
POSTmethod.- Returns:
Session response.
<Response [200]> Object successfully created,
<Response [500]> Object already exists.
- Return type:
Response
- delete(uid: str | int = '', filter: str | Sequence[str] = '', **kwargs) Response
Delete the fortigate-object from the Fortigate.
- Parameters:
uid (str or int) – Identifier of the fortigate-object. Used to delete a single object.
filter (str or List[str]) – Filter fortigate-objects by one or multiple Filtering conditions. Used to delete multiple objects.
kwargs – Fortigate REST API parameters. More details can be found at https://fndn.fortinet.net for related
DELETEmethod.
- Returns:
Session response.
<Response [200]> Object successfully deleted,
<Response [404]> Object not found in the Fortigate.
- Return type:
Response
- get(**kwargs) List[Dict[str, Any]]
Get fortigate-objects, all or filtered by some parameters.
- Parameters:
kwargs – Fortigate REST API parameters.
filter- Filter fortigate-objects by one or multiple Filtering conditions. More details can be found at https://fndn.fortinet.net for relatedGETmethod.- Returns:
List of the fortigate-objects.
- Return type:
List[dict]
- is_exist(uid: str | int) bool
Check if a fortigate-object exists in the Fortigate.
- Parameters:
uid (str or int) – Identifier of the fortigate-object.
- Returns:
True - object exists, False - object does not exist.
- Return type:
bool
- update(data: Dict[str, Any]) Response
Update fortigate-object on the Fortigate.
- Parameters:
data (dict) – Data of the fortigate-object to update. More details can be found at https://fndn.fortinet.net for related
PUTmethod.- Returns:
Session response.
<Response [200]> Object successfully updated,
<Response [404]> Object has not been updated.
- Return type:
Response
- property url: str
URL to the fortigate-object.
Usage
"""api/v2/cmdb/system.dhcp/server
- Create dhcp-server
- Get all dhcp-servers
- Filter dhcp-servers by id (unique identifier)
- Filter dhcp-servers by operator contains `=@`
- Filter dhcp-servers by multiple conditions
- Update dhcp-server data in the Fortigate
- Delete dhcp-server from the Fortigate by name
- Delete dhcp-servers from the Fortigate by filter
- Check for presence of dhcp-server in the Fortigate
"""
from pprint import pprint
from fortigate_api import FortiGateAPI
HOST = "host"
USERNAME = "username"
PASSWORD = "password"
api = FortiGateAPI(host=HOST, username=USERNAME, password=PASSWORD)
# Create dhcp-server
data = {
"default-gateway": "192.168.255.1",
"netmask": "255.255.255.0",
"interface": "port10",
"ip-range": [{"start-ip": "192.168.255.2", "end-ip": "192.168.255.254", }],
}
response = api.cmdb.system_dhcp.server.create(data)
print("server.create", response) # server.create <Response [200]>
# Get all dhcp-servers
items = api.cmdb.system_dhcp.server.get()
print(f"dhcp-servers count={len(items)}") # dhcp-servers count=3
# Filter dhcp-servers by id (unique identifier)
uid = items[-1]["id"]
items = api.cmdb.system_dhcp.server.get(id=uid)
print(f"dhcp-servers {uid=} count={len(items)}") # dhcp-servers uid=7 count=1
pprint(items)
# [{"id": 7,
# "interface": "port10",
# "ip-mode": "range",
# "ip-range": [{"end-ip": "192.168.255.254",
# "id": 1,
# "q_origin_key": 1,
# "start-ip": "192.168.255.2"}],
# ...
# }]
# Filter dhcp-servers by operator contains `=@`
items = api.cmdb.system_dhcp.server.get(filter="interface=@10")
print(f"dhcp-servers count={len(items)}") # dhcp-servers count=1
# Filter dhcp-servers by multiple conditions
items = api.cmdb.system_dhcp.server.get(
filter=["default-gateway==192.168.255.1", "interface=@port10"])
print(f"dhcp-servers count={len(items)}") # dhcp-servers count=1
# Update dhcp-server data in the Fortigate
data = {"id": uid, "dns-server1": "10.0.0.1"}
response = api.cmdb.system_dhcp.server.update(data)
print("dhcp-server.update", response, response.ok) # dhcp-server.update <Response [200]> True
# Delete dhcp-server from the Fortigate by name
response = api.cmdb.system_dhcp.server.delete(uid)
print("dhcp-server.delete", response, response.ok) # dhcp-server.delete <Response [200]> True
# Delete dhcp-servers from the Fortigate by filter
response = api.cmdb.system_dhcp.server.delete(filter="interface==port10")
print("dhcp-server.delete", response, response.ok) # dhcp-server.delete <Response [200]> True
# Check for presence of dhcp-server in the Fortigate
response = api.cmdb.system_dhcp.server.is_exist(uid)
print("dhcp-server.is_exist", response) # dhcp-server.is_exist False
api.logout()