iTop新增自定义REST接口ext/get_related

iTop Rest/JSON service 有个 core/get_related 接口,能查询对象的关联关系,但是只能查询一个方向的,比如影响的对象,或者依赖的对象,如果想同时查询上下游关联,需要自定义接口。

方法

新增一个模块,参考 core/get_related 来实现 iRestServiceProvider 接口即可。接口定义在 applicatin/applicationextension.inc.php 文件中。接口定义如下:

/**
 * Implement this interface to add new operations to the REST/JSON web service
 *
 * @api
 * @package     Extensibility
 * @since 2.0.1
 */
interface iRestServiceProvider
{
    /**
     * Enumerate services delivered by this class
     *
     * @param string $sVersion The version (e.g. 1.0) supported by the services
     *
     * @return array An array of hash 'verb' => verb, 'description' => description
     */
    public function ListOperations($sVersion);

    /**
     * Enumerate services delivered by this class
     *
     * @param string $sVersion The version (e.g. 1.0) supported by the services
     * @param string $sVerb
     * @param array $aParams
     *
     * @return RestResult The standardized result structure (at least a message)
     */
    public function ExecOperation($sVersion, $sVerb, $aParams);
}

实现一个名为 ext/get_related 的接口。示例如下:

class CustomExtServices implements iRestServiceProvider
{
	public function ListOperations($sVersion)
	{
		$aOps = array();
		if (in_array($sVersion, array('1.0', '1.1', '1.2', '1.3')))
		{
			$aOps[] = array(
				'verb' => 'ext/get_related',
				'description' => 'extend core/get_related: support custom filter of object and relation output'
			);
		}
		return $aOps;
	}

	public function ExecOperation($sVersion, $sVerb, $aParams)
	{
...

SDK

为 SDK 增加 ext/get_related 接口支持。以 PHP SDK ec-europa/itopapi 为例:

        /** 
         *  custom api: ext/get_related
         *  $class: mandatory, class name
         *  $key: mandatory, search object
		 *  $relation: impacts or depends on
         *  $optional: optional, an array with keys:filter,show_relations,output_fields,depth,direction,redundancy
         *      - filter: array of class name, like array("Person","Server"). only show objects in filter array
         *      - show_relations: array of class name, like array("Person", "Server"). onley show relations about class in the array
		 *      - hide_relations: opposite with show_relations
         *      - output_fields: array like array("classname"=>"fields")
         *      - depth: relation depth
         *      - direction: impacts direction(up,down or both)
         *      - redundancy: true of false
         */
        public function extRelated($class, $query, $relation="impacts", $optional=array())
        {
            $mandatory = array('class'=>$class, 'key'=>$query, 'relation'=>$relation);
            $param = array_merge($mandatory, $optional);
            return $this->operation('ext/get_related', $param);
        }

演示

ext/get_related 接口实现了以下功能:

  • objects输出指定类型的类
  • relations输出指定类型的类或者隐藏指定类型的类
  • objects输出的类指定输出fields
  • relations可以指定输出的深度
  • relations可以指定输出关联的方向(上游,下游,或者全部)

比如,一台机器 IP 为 10.0.0.2,使用此接口,限制 objects 只显示 Server 类,relations 显示 Server, Cluster, Rack, ApplicationSolution,并且同时显示机器的上游和下游关联关系,关联深度只显示 2 级,返回结果如下:

{
  "relations": {
    "Server::2::op.node.22": [
      {
        "key": "Cluster::3::op1"
      },
      {
        "key": "ApplicationSolution::54::op.appname"
      }
    ],
    "Cluster::3::op1": [
      {
        "key": "ApplicationSolution::53::op.monitor"
      }
    ],
    "Rack::11::土城4F.M1": [
      {
        "key": "Server::2::op.node.22"
      }
    ]
  },
  "objects": {
    "Server::2": {
      "code": 0,
      "message": "",
      "class": "Server",
      "key": "2",
      "fields": {
        "id": "2",
        "friendlyname": "op.node.22"
      }
    }
  },
  "code": 0,
  "message": "Scope: 1; Related objects: Server= 1",
}

用图形的方式表示,如下图所示。

附录

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注