Perl API Help

I’m somewhat new to perl, but I’m starting to pick up a bit of it. There
are a bunch of custom tasks I want to do to integrate RT with some other
systems. I have been looking for some documentation, but what I saw in
the RT wiki was very limited for someone in my position.

Can anyone point me towards some documentation that can give me a start
at writing some of my own scripts? I would like to see some basics on
modifying users, groups, tickets, etc and I can work from there.

I’ve been trying to get the same help. However, there is limited
documentation on the codebase. I’ve asked questions here but I often
get cryptic answers that leave me not know what even I’m talking about.

Mathew

John Arends wrote:

I’m somewhat new to perl, but I’m starting to pick up a bit of it. There
are a bunch of custom tasks I want to do to integrate RT with some other
systems. I have been looking for some documentation, but what I saw in
the RT wiki was very limited for someone in my position.

Can anyone point me towards some documentation that can give me a start
at writing some of my own scripts? I would like to see some basics on
modifying users, groups, tickets, etc and I can work from there.

You might want to start with the O’Reilly book…

While the book has done me wonders in learning to use and configure RT,
it has done little for me in the realm of the codebase. I’m hoping the
training in Boston helps in this regard :slight_smile:

Mathew

Jesse Vincent wrote:

I have the book right next to me. (It is a very good book, and buying it
was the least I could do to support this project.)

The problem is the book just touches on the subject. As someone who is
trying to learn perl I can’t pick up enough to figure out what to do
from the chapter on the perl API.

Maybe if someone gives a couple examples, I can work with that.

A few things I would like to do (in no particular order):

-see a list of all the members of a group
-add/remove a user from a group
-create a user
-set a user as privileged
-create a ticket (and populate a custom field while doing it)

Jesse Vincent wrote:

Matt;
I found the best resource is in /opt/rt3/lib/RT/
Find the appropriate module (Users_ , Tickets_ blah ) copy and hack away …
Roy

Mathew wrote:

I’ve been looking at that myself. However some things just aren’t clear.

For example: I need to create a script that pulls from the database for
each user the time spent on each customer (stored in a CF). So this
requires me to pull the user, the customer, all ticket transactions
associated with both, and the timespent for each transaction.

I’ve been looking at the lib/RT directory but have not found a clear
line of sight that leads to this.

This is just an example, mind you. Solving this one problem might shed
a bit of light on the next but it will be just as daunting sifting
through the code to find the next solution.

I think it would be helpful to have some documentation outside of the
perldocs for each .pm which provides more insight using example code
with real world problems such as the one above.

Mathew

Roy El-Hames wrote:

I have the book right next to me. (It is a very good book, and buying it
was the least I could do to support this project.)

The problem is the book just touches on the subject. As someone who is
trying to learn perl I can’t pick up enough to figure out what to do
from the chapter on the perl API.

Trying to learn Perl and trying to learn the RT API at the same time
is very ambitious.

The Wiki is your friend.

The various modules in RT do have manual pages, too. For example, to
find out what methods are available for a RT::Ticket object, just
issue this command:

$ perldoc /path/to/rt/lib/RT/Ticket_Overlay.pm

Another fine source of information is the code itself. Especially the
embedded test code in each module. Of course this is quite a
challenge, if you do not know Perl. :frowning:

Maybe if someone gives a couple examples, I can work with that.

A few things I would like to do (in no particular order):

# initialization for a RT batch program
# NOTE: NO ERROR CHECKING below -- very bad -- see the code to
# find out how to check return values from various methods

use warnings;
use strict;
use lib '/path/to/rt/lib';
use RT::Interface::CLI;

RT::LoadConfig();
RT::Init();

open my $fh, '> /path/to/log/file'
or die "can't open /path/to/log/file: $!\n";

-see a list of all the members of a group

my $group = RT::Group->new($RT::SystemUser);
$group->LoadUserDefinedGroup('my group name');
my $users = $group->UserMembersObj();

while (my $user = $users->Next()) {
print $fh $user->Name(), "\n";
}

-add/remove a user from a group

my $group = RT::Group->new($RT::SystemUser);
$group->LoadUserDefinedGroup('my group name');
my $user = RT::User->new($RT::SystemUser);
$user->Load('my_user_id');
$group->AddMember($user->PrincipalId());

# remove from group left as an exercise

-create a user

my $user = RT::User->new($RT::SystemUser);
$user->Create(Name         => 'my_user_id',
	  Password     => 'secret',
	  RealName     => 'John Doe',
	  EmailAddress => j@foo.invalid',
	  Privileged   => 0);

-set a user as privileged
-create a ticket (and populate a custom field while doing it)

These are left as an exercise for the reader. Use the code, Luke.
:slight_smile:

Garry T. Williams — 678-370-2438

Added examples to Request Tracker Wiki 1/29/07, Garry T. Williams garry.williams@cbeyond.net wrote:

[snip]

Best regards, Ruslan.

Hi everybody,

i already realised to create create queues and groups via the API, that
works all fine.
Where i hang at the moment is the right management.
I guess i can set rights for groups over something like
my $group = RT::Group->new($RT::SystemUser);
$group->LoadUserDefinedGroup(‘my group name’);
$group->SetPrincipal(‘?’);
and than to set the right i want for this group and the same for a queue.

But how to tell RT that for example in my Queue ‘testqueue’ the group
‘testgroup’ has the right to EditSafedSearches.
Wow to do this in the API and how does RT organises this, i can
understand how a group or a queue can get rights but the connection
between both is a bit buffling to me at the moment, even i guess
everything runs over ACL and Principals table.
Could anybody help me a bit out here.

Best Regards
Alexander Krieg

Alexander Krieg / IT-Benutzerservice / www.desy.de / Tel.+49-40-89983219
Deutsches Elektronen-Synchrotron DESY / Notkestr. 85 / D-22603 Hamburg

Hi everybody,

sorry i think in my last email i didn’t see clearly the through the
right management.
What i mean is, how to give inside the Queue ‘testqueue’ the Group
‘testgroup’ a queueright e.g. ‘SeeQueue’ or ‘showTicket’

Best regards
Alexander Krieg

Hi everybody,

i already realised to create create queues and groups via the API,
that works all fine.
Where i hang at the moment is the right management.
I guess i can set rights for groups over something like
my $group = RT::Group->new($RT::SystemUser);
$group->LoadUserDefinedGroup(‘my group name’);
$group->SetPrincipal(‘?’);
and than to set the right i want for this group and the same for a
queue.

But how to tell RT that for example in my Queue ‘testqueue’ the group
‘testgroup’ has the right to EditSafedSearches.
Wow to do this in the API and how does RT organises this, i can
understand how a group or a queue can get rights but the connection
between both is a bit buffling to me at the moment, even i guess
everything runs over ACL and Principals table.
Could anybody help me a bit out here.

Best Regards
Alexander Krieg

Alexander Krieg / IT-Benutzerservice / www.desy.de / Tel.+49-40-89983219
Deutsches Elektronen-Synchrotron DESY / Notkestr. 85 / D-22603 Hamburg

At Friday 3/9/2007 08:37 AM, Krieg, Alexander wrote:

Hi everybody,

sorry i think in my last email i didn’t see clearly the through the
right management.
What i mean is, how to give inside the Queue ‘testqueue’ the Group
’testgroup’ a queueright e.g. ‘SeeQueue’ or ‘showTicket’

Alexander,

Try this:

$grantee->GrantRight(Object => $object, Right => $right);

where $grantee is the principal for the group (e.g. testgroup) ,
$object is the queue (testqueue) and $right is the string describing
the right (e.g. ‘SeeQueue’).

The GrantRight method is in the Principal class.

Steve

Hi,

i just created a script to add existing users to existing groups, that
works fine.
I also can create queues, groups and set rights for all of them.

But i cannot create a simple user and find him than over the web-gui, i
think it cannot be so hard, but i do not see my mistake.
here the code i use, and i do not get any error messages or warnings.

use lib ("/opt/rt3/local/lib", “/opt/rt3/lib”, “/opt/rt3/etc/”);
use warnings;

use RT::User;
use RT::Group;
use Getopt::Long;
use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);

CleanEnv();
RT::LoadConfig();
RT::Init();

my $user = RT::User->new($RT::SystemUser);
$user->Create(Name => ‘foo’,
EmailAddress => ‘foo.foo@foo.de’,
Privileged => 1,
Password => ‘foo’,
RealName => ‘foo test’);

i think it should work !?

Cheers,
Alexander

Hi,

i just created a script to add existing users to existing groups, that
works fine.
I also can create queues, groups and set rights for all of them.

But i cannot create a simple user and find him than over the web-gui, i
think it cannot be so hard, but i do not see my mistake.
here the code i use, and i do not get any error messages or warnings.

use lib ("/opt/rt3/local/lib", “/opt/rt3/lib”, “/opt/rt3/etc/”);
use warnings;

use RT::User;
use RT::Group;
use Getopt::Long;
use RT::Interface::CLI qw(CleanEnv GetCurrentUser GetMessageContent loc);

CleanEnv();
RT::LoadConfig();
RT::Init();

my $user = RT::User->new($RT::SystemUser);
$user->Create(Name => ‘foo’,
EmailAddress => ‘foo.foo@foo.de’,
Privileged => 1,
Password => ‘foo’,
RealName => ‘foo test’);

i think it should work !?

It looks ok to me (I use some similar code in my scripts), try to catch an error:

my ($code, $msg) = $user->Create …
if ( ! $code )
{
print STDERR “Error creating user: $msg\n”;
}