iTop学习笔记--REST集成

早先有同事通过直接修改iTop数据库实现集成,由于这种方式略感粗暴(一旦类对象变化或版本升级等原因导致数据库表结构变化,比较容易出现问题),所以测试了iTop官方安装文档推荐的REST/JSON集成方式,这里使用python实现。

POST参数

要调用对应的action,通过下面几个参数给/webservices/rest.php

Argument Description Defaut value
version Version of the API. It is a way to make sure that the targetted iTop server can deliver some functionality, and it ensures stability for your scripts: as long as a version is available, the operations will remain unchanged, with the exception of the following cases: bug fixes, modification in the returned messages, new elements into the returned JSON structure. -
auth_user User login -
auth_pwd User password -
json_data Structure containing all the information required to process your request. In particular, the requested operation is given here.
callback If set, then JSON-P (JSON with padding) is used

在实际使用参数时,须要注意:

  • 传递auth_user和auth_pwd须要在配置文件/conf/production/config-itop.php开启url

    1
    'allowed_login_types' => 'form|basic|external|url',
  • 所有参数整体作为dictionary传递(并做urlencode),在整个dictionary中的json_data须要事先指定为JSON类型

  • 具体操作都是由json_data参数内完成,可以使用”operation”: “list_operations”列出所有支持的operation

python实现

这里测试实现了直接脚本里写了一个参数列表,POST到iTop实现ticket创建。生产环境可以通过传递相应参数来实现不通内容的传递。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'''
Created on Nov 6, 2017
@author: edxi
'''
import json
import urllib.request, urllib.error, urllib.parse
data = dict(auth_user="admin", auth_pwd="itopadminpass", version="1.3", json_data=json.dumps({
"operation": "core/create",
"comment": "Synchronization from python script",
"class": "UserRequest",
"output_fields": "id, friendlyname",
"fields":
{
"org_id": "SELECT Organization WHERE name = \"MyOrg\"",
"caller_id":
{
"name": "xi",
"first_name": "erde",
},
"title": "Got a problem!",
"description": "It's generated by python script"
}
}))
req = urllib.request.Request('http://192.168.145.130/webservices/rest.php',
data=urllib.parse.urlencode(data).encode("utf-8")) # this will make the method "POST"
response = urllib.request.urlopen(req)
print(response.read().decode('utf-8'))
欣赏不如打赏!