nexenta API cleanups

remove all uncommented sleep calls (will add them later if required). Use
new nexenta_request() syntax. Also removed strangs eval{} sections which
hide errors.
This commit is contained in:
Dietmar Maurer
2012-09-17 10:12:36 +02:00
parent d6eee582dc
commit 4b0dea6c82

View File

@ -14,119 +14,88 @@ use PVE::JSONSchema qw(get_standard_option);
use base qw(PVE::Storage::Plugin); use base qw(PVE::Storage::Plugin);
sub nexenta_request { sub nexenta_request {
my ($scfg, $json) = @_; my ($scfg, $method, $object, @params) = @_;
my $apicall = { method => $method, object => $object, params => [ @params ] };
my $json = encode_json($apicall);
my $uri = ( $scfg->{ssl} ? "https" : "http" ) . "://" . $scfg->{portal} . ":2000/rest/nms/"; my $uri = ( $scfg->{ssl} ? "https" : "http" ) . "://" . $scfg->{portal} . ":2000/rest/nms/";
my $req = HTTP::Request->new( 'POST', $uri ); my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' ); $req->header('Content-Type' => 'application/json');
$req->content( $json ); $req->content($json);
my $token = encode_base64("$scfg->{login}:$scfg->{password}"); my $token = encode_base64("$scfg->{login}:$scfg->{password}");
$req->header( Authorization => "Basic $token" ); $req->header( Authorization => "Basic $token" );
my $ua = LWP::UserAgent->new; # You might want some options here my $ua = LWP::UserAgent->new; # You might want some options here
my $res = $ua->request($req); my $res = $ua->request($req);
if (!$res->is_success) { die $res->content if !$res->is_success;
die $res->content;
}
my $obj = eval { from_json($res->content); }; my $obj = eval { from_json($res->content); };
die "JSON not valid. Content: " . $res->content if ($@); die "JSON not valid. Content: " . $res->content if ($@);
die "Nexenta API Error: " . $obj->{error}->{message} if $obj->{error}->{message}; die "Nexenta API Error: $obj->{error}->{message}\n" if $obj->{error}->{message};
return $obj->{result} if $obj->{result}; return $obj->{result};
return 1;
} }
sub nexenta_get_zvol_size {
my ($zvol, $scfg) = @_;
my $json = '{"method": "get_child_prop", "object": "zvol", "params": ["' . $zvol . '", "volsize"]}'; sub nexenta_get_zvol_size {
my $volsize = nexenta_request($scfg, $json); my ($scfg, $zvol) = @_;
if ($volsize =~ /^(\d+)([KMGT])$/) {
my ($size, $unit) = ($1, $2); return nexenta_request($scfg, 'get_child_prop', 'zvol', $zvol, 'size_bytes');
if ($unit eq 'K') {
$size *= 1024;
} elsif ($unit eq 'M') {
$size *= 1024*1024;
} elsif ($unit eq 'G') {
$size *= 1024*1024*1024;
} elsif ($unit eq 'T') {
$size *= 1024*1024*1024*1024;
}
return $size;
}
die "got undefined size '$volsize'\n";
} }
sub nexenta_list_lun_mapping_entries { sub nexenta_list_lun_mapping_entries {
my ($zvol, $scfg) = @_; my ($scfg, $zvol) = @_;
my $json = '{"method": "list_lun_mapping_entries","object" : "scsidisk","params": ["'.$scfg->{pool}.'/'.$zvol.'"]}'; return nexenta_request($scfg, 'list_lun_mapping_entries', 'scsidisk', "$scfg->{pool}/$zvol");
my $map = nexenta_request($scfg,$json);
return $map if $map;
return undef;
} }
sub nexenta_add_lun_mapping_entry { sub nexenta_add_lun_mapping_entry {
my ($zvol, $scfg) = @_; my ($scfg, $zvol) = @_;
my $json = '{"method": "add_lun_mapping_entry","object" : "scsidisk","params": ["'.$scfg->{pool}.'/'.$zvol.'",{"target_group": "All"}]}'; nexenta_request($scfg, 'add_lun_mapping_entry', 'scsidisk',
"$scfg->{pool}/$zvol", { target_group => "All" });
nexenta_request($scfg, $json);
return 1;
} }
sub nexenta_delete_lu { sub nexenta_delete_lu {
my ($zvol, $scfg) = @_; my ($scfg, $zvol) = @_;
my $json = '{"method": "delete_lu","object" : "scsidisk","params": ["'.$scfg->{pool}.'/'.$zvol.'"]}'; nexenta_request($scfg, 'delete_lu', 'scsidisk', "$scfg->{pool}/$zvol");
nexenta_request($scfg, $json);
return 1;
} }
sub nexenta_create_lu { sub nexenta_create_lu {
my ($zvol, $scfg) = @_; my ($scfg, $zvol) = @_;
my $json = '{"method": "create_lu","object" : "scsidisk","params": ["'.$scfg->{pool}.'/'.$zvol.'",{}]}'; nexenta_request($scfg, 'create_lu', 'scsidisk', "$scfg->{pool}/$zvol", {});
nexenta_request($scfg, $json);
return 1; return 1;
} }
sub nexenta_create_zvol { sub nexenta_create_zvol {
my ($zvol, $size, $scfg) = @_; my ($scfg, $zvol, $size) = @_;
my $blocksize = $scfg->{blocksize}; nexenta_request($scfg, 'create', 'zvol', "$scfg->{pool}/$zvol", "${size}KB",
my $nexentapool = $scfg->{pool}; $scfg->{blocksize}, 1);
my $json = '{"method": "create","object" : "zvol","params": ["'.$nexentapool.'/'.$zvol.'", "'.$size.'KB", "'.$blocksize.'", "1"]}';
nexenta_request($scfg, $json);
return 1; return 1;
} }
sub nexenta_delete_zvol { sub nexenta_delete_zvol {
my ($zvol, $scfg) = @_; my ($scfg, $zvol) = @_;
sleep 5; nexenta_request($scfg, 'destroy', 'zvol', "$scfg->{pool}/$zvol", '');
my $json = '{"method": "destroy","object" : "zvol","params": ["'.$scfg->{pool}.'/'.$zvol.'", ""]}';
nexenta_request($scfg, $json);
return 1;
} }
sub nexenta_list_zvol { sub nexenta_list_zvol {
my ($scfg) = @_; my ($scfg) = @_;
my $json = '{"method": "get_names","object" : "zvol","params": [""]}'; my $zvols = nexenta_request($scfg, 'get_names', 'zvol', '');
my $volumes = {};
my $zvols = nexenta_request($scfg, $json);
return undef if !$zvols; return undef if !$zvols;
my $list = {}; my $list = {};
foreach my $zvol (@$zvols) { foreach my $zvol (@$zvols) {
my @values = split('/', $zvol); my @values = split('/', $zvol);
#$volumes->{$values[0]}->{$values[1]}->{volname} = $values[1];
my $pool = $values[0]; my $pool = $values[0];
my $image = $values[1]; my $image = $values[1];
@ -137,11 +106,10 @@ sub nexenta_list_zvol {
$list->{$pool}->{$image} = { $list->{$pool}->{$image} = {
name => $image, name => $image,
size => nexenta_get_zvol_size($zvol, $scfg), size => nexenta_get_zvol_size($scfg, $zvol),
format => 'raw', format => 'raw',
vmid => $owner vmid => $owner
}; };
} }
return $list; return $list;
@ -217,7 +185,7 @@ sub path {
my $target = $scfg->{target}; my $target = $scfg->{target};
my $portal = $scfg->{portal}; my $portal = $scfg->{portal};
my $map = nexenta_list_lun_mapping_entries($name,$scfg); my $map = nexenta_list_lun_mapping_entries($scfg, $name);
die "could not find lun number" if !$map; die "could not find lun number" if !$map;
my $lun = @$map[0]->{lun}; my $lun = @$map[0]->{lun};
$lun =~ m/^(\d+)$/ or die "lun is not OK\n"; $lun =~ m/^(\d+)$/ or die "lun is not OK\n";
@ -255,11 +223,9 @@ sub alloc_image {
die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
if !$name; if !$name;
eval { nexenta_create_zvol($name, $size, $scfg); }; nexenta_create_zvol($scfg, $name, $size);
sleep 1; nexenta_create_lu($scfg, $name);
eval { nexenta_create_lu($name, $scfg); }; nexenta_add_lun_mapping_entry($scfg, $name);
sleep 1;
nexenta_add_lun_mapping_entry($name, $scfg);
return $name; return $name;
} }
@ -269,10 +235,8 @@ sub free_image {
my ($vtype, $name, $vmid) = $class->parse_volname($volname); my ($vtype, $name, $vmid) = $class->parse_volname($volname);
eval { nexenta_delete_lu($name, $scfg); }; nexenta_delete_lu($scfg, $name);
sleep 5; nexenta_delete_zvol($scfg, $name);
nexenta_delete_zvol($name, $scfg);
return undef; return undef;
} }
@ -345,45 +309,37 @@ sub deactivate_volume {
sub volume_size_info { sub volume_size_info {
my ($class, $scfg, $storeid, $volname, $timeout) = @_; my ($class, $scfg, $storeid, $volname, $timeout) = @_;
my $json = '{"method": "get_child_prop","object" : "zvol","params": ["'.$scfg->{pool}.'/'.$volname.'", "size_bytes"]}'; return nexenta_get_zvol_size($scfg, "$scfg->{pool}/$volname"),
my $size = nexenta_request($scfg, $json);
return $size;
} }
sub volume_resize { sub volume_resize {
my ($class, $scfg, $storeid, $volname, $size, $running) = @_; my ($class, $scfg, $storeid, $volname, $size, $running) = @_;
my $json = '{"method": "set_child_prop","object" : "zvol","params": ["'.$scfg->{pool}.'/'.$volname.'", "volsize", "'.($size/1024).'KB"]}'; nexenta_request($scfg, 'set_child_prop', 'zvol', "$scfg->{pool}/$volname", 'volsize', ($size/1024) . 'KB');
nexenta_request($scfg, $json);
return undef;
} }
sub volume_snapshot { sub volume_snapshot {
my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
my $json = '{"method": "create_snapshot","object" : "zvol","params": ["'.$scfg->{pool}.'/'.$volname.'", "'.$snap.'", ""]}'; nexenta_request($scfg, 'create_snapshot', 'zvol', "$scfg->{pool}/$volname", $snap, 0);
nexenta_request($scfg, $json);
return undef;
} }
sub volume_snapshot_rollback { sub volume_snapshot_rollback {
my ($class, $scfg, $storeid, $volname, $snap) = @_; my ($class, $scfg, $storeid, $volname, $snap) = @_;
eval { nexenta_delete_lu($volname, $scfg); }; nexenta_delete_lu($scfg, $volname);
my $json = '{"method": "rollback","object" : "snapshot","params": ["'.$scfg->{pool}.'/'.$volname.'@'.$snap.'", ""]}'; nexenta_request($scfg, 'rollback', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');
nexenta_request($scfg, $json);
eval { nexenta_create_lu($volname, $scfg); }; nexenta_create_lu($scfg, $volname);
nexenta_add_lun_mapping_entry($volname, $scfg); nexenta_add_lun_mapping_entry($scfg, $volname);
} }
sub volume_snapshot_delete { sub volume_snapshot_delete {
my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; my ($class, $scfg, $storeid, $volname, $snap, $running) = @_;
my $json = '{"method": "destroy","object" : "snapshot","params": ["'.$scfg->{pool}.'/'.$volname.'@'.$snap.'"]}'; nexenta_request($scfg, 'destroy', 'snapshot', "$scfg->{pool}/$volname\@$snap", '');
nexenta_request($scfg, $json);
} }
1; 1;