cancel
Showing results for 
Search instead for 
Did you mean: 
Login & Join the DevCentral Connects Group to watch the Recorded LiveStream (May 12) on Basic iControl Security - show notes included.

f5 sdk can't load pool objects after create pool.

Sinan
Legacy Employee
Legacy Employee

Hi experts:

 

I can't load a pool object after I created it.

from f5.bigip import ManagementRoot from f5.bigip.contexts import TransactionContextManager if __name__ == "__main__": for x in range(1, 50): mgmt = ManagementRoot('10.5.10.23', 'admin', 'XXXXX', token=True) tx = mgmt.tm.transactions.transaction with TransactionContextManager(tx) as api: name = 'pool' + str(x) api.tm.ltm.pools.pool.create(name=name, partition='Common') p = api.tm.ltm.pools.pool.load(name=name, partition='Common') p.delete() api.tm.util.bash.exec_cmd( command='run', utilCmdArgs="-c 'tmsh save sys config'" )

 

I got this error:

Traceback (most recent call last):   File "/Users/sinwang/PycharmProjects/Suzhou_Beer_CMCC/f5_sdk_transaction.py", line 31, in <module>     p = api.tm.ltm.pools.pool.load(name=name, partition='Common')   File "/Users/sinwang/PycharmProjects/Suzhou_Beer_CMCC/venv/lib/python2.7/site-packages/f5/bigip/resource.py", line 1110, in load     return self._load(**kwargs)   File "/Users/sinwang/PycharmProjects/Suzhou_Beer_CMCC/venv/lib/python2.7/site-packages/f5/bigip/resource.py", line 1084, in _load     response = refresh_session.get(base_uri, **kwargs)   File "/Users/sinwang/PycharmProjects/Suzhou_Beer_CMCC/venv/lib/python2.7/site-packages/icontrol/session.py", line 295, in wrapper     raise iControlUnexpectedHTTPError(error_message, response=response) icontrol.exceptions.iControlUnexpectedHTTPError: 404 Unexpected Error: Not Found for uri: https://10.5.10.23:443/mgmt/tm/ltm/pool/~Common~pool1 Text: u'{"code":404,"message":"01020036:3: The requested Pool (/Common/pool1) was not found.","errorStack":[],"apiError":3}'
11 REPLIES 11

consul_2019
Cirrus
Cirrus

What happens if you use CURL instead of your script? Do you still get the same 404 error?

 

{"code":404,"message":"01020036:3: The requested Pool (/Common/pool1) was not found.","errorStack":[],"apiError":3}

 

 

Same result.

consul_2019
Cirrus
Cirrus

Hmmm... and the pool is definitely created and saved if you check with tmsh and in the config file?

We should create a new transaction. A transaction does all of the config together, as one transaction. Therefore you can’t load it before the transaction has been committed.

consul_2019
Cirrus
Cirrus

So reviewing your script...

 

Why the 3rd line? p.delete() ?

 

api.tm.ltm.pools.pool.create(name=name, partition='Common')

p = api.tm.ltm.pools.pool.load(name=name, partition='Common')

p.delete()

 

I want to delete the pool after created it.

consul_2019
Cirrus
Cirrus

Okay. In that case, for that work you need to modify your script.

 

First confirm whether you can create a single pool and delete via curl. This is just my sanity check.

Next confirm you can create a single pool and delete via Python using those Libraries.

 

I would restructure the code to make it more simple/logical. I understand the for loop to create the different pool names. Maybe try using a list comprehension to create the pool name. And then iterate through the list.

 

Next. I believe the code is failing as the p.delete() is being executed within the loop where you are creating pool names.

 

Actually looks like you have two loops here. The for loop and when you do the with part... That is also a kind of loop.

 

I would create a function to create your pools and then a function to check the pools are in place and then create another function to delete the pools.

 

Does that make sense?

consul_2019
Cirrus
Cirrus

Here's a rough of what I meant... Not finished and no error checking, but I will leave the excitement of figuring out to you :-)... Devcentral does not handle me pasting code in so well here (had to manually put indentation back in. But there you go...

 

from f5.bigip import ManagementRoot

from f5.bigip.contexts import TransactionContextManager

 

 

mgmt = ManagementRoot('10.5.10.23', 'admin', 'XXXXX', token=True)

tx = mgmt.tm.transactions.transaction

pool_names = ['pool' + str(x) for x in range(1, 50) ]

 

 

def pool_create(): 

for name in pool_names: 

with TransactionContextManager(tx) as api: 

api.tm.ltm.pools.pool.create(name=name, partition='Common')

api.tm.util.bash.exec_cmd(

   command='run',

   utilCmdArgs="-c 'tmsh save sys config'"

 )

  

def pool_list(): 

 

 

def pool_remove(): 

 

consul_2019
Cirrus
Cirrus

You'll need to put the indentations back in...

from f5.bigip import ManagementRoot from f5.bigip.contexts import TransactionContextManager import threading import time     def create_pool(name): print time.ctime() + "create pool name: %s" % name tx = mgmt.tm.transactions.transaction with TransactionContextManager(tx) as api: api.tm.ltm.pools.pool.create(name=name)     def delete_pool(name): print time.ctime() + "delete pool name: %s" % name tx = mgmt.tm.transactions.transaction with TransactionContextManager(tx) as api: p = api.tm.ltm.pools.pool.load(name=name) p.delete()     def save_config(): print time.ctime() + "save sys config" tx = mgmt.tm.transactions.transaction with TransactionContextManager(tx) as api: api.tm.util.bash.exec_cmd( command='run', utilCmdArgs="-c 'tmsh save sys config'" )     if __name__ == "__main__":   for x in range(1, 50): mgmt = ManagementRoot('10.5.10.23', 'admin', 'XXXXX', token=True) name = 'pool' + str(x) # x = threading.Thread(target=create_pool, args=(name,)) # x.start() # # time.sleep(1) # x = threading.Thread(target=delete_pool, args=(name,)) # x.start() # time.sleep(1) # x = threading.Thread(target=save_config) # x.start() # time.sleep(1) create_pool(name) delete_pool(name) save_config()    

 

Tested this code,​ it works.

consul_2019
Cirrus
Cirrus

Awesome! That's beautiful code. 😉