GTM Service Creation

Problem this snippet solves:

A quick script to add a Wide-IP across two datacenters, as well as creating a pool for each DC and a virtual to span them.

Code :

import requests, json

# define program-wide variables
BIGIP_ADDRESS = '192.168.100.1'
BIGIP_USER = 'admin'
BIGIP_PASS = 'admin'

BIGIP_DC1_NAME = 'bigip-dc1.test.lab'
BIGIP_DC2_NAME = 'bigip-dc2.test.lab'

VS_NAME_SERVICE_BIGIP_DC1 = 'VS_Demo_DC1'
VS_ADDRESS_SERVICE_BIGIP_DC1 = '172.16.1.1:0'
VS_NAME_SERVICE_BIGIP_DC2 = 'VS_Demo_DC2'
VS_ADDRESS_SERVICE_BIGIP_DC2 = '172.16.2.2:0'

POOL_NAME_DC1 = 'Pool_Demo_DC1'
POOL_MEMBERS_DC1 = [ '%s:%s' % (BIGIP_DC1_NAME, VS_NAME_SERVICE_BIGIP_DC1) ]
POOL_NAME_DC2 = 'Pool_Demo_DC2'
POOL_MEMBERS_DC2 = [ '%s:%s' % (BIGIP_DC2_NAME, VS_NAME_SERVICE_BIGIP_DC2) ]

WIDEIP_NAME = 'www.test.lab'
WIDEIP_POOLS = [ POOL_NAME_DC1, POOL_NAME_DC2 ]

#Create Virtual Server into the BIG-IP system
def create_virtual(bigip, bigip_name, vs_name, vs_address):
payload = {}

payload['name'] = vs_name
payload['destination'] = vs_address
bigip.post('%s/gtm/server/%s/virtual-servers' % (BIGIP_URL_BASE, bigip_name), data=json.dumps(payload))

#Create pool for each DC and add the relevant virtual into it
def create_pool(bigip, bigip_ltm_name, pool_name, members):
        payload = {}

        # convert member format
        members = [ { 'name' : member , 'fullPath' : '/Common/%s:%s' % (bigip_ltm_name, member)} for member in members ]

        # define test pool
        payload['name'] = pool_name
        payload['loadBalancingMode'] = 'round-robin'
        payload['members'] = members
        bigip.post('%s/gtm/pool' % BIGIP_URL_BASE, data=json.dumps(payload))

#Create wideip based on the pool previously created
def create_wideip(bigip, name, pools):
payload = {}

payload['name'] = name
members = [ { 'name' : member } for member in pools ]
payload['pools'] = members
payload['poolLbMode'] = 'global-availability'
bigip.post('%s/gtm/wideip' % BIGIP_URL_BASE, data=json.dumps(payload))



# REST resource for BIG-IP that all other requests will use
bigip = requests.session()
bigip.auth = (BIGIP_USER, BIGIP_PASS)
bigip.verify = False
bigip.headers.update({'Content-Type' : 'application/json'})
print "created REST resource for BIG-IP at %s..." % BIGIP_ADDRESS

# Requests requires a full URL to be sent as arg for every request, define base URL globally here
BIGIP_URL_BASE = 'https://%s/mgmt/tm' % BIGIP_ADDRESS

#create virtual server into BIGIP definition
create_virtual(bigip, BIGIP_DC1_NAME, VS_NAME_SERVICE_BIGIP_DC1, VS_ADDRESS_SERVICE_BIGIP_DC1)
create_virtual(bigip, BIGIP_DC2_NAME, VS_NAME_SERVICE_BIGIP_DC2, VS_ADDRESS_SERVICE_BIGIP_DC2)

#create a pool for each DC containing the relevant virtual server
create_pool(bigip, BIGIP_DC1_NAME, POOL_NAME_DC1, POOL_MEMBERS_DC1)
create_pool(bigip, BIGIP_DC2_NAME, POOL_NAME_DC2, POOL_MEMBERS_DC2)

#create the wideip
create_wideip(bigip, WIDEIP_NAME, WIDEIP_POOLS)
Published Mar 09, 2015
Version 1.0
No CommentsBe the first to comment