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.