Forum Discussion

Townsie_105233's avatar
Townsie_105233
Historic F5 Account
Sep 04, 2012

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.

 

 

The portion that retrieves the list of virtual servers does work, but only retrives the virtual servers in /Common.

 

 

Any ideas/suggestions would be greatly appreciated.

 

 

!/usr/bin/perl

 

use strict;

 

use SOAP::Lite;

 

use MIME::Base64;

 

use Data::Dumper;

 

 

my $sHost = "1.0.0.21";

 

my $sUID = "admin";

 

my $sPWD = "f5rules";

 

my $sProtocol = "https";

 

my $sPort = 443;

 

my $debug = 1;

 

 

SOAP Transport

 

sub SOAP::Transport::HTTP::Client::get_basic_credentials {

 

return "$sUID" => "$sPWD";

 

}

 

 

Subroutins

 

 

 

BEGIN - Sub to extract System Info

 

 

 

sub iSystemInfo

 

{

 

my ($SoapRequest,$soapResponse,$version);

 

my($iURI,$iCREQ,$iCREQVAR) = @_;

 

 

print "\n\n10-VAR Systest: $iURI - $iCREQ - $iCREQVAR\n";

 

 

$SoapRequest = SOAP::Lite

 

-> uri($iURI)

 

-> proxy("https://$sHost:$sPort/iControl/iControlPortal.cgi");

 

 

$soapResponse = $SoapRequest->$iCREQ($iCREQVAR);

 

$SysInfo = $soapResponse->result;

 

print "soapResp: $soapResponse\n";

 

print "SYSINFO: $SysInfo\n";

 

 

return $SysInfo;

 

}

 

 

END - System Info Sub

 

 

 

 

BEGIN - Sub to extract System Info for Array Values

 

 

System::Session::set_active_folder

 

 

 

sub iSystemInfo_Array

 

{

 

my ($SoapRequest,$soapResponse,$version);

 

my($iURI,$iCREQ) = @_;

 

 

print "\n\n1-Array Systest: $iURI - $iCREQ\n";

 

 

$SoapRequest = SOAP::Lite

 

-> uri($iURI)

 

-> proxy("https://$sHost:$sPort/iControl/iControlPortal.cgi");

 

 

$soapResponse = $SoapRequest->$iCREQ();

 

 

@SysInfo = @{$soapResponse->result};

 

return @SysInfo;

 

 

}

 

 

 

END - System Info Sub

 

 

$sHost = "1.0.0.13";

 

 

 

Gets partition list

 

 

$GetActPart = iSystemInfo('urn:iControl:Management/Partition',"get_active_partition", "");

 

$SetActPart = iSystemInfo('urn:iControl:Management/Partition',"set_active_partition", "Test1");

 

$GetActPartx = iSystemInfo('urn:iControl:Management/Partition',"get_active_partition", "");

 

 

$GetActPart1 = iSystemInfo('urn:iControl:System/Session',"get_active_folder", "");

 

$SetActPart1 = iSystemInfo('urn:iControl:System/Session',"set_active_folder", "/Test1");

 

$GetActPartx1 = iSystemInfo('urn:iControl:System/Session',"get_active_folder", "");

 

 

@VirtualList = iSystemInfo_Array('urn:iControl:LocalLB/VirtualServer',"get_list");

 

foreach (@VirtualList) { print "Virtual: $_\n"; }

 

 

 

print "Partition: SET:$SetActPart GET: $GetActPart GET1: $GetActPartx\n";

 

print "Partition1: SET:$SetActPart1 GET: $GetActPart1 GET1: $GetActPartx1\n";

 

 

 

*******************************

 

output of script:

 

 

1-Array Systest: urn:iControl:LocalLB/VirtualServer - get_list

 

Virtual: /Common/V_TEST_35_443

 

Virtual: /Common/V_TEST_30_80

 

Virtual: /Common/vs_1_0_0_30_53_gtm

 

Virtual: /Common/FWD_virtual

 

Virtual: /Common/V_TEST_32_80

 

Virtual: /Common/V_TEST_31_80

 

Virtual: /Common/TLS_TEST

 

 

Partition: SET: GET: Common GET1: Common

 

Partition1: SET: GET: /Common GET1: /Common

 

 

 

7 Replies

  • Have you tried calling Management.Partition.get_partition_list() and/or Management.Folder.get_list() to get the list of available options? Keep in mind that top level folders are the same as the parititons. We originally developed partitions as a way to segment off configuration but we then added support for sub-folders within the partitions.

     

     

    I wrote a tech tip a while back about folders (https://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086495/iControl-101-24--Folders.aspx). You'll likely want to turn on the recursive query state to return a full listing instead of just the items in the "current" folder.

     

     

    -Joe
  • Townsie_105233's avatar
    Townsie_105233
    Historic F5 Account
    I've been through that doc, I'm just missing something..

     

     

    I'm using the following with the script above to retrieve the partition list:

     

     

    @Partition = iSystemInfo_Array('urn:iControl:Management/Partition',"get_partition_list");

     

    foreach (@Partition) {

     

    my %Partition_hash = %{$_};

     

    $partition_name = $Partition_hash{partition_name};

     

    print "Partition name: $_ - $partition_name\n";

     

    push (@Partition_list, $partition_name);

     

    }

     

     

     

    output:

     

    Partition name: Management::Partition::AuthZPartition=HASH(0x8cc1e1c) - Common

     

    Partition name: Management::Partition::AuthZPartition=HASH(0x8cb9710) - Test1

     

    Partition name: Management::Partition::AuthZPartition=HASH(0x8cb9620) - Test2
  • 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();
            }
    }
    
  • I realize this is an old question, but it's what popped up on google when I ran into the same issue.

     

    In the provided example, you're doing this... $SoapRequest->set_active_partition(Test1);

     

    What I had to do to get it working was... $SoapRequest->set_active_partition(SOAP::Data->name('active_partition')->value('Test1'));

     

  • hi,

     

    can someone please update the toggle perl script, with partition query. How to run toggle on different partitions, thanks.