Skip to main content
  • Place orders quickly and easily
  • View orders and track your shipping status
  • Create and access a list of your products
  • Manage your Dell EMC sites, products, and product-level contacts using Company Administration.

Dell Unity™ Family Unisphere® Management REST API Programmer's Guide

PDF

Example of creating multiple standalone LUNs

The following example is a Perl script that uses the REST API to consecutively create multiple standalone LUNs. To run the script, you must install the relevant Perl libraries listed at the top of the file.

#!/usr/bin/perl
#Required Debian Packages: apt-get install perl libwww-perl libjson-perl

use strict;
use LWP::UserAgent;
use HTTP::Cookies;
use JSON;
use Data::Dumper;

my $IP_ADDR = '';
my $USER = '';
my $PASS = '';
my $EMC_CSRF_TOKEN;

use constant {
    GET   => 'GET',
    POST   => 'POST',
    DELETE => 'DELETE',
};

my $ua = LWP::UserAgent->new(
             ssl_opts => { SSL_verify_mode => 'SSL_VERIFY_NONE'},
             cookie_jar => {}
);
my $json = JSON->new->allow_nonref();

sub request{
               #http://perl101.org/subroutines.html
my $type = $_[0];
       my $url = $_[1];
          my $post_data = $_[2];
   
         
         #Does an initial get request to make sure a login is done beforehand
   if(!defined($EMC_CSRF_TOKEN) && $type ne GET){
                     #First connection
                   request(GET, 'types/loginSessionInfo');
              }
        
         # set custom HTTP request header fields
      #my $req = HTTP::Request->new(GET => 'https://'.$IP_ADDR.'/api/types/'.$url.'/instances');
       #http://xmodulo.com/how-to-send-http-get-or-post-request-in-perl.html
          my $req = HTTP::Request->new;
           $req->uri('https://'.$IP_ADDR.'/api/'.$url);
  $req->method($type);
              
         $req->header('content-type' => 'application/json');
  $req->header('accept' => 'application/json');
               $req->header('X-EMC-REST-CLIENT' => 'true');
               if(defined($EMC_CSRF_TOKEN)){
                              $req->header('EMC-CSRF-TOKEN' => $EMC_CSRF_TOKEN);
       }
        
               #This is the first request, lets login!
      if($ua->cookie_jar->as_string eq ""){
                  $req->authorization_basic($USER, $PASS);
       }
        
         if (defined $post_data){
                              $req->content( $json->encode($post_data));
   }
        
        my $resp = $ua->request($req);
           
         #FOR DEBUG - PRINTS HEADERS
          #print $resp->headers_as_string;
  #PRINT COOKIES
        #print $ua->cookie_jar->as_string;
      
         if ($resp->is_success) {
                 
                        if(!defined($EMC_CSRF_TOKEN)){
                                      $EMC_CSRF_TOKEN = $resp->header('EMC-CSRF-TOKEN');
                      }
        
                        my $message = $resp->decoded_content;
                       #print "Received reply: $message\n";
                             return $json->decode($message);

  } else {
                           print "HTTP error code: ", $resp->code, "\n";
                  print "HTTP error message: ", $resp->message, "\n";
                         return 0;
        }
        
         #print $ua->cookie_jar->as_string;
}

sub getInputLine{
             my $lowercase = $_[0];
           my $question = $_[1];
             my $cmp = $_[2];
       
         if(defined $question){
                              print $question;
     }

  my $input = <STDIN>;
               chomp($input);
           $input = $lowercase ? lc($input) : $input;
   #print $input;
               if(defined $cmp){
                       return $input eq $cmp;
             }else{
                             return $input;
              }
}

print "Enter IP and credentials for your storage system.\n";
$IP_ADDR = getInputLine(1, 'IP Address: ');
$USER = getInputLine(0, 'Username: ');
$PASS = getInputLine(0, 'Password: ');

do{
  my $pool_id = getInputLine(1, 'CLI_ID of pool to add LUN to: ');
  my $lun_name = getInputLine(0, 'LUN Name: ');
  my $lun_size = getInputLine(1, 'LUN Size (Gigabytes): ');
  my $lun_isThinEnabled = getInputLine(1, 'LUN type (thick/thin): ', 'thin');
  
  my $resp = request(POST, 'types/storageResource/action/createLun',
     {              
                        "name" => $lun_name,
                              "lunParameters" => {"pool" => {"id" => $pool_id},
                        "size" => $lun_size*1024*1024*1024,
                 'isThinEnabled' => ($lun_isThinEnabled ? 'true' : 'false')},
                              #'hostAccess' =>
         });
     
  print $resp eq 0 ? "FAILED TO CREATE LUN $lun_name!\n" : "LUN $lun_name sucessfully created!\n";
}while(getInputLine(1, 'Would you like to create another LUN? (y/n) ', 'y'));

#Creates a 20GB LUN
#request(POST, 'types/storageResource/action/createLun', {"name" => "LUN-RestAPI", "lunParameters" => {"pool" => {"id" => "pool_1"}, "size" => 21474836480}});

Rate this content

Accurate
Useful
Easy to understand
Was this article helpful?
0/3000 characters
  Please provide ratings (1-5 stars).
  Please provide ratings (1-5 stars).
  Please provide ratings (1-5 stars).
  Please select whether the article was helpful or not.
  Comments cannot contain these special characters: <>()\