Forum Discussion

Koragg332's avatar
Koragg332
Icon for Altostratus rankAltostratus
Mar 20, 2022

Manipulate state of pool members via Python SDK

Hello all.

I would like to ask you for help regarding the Python SDK for F5. I'm trying to achive the manipulation of state of pool members via API calls, but I"m not able to figured out what is the correct way... Below is my code:

 

 

from f5.bigip import ManagementRoot
LB01 = ManagementRoot("testlb01", "weakuser", "weakpassword")
# 
# command LB to load pool details
pool_1 = LB01.tm.ltm.pools.pool.load (name="test-pool", partition="Common")
pool_1.members_s.get_collection()
#
# 
pool_1.members_s.items
<<< expected output:
[{'kind': 'tm:ltm:pool:members:membersstate', 'name': 'test-node-1:5050', 'partition': 'Common', 'fullPath': '/Common/test-node-1:5050', 'generation': 14335, 'selfLink': 'https://localhost/mgmt/tm/ltm/pool/~Common~test_pool/members/~Common~test-node-1:5050?ver=15.1.4', 'address': '192.168.77.21', bla bla bla ... truncated
>>>
#
# via single for loop, I can extract the necessary info about pool members:
for item in pool_1.members_s.raw["items"]:
	print("Node name: " + item["name"])
	print("Node IP: " + item["address"])
	print("Node full path: " + item["fullPath"])
	#
	if item["session"] == "monitor-enabled":
		print("Node state: enable")
	elif item["session"] == "user-disabled":
		print("Node state: disable")
	#
	print("Node reachability: " + item["state"])
	print("\n")
#
# output:
Node name: test-node-1:5050
Node IP: 192.168.77.21
Node full path: /Common/test-node-1:5050
Node state: enable
Node reachability: down

Node name: test-node-2:5050
Node IP: 192.168.77.22
Node full path: /Common/test-node-2:5050
Node state: enable
Node reachability: down

Node name: test-node-3:5050
Node IP: 192.168.77.23
Node full path: /Common/test-node-3:5050
Node state: enable
Node reachability: down

 

 

Now, I want to tears the status of test-node-3 to be "user-disabled":

 

 

pool_1.members_s.items[2]["session"]
'monitor-enabled'
#
pool_1.members_s.items[2]["session"] = "user-disabled"
#
pool_1.members_s.items[2]["session"]
'user-disabled'
#
pool_1.update()
#
pool_1.members_s.get_collection()
[<f5.bigip.tm.ltm.pool.Members object at 0x7f6eb350c550>, <f5.bigip.tm.ltm.pool.Members object at 0x7f6eb350c358>, <f5.bigip.tm.ltm.pool.Members object at 0x7f6eb350c2b0>]
#
pool_1.members_s.items[2]["session"]
'monitor-enabled'

 

 

Can you please help me with this script ?

I appreciate your help.

Thank you.

Best Regards

Michal

  • I modified your script so that the relevant poolmemeber of the pool can be listed
    Then perform the desired action,

     

    from f5.bigip import ManagementRoot
    #
    # Choice Poolmemeber
    def cpm():
        print("\nChoice Pool Memeber:\n")
        index = 1 #
        ListPoolm = {}
        for poolm in pool_1.members_s.get_collection():
            print(str(index) + ". " + poolm.name)
            ListPoolm[index] = poolm.name
            index += 1 
        choice = input("Which Poolmember do you want to action? ")
        return ListPoolm[int(choice)]
    
    # Action For Poolmember
    def act():
        member = pool_1.members_s.members.load(partition='Common', name=cpm())
        #
        action = input("enabled, disabled, forced_offline, Your input (press enter to skip): ")
        # 
        if action == 'enabled':
            # enables member
            member.state = 'user-up'
            member.session = 'user-enabled'
        elif action == 'disabled':
            # disables member
            member.session = 'user-disabled'
        elif action == 'forced_offline':
            # forces online member
            member.state = 'user-down'
            member.session = 'user-disabled'
        
        if action is not None:
            member.update()
        else:
            print('readonly mode, no changes applied')
        
    
    if __name__ == "__main__":
        LB01 = ManagementRoot("testlb01", "weakuser", "weakpassword")
        pool_1 = LB01.tm.ltm.pools.pool.load (name="test-pool", partition="Common")
        #
        act()
        # via single for loop, I can extract the necessary info about pool members:
        for item in pool_1.members_s.raw["items"]:
            print("Node name: " + item["name"])
            print("Node IP: " + item["address"])
            print("Node full path: " + item["fullPath"])
            #
            if item["session"] == "monitor-enabled":
                print("Node state: enable")
            elif item["session"] == "user-disabled":
                print("Node state: disable")
            #
            print("Node reachability: " + item["state"])
            print("\n")

     


    I hope I can help you

  • Hi oscarnet.

    Your reply is what I wanted, just simplified:

    #BRING POOL MEMBER UP:
    pool_1.state = "user-up"
    pool_1.session = "user-enabled"
    pool_1.update()

    # BRING POOL MEMBER DOWN:
    pool_1.state = "user-down"
    pool_1.session = "user-disabled"
    pool_1.update()

    Thank you very much for help.

    Best Regards

    Michal

  • I modified your script so that the relevant poolmemeber of the pool can be listed
    Then perform the desired action,

     

    from f5.bigip import ManagementRoot
    #
    # Choice Poolmemeber
    def cpm():
        print("\nChoice Pool Memeber:\n")
        index = 1 #
        ListPoolm = {}
        for poolm in pool_1.members_s.get_collection():
            print(str(index) + ". " + poolm.name)
            ListPoolm[index] = poolm.name
            index += 1 
        choice = input("Which Poolmember do you want to action? ")
        return ListPoolm[int(choice)]
    
    # Action For Poolmember
    def act():
        member = pool_1.members_s.members.load(partition='Common', name=cpm())
        #
        action = input("enabled, disabled, forced_offline, Your input (press enter to skip): ")
        # 
        if action == 'enabled':
            # enables member
            member.state = 'user-up'
            member.session = 'user-enabled'
        elif action == 'disabled':
            # disables member
            member.session = 'user-disabled'
        elif action == 'forced_offline':
            # forces online member
            member.state = 'user-down'
            member.session = 'user-disabled'
        
        if action is not None:
            member.update()
        else:
            print('readonly mode, no changes applied')
        
    
    if __name__ == "__main__":
        LB01 = ManagementRoot("testlb01", "weakuser", "weakpassword")
        pool_1 = LB01.tm.ltm.pools.pool.load (name="test-pool", partition="Common")
        #
        act()
        # via single for loop, I can extract the necessary info about pool members:
        for item in pool_1.members_s.raw["items"]:
            print("Node name: " + item["name"])
            print("Node IP: " + item["address"])
            print("Node full path: " + item["fullPath"])
            #
            if item["session"] == "monitor-enabled":
                print("Node state: enable")
            elif item["session"] == "user-disabled":
                print("Node state: disable")
            #
            print("Node reachability: " + item["state"])
            print("\n")

     


    I hope I can help you

  • Hi oscarnet.

    Your reply is what I wanted, just simplified:

    #BRING POOL MEMBER UP:
    pool_1.state = "user-up"
    pool_1.session = "user-enabled"
    pool_1.update()

    # BRING POOL MEMBER DOWN:
    pool_1.state = "user-down"
    pool_1.session = "user-disabled"
    pool_1.update()

    Thank you very much for help.

    Best Regards

    Michal