2 Replies Latest reply on Apr 30, 2015 11:25 AM by Oren Mazor

    REST API using Perl

    Benjamin Torgesen

      Hi All,

       

      I haven't seen any posts about using the REST API with Perl, so I thought I'd contribute.  We use Perl a lot to script our Tableau server maintenance jobs.  Perl scripts query the Postgres repository and send emails when background jobs fail or extracts are too large.  Since the REST API is so much faster than TABCMD commands, I've been changing some of our other administrative scripts (adding/removing users) to use the REST API.  The following is a test Perl script which uses each of the four methods (GET, POST, PUT, and DELETE).  This script does not show every API call, but it is a good representation of each type.

       

      #!perl

       

      ####################################################################

      #

      #       NAME:     RestAPI.pl

      #       PURPOSE:  This script will use the Rest API to do various things

      #       AUTHOR:   btorgese

      #       DATE:     09-NOV-2014

      #

      ####################################################################

       

       

      ########### LOAD MODULES #############

      use strict;

      use LWP;

      use lib 'E:/Applications/CDASadmin/scripts';

      use TabPasswd;

       

       

      ########### VARIABLES #############

      my $URLname = $TabPasswd::URLname;  ## URL of your Tableau server (http://tableau.abc.com)

      my $tabuser = $TabPasswd::tabuser;  ## system admin userID

      my $tabpass = $TabPasswd::tabpass;  ## systeam admin password

       

       

      my $currdate = `echo %date:~10,4%-%date:~4,2%-%date:~7,2%%time:~0,5%`;

      chomp $currdate;

       

       

      my $browser;

      my $url; my $payload; my $response;

      my $token; my $siteid; my $userid;

       

       

      my $sitename = "<a valid site name on your server>";

      my $username = "<a valid userID in your company>";

       

      $browser = LWP::UserAgent->new;

       

       

      ########### SIGN IN #############

      $url = "$URLname/api/2.0/auth/signin";

      $payload = "<tsRequest>

        <credentials name=\"$tabuser\" password=\"$tabpass\" >

          <site contentUrl=\"\" />

        </credentials>

      </tsRequest>";

      $response = $browser->post($url, Content => $payload);

      debug("SIGN IN -------------- \n" . $response->content . "\n");

       

        ### Parse authentication token

        $token = get_xmlvalue($response->content, "token");

        debug("TOKEN -------------- \n" . $token . "\n");

       

       

      ########### GET SITES #############

      $url = "$URLname/api/2.0/sites/";

      $response = $browser->get($url, 'X-Tableau-Auth', $token);

      debug("SITES -------------- \n" . $response->content . "\n");

       

       

      ########### GET SITE #############

      $url = "$URLname/api/2.0/sites/$sitename?key=name";

      $response = $browser->get($url, 'X-Tableau-Auth', $token);

      debug("SITE -------------- \n" . $response->content . "\n");

       

        ### Parse SiteID

        $siteid = get_xmlvalue($response->content, "site id");

        debug("SITEID -------------- \n" . $siteid . "\n");

       

       

      ########### UPDATE SITE #############

      $url = "$URLname/api/2.0/sites/$siteid";

      $payload = "<tsResponse>

      <site adminMode=\"ContentOnly\"

        userQuota=\"500\"

        state=\"active\"

        statusReason=\"MyTest\"

          storageQuota=\"10000\" />

      </tsResponse>";

      $response = $browser->put($url, 'X-Tableau-Auth' => $token, Content => $payload);

      debug("UPDATE SITE -------------- \n" . $response->content . "\n");

       

       

      ########### ADD USER #############

      $url = "$URLname/api/2.0/sites/$siteid/users/";

      $payload = "<tsRequest>

        <user name=\"$username\"

          role=\"interactor\"

          publish=\"false\"

          contentAdmin=\"false\" />

      </tsRequest>";

      $response = $browser->post($url, 'X-Tableau-Auth' => $token, Content => $payload);

      debug("ADD USER -------------- \n" . $response->content . "\n");

       

       

      ########### GET USERS #############

      $url = "$URLname/api/2.0/sites/$siteid/users/";

      $response = $browser->get($url, 'X-Tableau-Auth' => $token);

      debug("GET USERS -------------- \n" . $response->content . "\n");

       

        ### Parse UserID

        $userid = get_xmlvalue($response->content, $username);

        debug("USERID -------------- \n" . $userid . "\n");

       

       

      ########### DELETE USER #############

      $url = "$URLname/api/2.0/sites/$siteid/users/$userid";

      $response = $browser->delete($url, 'X-Tableau-Auth' => $token);

      debug("DELETE USER -------------- \n" . $response->content . "\n");

       

       

      ########### SIGN OUT #############

      $url = "$URLname/api/2.0/auth/signout";

      $response = $browser->post($url, 'X-Tableau-Auth' => $token);

      debug("SIGN OUT -------------- \n" . $response->content . "\n");

       

       

      exit;

       

       

      #############################################################################################

      ### Subroutines


      sub get_xmlvalue

      {

        my $line; my @lines; my @data;

       

        @lines = split('\n', $_[0]);

        foreach $line (@lines)

        {

        if ($line =~ m/$_[1]/) {

        @data = split('"', $line);

        }

        }

        return $data[1];

      }

       

       

      sub debug

      {

        print "DEBUG: @_";

      }

       

      The only one that still seems to be having trouble is the UPDATE SITE section.  I don't actually use this function in any of my real scripts so I haven't needed to debug it.  The error is "Payload is either malformed XML or incomplete".

       

      In addition, can anyone confirm if they've heard that Tableau will allow site administrators to use their credentials for the REST API?  I've only been able to get it working with our system administrator credentials.

       

      Thanks,

      Ben