#!/usr/bin/perl

use strict;
use warnings;

# Create a new volume on an XtremIO array
#
# Scott Howard, scott.howard@emc.com

my $username="admin";
my $password="Xtrem10";
my $xms="xms.example.com";

my $VolName="MyNewVolume";
my $VolSize="100G";

#
# If your XMS does not have a valid SSL certificate then uncomment the below
# line to avoid failing due to a cert error
#
#$ENV{'PERL_LWP_SSL_VERIFY_HOSTNAME'} = 0;

# =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= #

use REST::Client;
use MIME::Base64;
use JSON;
use Data::Dumper;

#
# Check the response from all API calls, and exit if they fail
#
sub checkerr($) {
	my ($cl) = @_;

	my $respcode=$cl->responseCode();
	return 0 if ($respcode>=200 && $respcode <300);

	# Error message is normally JSON, but sometimes it isnt...
	my $msg = $cl->responseContent();
	if ($msg =~ /^{/) {
		$msg = from_json($msg)->{message};
	}
	print STDERR "Error - $msg (response code $respcode)\n";
	exit(2);
}

my $client = REST::Client->new();
my $headers = {Authorization => "Basic ".encode_base64($username.":".$password), "Content-Type" => 'application/json'};

#

my %body= ('vol-name' => $VolName,
           'vol-size' => $VolSize,
        );

$client->POST("https://$xms/api/json/v2/types/volumes", encode_json(\%body), $headers);
checkerr($client);
my $resp = from_json($client->responseContent());
print "Volume created successfully\n";

# For debugging/learning purposes, uncomment the following
# line to see the full details returned and the JSON structure
print Dumper($resp);

exit(0);

