Advertisement
rplantiko

Requesting HP Quality Center Defects

Nov 1st, 2012
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.07 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # This script accesses, as a proxy, the REST API of the HP quality center
  4. # Running it without query parameter, the complete list of defects is returned
  5. # A query parameter, e.g. 'query={id[2283]}' will be passed as is to the HP QC API
  6.  
  7. # We are using the libcurl wrapper WWW::Curl::Easy
  8. # The access is https, so a certificate has to be passed to libcurl
  9. # The main point for using curl, however, is the authentication procedure:
  10. # HP requires a preparative call to a special authentication service
  11. # The authentication ticket will then be passed back as a cookie
  12. # Only with this ticket, the real GET request on the defects can be performed
  13.  
  14. use WWW::Curl::Easy;
  15.  
  16. use strict;
  17. use warnings;
  18.  
  19. use constant {
  20.   URL_QC_DEFECTS   => "https://[QC DOMAIN]/qcbin/rest/domains/[DOMAIN]/projects/[PROJECT]/defects/",
  21.   URL_QC_AUTH      => "https://[QC DOMAIN]/qcbin/authentication-point/authenticate",
  22.   PATH_CERT        => "[PATH TO CREDENTIALS]"  # contains certificate and credentials, see below
  23.   };
  24.  
  25. doRequest( URL_QC_DEFECTS . "?" . $ENV{QUERY_STRING} );
  26. return 0;
  27.  
  28. sub doRequest {
  29.   my ($url,$cookies,$response) = (shift,"","");
  30.   eval {
  31.     my $curl = get_curl_instance(\$cookies,\$response);
  32.     authenticate( $curl );
  33.     get( $curl, $url );
  34.     if ($response =~ /.*?(<\?xml\b.*)/s) {
  35.       print "Content-Type:text/xml\n\n";
  36.       print $1;
  37.       }
  38.     else {
  39.       die "The response from HP QC is not in XML format";
  40.       }
  41.     };
  42.   if ($@) {
  43.     print "Content-Type:text/plain\n\n$@";
  44.     }
  45.   }
  46.  
  47. sub get_curl_instance {
  48.  
  49.   my ($cookie,$response) = @_;
  50.  
  51.   my $curl = WWW::Curl::Easy->new( );  
  52.  
  53.   open( my $cookiefile, ">", $cookie) or die "$!";
  54.   $curl->setopt( CURLOPT_COOKIEFILE, $cookiefile );  
  55.   open( my $responsefile, ">", $response) or die "$!";  
  56.   $curl->setopt( CURLOPT_WRITEDATA, $responsefile );  
  57.   $curl->setopt( CURLOPT_SSL_VERIFYPEER, 1);
  58.   $curl->setopt( CURLOPT_SSL_VERIFYHOST, 2);
  59.   $curl->setopt( CURLOPT_CAINFO, cert() );  
  60.   $curl->setopt( CURLOPT_FOLLOWLOCATION, 1 );
  61.   return $curl;
  62.   }
  63.  
  64. sub authenticate {
  65.   my $curl = shift;
  66.   my ($rc,$status);
  67.   $curl->setopt( CURLOPT_URL, URL_QC_AUTH );
  68.   $curl->setopt( CURLOPT_USERPWD, cred( ) );  
  69.   if (($rc = $curl->perform( )) != 0) {
  70.     die "Error Code $rc in curl->perform( ) on URL " . URL_QC_AUTH;
  71.     }
  72.   if (($status=$curl->getinfo(CURLINFO_HTTP_CODE))!="200") {
  73.     die "HTTP-Statuscode $status from authentication call";
  74.     }  
  75.   }
  76.  
  77.  
  78. sub  get {
  79.   my ($curl,$url) = @_;
  80.   my ($rc,$status);
  81.   $curl->setopt( CURLOPT_URL, $url );
  82.   $curl->setopt( CURLOPT_HEADER, { Accept => "text/xml" } );
  83.   if (($rc = $curl->perform( )) != 0) {
  84.     die "Error Code $rc from defects request";
  85.     }
  86.   if (($status=$curl->getinfo(CURLINFO_HTTP_CODE))!="200") {
  87.     die "HTTP Statuscode $status from defects request";
  88.     }  
  89.   }
  90.  
  91. sub cred {
  92.   open CRED, PATH_CERT . '/.cred_qc' or die "Can't open credentials file: $!";
  93.   chomp( my $cred = <CRED>);
  94.   close CRED;
  95.   return $cred;
  96.   }
  97.  
  98. sub cert {
  99.   return PATH_CERT . '/qc.migros.net.crt';
  100.   }  
  101.  
  102. 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement