[python]Using F5 iControl API without F5-SDK

I am experimenting myself, I have not worked with json type before, I worked frequently with the xml type, I will need to learn how to parse json type data to get meaningful output..

here’s the sample code to test on GET.

import requests, logging

"""
Reference: 
https://codereview.stackexchange.com/questions/123571/rest-api-calls-to-big-ip-ltm-to-get-the-status-of-pool-members
https://devcentral.f5.com/wiki/iControlREST.Python-Virtual-Server-Pool-Create.ashx
"""

# Global parameters should be defined here
# credential
USERNAME = "admin"
PASSWORD = "121278"

# Bigip appliance address, use an array to store it
BIGIP_ADDRESSES = ["192.168.1.11"]

# Bigip LTM path, from this path we can cocatenate pool, member, and virtual server
BIGIP_LTM = "/mgmt/tm/ltm"

# Pools, use a dictionary so that the pool name can be reference to an index.
POOLS = {}

# Pool members (nodes), use an array to store the address with ports.
POOL_MEMBERS = []

# Log filename
LOG_FILENAME = "bigip_script.log"

# Logging configuration
logging.basicConfig(filename=LOG_FILENAME, format="%(asctime)s %(levelname)s:%(message)s",
                    level=logging.INFO)

# Package the session for bigip
bigip = requests.session()
bigip.auth = requests.auth.HTTPBasicAuth(USERNAME,PASSWORD)
# Specify the request header
bigip.headers.update({"Content-Type" : "application/json"})
# ignoring certificate security warning, meaning do not check for valid bigip certificate
bigip.verify = False
logging.debug("Preparing for bigip REST API sessions...")

payload = {}

for bigip_address in BIGIP_ADDRESSES:
    try:
        response = bigip.get("https://" + bigip_address + BIGIP_LTM + "/pool")
    except Exception as e:
        logging.error(e)
        exit(1)

payload = response.json()

for item in payload['items']:
    print(item['name'])
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s