14 October, 2021 #Citrix

Converge 2021 – Automating your way to greatness

Hello Awesome Person,

I hope you enjoyed watching the session “Automating your way to greatness with the ADC Nitro API”. This page will serve the purpose of hosting the video later on and providing the scripts created with commented code.
Please do get started on trying awesome stuff, and always feel free to hit me up on any socials if you need help or want to chat.

Thank you very much for watching.

——————————————-

The intro blog about the NITRO API
The Citrix Documentation


The code we produced during the session with commenting:

#imports#

#Import nitro_server for login
from nssrc.com.citrix.netscaler.nitro.service.nitro_service import nitro_service
#Import lbvserver for editing Load Balancing Virtual Servers
from nssrc.com.citrix.netscaler.nitro.resource.config.lb.lbvserver import lbvserver

#Try to login, else print error
try:
    #Declare ns_session as the nitro_service session
    ns_session = nitro_service("192.168.176.100", "http")
    #Login to ns_session with username/password/TTL)
    ns_session.login("nsroot", "nsroot1", 3600)

    #If logged in print: Success!
    if ns_session.isLogin() == True:
        print("Success!")

#if login fails for any reason print error
except Exception as error:
    print("Error: " + str(error.args))


#Function to create new LB Vserver based on parsed session, lbname and ip
def NewLbvserver(ns_session, lbname, ip):

    try:
        #instantiate copy op 'lbvserver()'
        new_lbvserver_obj = lbvserver()

        #set name of copy
        new_lbvserver_obj.name = f"{lbname}"
        #set ip of copy
        new_lbvserver_obj.ipv46 = f"{ip}"
        #set port of copy
        new_lbvserver_obj.port = 88
        #set protocol of copy
        new_lbvserver_obj.servicetype = "HTTP"
        #set load balancing method of copy
        new_lbvserver_obj.lbmethod = "ROUNDROBIN"

        #add copy to current ns_session
        lbvserver.add(ns_session, new_lbvserver_obj)
        #Debug print what has been created if success
        print("Server created: " + new_lbvserver_obj.name)

    #Print error if anything goes wrong + error code
    except Exception as error:
        print("Error on NewLbvserver: " + str(error.args))

#Commented out: quick and dirty code to run your newly created function
#if ns_session.isLogin() == True:
    #NewLbvserver(ns_session, "lb_testscript", "192.168.176.190")





#Function to update your existing virtual servers
def updateMethod(ns_session):

    try:
        #get all lbvservers in current session
        servers = lbvserver.get(ns_session)
        #create empty list for later use
        serverlist = []

        #iterate through existing servers and add their name to the empty list
        for i in (range(0, len(servers))):
            serverlist.append(servers[i].name)
        #for names in the now filled list, update the following entries:
        for i in serverlist:

            update_lb = lbvserver()
            update_lb.name = f"{i}"

            #Update Load Balancing Method
            update_lb.lbmethod = "LEASTCONNECTION"

            #Apply edit
            lbvserver.update(ns_session, update_lb)
    #If anything is wrong, print error + thrown error code
    except Exception as error:
        print(str(error.args))

#run the updateMethod function
updateMethod(ns_session)

#Save Nsconfig
ns_session.save_config()

0 Comments on Converge 2021 – Automating your way to greatness

Leave a Comment

Your email address will not be published. Required fields are marked *

You Might Be Interested In