- Code formatting and whitespace cleanup

Just conformed to a certain coding, space and comment that was already
in the code for better readability.
This commit is contained in:
Kevin Scott Adams 2018-04-18 12:59:44 -04:00
parent f3c51d8e8a
commit 6931f4a232
1 changed files with 212 additions and 146 deletions

View File

@ -9,10 +9,19 @@ use REST::Client;
use MIME::Base64;
use JSON;
# Max LUNS per target on the iSCSI server
my $MAX_LUNS = 255;
sub get_base { return '/dev/zvol'; }
#
#
#
sub get_base {
return '/dev/zvol';
}
#
#
#
sub run_lun_command {
my ($scfg, $timeout, $method, @params) = @_;
@ -24,22 +33,43 @@ sub run_lun_command {
syslog("info","FreeNAS::lun_command : $method(@params)");
if( $method eq "create_lu" ) { return run_create_lu($scfg,$timeout,$method,@params); }
if( $method eq "delete_lu" ) { return run_delete_lu($scfg,$timeout,$method,@params); }
if( $method eq "import_lu" ) { return run_create_lu($scfg,$timeout,$method,@params); }
if( $method eq "modify_lu" ) { return run_modify_lu($scfg,$timeout,$method,@params); }
if( $method eq "add_view" ) { return run_add_view($scfg,$timeout,$method,@params); }
if( $method eq "list_view" ) { return run_list_view($scfg,$timeout,$method, @params); }
if( $method eq "list_lu" ) { return run_list_lu($scfg,$timeout,$method,"name", @params); }
if($method eq "create_lu") {
return run_create_lu($scfg, $timeout, $method, @params);
}
if($method eq "delete_lu") {
return run_delete_lu($scfg, $timeout, $method, @params);
}
if($method eq "import_lu") {
return run_create_lu($scfg, $timeout, $method, @params);
}
if($method eq "modify_lu") {
return run_modify_lu($scfg, $timeout, $method, @params);
}
if($method eq "add_view") {
return run_add_view($scfg, $timeout, $method, @params);
}
if($method eq "list_view") {
return run_list_view($scfg, $timeout, $method, @params);
}
if($method eq "list_lu") {
return run_list_lu($scfg, $timeout, $method, "name", @params);
}
syslog("error","FreeNAS::lun_command : unknown method $method");
return undef;
}
sub run_add_view { return ''; }
#
#
#
sub run_add_view {
return '';
}
#
# a modify_lu occur by example on a zvol resize. we just need to destroy and recreate the lun with the same zvol.
# Be careful, the first param is the new size of the zvol, we must shift params
#
sub run_modify_lu {
my ($scfg, $timeout, $method, @params) = @_;
shift(@params);
@ -47,11 +77,17 @@ sub run_modify_lu {
return run_create_lu($scfg, $timeout, $method, @params);
}
#
#
#
sub run_list_view {
my ($scfg, $timeout, $method, @params) = @_;
return run_list_lu($scfg, $timeout, $method, "lun-id", @params);
}
#
#
#
sub run_list_lu {
my ($scfg, $timeout, $method, $result_value_type, @params) = @_;
my $object = $params[0];
@ -72,6 +108,9 @@ sub run_list_lu {
return $result;
}
#
#
#
sub run_create_lu {
my ($scfg, $timeout, $method, @params) = @_;
@ -107,6 +146,9 @@ sub run_create_lu {
return "";
}
#
#
#
sub run_delete_lu {
my ($scfg, $timeout, $method, @params) = @_;
@ -156,9 +198,9 @@ sub run_delete_lu {
return "";
}
#
### FREENAS API CALLING ###
#
sub freenas_api_call {
my ($scfg, $method, $path, $data) = @_;
my $client = undef;
@ -168,13 +210,22 @@ sub freenas_api_call {
$client->addHeader('Content-Type' , 'application/json');
$client->addHeader('Authorization' , 'Basic ' . encode_base64( $scfg->{freenas_user} . ':' . $scfg->{freenas_password}));
if ($method eq 'GET') { $client->GET($path); }
if ($method eq 'DELETE') { $client->DELETE($path); }
if ($method eq 'POST') { $client->POST($path, encode_json($data) ); }
if ($method eq 'GET') {
$client->GET($path);
}
if ($method eq 'DELETE') {
$client->DELETE($path);
}
if ($method eq 'POST') {
$client->POST($path, encode_json($data));
}
return $client
}
#
# Writes the Response and Content to SysLog
#
sub freenas_api_log_error {
my ($client, $method) = @_;
syslog("info","[ERROR]FreeNAS::API::" . $method . " : Response code: " . $client->responseCode());
@ -182,6 +233,9 @@ sub freenas_api_log_error {
return 1;
}
#
#
#
sub freenas_iscsi_get_globalconfiguration {
my ($scfg) = @_;
my $client = freenas_api_call($scfg, 'GET', "/api/v1.0/services/iscsi/globalconfiguration/", undef);
@ -197,9 +251,10 @@ sub freenas_iscsi_get_globalconfiguration {
}
}
#
# Returns a list of all extents.
# http://api.freenas.org/resources/iscsi/index.html#get--api-v1.0-services-iscsi-extent-
#
sub freenas_iscsi_get_extent {
my ($scfg) = @_;
my $client = freenas_api_call($scfg, 'GET', "/api/v1.0/services/iscsi/extent/?limit=0", undef);
@ -215,13 +270,14 @@ sub freenas_iscsi_get_extent {
}
}
#
# Create an extent on FreeNas
# http://api.freenas.org/resources/iscsi/index.html#create-resource
# Parameters:
# - target config (scfg)
# - lun_path
# - lun_bs
#
sub freenas_iscsi_create_extent {
my ($scfg, $lun_path, $lun_bs) = @_;
@ -251,12 +307,13 @@ sub freenas_iscsi_create_extent {
}
}
#
# Remove an extent by it's id
# http://api.freenas.org/resources/iscsi/index.html#delete-resource
# Parameters:
# - scfg
# - extent_id
#
sub freenas_iscsi_remove_extent {
my ($scfg, $extent_id) = @_;
@ -271,9 +328,10 @@ sub freenas_iscsi_remove_extent {
}
}
#
# Returns a list of all targets
# http://api.freenas.org/resources/iscsi/index.html#get--api-v1.0-services-iscsi-target-
#
sub freenas_iscsi_get_target {
my ($scfg) = @_;
@ -289,9 +347,10 @@ sub freenas_iscsi_get_target {
}
}
#
# Returns a list of associated extents to targets
# http://api.freenas.org/resources/iscsi/index.html#get--api-v1.0-services-iscsi-targettoextent-
#
sub freenas_iscsi_get_target_to_extent {
my ($scfg) = @_;
@ -307,6 +366,7 @@ sub freenas_iscsi_get_target_to_extent {
}
}
#
# Associate a FreeNas extent to a FreeNas Target
# http://api.freenas.org/resources/iscsi/index.html#post--api-v1.0-services-iscsi-targettoextent-
# Parameters:
@ -314,7 +374,7 @@ sub freenas_iscsi_get_target_to_extent {
# - FreeNas Target ID
# - FreeNas Extent ID
# - Lun ID
#
sub freenas_iscsi_create_target_to_extent {
my ($scfg, $target_id, $extent_id, $lun_id) = @_;
@ -336,12 +396,13 @@ sub freenas_iscsi_create_target_to_extent {
}
}
#
# Remove a Target to extent by it's id
# http://api.freenas.org/resources/iscsi/index.html#delete--api-v1.0-services-iscsi-targettoextent-(int-id)-
# Parameters:
# - scfg
# - link_id
#
sub freenas_iscsi_remove_target_to_extent {
my ($scfg, $link_id) = @_;
@ -356,6 +417,7 @@ sub freenas_iscsi_remove_target_to_extent {
}
}
#
# Returns all luns associated to the current target defined by $scfg->{target}
# This method returns an array reference like "freenas_iscsi_get_extent" do
# but with an additionnal hash entry "iscsi_lunid" retrieved from "freenas_iscsi_get_target_to_extent"
@ -387,6 +449,7 @@ sub freenas_list_lu {
return \@luns;
}
#
# Returns the first available "lunid" (in all targets namespaces)
#
sub freenas_get_first_available_lunid {
@ -425,7 +488,10 @@ sub freenas_get_targetid {
foreach my $target (@$targets) {
my $iqn = $global->{'iscsi_basename'} . ':' . $target->{'iscsi_target_name'};
if( $iqn eq $scfg->{target} ) { $target_id = $target->{'id'}; last }
if($iqn eq $scfg->{target}) {
$target_id = $target->{'id'};
last;
}
}
return $target_id;