Forum Discussion
Building a policy through python sdk
Hi,
I was trying to create a policy on BIG-IP 12.1 with the following code but encountered an error like below. Does anybody know what the error means?
It seems the error occurs when rule condition or action is created but don't know what is complained about?
mr = ManagementRoot('192.168.80.150', 'admin', 'admin')
controls=['forwarding', 'persistence']
requires = ['http', 'client-ssl']
try:
newpol = mr.tm.ltm.policys.policy.create(name='testpol5', controls=controls, requires=requires, \
subPath='Drafts', strategy='first-match')
tx=mr.tm.transactions.transaction
with TransactionContextManager(tx) as api:
newRules = newpol.rules_s.rules.create(name='r1', ordinal=0, description='r1 desc')
values=['90']
newRules.conditions_s.conditions.create(name='0', cpuUsage=True, greater=True, last_15secs=True, values=values, request=True)
newRules.actions_s.actions.create(name='0', forward=True, node='1.1.1.1', request=True )
newpol.update()
newpol.publish()
except Exception as e:
print("Exception during policy creation: " + str(e))
`
`400 Unexpected Error: Bad Request for uri: https://192.168.80.150:443/mgmt/tm/transaction/1520127675018281/commands/1/conditions/
Text: '{"code":400,"message":"Found invalid command id 1/conditions/","errorStack":[],"apiError":1}'
Thank you
I figured out by myself.
The issue was the rule creation where it was the part of transaction. By definition anything within transaction won't be built until whole transaction part is completed.
With my code, the rule was the part of transaction and the following commands of building conditions and actions depended on the rule. Chicken and egg problem.
So to fix this issue, I simply moved the rule creation part out of transaction and it fixed all problem.
Following is the code for your reference.
mr = ManagementRoot('192.168.80.150', 'admin', 'admin') controls=['forwarding', 'persistence'] requires = ['http', 'client-ssl'] try: newpol = mr.tm.ltm.policys.policy.create(name='testpol5', controls=controls, requires=requires, \ subPath='Drafts', strategy='first-match') Rule creation has been placed out of transaction newRules = newpol.rules_s.rules.create(name='r1', ordinal=0, description='r1 desc') tx=mr.tm.transactions.transaction with TransactionContextManager(tx) as api: values=['90'] newRules.conditions_s.conditions.create(name='0', cpuUsage=True, greater=True, last_15secs=True, values=values, request=True) newRules.actions_s.actions.create(name='0', forward=True, node='1.1.1.1', request=True ) newRules.update() newpol.publish() except Exception as e: print("Exception during policy creation: " + str(e))
I figured out by myself.
The issue was the rule creation where it was the part of transaction. By definition anything within transaction won't be built until whole transaction part is completed.
With my code, the rule was the part of transaction and the following commands of building conditions and actions depended on the rule. Chicken and egg problem.
So to fix this issue, I simply moved the rule creation part out of transaction and it fixed all problem.
Following is the code for your reference.
mr = ManagementRoot('192.168.80.150', 'admin', 'admin') controls=['forwarding', 'persistence'] requires = ['http', 'client-ssl'] try: newpol = mr.tm.ltm.policys.policy.create(name='testpol5', controls=controls, requires=requires, \ subPath='Drafts', strategy='first-match') Rule creation has been placed out of transaction newRules = newpol.rules_s.rules.create(name='r1', ordinal=0, description='r1 desc') tx=mr.tm.transactions.transaction with TransactionContextManager(tx) as api: values=['90'] newRules.conditions_s.conditions.create(name='0', cpuUsage=True, greater=True, last_15secs=True, values=values, request=True) newRules.actions_s.actions.create(name='0', forward=True, node='1.1.1.1', request=True ) newRules.update() newpol.publish() except Exception as e: print("Exception during policy creation: " + str(e))
- JG
Cumulonimbus
Thanks for reporting back. Is is possible to mark your own post as the right "answer"?
I think I did that :) I don't know if there is any F5 posting policy blocking this..
Recent Discussions
Related Content
* 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