Forum Discussion
Townsie_105233
Sep 04, 2012Historic F5 Account
Problem changing Partitions in Perl
I'm trying to list all the virtual servers in Test1. I'm trying a couple of different calls to try to get the partition (folder) to change to /Test1, but it doesn't seem to work.
...
Cory_del_Mar_67
Jun 13, 2014Nimbostratus
It does not printout all pools in all partitions - it only did for /Common,
!/usr/bin/perl
use SOAP::Lite + trace => qw(method debug);
use SOAP::Lite;
----------------------------------------------------------------------------
Validate Arguments
----------------------------------------------------------------------------
my $sHost = '192.168.1.2';
my $sPort = '443';
my $sUID = 'blaster';
my $sPWD = 'hhhjkk123';
my $sPool = $ARGV[0];
my $sNodeAddr = $ARGV[1];
my $sProtocol = "https";
sub usage()
{
die ("Usage: toggle [pool [addr:port]]\n");
}
if ( ($sHost eq "") or ($sPort eq "") or ($sUID eq "") or ($sPWD eq "") )
{
usage();
}
if ( ("80" eq $sPort) or ("8080" eq $sPort) )
{
$sProtocol = "http";
}
----------------------------------------------------------------------------
Transport Information
----------------------------------------------------------------------------
sub SOAP::Transport::HTTP::Client::get_basic_credentials
{
return "$sUID" => "$sPWD";
}
$Pool = SOAP::Lite
-> uri('urn:iControl:LocalLB/Pool')
-> readable(1)
-> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");
$PoolMember = SOAP::Lite
-> uri('urn:iControl:LocalLB/PoolMember')
-> readable(1)
-> proxy("$sProtocol://$sHost:$sPort/iControl/iControlPortal.cgi");
----------------------------------------------------------------------------
Attempt to add auth headers to avoid dual-round trip
----------------------------------------------------------------------------
eval { $Pool->transport->http_request->header
(
'Authorization' =>
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };
eval { $Pool->transport->http_request->header
(
'Authorization' =>
'Basic ' . MIME::Base64::encode("$sUID:$sPWD", '')
); };
----------------------------------------------------------------------------
support for custom enum types
----------------------------------------------------------------------------
sub SOAP::Deserializer::typecast
{
my ($self, $value, $name, $attrs, $children, $type) = @_;
my $retval = undef;
if ( "{urn:iControl}Common.EnabledState" == $type )
{
$retval = $value;
}
return $retval;
}
----------------------------------------------------------------------------
Main logic
----------------------------------------------------------------------------
if ( "" eq $sPool )
{
------------------------------------------------------------------------
No pool supplied. Query pool list and display members for given pool
------------------------------------------------------------------------
$soapResponse = $Pool->get_list();
&checkResponse($soapResponse);
@pool_list = @{$soapResponse->result};
&showPoolMembers(@pool_list);
}
elsif ( "" eq $sNodeAddr )
{
------------------------------------------------------------------------
Pool supplied, but now member so display given pools members
------------------------------------------------------------------------
&showPoolMembers($sPool);
}
else
{
------------------------------------------------------------------------
both pool and member supplied so toggle the specified member.
------------------------------------------------------------------------
&togglePoolMember($sPool, $sNodeAddr);
}
----------------------------------------------------------------------------
Show list of pools and members
----------------------------------------------------------------------------
sub showPoolMembers()
{
my (@pool_list) = @_;
my @member_state_lists = &getPoolMemberStates(@pool_list);
$rdate = `date`;
print "\n\n$rdate\n";
print "Available pool members\n";
print "\n======================\n";
$i = 0;
foreach $pool (@pool_list)
{
print "pool $pool\n{\n";
@member_state_list = @{@member_state_lists[$i]};
foreach $member_state (@member_state_list)
{
$member = $member_state->{"member"};
$addr = $member->{"address"};
$port = $member->{"port"};
$session_state = $member_state->{"session_state"};
print "$pool:$addr:$port:$session_state\n";
}
print "}\n";
$i++;
}
}
----------------------------------------------------------------------------
Toggle a specified pool member
----------------------------------------------------------------------------
sub togglePoolMember()
{
my ($pool_name, $member_def) = (@_);
------------------------------------------------------------------------
Split apart node:port
------------------------------------------------------------------------
($sNodeIP, $sNodePort) = split(/:/, $member_def, 2);
if ( "" eq $sNodePort )
{
$sNodePort = "0";
}
$member = { address => $sNodeIP, port => $sNodePort };
--------------------------------------------------------------------
Query enabled state for given Node:port
--------------------------------------------------------------------
$pool_member_state = &getPoolMemberState($pool_name, $member);
----------------------------------------------------------------
Set the state to be toggled to.
----------------------------------------------------------------
my $toggleState = "STATE_DISABLED";
if ( "STATE_DISABLED" eq $pool_member_state )
{
$toggleState = "STATE_ENABLED";
}
elsif ( "STATE_ENABLED" eq $pool_member_state )
{
$toggleState = "STATE_DISABLED";
}
else
{
die("Couldn't find member $member_def in pool $pool_name\n");
}
$MemberSessionState =
{
member => $member,
session_state => $toggleState
};
push @MemberSessionStateList, $MemberSessionState;
push @MemberSessionStateLists, [@MemberSessionStateList];
----------------------------------------------------------------
Toggle the state.
----------------------------------------------------------------
$soapResponse =
$PoolMember->set_session_enabled_state
(
SOAP::Data->name ( pool_names => ( [$pool_name] ) ),
SOAP::Data->name ( session_states => [@MemberSessionStateLists] )
);
&checkResponse($soapResponse);
print "Pool Member $pool_name {$sNodeIP:$sNodePort} state set from '$pool_member_state' to '$toggleState'\n";
}
----------------------------------------------------------------------------
returns the status structures for the members of the specified pools
----------------------------------------------------------------------------
sub getPoolMemberStates()
{
my (@pool_list) = @_;
$soapResponse = $PoolMember->get_session_enabled_state
(
SOAP::Data->name(pool_names => [@pool_list])
);
&checkResponse($soapResponse);
@member_state_lists = @{$soapResponse->result};
return @member_state_lists;
}
----------------------------------------------------------------------------
Get the actual state of a given pool member
----------------------------------------------------------------------------
sub getPoolMemberState()
{
my ($pool_name, $member_def) = (@_);
my $state = "";
@member_state_lists = &getPoolMemberStates($pool_name);
@member_state_list = @{@member_state_lists[0]};
foreach $member_state (@member_state_list)
{
my $member = $member_state->{"member"};
if ( ($member->{"address"} eq $member_def->{"address"}) and
($member->{"port"} eq $member_def->{"port"}) )
{
$state = $member_state->{"session_state"}
}
}
return $state;
}
----------------------------------------------------------------------------
checkResponse
----------------------------------------------------------------------------
sub checkResponse()
{
my ($soapResponse) = (@_);
if ( $soapResponse->fault )
{
print $soapResponse->faultcode, " ", $soapResponse->faultstring, "\n";
exit();
}
}
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects