├── COPYING ├── CREDITS ├── LICENSE ├── README.md └── glpi_client ├── RESTClient.py ├── XMLRPCClient.py └── __init__.py /COPYING: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2004 Sam Hocevar 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. 14 | -------------------------------------------------------------------------------- /CREDITS: -------------------------------------------------------------------------------- 1 | This client is used in Mandriva Pulse project: https://github.com/mandriva-management-console/mmc/ 2 | 3 | Thanks to my co-worker Ishak Belahmar (https://github.com/mont5piques) for code review and suggestions. 4 | 5 | May the UUID be with you ! 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2014 Jean-Christophe VASSORT 2 | This work is free. You can redistribute it and/or modify it under the 3 | terms of the Do What The Fuck You Want To Public License, Version 2, 4 | as published by Sam Hocevar. See the COPYING file for more details. 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | glpi-client 2 | =========== 3 | 4 | Python client to interact with GLPI webservices plugin 5 | 6 | --- 7 | 8 | glpi-client provides 2 clients to interact with GLPI webservices plugin: 9 | 10 | - XMLRPC client (preferred) 11 | - REST Client 12 | 13 | ####Usage:#### 14 | 15 | from glpi_client.XMLRPCClient import XMLRPCClient 16 | glpi = XMLRPCClient('http://localhost/glpi') 17 | glpi.connect('login', 'password') 18 | 19 | From this glpi object, you can call all GLPI webservices methods listed on this page: https://forge.glpi-project.org/projects/webservices/wiki/En_devguide 20 | 21 | For each webservices method, you can use python help() method 22 | 23 | #### Examples: #### 24 | 25 | **glpi.getObject** method (https://forge.glpi-project.org/projects/webservices/wiki/GlpigetObject): 26 | 27 | help(glpi.getObject) 28 | 29 | Will give you this output: 30 | 31 | Help on function getObject in module glpi_client.XMLRPCClient: 32 | getObject(module='glpi', **kwargs) 33 | Wrapper for GLPI webservices getObject method: 34 | It could be a good idea to see method's reference page: 35 | https://forge.glpi-project.org/projects/webservices/wiki/GlpigetObject 36 | @param module: webservices module to call (default: glpi) 37 | @type module: str 38 | @param kwargs: options for getObject method: 39 | - with_softwareversion: bool 40 | - with_document: bool 41 | - show_label: bool, optional 42 | - with_ticketvalidation: bool 43 | - with_peripheral: bool 44 | - help: bool,optional 45 | - with_infocom: bool, optional 46 | - with_contract: bool 47 | - show_name: bool, optional 48 | - with_tickettask: bool 49 | - with_ticketfollowup: bool 50 | - with_software: bool 51 | - with_networkport: bool, optional 52 | - with_reservation: bool 53 | - with_softwarelicense: bool 54 | - with_printer: bool 55 | - with_monitor: bool 56 | - with_ticket: bool 57 | - id: integer 58 | - with_phone: bool, optional (Computer only) 59 | @type kwargs: dict 60 | 61 | Then, use it ! 62 | 63 | glpi.getObject( 64 | itemtype='Computer', 65 | id=290, 66 | ) 67 | 68 | On my GLPI install, it provides this result: 69 | 70 | {'autoupdatesystems_id': '1', 71 | 'computermodels_id': '1', 72 | 'computertypes_id': '1', 73 | 'contact': 'jc', 74 | 'date_mod': '2014-02-13 09:50:20', 75 | 'domains_id': '1', 76 | 'entities_id': '1', 77 | 'groups_id': '0', 78 | 'groups_id_tech': '0', 79 | 'id': '290', 80 | 'locations_id': '0', 81 | 'manufacturers_id': '1', 82 | 'name': 'trinity', 83 | 'networks_id': '0', 84 | 'operatingsystems_id': '1', 85 | 'operatingsystemservicepacks_id': '0', 86 | 'operatingsystemversions_id': '11', 87 | 'serial': 'R9PZ865', 88 | 'states_id': '1', 89 | 'users_id': '0', 90 | 'users_id_tech': '0', 91 | 'uuid': '10F82C81-5267-11CB-863F-DC00B89EBF52'} 92 | 93 | **glpi.updateObjects** method (https://forge.glpi-project.org/projects/webservices/wiki/GlpiupdateObjects): 94 | 95 | help(glpi.updateObjects) 96 | 97 | Will give you this output: 98 | 99 | Help on function updateObjects in module glpi_client.XMLRPCClient: 100 | updateObjects(module='glpi', **kwargs) 101 | Wrapper for GLPI webservices updateObjects method: 102 | It could be a good idea to see method's reference page: 103 | https://forge.glpi-project.org/projects/webservices/wiki/GlpiupdateObjects 104 | @param module: webservices module to call (default: glpi) 105 | @type module: str 106 | @param kwargs: options for updateObjects method: 107 | - fields: array, mandatory 108 | - help: bool, optional 109 | @type kwargs: dict 110 | 111 | Then, use it ! 112 | 113 | update_info = { 114 | 'Computer': [ 115 | { 116 | 'id':'6793', 117 | 'states_id' : '16' 118 | } 119 | ] 120 | } 121 | result = glpi.updateObjects(fields=update_info) 122 | -------------------------------------------------------------------------------- /glpi_client/RESTClient.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- coding:utf-8 -*- 3 | 4 | # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 5 | # Version 2, December 2004 6 | # 7 | # Copyright (C) 2004 Sam Hocevar 8 | # 9 | # Everyone is permitted to copy and distribute verbatim or modified 10 | # copies of this license document, and changing it is allowed as long 11 | # as the name is changed. 12 | # 13 | # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 14 | # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 15 | # 16 | # 0. You just DO WHAT THE FUCK YOU WANT TO. 17 | 18 | 19 | import urllib, urllib2 20 | import json 21 | import logging 22 | 23 | class RESTClient(object): 24 | """ 25 | Python client to interact with GLPI webservices plugin 26 | """ 27 | def __init__(self, baseurl="http://localhost/glpi"): 28 | """ 29 | @param baseurl: Base URL of your GLPI instance 30 | @type baseurl: str 31 | """ 32 | self.baseurl = baseurl 33 | self.resturl = self.baseurl + '/plugins/webservices/rest.php?' 34 | self.session = None 35 | self.logger = logging.getLogger() 36 | 37 | def connect(self, login_name=None, login_password=None): 38 | """ 39 | Connect to a running GLPI instance with webservices 40 | plugin enabled. 41 | 42 | Returns True if connection was successful. 43 | 44 | @param login_name: your GLPI username 45 | @type login_name: string 46 | @param login_password: your GLPI password 47 | @type login_password: string 48 | """ 49 | 50 | if not None in [login_name, login_password]: 51 | params = { 52 | 'method':'glpi.doLogin', 53 | 'login_name': login_name, 54 | 'login_password': login_password, 55 | } 56 | response = urllib2.urlopen(self.resturl + urllib.urlencode(params)) 57 | result = json.loads(response.read()) 58 | if 'session' in result: 59 | self.session = result['session'] 60 | else: 61 | raise Exception("Login incorrect or server down") 62 | else: 63 | self.logger.warn("Connected anonymously, will only be able to use non-authenticated methods") 64 | return True 65 | 66 | def __getattr__(self, attr): 67 | def _get_doc(attr, _help): 68 | """ 69 | Format docstring for wrapped method 70 | """ 71 | ret = "Wrapper for GLPI webservices %s method:\n\n" % attr 72 | ret += "It could be a good idea to see method's reference page:\n" 73 | ret += "https://forge.glpi-project.org/projects/webservices/wiki/Glpi%s\n\n" % attr 74 | ret += "@param module: webservices module to call (default: glpi)\n" 75 | ret += "@type module: str\n" 76 | ret += "@param kwargs: options for %s method:\n\n" % attr 77 | 78 | for (key, value) in _help.items(): 79 | ret += '\t- %s: %s\n' % (key, value) 80 | 81 | ret += "\n@type kwargs: dict" 82 | 83 | return ret 84 | 85 | def treatFields(params): 86 | """ 87 | Format fields for REST API 88 | 89 | With REST API, fields must be formatted. 90 | Used for methods such as deleteObjects and updateObjects 91 | """ 92 | fields = params.pop('fields', []) 93 | if attr == 'deleteObjects': 94 | for glpi_type in fields: 95 | for key, value in fields[glpi_type].items(): 96 | params['fields[%s][%s]' % (glpi_type, key)] = value 97 | elif attr == 'updateObjects': 98 | for glpi_type in fields: 99 | for elem in fields[glpi_type]: 100 | elem_id = elem['id'] 101 | for key, value in elem.items(): 102 | params['fields[%s][%s][%s]' % (glpi_type, elem_id, key)] = value 103 | return params 104 | 105 | def call(module='glpi', *args, **kwargs): 106 | params = {'method': '.'.join([module, attr])} 107 | if self.session: 108 | params['session'] = self.session 109 | 110 | params = dict(params.items() + kwargs.items()) 111 | 112 | if 'fields' in params: 113 | params = treatFields(params) 114 | 115 | response = urllib2.urlopen(self.resturl + urllib.urlencode(params)) 116 | return json.loads(response.read()) 117 | 118 | call.__name__ = attr 119 | call.__doc__ = _get_doc(attr, call(help=True)) 120 | return call 121 | -------------------------------------------------------------------------------- /glpi_client/XMLRPCClient.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- coding:utf-8 -*- 3 | 4 | # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 5 | # Version 2, December 2004 6 | # 7 | # Copyright (C) 2004 Sam Hocevar 8 | # 9 | # Everyone is permitted to copy and distribute verbatim or modified 10 | # copies of this license document, and changing it is allowed as long 11 | # as the name is changed. 12 | # 13 | # DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 14 | # TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 15 | # 16 | # 0. You just DO WHAT THE FUCK YOU WANT TO. 17 | 18 | import logging 19 | import xmlrpclib 20 | 21 | class XMLRPCClient(object): 22 | """ 23 | Python client to interact with GLPI webservices plugin 24 | """ 25 | def __init__(self, baseurl="http://localhost/glpi"): 26 | """ 27 | @param baseurl: Base URL of your GLPI instance 28 | @type baseurl: str 29 | """ 30 | self.baseurl = baseurl 31 | self.serviceurl = self.baseurl + '/plugins/webservices/xmlrpc.php' 32 | self.session = None 33 | self.server = xmlrpclib.ServerProxy(self.serviceurl) 34 | self.logger = logging.getLogger() 35 | 36 | def connect(self, login_name=None, login_password=None): 37 | """ 38 | Connect to a running GLPI instance with webservices 39 | plugin enabled. 40 | 41 | Returns True if connection was successful. 42 | 43 | @param login_name: your GLPI username 44 | @type login_name: string 45 | @param login_password: your GLPI password 46 | @type login_password: string 47 | """ 48 | if not None in [login_name, login_password]: 49 | params = { 50 | 'login_name':login_name, 51 | 'login_password':login_password, 52 | } 53 | response = self.server.glpi.doLogin(params) 54 | 55 | if 'session' in response: 56 | self.session = response['session'] 57 | else: 58 | raise Exception("Login incorrect or server down") 59 | else: 60 | self.logger.warn("Connected anonymously, will only be able to use non-authenticated methods") 61 | return True 62 | 63 | def __getattr__(self, attr): 64 | def _get_doc(attr, _help): 65 | """ 66 | Format docstring for wrapped method 67 | """ 68 | ret = "Wrapper for GLPI webservices %s method:\n\n" % attr 69 | ret += "It could be a good idea to see method's reference page:\n" 70 | ret += "https://forge.glpi-project.org/projects/webservices/wiki/Glpi%s\n\n" % attr 71 | ret += "@param module: webservices module to call (default: glpi)\n" 72 | ret += "@type module: str\n" 73 | ret += "@param kwargs: options for %s method:\n\n" % attr 74 | 75 | for (key, value) in _help.items(): 76 | ret += '\t- %s: %s\n' % (key, value) 77 | 78 | ret += "\n@type kwargs: dict" 79 | 80 | return ret 81 | 82 | def call(module='glpi', **kwargs): 83 | params = {} 84 | if self.session: 85 | params['session'] = self.session 86 | 87 | params = dict(params.items() + kwargs.items()) 88 | 89 | called_module = getattr(self.server, module) 90 | return getattr(called_module, attr)(params) 91 | 92 | call.__name__ = attr 93 | call.__doc__ = _get_doc(attr, call(help=True)) 94 | return call 95 | -------------------------------------------------------------------------------- /glpi_client/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AnatomicJC/glpi-client/903fa2381aa0360f86d3a43d3e858924306ad6ab/glpi_client/__init__.py --------------------------------------------------------------------------------