Skip to content

Commit

Permalink
2.0.1 (2024-01-24)
Browse files Browse the repository at this point in the history
------------------
**Fixed:** FortigateAPI `get`, `update` methods for objects without UID
  • Loading branch information
vladimirs-git committed Jan 24, 2024
1 parent 49ef728 commit 401b2c7
Show file tree
Hide file tree
Showing 16 changed files with 421 additions and 272 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Unreleased
**New:** `FortiGateAPI.monitor` connectors, to work with all `Monitor API` endpoints.


2.0.1 (2024-01-24)
------------------
**Fixed:** FortigateAPI `get`, `update` methods for objects without UID


2.0.0 (2024-01-16)
------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
project = "fortigate-api"
copyright = "2021, Vladimirs Prusakovs"
author = "Vladimirs Prusakovs"
release = "2.0.0"
release = "2.0.1"

extensions = [
"sphinx.ext.autodoc",
Expand Down
39 changes: 39 additions & 0 deletions docs/fortigateapi/cmdb/system/global_.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,42 @@ FortiGateAPI.cmdb.system.global_
:inherited-members:
:class-doc-from: class


Usage
-----

.. code:: python
"""api/v2/cmdb/system/global
- Update data in the Fortigate
- Get data from 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)
# Update data in the Fortigate
data = {"timezone": 80}
response = api.cmdb.system.global_.update(data)
print(f"update {response}") # update <Response [200]>
# Get data from the Fortigate
result = api.cmdb.system.global_.get()
pprint(result)
# [{"admin-concurrent": "enable",
# "admin-console-timeout": 300,
# "admin-hsts-max-age": 15552000,
# "timezone": "80",
# ...
api.logout()
31 changes: 31 additions & 0 deletions examples/cmdb/system/global_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""api/v2/cmdb/system/global
- Update data in the Fortigate
- Get data from 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)

# Update data in the Fortigate
data = {"timezone": 80}
response = api.cmdb.system.global_.update(data)
print(f"update {response}") # update <Response [200]>

# Get data from the Fortigate
result = api.cmdb.system.global_.get()
pprint(result)
# [{"admin-concurrent": "enable",
# "admin-console-timeout": 300,
# "admin-hsts-max-age": 15552000,
# "timezone": "80",
# ...

api.logout()
1 change: 1 addition & 0 deletions fortigate_api/cmdb/system/global_.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ class GlobalSC(Connector):

uid = ""
_path = "api/v2/cmdb/system/global"
_path_ui = "/ng/system/settings"
53 changes: 32 additions & 21 deletions fortigate_api/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ def delete(
- `<Response [404]>` Object not found in the Fortigate.
:rtype: Response
"""
_ = kwargs # noqa todo extract uid key from kwargs (if uid not specified)
if not uid:
uid = str(kwargs.get(self.uid) or "")
h.check_uid_filter(uid, filter)
if filter:
return self._delete_by_filter(filter)
Expand Down Expand Up @@ -101,25 +102,6 @@ def get(self, **kwargs) -> LDAny:
items = [item]
return items

def update(self, data: DAny) -> Response:
"""Update fortigate-object on the Fortigate.
:param dict data: Data of the fortigate-object to update.
More details can be found at https://fndn.fortinet.net for related ``PUT`` method.
:return: Session response.
- `<Response [200]>` Object successfully updated,
- `<Response [404]>` Object has not been updated.
:rtype: Response
"""
uid = str(data[self.uid])
uid = h.quote(uid)
if not uid:
raise ValueError(f"{self.uid} value is required.")
url = f"{self.url}/{uid}"
return self.fortigate.put(url=url, data=data)

def is_exist(self, uid: StrInt) -> bool:
"""Check if a fortigate-object exists in the Fortigate.
Expand All @@ -136,7 +118,21 @@ def is_exist(self, uid: StrInt) -> bool:
response = self.fortigate.exist(url)
return response.ok

# =========================== helpers ===========================
def update(self, data: DAny) -> Response:
"""Update fortigate-object on the Fortigate.
:param dict data: Data of the fortigate-object to update.
More details can be found at https://fndn.fortinet.net for related ``PUT`` method.
:return: Session response.
- `<Response [200]>` Object successfully updated,
- `<Response [404]>` Object has not been updated.
:rtype: Response
"""
uid: str = self._get_uid(data)
url = f"{self.url}/{uid}".rstrip("/")
return self.fortigate.put(url=url, data=data)

# noinspection PyShadowingBuiltins
def _delete_by_filter(self, filter: UStr) -> Response: # pylint: disable=redefined-builtin
Expand All @@ -161,3 +157,18 @@ def _delete_by_filter(self, filter: UStr) -> Response: # pylint: disable=redefi
response = self.fortigate.delete(url=url)
responses.append(response)
return h.highest_response(responses)

def _get_uid(self, data) -> str:
"""Get UID value based on the UID key
:param data: A dictionary containing the UID value.
:return: The UID value extracted from the data.
:raises ValueError: If the UID is required but not present in the data.
"""
if not self.uid:
return ""
if self.uid not in data:
raise ValueError(f"{self.uid} value is required.")
uid = str(data[self.uid])
uid = h.quote(uid)
return uid
3 changes: 3 additions & 0 deletions fortigate_api/schema/custom/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
"external-resource": {
"path_ui": "ng/external-connector",
},
"global": {
"path_ui": "/ng/system/settings",
},
"interface": {
"is_custom_model": True,
},
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "fortigate_api"
version = "2.0.0"
version = "2.0.1"
description = "Python package to configure Fortigate (Fortios) devices using REST API and SSH"
authors = ["Vladimirs Prusakovs <[email protected]>"]
readme = "README.rst"
Expand Down Expand Up @@ -55,7 +55,7 @@ test = ["pytest"]

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

[tool.pylint]
max-line-length = 100
Expand Down
Loading

0 comments on commit 401b2c7

Please sign in to comment.