RESTful API usage examples in Perl

The following Perl example shows how to set up to start interacting with the system. Following the sample are Perl function calls that import the example file, specify a target IP address, retrieve an authentication token, and finally use the authentication token to carry out an action on a system target.

See Spectrum Virtualize RESTful API to view a Python 3 language example or rest_api_usage_examples.html to view examples that use the curl command-line utility.

Note: Values in italics represent values that you supply.
Assume the following file is named rest_perl.pl. Included in the file is a function to construct an HTTP request and return the JSON data from the response. These functions can either be imported or copied and pasted at the top of the Perl script.
#!/usr/bin/perl -w use strict;
use warnings;
use HTTP::Request; use HTTP::Headers; use LWP::UserAgent; use JSON::PP;
my $ua = LWP::UserAgent->new(ssl_opts => { SSL_verify_mode => 'SSL_VERIFY_NONE' }j;
sub command{
my($host, $target, $method, $header_ref, $data_ref) = @_;
my $data_json = %{$data_ref} ? encode_json($data_ref) : "";
my $request = HTTP::Request->new($method => 
   "https://$host:7443/$target", HTTP::Headers->new(%{$header_ref}), $data_json);
$request->header(Content_Type => 'application/json');
my $response = $ua->request($request);
return decode_json($response->content);
}
If the file is being imported, use the following line in another Perl script, or in the Perl debugger:
do “rest_perl.pl”;
Specify the node IP address for the desired target node:
$host = “system_IP_address”;
Provide authentication information to create a token for use in future commands:
$token = command($host,
'API_version/auth',
'POST',
{"X-Auth-Password" => "password", "X-Auth-Username" => "username"}, {})->{"token"};
To make an array, include the POST method, a target, JSON parameters, and the newly created token, as in the following example:
$out = command($host,
'1.0/array',
'POST',
{"X-Auth-Token" => $token}, {raid_level => "raid5"});