Skip to content

Commit 401b2c7

Browse files
committed
2.0.1 (2024-01-24)
------------------ **Fixed:** FortigateAPI `get`, `update` methods for objects without UID
1 parent 49ef728 commit 401b2c7

File tree

16 files changed

+421
-272
lines changed

16 files changed

+421
-272
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Unreleased
1212
**New:** `FortiGateAPI.monitor` connectors, to work with all `Monitor API` endpoints.
1313

1414

15+
2.0.1 (2024-01-24)
16+
------------------
17+
**Fixed:** FortigateAPI `get`, `update` methods for objects without UID
18+
19+
1520
2.0.0 (2024-01-16)
1621
------------------
1722

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
project = "fortigate-api"
99
copyright = "2021, Vladimirs Prusakovs"
1010
author = "Vladimirs Prusakovs"
11-
release = "2.0.0"
11+
release = "2.0.1"
1212

1313
extensions = [
1414
"sphinx.ext.autodoc",

docs/fortigateapi/cmdb/system/global_.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,42 @@ FortiGateAPI.cmdb.system.global_
77
:inherited-members:
88
:class-doc-from: class
99

10+
11+
Usage
12+
-----
13+
14+
.. code:: python
15+
16+
"""api/v2/cmdb/system/global
17+
18+
- Update data in the Fortigate
19+
- Get data from the Fortigate
20+
"""
21+
22+
from pprint import pprint
23+
24+
from fortigate_api import FortiGateAPI
25+
26+
HOST = "host"
27+
USERNAME = "username"
28+
PASSWORD = "password"
29+
30+
api = FortiGateAPI(host=HOST, username=USERNAME, password=PASSWORD)
31+
32+
# Update data in the Fortigate
33+
data = {"timezone": 80}
34+
response = api.cmdb.system.global_.update(data)
35+
print(f"update {response}") # update <Response [200]>
36+
37+
# Get data from the Fortigate
38+
result = api.cmdb.system.global_.get()
39+
pprint(result)
40+
# [{"admin-concurrent": "enable",
41+
# "admin-console-timeout": 300,
42+
# "admin-hsts-max-age": 15552000,
43+
# "timezone": "80",
44+
# ...
45+
46+
api.logout()
47+
48+

examples/cmdb/system/global_.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""api/v2/cmdb/system/global
2+
3+
- Update data in the Fortigate
4+
- Get data from the Fortigate
5+
"""
6+
7+
from pprint import pprint
8+
9+
from fortigate_api import FortiGateAPI
10+
11+
HOST = "host"
12+
USERNAME = "username"
13+
PASSWORD = "password"
14+
15+
api = FortiGateAPI(host=HOST, username=USERNAME, password=PASSWORD)
16+
17+
# Update data in the Fortigate
18+
data = {"timezone": 80}
19+
response = api.cmdb.system.global_.update(data)
20+
print(f"update {response}") # update <Response [200]>
21+
22+
# Get data from the Fortigate
23+
result = api.cmdb.system.global_.get()
24+
pprint(result)
25+
# [{"admin-concurrent": "enable",
26+
# "admin-console-timeout": 300,
27+
# "admin-hsts-max-age": 15552000,
28+
# "timezone": "80",
29+
# ...
30+
31+
api.logout()

fortigate_api/cmdb/system/global_.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ class GlobalSC(Connector):
88

99
uid = ""
1010
_path = "api/v2/cmdb/system/global"
11+
_path_ui = "/ng/system/settings"

fortigate_api/connector.py

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def delete(
7373
- `<Response [404]>` Object not found in the Fortigate.
7474
:rtype: Response
7575
"""
76-
_ = kwargs # noqa todo extract uid key from kwargs (if uid not specified)
76+
if not uid:
77+
uid = str(kwargs.get(self.uid) or "")
7778
h.check_uid_filter(uid, filter)
7879
if filter:
7980
return self._delete_by_filter(filter)
@@ -101,25 +102,6 @@ def get(self, **kwargs) -> LDAny:
101102
items = [item]
102103
return items
103104

104-
def update(self, data: DAny) -> Response:
105-
"""Update fortigate-object on the Fortigate.
106-
107-
:param dict data: Data of the fortigate-object to update.
108-
More details can be found at https://fndn.fortinet.net for related ``PUT`` method.
109-
110-
:return: Session response.
111-
112-
- `<Response [200]>` Object successfully updated,
113-
- `<Response [404]>` Object has not been updated.
114-
:rtype: Response
115-
"""
116-
uid = str(data[self.uid])
117-
uid = h.quote(uid)
118-
if not uid:
119-
raise ValueError(f"{self.uid} value is required.")
120-
url = f"{self.url}/{uid}"
121-
return self.fortigate.put(url=url, data=data)
122-
123105
def is_exist(self, uid: StrInt) -> bool:
124106
"""Check if a fortigate-object exists in the Fortigate.
125107
@@ -136,7 +118,21 @@ def is_exist(self, uid: StrInt) -> bool:
136118
response = self.fortigate.exist(url)
137119
return response.ok
138120

139-
# =========================== helpers ===========================
121+
def update(self, data: DAny) -> Response:
122+
"""Update fortigate-object on the Fortigate.
123+
124+
:param dict data: Data of the fortigate-object to update.
125+
More details can be found at https://fndn.fortinet.net for related ``PUT`` method.
126+
127+
:return: Session response.
128+
129+
- `<Response [200]>` Object successfully updated,
130+
- `<Response [404]>` Object has not been updated.
131+
:rtype: Response
132+
"""
133+
uid: str = self._get_uid(data)
134+
url = f"{self.url}/{uid}".rstrip("/")
135+
return self.fortigate.put(url=url, data=data)
140136

141137
# noinspection PyShadowingBuiltins
142138
def _delete_by_filter(self, filter: UStr) -> Response: # pylint: disable=redefined-builtin
@@ -161,3 +157,18 @@ def _delete_by_filter(self, filter: UStr) -> Response: # pylint: disable=redefi
161157
response = self.fortigate.delete(url=url)
162158
responses.append(response)
163159
return h.highest_response(responses)
160+
161+
def _get_uid(self, data) -> str:
162+
"""Get UID value based on the UID key
163+
164+
:param data: A dictionary containing the UID value.
165+
:return: The UID value extracted from the data.
166+
:raises ValueError: If the UID is required but not present in the data.
167+
"""
168+
if not self.uid:
169+
return ""
170+
if self.uid not in data:
171+
raise ValueError(f"{self.uid} value is required.")
172+
uid = str(data[self.uid])
173+
uid = h.quote(uid)
174+
return uid

fortigate_api/schema/custom/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
"external-resource": {
5454
"path_ui": "ng/external-connector",
5555
},
56+
"global": {
57+
"path_ui": "/ng/system/settings",
58+
},
5659
"interface": {
5760
"is_custom_model": True,
5861
},

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fortigate_api"
3-
version = "2.0.0"
3+
version = "2.0.1"
44
description = "Python package to configure Fortigate (Fortios) devices using REST API and SSH"
55
authors = ["Vladimirs Prusakovs <[email protected]>"]
66
readme = "README.rst"
@@ -55,7 +55,7 @@ test = ["pytest"]
5555

5656
[tool.poetry.urls]
5757
"Bug Tracker" = "https://github.com/vladimirs-git/fortigate-api/issues"
58-
"Download URL" = "https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/2.0.0.tar.gz"
58+
"Download URL" = "https://github.com/vladimirs-git/fortigate-api/archive/refs/tags/2.0.1.tar.gz"
5959

6060
[tool.pylint]
6161
max-line-length = 100

0 commit comments

Comments
 (0)