25-Feb-2020 03:16
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}'
25-Feb-2020 07:57
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}
25-Feb-2020 08:36
Same result.
25-Feb-2020 09:01
Hmmm... and the pool is definitely created and saved if you check with tmsh and in the config file?
25-Feb-2020 09:04
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.
25-Feb-2020 09:04
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()
25-Feb-2020 09:06
I want to delete the pool after created it.
25-Feb-2020 09:16
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?
25-Feb-2020 09:35
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():
25-Feb-2020 09:35
You'll need to put the indentations back in...
25-Feb-2020 09:39
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.
25-Feb-2020 09:44
Awesome! That's beautiful code. 😉