如何通过OTRS中的Web服务(SOAP或REST)将配置项链接/获取到故障单

时间:2016-01-08 10:57:36

标签: perl web-services rest soap otrs

我想知道如何通过SOAP或REST Webservice获取和链接票证到配置项。 我已在管理控制台中导入此Restfull Web service,并使用此网址成功创建并获取票证信息 http://XXX.XXX.XXX.XXX/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/Ticket/1

但问题是当我收到故障单信息时链接的配置项信息没有到来。

我在google上进行了大量搜索,发现可以通过OTRS GUI将票证链接到Config Item,并在AgentTicketzoom页面中显示,我希望通过Web服务完成此操作。 任何人都可以帮助我解决这个问题,或者建议一些关于如何创建Web服务以从票证中获取链接对象信息的文档。

更新#1

我成功将web控制器添加到现有的Ticket连接器。使用POST Call,网址为http://XXX.XXX.XXX.XXX/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorRest/LinkObject。但是我收到此错误

{" faultcode":" Server"," faultstring":"没有ConfigObject!"}

我也检查了初始参数

$VAR1 = {  'Password' => '1234567',  'RequestMethod' => 'POST','SourceKey' => '1',  'SourceObject' => 'Ticket',  'State' => 'valid',  'TargetKey' => '2',  'TargetObject' => 'ITSMConfigItem',  'Type' => 'ParentChild',  'UserID' => '1',  'UserLogin' => 'XXXXX.XXXX@XXXX.com'};

$VAR1 = {  'ErrorMessage' => 'Got no ConfigObject!',  'Success' => 0};

2 个答案:

答案 0 :(得分:2)

是的,票证可以通过GUI链接到configItem,可以通过Web服务完成。

首先,您应该编写一个新的通用接口连接器操作,该操作将处理来自 LinkObject 类(APIdoc)的方法 LinkAdd

然后通过新的XML文件创建并注册新操作,如下所示:

文件名:GenericInterfaceLinkObjectConnector.xml

<?xml version="1.0" encoding="utf-8"?>
<otrs_config version="1.0" init="Application">
<ConfigItem Name="GenericInterface::Operation::Module###LinkObject::LinkAdd" Required="0" Valid="1">
        <Description Translatable="1">GenericInterface module registration for the operation layer.</Description>
        <Group>GenericInterface</Group>
        <SubGroup>GenericInterface::Operation::ModuleRegistration</SubGroup>
        <Setting>
            <Hash>
                <Item Key="Name">LinkAdd</Item>
                <Item Key="Controller">LinkObject</Item>
                <Item Key="ConfigDialog">AdminGenericInterfaceOperationDefault</Item>
            </Hash>
        </Setting>
    </ConfigItem>
</otrs_config>

之后,您可以从OTRS GUI发布新的提供者WebService,其中使用了新创建的连接器。

确保您传递方法所需的所有参数!!!

 $True = $LinkObject->LinkAdd(
    SourceObject => 'Ticket',
    SourceKey    => '321',
    TargetObject => 'FAQ',
    TargetKey    => '5',
    Type         => 'ParentChild',
    State        => 'Valid',
    UserID       => 1,
);

更新:

请阅读此Document以了解Generic Interface的构建方式,然后请添加新的连接器(LinkObject)

注册连接器及其操作 - 将XML文件放在/ Kernel / Config / Files /...中。

然后转到Sysconfig - &gt; GenericInterface - &gt; GenericInterface :: Operation :: ModuleRegistration并在GenericInterface :: Operation :: Module ### LinkObject :: LinkAdd旁边设置一个勾号并保存更改

然后将此连接器文件添加到/Custom/Kernel/GenericInterface/Operation/LinkObject/LinkAdd.pm

# --
# Kernel/GenericInterface/Operation/LinkObject/LinkAdd.pm - GenericInterface LinkAdd operation backend
# Copyright (C) 2016 ArtyCo (Artjoms Petrovs), http://artjoms.lv/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::GenericInterface::Operation::LinkObject::LinkAdd;

use strict;
use warnings;

use Kernel::GenericInterface::Operation::Common;
use Kernel::System::LinkObject;
use Kernel::System::VariableCheck qw(IsStringWithData IsHashRefWithData);

=head1 NAME

Kernel::GenericInterface::Operation::LinkObject::LinkAdd - GenericInterface Link Create Operation backend

=head1 SYNOPSIS

=head1 PUBLIC INTERFACE

=over 4

=cut

=item new()

usually, you want to create an instance of this
by using Kernel::GenericInterface::Operation->new();

=cut

sub new {
    my ( $Type, %Param ) = @_;

    my $Self = {};
    bless( $Self, $Type );

    # check needed objects
    for my $Needed (
        qw(DebuggerObject ConfigObject MainObject LogObject TimeObject DBObject EncodeObject WebserviceID)
        )
    {
        if ( !$Param{$Needed} ) {
            return {
                Success      => 0,
                ErrorMessage => "Got no $Needed!"
            };
        }

        $Self->{$Needed} = $Param{$Needed};
    }

    # create additional objects
    $Self->{CommonObject} = Kernel::GenericInterface::Operation::Common->new( %{$Self} );
    $Self->{LinkObject}
        = Kernel::System->LinkObject->new( %{$Self} );

    return $Self;
}

=item Run()

Create a new link.

    my $Result = $OperationObject->Run(
        Data => {
            SourceObject => 'Ticket',
            SourceKey    => '321',
            TargetObject => 'Ticket',
            TargetKey    => '12345',
            Type         => 'ParentChild',
            State        => 'Valid',
            UserID       => 1,
        },
    );

    $Result = {
        Success      => 1,                                # 0 or 1
        ErrorMessage => '',                               # In case of an error
        Data         => {
            Result => 1,                                  # 0 or 1 
        },
    };

=cut

sub Run {
    my ( $Self, %Param ) = @_;

    # check needed stuff
    if ( !IsHashRefWithData( $Param{Data} ) ) {
        return $Self->{CommonObject}->ReturnError(
            ErrorCode    => 'LinkAdd.MissingParameter',
            ErrorMessage => "LinkAdd: The request is empty!",
        );
    }



    my $LinkID = $Self->{LinkObject}->LinkAdd(
        %Param,
    );

    if ( !$LinkID ) {
        return $Self->{CommonObject}->ReturnError(
            ErrorCode    => 'LinkAdd.AuthFail',
            ErrorMessage => "LinkAdd: Authorization failing!",
        );
    }

    return {
        Success => 1,
        Data    => {
            Result => $LinkID,
        },
    };
}

1;

=back

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (L<http://otrs.org/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.

=cut

然后它应该出现并且可以在Admin中使用 - &gt; Web服务 - &gt;可用操作下拉列表当然可以用作Web服务。

PHP使用示例如下所示:

    #### Initialize new client session ####
$client = new SoapClient(
 null, 
 array(
 'location' => $url,
 'uri' => "Core",
 'trace' => 1,
 'login' => $username,
 'password' => $password,
 'style' => SOAP_RPC,
 'use' => SOAP_ENCODED
 )
);
#### Create and send the SOAP Function Call ####
$success = $client->__soapCall("Dispatch", 
array($username, $password,
"LinkObject", "LinkAdd",
"SourceObject", 'Ticket',
"SourceKey", $ticket_id1,
"TargetObject", 'Ticket',
"TargetKey", $ticket_id2,
"Type", 'ParentChild',
"State", 'Valid',
"UserID", '1'
));

如果出现错误 - 启用调试,请查看系统日志并检查OTRS的所有初始设置

祝你好运!

更新#2

要注册Web服务 - 按“添加新的Web服务”按钮,根据需要对其进行命名并设置以下设置(选择LinkAdd操作)并保存

OTRS webservice

更新#3

这是OTRS 5的更新模块文件

    # --
# Kernel/GenericInterface/Operation/LinkObject/LinkAdd.pm - GenericInterface LinkAdd operation backend
# Copyright (C) 2016 ArtyCo (Artjoms Petrovs), http://artjoms.lv/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::GenericInterface::Operation::LinkObject::LinkAdd;

use strict;
use warnings;

use Kernel::GenericInterface::Operation::Common;
use Kernel::System::LinkObject;
use Kernel::System::VariableCheck qw(IsStringWithData IsHashRefWithData);

=head1 NAME

Kernel::GenericInterface::Operation::LinkObject::LinkAdd - GenericInterface Link Create Operation backend

=head1 SYNOPSIS

=head1 PUBLIC INTERFACE

=over 4

=cut

=item new()

usually, you want to create an instance of this
by using Kernel::GenericInterface::Operation->new();

=cut

sub new {
    my ( $Type, %Param ) = @_;

    my $Self = {};
    bless( $Self, $Type );

    # check needed objects
    for my $Needed (
        qw( DebuggerObject WebserviceID )
        )
    {
        if ( !$Param{$Needed} ) {
            return {
                Success      => 0,
                ErrorMessage => "Got no $Needed!"
            };
        }

        $Self->{$Needed} = $Param{$Needed};
    }

    # create additional objects
    $Self->{CommonObject} = Kernel::GenericInterface::Operation::Common->new( %{$Self} );
    $Self->{LinkObject}
        = $Kernel::OM->Get('Kernel::System::LinkObject');

    return $Self;
}

=item Run()

Create a new link.

    my $Result = $OperationObject->Run(
        Data => {
            SourceObject => 'Ticket',
            SourceKey    => '321',
            TargetObject => 'Ticket',
            TargetKey    => '12345',
            Type         => 'ParentChild',
            State        => 'Valid',
            UserID       => 1,
        },
    );

    $Result = {
        Success      => 1,                                # 0 or 1
        ErrorMessage => '',                               # In case of an error
        Data         => {
            Result => 1,                                  # 0 or 1 
        },
    };

=cut

sub Run {
    my ( $Self, %Param ) = @_;

    # check needed stuff
    if ( !IsHashRefWithData( $Param{Data} ) ) {
        return $Self->{CommonObject}->ReturnError(
            ErrorCode    => 'LinkAdd.MissingParameter',
            ErrorMessage => "LinkAdd: The request is empty!",
        );
    }



    my $LinkID = $Self->{LinkObject}->LinkAdd(
        %Param,
    );

    if ( !$LinkID ) {
        return $Self->{CommonObject}->ReturnError(
            ErrorCode    => 'LinkAdd.AuthFail',
            ErrorMessage => "LinkAdd: Authorization failing!",
        );
    }

    return {
        Success => 1,
        Data    => {
            Result => $LinkID,
        },
    };
}

1;

=back

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (L<http://otrs.org/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.

=cut

答案 1 :(得分:2)

经过大量的工作,我找到了通过POST REST调用实现的解决方案。 我的主要麻烦是使用正确的%Param创建LinkObject。 所以,我已经实现了直接代码来获取正确的调用LinkAdd。 主要是,Artjoman提供了方式。但是我发现了一些错误,所以这里是OTRS_HOME / Custom / Kernel / GenericInterface / Operation / LinkObject / LinkAdd.pm中的另一个perl模块:

# --
# Kernel/GenericInterface/Operation/LinkObject/LinkAdd.pm - GenericInterface LinkAdd operation backend
# Copyright (C) 2016 ArtyCo (Artjoms Petrovs), http://artjoms.lv/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::GenericInterface::Operation::LinkObject::LinkAdd;

use strict;
use warnings;

use Kernel::System::ObjectManager;
use Kernel::System::VariableCheck qw(IsStringWithData IsHashRefWithData);

=head1 NAME

Kernel::GenericInterface::Operation::LinkObject::LinkAdd - GenericInterface Link Create Operation backend

=head1 SYNOPSIS

=head1 PUBLIC INTERFACE

=over 4

=cut

=item new()

usually, you want to create an instance of this
by using Kernel::GenericInterface::Operation->new();

=cut

sub new {
    my ( $Type, %Param ) = @_;

    my $Self = {};
    bless( $Self, $Type );

    # check needed objects
    for my $Needed (
        qw( DebuggerObject WebserviceID )
        )
    {
        if ( !$Param{$Needed} ) {
            return {
                Success      => 0,
                ErrorMessage => "Got no $Needed!"
            };
        }

        $Self->{$Needed} = $Param{$Needed};
    }

    # create additional objects

    local $Kernel::OM = Kernel::System::ObjectManager->new( %{$Self} );
    $Self->{LinkObject}
        = $Kernel::OM->Get('Kernel::System::LinkObject');

    return $Self;
}

=item Run()

Create a new link.

    my $Result = $OperationObject->Run(
        Data => {
            SourceObject => 'Ticket',
            SourceKey    => '321',
            TargetObject => 'Ticket',
            TargetKey    => '12345',
            Type         => 'ParentChild',
            State        => 'Valid',
            UserID       => 1,
        },
    );

    $Result = {
        Success      => 1,                                # 0 or 1
        ErrorMessage => '',                               # In case of an error
        Data         => {
            Result => 1,                                  # 0 or 1 
        },
    };

=cut

sub Run {
    my ( $Self, %Param ) = @_;

    # check needed stuff
    if ( !IsHashRefWithData( $Param{Data} ) ) {
        return $Self->ReturnError(
            ErrorCode    => 'LinkAdd.MissingParameter',
            ErrorMessage => "LinkAdd: The request is empty!",
        );
    }

    my $LinkID = $Self->{LinkObject}->LinkAdd(
        'SourceKey' => $Param{Data}{SourceKey},
        'SourceObject' => $Param{Data}{SourceObject},
        'State' => $Param{Data}{State},
        'TargetKey' => $Param{Data}{TargetKey},
        'TargetObject' => $Param{Data}{TargetObject},
        'Type' => $Param{Data}{Type},
        'UserID' => $Param{Data}{UserID},
    );

    if ( !$LinkID ) {
        return $Self->ReturnError(
            ErrorCode    => 'LinkAdd.AuthFail',
            ErrorMessage => "LinkAdd: Authorization failing!",
        );
    }

    return {
        Success => 1,
        Data    => {
            Result => $LinkID,
        },
    };
}

sub ReturnError {
    my ( $Self, %Param ) = @_;

    $Self->{DebuggerObject}->Error(
        Summary => $Param{ErrorCode},
        Data    => $Param{ErrorMessage},
    );

    # return structure
    return {
        Success      => 1,
        ErrorMessage => "$Param{ErrorCode}: $Param{ErrorMessage}",
        Data         => {
            Error => {
                ErrorCode    => $Param{ErrorCode},
                ErrorMessage => $Param{ErrorMessage},
            },
        },
    };
}

1;

=back

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (L<http://otrs.org/>).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (AGPL). If you
did not receive this file, see L<http://www.gnu.org/licenses/agpl.txt>.

=cut

因此,使用json

进行POST(http://otrs_host/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnectorREST/LinkAdd?UserLogin=login&Password=password)调用

{ “SourceObject”: “票”, “SourceKey”: “7”, “TargetObject”: “ITSMConfigItem”, “TargetKey”: “1”, “类型”: “DependsOn”, “国家”:“有效”, “用户ID”: “1”}

将在Ticket和ITSMConfigItem(不是计算机,硬件等)之间创建链接。 我认为,这个简单且非常粗鲁的解决方案将有助于理解如何以更好(但更有效)的方式为REST otrs添加完整的api操作。