[python]creating pool without F5 sdk

This is a sample code to create a pool, I will add on option to add members on later post.

This code is a function of the greater project i am planning as a personal hobby.

def create_bigip_pool():
    lbmethod_option = {
        "1": "round-robin",
        "2": "least-connections-member",
        "3": "least-connections-node",
        "4": "least-sessions"
    }
    pool_name = input("Enter pool name: ")
    if pool_name in list_bigip_pools():
        print("{} is already exist in bigip!".format(pool_name))
        logging.error("{} is already exist in bigip!".format(pool_name))
        exit(1)
    else:
        print("Choose load balancing method\n")
        for lbmethod in lbmethod_option:
            print(lbmethod, lbmethod_option[lbmethod])
        choice = input("Your choice: ")
        if not choice:
            lbmode = "round-robin"
        elif choice is "1" or choice <= "4":
            lbmode = lbmethod_option[choice]
        else:
            lbmode = "round-robin"
            print("You have entered invalid choice, round robin is chosen.")
    body = {
        "kind": "tm:ltm:pool:poolstate",
        "name": pool_name,
        "loadBalancingMode": lbmode
    }
    for bigip_address in BIGIP_ADDRESSES:
        try:
            bigip.post("https://" + bigip_address + LTM_BASE_POOL_URI, json.dumps(body))
        except Exception as e:
            logging.error(e)
            exit(1)

This is the result when testing the function:

Enter pool name: pool-MarvelHeroes
Choose load balancing method

1 round-robin
2 least-connections-member
3 least-connections-node
4 least-sessions
Your choice: 4

Process finished with exit code 0

And a fail test:

Enter pool name: pool-MarvelHeroes
pool-MarvelHeroes is already exist in bigip!

Process finished with exit code 1

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