REST API Reference Version 5.2.1.0Search

Perl Code Sample

############################################
# This sample script creates a token,  
# and creates, updates and deletes a volume.
############################################


############################################
# Token create
############################################

use REST::Client;
use JSON;
 
use LWP::UserAgent;
use Net::SSL;
use IO::Socket::SSL;
use Data::Dumper;
 
    my ($login_cred);
 
    $ENV{HTTPS_CA_FILE} = "<FULL_PATH_OF_CA_CRT_FILE>";
 
    $client = REST::Client->new( ca => "<FULL_PATH_OF_CA_CRT_FILE>" );
 
    $client->setHost( "https://<ARRAY_MGMT_IP>:5392/v1/" );
 
    $login_cred->{"data"}->{"username"} = "admin";
    $login_cred->{"data"}->{"password"} = "admin";
 
    $login_cred = encode_json $login_cred;
 
    $client->request( "POST", "tokens", $login_cred );
 
    die "Failed to connect: $!\n" . $client->responseContent()
      if $client->responseContent() =~ /t connect/;
 
    my $tokenObj = decode_json $client->responseContent();
 
    my $token   = $tokenObj->{"data"}->{"session_token"};
    my $tokenId = $tokenObj->{"data"}->{"id"};
 
    print "\nGot token $token with id $tokenId ";
    print "for user $tokenObj->{data}->{username}\n\n";
 
    $client->addHeader( "X-Auth-Token", $token );

############################################
# Volume create
############################################

    my $opts;
    $opts->{data} = { "name","vol1","size","1024" };
    $opts = encode_json $opts;
 
    $client->request( "POST", "volumes", $opts );
 
    my $out = decode_json $client->responseContent();
 
    print "\nOutput is :\n\n" . Dumper $out;
  
	Volume update:
    my $opts;
    $opts->{data} = { "online","false" };
    $opts = encode_json $opts;
 
    $client->request( "PUT", "volumes/0606e4994b18fb160c000000000000000000000079", $opts );
 
    my $out = decode_json $client->responseContent();
 
    print "\nOutput is :\n\n" . Dumper $out;

############################################
# Volume read
############################################

    my $opts;
    $opts->{data} = {  };
    $opts = encode_json $opts;
 
    $client->request( "GET", "volumes/0606e4994b18fb160c000000000000000000000079?fields=online,name", $opts );
 
    my $out = decode_json $client->responseContent();
 
    print "\nOutput is :\n\n" . Dumper $out;

############################################
# Volume delete
############################################

    my $opts;
    $opts->{data} = {  };
    $opts = encode_json $opts;
 
    $client->request( "DELETE", "volumes/0606e4994b18fb160c000000000000000000000079", $opts );
 
    my $out = decode_json $client->responseContent();
 
    print "\nOutput is :\n\n" . Dumper $out;