[python]Listing items with iControl API without using F5 SDK

After I have discovered that F5 SDK could not do certain things, I have decided to learn how to call the REST APIs using request and parse the json response. The first step i am learning is to use GET after that I will use POST using the json.dumps method.

This code is part of my personal project for doing auto provisioning with user’s input. A real life scenario is that there is an orchestrator with a workflow for creating virtual machines, the workflow includes assigning vlan id and ip address and gateway, user puts a request that the newly created servers should be load balanced.

Anyway while building my interactive auto provisioning project here are the two functions for doing listing of pools and pool members, in real world situation a script should dynamically poll the existing available resources from F5 bigip lots of demo scripts i have come across gave impractical examples like hardcode the resources and collections.

I will be building a series of scripts and share in my series of post, if there is a better way of writing it please put in the comments.

Functions have to be tested before proceeding to the next functions, this is to ensure the entire development has manageable bugs.

import requests, logging

# This code from solution of Shazow reference:
# https://stackoverflow.com/questions/27981545/suppress-insecurerequestwarning-unverified-https-request-is-being-made-in-pytho
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)

# setting up logging
logging.basicConfig(filename="bigip_script.log",
                    format="%(asctime)s %(levelname)s:%(message)s",
                    level=logging.INFO)

# parameters of bigip for readability
USERNAME = "admin"
PASSWORD = "121278"
LTM_BASE_URI = '/mgmt/tm/ltm'
LTM_BASE_POOL_URI = LTM_BASE_URI + '/pool'
LTM_BASE_VS_URI = LTM_BASE_URI + '/virtual'
PARTITION = '/~Common~'
BIGIP_ADDRESSES = ["192.168.1.11"]

# setting up for the REST API request
bigip = requests.session()
bigip.auth = requests.auth.HTTPBasicAuth(USERNAME, PASSWORD)
bigip.headers.update({"Content-Type" : "application/json"})
bigip.verify = False


def list_bigip_pools():
    pools = []
    for bigip_address in BIGIP_ADDRESSES:
        try:
            response = bigip.get("https://" + bigip_address + LTM_BASE_POOL_URI)
            pool_collections = response.json()
            logging.info("Successfully connected to bigip, collecting the pool parameters...")
        except Exception as e:
            logging.error(e)
            exit(1)
    for pool in pool_collections['items']:
        pools.append(pool['name'])
        logging.info("Collecting avaialble pool names...")
    return pools


def list_bigip_pool_members(pool_name):
    pool_members = []
    for bigip_address in BIGIP_ADDRESSES:
        try:
            response = bigip.get("https://" + bigip_address + LTM_BASE_POOL_URI + PARTITION + pool_name + "/members")
            member_collections = response.json()
            logging.info("Collecting members parameter from pool {}".format(pool_name))
        except Exception as e:
            logging.error(e)
            exit(1)
    for member in member_collections['items']:
        pool_members.append(member['name'])
        logging.info("Collecting member names from pool {}".format(pool_name))
    return pool_members


if __name__ == '__main__':
# testing the operabability of the two functions.
    pools = list_bigip_pools()
    for pool in pools:
        members = list_bigip_pool_members(pool)
        print("Members of pool {}".format(pool))
        for member in members:
            print(member)

Here is how it looks like:

Members of pool google_dns
8.8.4.4:53
8.8.8.8:53
Members of pool pool-Gwen
Members of pool pool-Gwenpool
192.168.100.1:80
192.168.100.3:80
192.168.200.2:80
Members of pool pool-Luna

Here is the log:

2018-04-24 01:33:08,865 INFO:Successfully connected to bigip, collecting the pool parameters...
2018-04-24 01:33:08,869 INFO:Collecting avaialble pool names...
2018-04-24 01:33:08,869 INFO:Collecting avaialble pool names...
2018-04-24 01:33:08,869 INFO:Collecting avaialble pool names...
2018-04-24 01:33:08,869 INFO:Collecting avaialble pool names...
2018-04-24 01:33:08,891 INFO:Collecting members parameter from pool google_dns
2018-04-24 01:33:08,891 INFO:Collecting member names from pool google_dns
2018-04-24 01:33:08,891 INFO:Collecting member names from pool google_dns
2018-04-24 01:33:08,921 INFO:Collecting members parameter from pool pool-Gwen
2018-04-24 01:33:08,938 INFO:Collecting members parameter from pool pool-Gwenpool
2018-04-24 01:33:08,938 INFO:Collecting member names from pool pool-Gwenpool
2018-04-24 01:33:08,938 INFO:Collecting member names from pool pool-Gwenpool
2018-04-24 01:33:08,938 INFO:Collecting member names from pool pool-Gwenpool
2018-04-24 01:33:08,963 INFO:Collecting members parameter from pool pool-Luna
2018-04-24 01:41:28,065 INFO:Successfully connected to bigip, collecting the pool parameters...
2018-04-24 01:41:28,067 INFO:Collecting avaialble pool names...
2018-04-24 01:41:28,067 INFO:Collecting avaialble pool names...
2018-04-24 01:41:28,067 INFO:Collecting avaialble pool names...
2018-04-24 01:41:28,067 INFO:Collecting avaialble pool names...
2018-04-24 01:41:28,092 INFO:Collecting members parameter from pool google_dns
2018-04-24 01:41:28,092 INFO:Collecting member names from pool google_dns
2018-04-24 01:41:28,092 INFO:Collecting member names from pool google_dns
2018-04-24 01:41:28,113 INFO:Collecting members parameter from pool pool-Gwen
2018-04-24 01:41:28,133 INFO:Collecting members parameter from pool pool-Gwenpool
2018-04-24 01:41:28,133 INFO:Collecting member names from pool pool-Gwenpool
2018-04-24 01:41:28,133 INFO:Collecting member names from pool pool-Gwenpool
2018-04-24 01:41:28,133 INFO:Collecting member names from pool pool-Gwenpool
2018-04-24 01:41:28,157 INFO:Collecting members parameter from pool pool-Luna

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