Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update glpi_api.py to support update/add/delete sub_items #22

Merged
merged 3 commits into from
Jan 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions glpi_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,35 @@ def add(self, itemtype, *items):
400: _glpi_error,
401: _glpi_error
}.get(response.status_code, _unknown_error)(response)

@_catch_errors
def add_sub_items(self, itemtype, item_id, sub_itemtype, *items):
"""`API documentation
Same method used as get-sub-items, same parameter
<https://github.com/glpi-project/glpi/blob/master/apirest.md#get-sub-items>`__

Add a collection of rows of the ``sub_itemtype`` for the identified
item of type ``itemtype`` and id ``item_id``. ``kwargs`` contains
additional parameters allowed by the API.

.. code::

# Add a operatingsystem of a computer.
>>> In [241]: glpi.add_sub_items('Computer',1,'Item_OperatingSystem',{'items_id': 1 ,'itemtype':'Computer','operatingsystems_id':1 })
[{'id': 261,
'itemtype': 'Computer',
'items_id': 1,
...
"""
url = self._set_method(itemtype, item_id, sub_itemtype)
response = self.session.post(url,
json={'input': items})
return {
201: lambda r: r.json(),
207: lambda r: r.json()[1],
400: _glpi_error,
401: _glpi_error
}.get(response.status_code, _unknown_error)(response)

@_catch_errors
def update(self, itemtype, *items):
Expand All @@ -749,10 +778,41 @@ def update(self, itemtype, *items):
json={'input': items})
return {
200: lambda r: r.json(),
201: lambda r: r.json(),
207: lambda r: r.json()[1],
400: _glpi_error,
401: _glpi_error
}.get(response.status_code, _unknown_error)(response)

@_catch_errors
def update_sub_items(self, itemtype, item_id, sub_itemtype, *items):
"""`API documentation
Same method used as get-sub-items, same parameters
<https://github.com/glpi-project/glpi/blob/master/apirest.md#get-sub-items>`__

Updates a collection of rows of the ``sub_itemtype`` for the identified
item of type ``itemtype`` and id ``item_id``. ``kwargs`` contains
additional parameters allowed by the API.

.. code::

# update the operatingsystem a computer.
>>> In [241]: glpi.update_sub_items('Computer',1,'Item_OperatingSystem' {'id': 1 ,'itemtype':'Computer','operatingsystem_id':1 })
[{'id': 261,
'itemtype': 'Computer',
'items_id': 1,
...
"""
url = self._set_method(itemtype, item_id, sub_itemtype)
response = self.session.put(url,
json={'input': items})
return {
200: lambda r: r.json(),
201: lambda r: r.json(),
207: lambda r: r.json(),
400: _glpi_error,
401: _glpi_error
}.get(response.status_code, _unknown_error)(response)

@_catch_errors
def delete(self, itemtype, *items, **kwargs):
Expand Down Expand Up @@ -783,6 +843,37 @@ def delete(self, itemtype, *items, **kwargs):
400: lambda r: _glpi_error(r) if r.json()[0] != 'ERROR_GLPI_DELETE' else r.json()[1],
401: _glpi_error
}.get(response.status_code, _unknown_error)(response)

@_catch_errors
def delete_sub_items(self, itemtype, item_id, sub_itemtype, *items):
"""`API documentation
Same method used as get-sub-items, same parameters
<https://github.com/glpi-project/glpi/blob/master/apirest.md#get-sub-items>`__

deletes a collection of rows of the ``sub_itemtype`` for the identified
item of type ``itemtype`` and id ``item_id``. ``kwargs`` contains
additional parameters allowed by the API.

.. code::

# delete the operatingsystem a computer.
>>> In [241]: glpi.delete_sub_items('Computer',1,'Item_OperatingSystem' {'id': 1 ,'itemtype':'Computer','operatingsystem_id':1 })
[{'id': 261,
'itemtype': 'Computer',
'items_id': 1,
...
"""
url = self._set_method(itemtype, item_id, sub_itemtype)
response = self.session.delete(url,
json={'input': items})
return {
200: lambda r: r.json(),
204: lambda r: r.json(),
207: lambda r: r.json()[1],
400: lambda r: _glpi_error(r) if r.json()[0] != 'ERROR_GLPI_DELETE' else r.json()[1],
401: _glpi_error
}.get(response.status_code, _unknown_error)(response)


@_catch_errors
def upload_document(self, name, filepath):
Expand Down