Forum Discussion

Ken_R_'s avatar
Ken_R_
Icon for Nimbostratus rankNimbostratus
Apr 08, 2020
Solved

BigIP iControl REST

I'm using the requests module (not the f5-sdk library) with Python to add LTM objects and am having an issue with POST. The following code works:

 

def node():

  body = """

    {

      "name": "test-node1",

      "address": "1.1.1.40"

    }

      """

  headers = {"Content-Type": "application/json"}

  uri = f"https://ltmlab/mgmt/tm/ltm/node"

  response = requests.post(

    uri, auth=("admin", PASSWORD), headers=headers, data=body, verify=False

  )

  print('status code:', response.status_code)

 

However, the following code produces a 400 bad request:

 

def node():

    body =  \

        {

            "name": "test-node1",

            "address": "1.1.1.40"

        }

    headers = {"Content-Type": "application/json"}

    uri = f"https://ltmlab/mgmt/tm/ltm/node"

    response = requests.post(

        uri, auth=("admin", PASSWORD), headers=headers, data=body, verify=False

    )

    print('status code:', response.status_code)

 

I suppose I could continue to use the function that works, however does not seem very 'Pythonic' and also presents messy code if I have multiple objects of the same type to configure. I've also tried iterating over the following json file, but I still receive the same 400 bad request:

 

with open('examaple.json') as f:

json_dict = json.load(f)

 

# json file contents

{

    "nodes": [

        {

            "name": "node1",

            "address": "1.1.1.40"

        },

        {

            "name": "node2",

            "address": "2.2.2.40"

        }

    ]      

}

 

Any help would be appreciated. Thanks in advance.

 

  • Hello Ken.

    I recommend you to use the F5-SDK. You can install the module using pip:

    REF - https://f5-sdk.readthedocs.io/en/latest/

    To create a node is as simple as this:

    from f5.bigip import ManagementRoot
    session = ManagementRoot("F5_mgmt_IP","username","password",token=True)
    node = session.tm.ltm.nodes.node.create(name='node_10.1.1.1', address='10.1.1.1', description='123abc', partition='Common')

    KR,

    Dario.

2 Replies

  • Hello Ken.

    I recommend you to use the F5-SDK. You can install the module using pip:

    REF - https://f5-sdk.readthedocs.io/en/latest/

    To create a node is as simple as this:

    from f5.bigip import ManagementRoot
    session = ManagementRoot("F5_mgmt_IP","username","password",token=True)
    node = session.tm.ltm.nodes.node.create(name='node_10.1.1.1', address='10.1.1.1', description='123abc', partition='Common')

    KR,

    Dario.

  • Ken_R_'s avatar
    Ken_R_
    Icon for Nimbostratus rankNimbostratus

    Thank you Dario. I agree with what you're saying, it just seems that REST using the requests module seems to match up a little better with the TMSH command structure than the sdk. Taking your advice, the following code worked well. Thank you for your input.

     

    from f5.bigip import ManagementRoot
    from getpass import getpass
     
    PASSWORD = getpass()
     
    session = ManagementRoot("lab-ltm", "admin", PASSWORD, token=True)
     
     
    def node():
        nodes = [
            {"name": "node-test1", "address": "1.1.1.40"},
            {"name": "node-test2", "address": "2.2.2.80"},
        ]
        for node in nodes:
            nodes = session.tm.ltm.nodes.node.create(
                name=node["name"], address=node["address"], partition="Common"
            )
        pool()
     
     
    def pool():
        pool = session.tm.ltm.pools.pool.create(name="pool-test", monitor="tcp")
        pool_load = session.tm.ltm.pools.pool.load(name="pool-test")
        nodes = ["node-test1:80", "node-test2:80"]
        for node in nodes:
            member_load = pool_load.members_s.members.create(name=node, partition="Common")
        virtual()
     
     
    def virtual():
        params = {
            "name": "vs-test-virtual",
            "destination": "200.200.200.200:8080",
            "mask": "255.255.255.255",
            "ipProtocol": "tcp",
            "pool": "test_pool",
            "profiles": [
                "http",
                "f5-tcp-wan",
                {"name": "clientssl", "context": "clientside"},
            ],
            "partition": "Common",
            "sourceAddressTranslation": {"type": "automap"},
        }
        virtual = session.tm.ltm.virtuals.virtual.create(**params)
     
     
    if __name__ == "__main__":
        node()