Forum Discussion

F5_Digger_13600's avatar
Mar 04, 2018
Solved

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 ...
  • F5_Digger_13600's avatar
    Mar 04, 2018

    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))