Merge pull request #54 from DaanSelen/feat-remove-dev

feat: remove devices function

resolves #52
This commit is contained in:
Josiah Baldwin
2025-09-26 15:32:09 -07:00
committed by GitHub
3 changed files with 46 additions and 1 deletions

View File

@@ -295,6 +295,23 @@ class Device(object):
'''
return await self._session.reset_devices(self.nodeid, timeout=timeout)
async def remove(self, timeout=None):
'''
Remove device from MeshCentral
Args:
nodeids (str|list[str]): nodeid(s) of the device(s) that have to be removed
timeout (int): duration in seconds to wait for a response before throwing an error
Returns:
bool: True on success, raise otherwise
Raises:
:py:class:`~meshctrl.exceptions.SocketError`: Info about socket closure
asyncio.TimeoutError: Command timed out
'''
return self._session.remove_devices(self.nodeid, timeout)
async def sleep(self, timeout=None):
'''
Sleep device

View File

@@ -571,7 +571,7 @@ class Session(object):
while True:
data = await event_queue.get()
if filter and not util.compare_dict(filter, data):
continue
continue
yield data
finally:
self._eventer.off("server_event", _)
@@ -1062,6 +1062,30 @@ class Session(object):
raise exceptions.ServerError(data["result"])
return True
async def remove_devices(self, nodeids, timeout=None):
'''
Remove device(s) from MeshCentral
Args:
nodeids (str|list[str]): nodeid(s) of the device(s) that have to be removed
timeout (int): duration in seconds to wait for a response before throwing an error
Returns:
bool: True on success, raise otherwise
Raises:
:py:class:`~meshctrl.exceptions.ServerError`: Error text from server if there is a failure
:py:class:`~meshctrl.exceptions.SocketError`: Info about socket closure
asyncio.TimeoutError: Command timed out
'''
if isinstance(nodeids, str):
nodeids = [nodeids]
data = await self._send_command({ "action": 'removedevices', "nodeids": nodeids}, "remove_devices", timeout=timeout)
if data.get("result", "ok").lower() != "ok":
raise exceptions.ServerError(data["result"])
return True
async def add_device_group(self, name, description="", amtonly=False, features=0, consent=0, timeout=None):
'''

View File

@@ -336,6 +336,10 @@ async def test_mesh_device(env):
r = await admin_session.remove_users_from_device_group((await privileged_session.user_info())["_id"], mesh.meshid, timeout=10)
print("\ninfo remove_users_from_device_group: {}\n".format(r))
assert (r[(await privileged_session.user_info())["_id"]]["success"]), "Failed to remove user from device group"
# Dunno how to test if this actually works, so just check for errors, I guess.
admin_session.remove_devices(agent.nodeid, timeout=10)
assert (await admin_session.remove_users_from_device(agent.nodeid, (await unprivileged_session.user_info())["_id"], timeout=10)), "Failed to remove user from device"