Alert.png The wiki is deprecated and due to be decommissioned by the end of September 2022.
The content is being migrated to other supports, new updates will be ignored and lost.
If needed you can get in touch with EGI SDIS team using operations @ egi.eu.

Ticket.cgi

From EGIWiki
Jump to navigation Jump to search
  1. !/usr/bin/perl -w

use strict; use lib '/usr/share/request-tracker3.6/lib'; use lib '/root/ws/';

use SOAP::Transport::HTTP; use MIME::Entity; use MIME::Base64; use IO::Scalar; use File::MimeInfo::Magic; use RT; use RT::User; use RT::CurrentUser; use RT::Ticket; use GGUS::Transaction; use GGUS::Transactions;

SOAP::Transport::HTTP::CGI

   -> dispatch_to('TicketWS')
   -> handle;

package TicketWS;

  1. Create a new ticket
  2. Parameters:
  3. Necessary:
  4. ggus_id: int, id in ggus database
  5. requestor: string, email address of requestor
  6. subject: string
  7. content: string
  8. priority: string, as defined in ggus wsdl
  9. affected_site: string, site that affects the ticket

sub create_ticket {

   my $self = shift;
   RT::LoadConfig();
   RT::Init();
   my $num_args = 6;
   unless (@_ == $num_args) {
       $RT::Logger->log(
                        level => 'err',
                        message => "Not enough arguments."
                       );
       return (0, "ngigrnet");
   }
   my $ggus_id = shift;
   my $requestor = shift;
   $requestor =~ s/ //g;
   my $subject = shift;
   my $content = shift;
   my $priority = shift;
   $priority = convert_priority($priority);
   my $affected_site = shift;
   # queue name
   my $queue = get_queue($affected_site);
   unless (is_int($ggus_id)) {
       $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id");
       return (0, "ngigrnet");
   }
   $RT::Logger->log(level => 'info', message => "ggus $ggus_id added by $requestor.");
   $RT::Logger->log(level => 'crit', message => "affected site: $affected_site");
   # build MIME encoded content
   my $MIMEContent = MIME::Entity->build(
                                         Data => [ $content ]
                                        );
   my $ticket = new RT::Ticket($RT::SystemUser);
   my %ticket_args = (
       "Queue" => $queue,
       "Requestor" => [$requestor,],
       "Subject" => $subject,
       "Priority" => $priority,
       "MIMEObj" => $MIMEContent,
   );
   my @res = $ticket->Create(%ticket_args);
   if ($res[0] == 0) {
       # error creating ticket
       $RT::Logger->log(level => 'err', message => $res[2]);
       return (0, "ngigrnet");
   }
   $RT::Logger->log(level => 'debug', message => $res[2]);
   # Add ggus id to ggus_rt table
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   $rt_handle->BeginTransaction;
   $rt_handle->Insert('ggus_rt', ('ggus_id', $ggus_id, 'rt_id', $res[0]));
   $rt_handle->Commit;
   # get user from requestor's address
   my $user = RT::User->new($RT::SystemUser);
   $RT::Logger->log(
                    level => 'info',
                    message => "user: $requestor",
                   );
   $user->LoadOrCreateByEmail($requestor);
   # set Creator and Created fields
   ticket_set_creat($res[0], $user->id);
   # set corresponding transaction's creater and created
   my $trans_id = $res[1];
   trans_set_creat($trans_id, $user->id);
   return ($res[0], "ngigrnet");

}

  1. Edit an existing ticket.
  2. Parameters:
  3. Necessary:
  4. ggus_id: int, from ggus
  5. requestor: email address
  6. content: string
  7. status: string
  8. priority: string

sub edit_ticket {

   my $self = shift;
   RT::LoadConfig();
   RT::Init();
   my $num_args = 5;
   unless (@_ == $num_args) {
       $RT::Logger->log(
                        level => 'err',
                        message => 'Not enough arguments.'
                       );
       return (0, "ngigrnet");
   }
   
   my $ggus_id = shift;
   my $requestor = shift;
   $requestor =~ s/ //g;
   my $content = shift;
   my $status = shift;
   my $priority = shift;
   unless (is_int($ggus_id)) {
       $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id");
       return (0, "ngigrnet");
   }
  1. get user from requestor's address
   my $user = RT::User->new($RT::SystemUser);
   $RT::Logger->log(
                    level => 'crit',
                    message => "user: $requestor",
                   );
   $user->LoadOrCreateByEmail($requestor);
  1. get rt_id for this ggus_id
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   my $query = 'SELECT rt_id FROM ggus_rt WHERE ggus_id=?';
   my @res = $rt_handle->FetchResult($query, ($ggus_id, ));
   unless (@res) {
       $RT::Logger->log(
                        level => 'err', 
                        message => "No rt ticket for ggus $ggus_id"
                       );
       return (0, "ngigrnet");
   }
   my $ticket_id = $res[0];
   $RT::Logger->log(
                    level => 'crit', 
                    message => "ggus: $ticket_id for ggus $ggus_id edited."
                   );
   unless ($content eq ) {
       my %args = (
           'Content' => $content,
       );
       my $ticket = new RT::Ticket($RT::SystemUser);
       $ticket->Load($ticket_id);
       @res = $ticket->Comment(%args);
       if ($res[0] == 0) {
           $RT::Logger->log(level => 'crit', message => $res[1]);
           return (0, "ngigrnet");
       }
       $RT::Logger->log(level => 'crit', message => $res[1]);
       # set creator of transaction
       trans_set_creat($res[0], $user->id);
   }
   # set status
   set_status($ggus_id, $status);
   # set priority
   set_priority($ggus_id, $status);
   # scrips have been run
   # mark transaction done
   mark_transaction_done($res[0]);
   return ($ticket_id, "ngigrnet");

}


  1. Add an attachemnt
  2. params:
  3. ggus_id: int from ggus
  4. atch_data: base64string the data of the attachment
  5. atch_name: string the name of the attachment

sub add_attachment {

   my $self = shift;
   RT::LoadConfig();
   RT::Init();
   my $num_args = 3;
   unless (@_ == $num_args) {
       $RT::Logger->log(
                        level => 'err',
                        message => 'Not enough arguments.'
                       );
       return (0, "ngigrnet");
   }
   
   my $ggus_id = shift;
   my $atch_data = shift;
   my $atch_name = shift;
   unless (is_int($ggus_id)) {
       $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id");
       return (0, "ngigrnet");
   }
  1. get rt_id for this ggus_id
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   my $query = 'SELECT rt_id FROM ggus_rt WHERE ggus_id=?';
   my @res = $rt_handle->FetchResult($query, ($ggus_id, ));
   unless (@res) {
       $RT::Logger->log(
                        level => 'err', 
                        message => "No rt ticket for ggus $ggus_id"
                       );
       return (0, "ngigrnet");
   }
   my $ticket_id = $res[0];
   $RT::Logger->log(
                    level => 'info', 
                    message => "ggus: $ticket_id for ggus $ggus_id attachment added."
                   );
   my $data = MIME::Base64::decode_base64($atch_data);
   $RT::Logger->log(
                    level => 'crit', 
                    message => "ggus: attachment $data"
                   );


   my $content = ;
  1. build MIME encoded content
   my $MIMEContent = MIME::Entity->build(
                                         Data => [ $content ]
                                        );
   my $atch_type = get_mime_type($atch_data);
   add_attachment_to_mimeobj(\$MIMEContent, $atch_data, $atch_type, $atch_name);
   my %args = (
       'MIMEObj' => $MIMEContent,
   );
  1. make transaction
   my $ticket = new RT::Ticket($RT::SystemUser);
   $ticket->Load($ticket_id);
   @res = $ticket->Comment(%args);
   if ($res[0] == 0) {
       $RT::Logger->log(level => 'err', message => $res[1]);
       return (0, "ngigrnet");
   }
   $RT::Logger->log(level => 'info', message => $res[1]);
  1. scrips have been run
  2. mark transaction done
   mark_transaction_done($res[0]);
   return ($ticket_id, "ngigrnet");

}

  1. Change priority
  1. params:
  2. id: int, from ggus
  3. priority: string

sub set_priority {

   RT::LoadConfig();
   RT::Init();
   my $num_args = 2;
   unless (@_ >= $num_args) {
       $RT::Logger->log(
                        level => 'err',
                        message => 'Not enough arguments.'
                       );
       return (0, "ngigrnet");
   }
   my $ggus_id = shift;
   unless (is_int($ggus_id)) {
       $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id");
       return (0, "ngigrnet");
   }
   my $ticket_id = ggus_to_rt($ggus_id);
   my $priority = shift;
   $priority = convert_priority($priority);
   my $ticket = new RT::Ticket($RT::SystemUser);
   $ticket->Load($ticket_id);
   $ticket->SetPriority($priority);
   return ($ticket_id, "ngigrnet");

}

  1. Change status
  2. params:
  3. id: int, from ggus
  4. status: string

sub set_status {

   RT::LoadConfig();
   RT::Init();
   my $num_args = 2;
   unless (@_ >= $num_args) {
       $RT::Logger->log(
                        level => 'err',
                        message => 'Not enough arguments.'
                       );
       return (0, "ngigrnet");
   }
   my $ggus_id = shift;
   unless (is_int($ggus_id)) {
       $RT::Logger->log(level=>'alert', message => "Requested ticket from ggus $ggus_id");
       return (0, "ngigrnet");
   }
   my $ticket_id = ggus_to_rt($ggus_id);
   my $status = shift;
   $status = convert_status($status);
   my $ticket = new RT::Ticket($RT::SystemUser);
   $ticket->Load($ticket_id);
   $ticket->SetStatus($status);
   return ($ticket_id, "ngigrnet");

}

  1. Handle attachments.
  2. Add them as part of a MIMEEntity. parameteres are passwd as references.
  3. MIMEContent: MIMEEntity, where to add the attachment
  4. atch_data: base64string, the data of the attachment
  5. atch_type: string, the MIME type of the attachment
  6. atch_filename: string, the filename of the attachment

sub add_attachment_to_mimeobj {

   unless (@_ == 4) {
       die 'Incorrect number of arguments.';
   }
   my $MIMEContent = shift;
   my $atch_data = shift;
   my $atch_type = shift;
   my $atch_filename = shift;
   $atch_data = MIME::Base64::decode_base64($atch_data);
   
   $$MIMEContent->make_multipart;
   $$MIMEContent->attach(
                         Data => [ $atch_data ],
                         Type => $atch_type,
                         Filename => $atch_filename,
                        );

}

  1. Check if argument(s) is(are) integers.

sub is_int {

   for (@_) {
       return 0 if (/\D/);
   }
   return 1;

}

  1. Mark transaction as done

sub mark_transaction_done {

   my $id = shift;
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   my $t = GGUS::Transaction->new( $rt_handle);
   $t->LoadByCol('transaction_id', $id);
   $t->Setdone(1);
   return 0;

}

  1. Convert priority from ggus to rt

sub convert_priority {

   my $priority = shift;
   $RT::Logger->log(level=>'crit', message => "Requested ticket with priority $priority");
   my %priorities = (
                     'less urgent' => 0,
                     'urgent' => 50,
                     'very urgent' => 75,
                     'top priority' => 99,
                    );
   return $priorities{$priority};

}

  1. Convert status from ggus to rt

sub convert_status {

   my $status = shift;
   return "resolved" if ($status eq "solved");
   return "open";

}

  1. Set the creator and created to transaction

sub trans_set_creat {

   my $transaction = shift;
   my $user = shift;
   my $query_t = "UPDATE Transactions SET Creator=? WHERE id=?";
   my $query_a = "UPDATE Attachments SET Creator=? WHERE TransactionId=?";
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   $rt_handle->BeginTransaction;
   $rt_handle->SimpleQuery($query_t, $user, $transaction);
   $rt_handle->SimpleQuery($query_a, $user, $transaction);
   $rt_handle->Commit;

}

  1. Set the creator to transaction

sub trans_set_creator {

   my $transaction = shift;
   my $user = shift;
   my $query = "UPDATE Transactions SET Creator=? WHERE id=?";
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   $rt_handle->BeginTransaction;
   $rt_handle->SimpleQuery($query, $user, $transaction);
   $rt_handle->Commit;

}

  1. Set create fields for new tickets
  2. params:
  3. ticket: id
  4. user: id

sub ticket_set_creat {

   my $ticket = shift;
   my $user = shift;
   my $query = "UPDATE Tickets SET Creator=? WHERE id=?";
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   $rt_handle->BeginTransaction;
   $rt_handle->SimpleQuery($query, $user, $ticket);
   $rt_handle->Commit;

}

  1. Get rt id from ggus id
  2. params:
  3. ggus_id
  4. returns:
  5. ticket_id

sub ggus_to_rt {

   my $ggus_id = shift;
   my $rt_handle = $RT::Handle->new($RT::SystemUser);
   my $query = 'SELECT rt_id FROM ggus_rt WHERE ggus_id=?';
   my @res = $rt_handle->FetchResult($query, ($ggus_id, ));
   unless (@res) {
       $RT::Logger->log(
                        level => 'err', 
                        message => "No rt ticket for ggus $ggus_id"
                       );
       exit(0);
   }
   my $ticket_id = $res[0];
   return $ticket_id;

}

  1. Get the MIME type of base64 encoded data
  2. params:
  3. $data base64 encoded data

sub get_mime_type {

   my $data = shift;
   my $raw_data = MIME::Base64::decode($data);
   my $sh = new IO::Scalar \$raw_data;
   return File::MimeInfo::Magic::mimetype($sh);

}

sub get_queue {

   my $site = shift;
   return "GR-01-AUTH" if ($site eq "GR-01-AUTH");
   return "GR-02-UoM" if ($site eq "GR-02-UoM");
   return "GR-03-HEPNTUA" if ($site eq "GR-03-HEPNTUA");
   return "GR-04-FORTH-ICS" if ($site eq "GR-04-FORTH-ICS");
   return "GR-05-DEMOKRITOS" if ($site eq "GR-05-DEMOKRITOS");
   return "GR-06-IASA" if ($site eq "GR-06-IASA");
   return "GR-07-UOI-HEPLAB" if ($site eq "GR-07-UOI-HEPLAB");
   return "GR-08-CEID" if ($site eq "GR-08-CEID");
   return "GR-09-UoA" if ($site eq "GR-09-UoA");
   return "GR-10-UOI" if ($site eq "GR-10-UOI");
   return "HG-01-GRNET" if ($site eq "HG-01-GRNET");
   return "HG-02-IASA" if ($site eq "HG-02-IASA");
   return "HG-03-AUTH" if ($site eq "HG-03-AUTH");
   return "HG-04-CTI-CEID" if ($site eq "HG-04-CTI-CEID");
   return "HG-05-FORTH" if ($site eq "HG-05-FORTH");
   return "HG-06-EKT" if ($site eq "HG-06-EKT");
   return "NGI_GRNET";

}