unistra / python-glpi-api

Python module for interacting with GLPI using the API.
GNU General Public License v3.0
18 stars 10 forks source link

Assign a group to a ticket #7

Closed jsalatiel closed 3 years ago

jsalatiel commented 3 years ago

Hi, thanks again for this awesome project!

This is not really a issue, but more like a help request. I read a lot the glpi documentation and this module's documentation and I already can do lots of things, but there is one "simple" thing that I am not able to do: I can not find a way to assign a group to a ticket.

I have tried: glpi.update('Group_Ticket' , { 'tickets_id': 2021060118, 'groups_id': 16, type: '2' } )

Do you think you could help me with this ?

I found a PHP code that works, but I would lile to use only python for this. This is the PHP code that works( extracted from this link ):

$url=$api_url . "/Ticket/".$ticket_id."/group_ticket/";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $json = curl_exec($ch);
        curl_close ($ch);
        $obj = json_decode($json,true);

        $gp_ticket_id= $obj[0][id]; //Get the ID value

        //And Update it //

        $url=$api_url . "/group_ticket/".$gp_ticket_id."/";
        $input='{ "input": {"id" : "'.$gp_ticket_id.'","groups_id": "'.$group_id.'","type": "2","use_notification": "1"}}';

            $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
                curl_setopt($ch, CURLOPT_POSTFIELDS,$input);
                $request_result = curl_exec($ch);
                curl_close ($ch);
                $obj = json_decode($request_result,true);
            print_r($request_result);
fmenabe commented 3 years ago

Apparently, tickets assignements are managed by sub items: Ticket_User for users, Group_Ticket for groups and Supplier_Ticket for suppliers

So, to manage ticket assignments, you need to manipulate theses objects.

For example:

# Create a ticket
>>> glpi.add('Ticket', {'name': 'Test', 'content': 'API test'})
[{'id': 9, 'message': 'Item Successfully Added: Test'}]

# List default affiliations (by default, requester and assignment are set to the current user)
>>> glpi.get_sub_items('Ticket', 9, 'Ticket_User')
[{'id': 20,
  'tickets_id': 9,
  'users_id': 2,
  'type': 1,
  'use_notification': 1,
  'alternative_email': '',
  'links': [{'rel': 'Ticket',
    'href': 'http://127.0.0.1:8000/apirest.php/Ticket/9'},
   {'rel': 'User', 'href': 'http://127.0.0.1:8000/apirest.php/User/2'}]},
 {'id': 21,
  'tickets_id': 9,
  'users_id': 2,
  'type': 2,
  'use_notification': 1,
  'alternative_email': '',
  'links': [{'rel': 'Ticket',
    'href': 'http://127.0.0.1:8000/apirest.php/Ticket/9'},
   {'rel': 'User', 'href': 'http://127.0.0.1:8000/apirest.php/User/2'}]}]
>>> glpi.get_sub_items('Ticket', 9, 'Group_Ticket')
[]
>>> glpi.get_sub_items('Ticket', 9, 'Supplier_Ticket')
[]

# Change requester to another user.
glpi.update('Ticket_User', {'id': 20, 'users_id': 5})

# Delete user assignment
glpi.delete('Ticket_User', {'id': 21})

# Assign ticket to a group
glpi.add('Group_Ticket', {'tickets_id': 9, 'groups_id': 1, 'type': 2})

# List affiliations
>>> glpi.get_sub_items('Ticket', 9, 'Ticket_User')
[{'id': 20,
  'tickets_id': 9,
  'users_id': 5,
  'type': 1,
  ...
>>> glpi.get_sub_items('Ticket', 9, 'Group_Ticket')
[{'id': 16,
  'tickets_id': 9,
  'groups_id': 1,
  'type': 2,
  ...
>>> glpi.get_sub_items('Ticket', 9, 'Supplier_Ticket')
[]

Affiliation types are:

As for users (see the prevous issue), it seems possible to set groups affiliations at the creation of the ticket using _groups_id_requester, _groups_id_assign and _groups_id_observer fields:

>>> glpi.add('Ticket', {'name': 'Test', 'content': 'API test', '_users_id_requester': [3], '_groups_id_assign': [1]})
[{'id': 10, 'message': 'Item Successfully Added: Test'}]
>>> glpi.get_sub_items('Ticket', 11, 'Ticket_User')
[{'id': 24,
  'tickets_id': 10,
  'users_id': 3,
  'type': 1,
  ...
 {'id': 25,
  'tickets_id': 10,
  'users_id': 2,
  'type': 2,
  ...

>>> glpi.get_sub_items('Ticket', 11, 'Group_Ticket')
[{'id': 18,
  'tickets_id': 10,
  'groups_id': 1,
  'type': 2,
  ...
jsalatiel commented 3 years ago

Thank you very much!