Skip to content

Commit 7b27a36

Browse files
refactor: Improve add_device, get_device methods in inventory_api. Add optional config_only parameter to update_device in inventory_app. Bump version for 0.2.3 release
1 parent 9eb245e commit 7b27a36

3 files changed

Lines changed: 43 additions & 44 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "videoipath-automation-tool"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
description = "A Python package for automating VideoIPath configuration workflows."
55
license = { text = "AGPL-3.0-only" }
66
license-files = ["LICENSE"]

src/videoipath_automation_tool/apps/inventory/app/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ def add_device(
8888
)
8989

9090
online_device = self._inventory_api.add_device(device, config_only=config_only)
91-
self._logger.info(f"Device '{online_device.label}' added to Inventory with id '{online_device.device_id}'.")
91+
self._logger.info(
92+
f"Device '{online_device.label}' added successfully to Inventory with id '{online_device.device_id}'."
93+
)
9294

9395
return online_device
9496

9597
def update_device(
96-
self, device: InventoryDevice[CustomSettingsType], compare_config: bool = True
98+
self, device: InventoryDevice[CustomSettingsType], compare_config: bool = True, config_only: bool = False
9799
) -> InventoryDevice[CustomSettingsType]:
98100
"""
99101
Method to update a devices configuration in VideoIPath-Inventory.
@@ -102,6 +104,7 @@ def update_device(
102104
Args:
103105
device (InventoryDevice): Device to update in Inventory.
104106
compare_config (bool, optional): Compare the configuration of the device with the existing device configuration in Inventory, to prevent unnecessary updates. Defaults to True.
107+
config_only (bool, optional): Update device with configuration only. Defaults to False.
105108
106109
Raises:
107110
ValueError: If no device_id is given in device configuration (in InventoryDevice instance).
@@ -130,15 +133,15 @@ def update_device(
130133
if filtered_diffs.added or filtered_diffs.changed or filtered_diffs.removed:
131134
debug_message = self._hide_passwords_in_diffs(filtered_diffs.model_dump(mode="json"))
132135
self._logger.info(
133-
f"Device configuration changed. Updating device in Inventory. " f"Changes: {debug_message}"
136+
f"Device configuration changed. Updating device in Inventory. Changes: {debug_message}"
134137
)
135138
else:
136139
self._logger.info("Device configuration did not change. No update necessary.")
137140
return device
138141
else:
139142
self._logger.info("Updating device in Inventory without comparing configuration.")
140143

141-
online_device = self._inventory_api.update_device(device)
144+
online_device = self._inventory_api.update_device(device, config_only=config_only)
142145
self._logger.info(f"Device '{online_device.label}' updated in Inventory with id '{online_device.device_id}'.")
143146
return online_device
144147

src/videoipath_automation_tool/apps/inventory/inventory_api.py

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ def get_device(
5858
if not device_id.startswith("device"):
5959
raise ValueError("device_id must start with 'device'.")
6060
self._logger.debug(f"Retrieving device '{device_id}' from VideoIPath-Inventory.")
61+
if custom_settings_type:
62+
self._logger.debug(
63+
f"Using custom settings type '{custom_settings_type.__name__}' for device '{device_id}'."
64+
)
65+
6166
online_device = self._fetch_device_config(device_id)
67+
6268
if not config_only:
6369
if not isinstance(status_fetch_retry, int):
6470
self._logger.warning("status_fetch_retry must be an integer. Using default value of 20.")
@@ -80,7 +86,7 @@ def get_device(
8086
break
8187
except ValueError:
8288
self._logger.debug(
83-
f"Failed to get device status for device '{online_device.configuration.id}', retrying ({21-retry_cnt}/{status_fetch_retry}) ..."
89+
f"Failed to get device status for device '{online_device.configuration.id}', retrying ({21 - retry_cnt}/{status_fetch_retry}) ..."
8490
)
8591
time.sleep(status_fetch_delay)
8692
retry_cnt -= 1
@@ -90,6 +96,7 @@ def get_device(
9096
)
9197
else:
9298
self._logger.debug(f"Skipping status update for device '{device_id}'.")
99+
93100
self._logger.debug(f"Device '{device_id}' retrieved from VideoIPath-Inventory.")
94101
return online_device
95102

@@ -120,11 +127,11 @@ def add_device(
120127
if device.configuration.id != "":
121128
raise ValueError(
122129
"Device ID must be empty when adding a new device! "
123-
"Set 'id' to an empty string 'InventoryDevice.remove_device_id()' or use 'inventory_api.update_device()' for existing devices."
130+
"Set 'id' to an empty string 'InventoryDevice.remove_device_id()' or use 'inventory_api.update_device()' if an existing device should be updated."
124131
)
125132

126133
self._logger.debug(
127-
f"Adding new device with label '{device.configuration.config.desc.label}' to VideoIPath-Inventory."
134+
f"Adding new device with label '{device.configuration.config.desc.label}' and address '{device.configuration.config.cinfo.address}' to VideoIPath-Inventory."
128135
)
129136

130137
tracking_id = str(uuid4())
@@ -158,50 +165,39 @@ def add_device(
158165

159166
online_device = self._fetch_device_config_by_uuid(uuid=tracking_id)
160167

161-
if not config_only:
162-
if not isinstance(status_fetch_retry, int):
163-
self._logger.warning("status_fetch_retry must be an integer. Using default value of 20.")
164-
status_fetch_retry = 20
165-
if not isinstance(status_fetch_delay, int):
166-
self._logger.warning("status_check_delay must be an integer. Using default value of 2.")
167-
status_fetch_delay = 2
168-
if status_fetch_retry < 1:
169-
self._logger.warning("status_fetch_retry must be greater than 0. Using default value of 20.")
170-
status_fetch_retry = 20
171-
if status_fetch_delay < 1:
172-
self._logger.warning("status_check_delay must be greater than 0. Using default value of 2.")
173-
status_fetch_delay = 2
174-
retry_cnt = status_fetch_retry
175-
while retry_cnt > 0:
176-
try:
177-
online_device.status = self._fetch_device_status(online_device.configuration.id)
178-
if online_device.status:
179-
break
180-
except ValueError:
181-
self._logger.debug(
182-
f"Failed to get device status for device '{online_device.configuration.id}', retrying ({21-retry_cnt}/{status_fetch_retry}) ..."
183-
)
184-
time.sleep(status_fetch_delay)
185-
retry_cnt -= 1
186-
if retry_cnt == 0 and not online_device.status:
187-
self._logger.warning(
188-
f"Failed to get device status for device '{online_device.configuration.id}'. Retry limit reached. Returning device without status."
189-
)
190-
self._logger.debug(f"Device added successfully with id: {online_device.configuration.id}")
168+
self._logger.debug(
169+
f"Device with id '{online_device.configuration.id}' added successfully to VideoIPath-Inventory."
170+
)
191171

192-
if clear_uuid_after_add:
172+
if not clear_uuid_after_add:
173+
self._logger.debug("Skip removing tracking ID ('uuid' meta field) from device configuration.")
174+
else:
193175
modified_device = online_device.model_copy(deep=True)
194176
modified_device.configuration.meta.pop("uuid")
195177
self._logger.debug(
196178
"Remove tracking ID ('uuid' meta field) from device configuration and update device in VideoIPath-Inventory."
197179
)
180+
online_device = self.update_device(
181+
device=modified_device, config_only=True
182+
) # Use config_only=True to avoid status fetch and speed up the process
183+
self._logger.debug("Tracking ID removed successfully from device configuration.")
184+
185+
if config_only:
186+
self._logger.debug("Skip fetching device status after adding.")
187+
else:
188+
self._logger.warning(
189+
"Fetching device status is enabled and may take up to 30 seconds for devices, which are not accessible for VideoIPath-Server (e.g. offline devices). "
190+
"To speed up the process for those devices, `add_device(..., config_only=True)` can be used and the status retrieval can be omitted."
191+
)
192+
online_device = self.get_device(
193+
device_id=online_device.configuration.id,
194+
config_only=config_only,
195+
status_fetch_delay=status_fetch_delay,
196+
status_fetch_retry=status_fetch_retry,
197+
)
198198

199-
self.update_device(device=modified_device)
200-
if response.header.status != "OK":
201-
raise ValueError(f"Failed to update device in VideoIPath-Inventory. Error: {response}")
199+
self._logger.debug(f"Device added successfully with id: {online_device.configuration.id}")
202200

203-
online_device = modified_device
204-
self._logger.debug("Tracking ID removed successfully from device configuration.")
205201
return online_device
206202

207203
def update_device(

0 commit comments

Comments
 (0)